4 - Elementary Programming - Examples
4 - Elementary Programming - Examples
Exercise (1):
Type casting - convert the following double type (myDouble) to an int type:
Exercise (2):
Fill in the missing part to create a greeting variable of type String and assign it the
value Hello.
greeting = ;
1)
public class Quiz1 {
public static void main(String[] args) {
int a = 7;
int b = 3;
int c = ++a/--b;
System.out.println( c != 4);
System.out.println( a > c);
a += 2;
b *= 2;
System.out.println("a= "+ a );
System.out.println("b= "+ b);
}
}
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
2)
public class Test {
public static void main(String[] args) {
char ch1 = 97;
char ch2 = 'A';
System.out.println(ch1==ch2 && ch1=='a');
System.out.println(ch2);
double x = 5.9;
int y = (int)x;
System.out.println(!(y>x || y==5.9));
System.out.println(y);
}
}
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
3)
public class Test {
public static void main(String[] args) {
String txt1 = "JAVAS\b Programming \nis\\FUN, \"so\tfun\"";
String txt2 = "C# is Fun\rJava";
String a = "50";
int b = 50;
String c = a + b;
System.out.println(txt2);
System.out.println(txt1);
System.out.println("50 + 50 = 100\nc= " + c);
}
}
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
Write a complete java program that takes three numbers (x, y, z) as input
and print the output of (x+y)z and xy + yz
Test Data:
Enter first number: 5
Enter second number: 6
Enter third number: 7
Expected Output:
Result of specified numbers 5, 6, 7:
(x+y)z is 77
xy + yz is 72