0% found this document useful (0 votes)
42 views3 pages

2 - Math and Strings

Uploaded by

headcodfree
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views3 pages

2 - Math and Strings

Uploaded by

headcodfree
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd

Java Exercises:

A) What is the output of the following fragments:


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

4)
public class Example {
public static void main(String[] args) {
char i = 30 + 35;
[Link]("i is " + i);
int j = 2 + 'a';
[Link]("j is " + j);
[Link](j + " is the Unicode for character " + (char)j);
[Link]("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";
[Link](s1 == s2);
[Link](s2 == s3);
[Link]([Link]('j'));
[Link]([Link]("to"));
[Link]([Link]());
[Link]([Link]());
[Link]([Link]());
[Link]([Link](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