lab 2.docx - Package Title Lab Questions Course Title Big Java Chapter Number 4 Fundamental Data Types 1 Write a program that does the following Create

lab 2.docx - Package Title Lab Questions Course Title Big...

This preview shows page 1 - 4 out of 9 pages.

Package Title: Lab Questions Course Title: Big Java Chapter Number: 4 Fundamental Data Types 1) Write a program that does the following: Create seven variables, one for each of the primitive number types in Java, and initialize each variable with any appropriate value. Print out the name of each variable and its value. Modify the value of each variable with an assignment statement and print out the names of the variables and their new values. Next, create seven constants, one for each of the primitive number types in Java. Print the name of the constant and its value. int a = 2000000000; long b = a * a ; short c = 32767; byte d = 127; float e = ( float ) Math. PI ; double f = Math. PI ; boolean g = true ; System. out .println( "A:" + a ); System. out .println( "B:" + b ); System. out .println( "C:" + c ); System. out .println( "D:" + d ); System. out .println( "E:" + e ); System. out .println( "F:" + f ); System. out .println( "G:" + g ); a = -2000000000; b = 2 * a * a ; c = -32768; d = -128; e = ( float ) Math. E ; f = Math. E ; System. out .println( "A:" + a ); System. out .println( "B:" + b ); System. out .println( "C:" + c ); System. out .println( "D:" + d ); System. out .println( "E:" + e ); System. out .println( "F:" + f ); What happens if you try to assign a value to a constant? You cannot change a constant. If you try to, you will receive a complier error.
Image of page 1
2) Execute the program below. Each invocation of println outputs an arithmetic expression. The first two println commands are followed by comments that describe the operations that occur in each expression. Complete the program by adding a comment after each println statement that describes all the arithmetic operations that occur when evaluating the expression that is printed. public class Expressions { public static void main(String[] args) { int a = 3; int b = 4; int c = 5; int d = 17; System.out.println((a + b)/ c); // 3 and 4 are added with sum 7 // 7 is divided by 5 with quotient 1 System.out.println(a + b / c); // 4 is divided by 5 with quotient 0 // 3 is added to 0 with sum 3 System.out.println(a++); System.out.println(a--); System.out.println(a + 1); System.out.println(d % c); System.out.println(d / c); System.out.println(d % b); System.out.println(d / b); System.out.println(d + a / d + b); System.out.println((d + a) / (d + b));
Image of page 2
System.out.println(Math.sqrt(b));
Image of page 3
Image of page 4

You've reached the end of your free preview.

Want to read all 9 pages?

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture