0% found this document useful (0 votes)
6 views

Math and Strings

The document contains 5 Java code fragments and exercises: 1) The first code fragment outputs the absolute value, square, maximum and minimum of some numbers. The second outputs the length, uppercase, lowercase, and indexes of a string. 2) The second evaluates Math functions like square root, power and maximum/minimum of numbers. 3) The third outputs the character codes and concatenation of strings. 4) The fourth generates random numbers between ranges. 5) The exercises ask to write code to generate random numbers between given ranges.

Uploaded by

headcodfree
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Math and Strings

The document contains 5 Java code fragments and exercises: 1) The first code fragment outputs the absolute value, square, maximum and minimum of some numbers. The second outputs the length, uppercase, lowercase, and indexes of a string. 2) The second evaluates Math functions like square root, power and maximum/minimum of numbers. 3) The third outputs the character codes and concatenation of strings. 4) The fourth generates random numbers between ranges. 5) The exercises ask to write code to generate random numbers between given ranges.

Uploaded by

headcodfree
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Java Exercises:

A) What is the output of the following fragments:


1)
public class Test {
public static void main(String[] args) {
double a = Math.abs(-5.5);
double b = Math.pow(2, 2);
double max = Math.max(a, b);
double min = Math.min(a, b);
double c = Math.sqrt(b);
System.out.println("a= "+a+" b= "+b+" c ="+c);
System.out.println("Max= "+max+" Min= "+min);
}
}
………………………………………………………………………………………………
………………………………………………………………………………………………
2)
public class Test {
public static void main(String[] args) {
String txt = "JAVA Programming is FUN, so fun";
System.out.println("The length of the txt string is: " + txt.length());
System.out.println(txt.toUpperCase());
System.out.println(txt.toLowerCase());
System.out.println(txt.indexOf("fun"));
System.out.println(txt.indexOf("java"));
System.out.println("Welcome ".concat("to java"));
}
}
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
3) Evaluate the following method calls:
(a) Math.sqrt(4.0) …………………………………………………………………
(b) Math.pow(2.0, 2.0) ……………………………………………………………
(c) Math.max(2.0, Math.min(3.0, 4.0)) …………………………………………...

4)
public class Example {
public static void main(String[] args) {
char i = 30 + 35;
System.out.println("i is " + i);
int j = 2 + 'a';
System.out.println("j is " + j);
System.out.println(j + " is the Unicode for character " + (char)j);
System.out.println("Chapter " + '9');
}
}
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
5)
public class Example {
public static void main(String[] args) {
String s1 = "Welcome to Java";
String s2 = " Programming is fun";
String s3 = "Welcome to Java";
System.out.println(s1 == s2);
System.out.println(s2 == s3);
System.out.println(s1.indexOf('j'));
System.out.println(s1.indexOf("to"));
System.out.println(s1.length());
System.out.println(s1.toLowerCase());
System.out.println(s1.toUpperCase());
System.out.println(s1.concat(s2));
}
}
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………

B) Write a Java program:


1) Write a Java program that generates a random number in the range of 0
through 49 (0 to 49)
2) Write an expression that obtains a random integer between 34 and 55.
Write an expression that obtains a random integer between 0 and 999. Write
an expression that obtains a random number between 5.5 and 55.5

You might also like