Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Computer programming- II (CS 2300) Java Lab Exercise #01 Lab Session - #01 Java fundamental Basic Input/Output Evaluation: Student Name & Number Instructor: Maha & dhekra Date: Q1: Which of the following is a correct Java identifier: SS N1 a$total2 D_D F TR R.ER 2UYR #FER public a-total Num of hours totalGrade Score_a int double import Sara’sBook 3rd s-3 G-R-Y St8first sa$ _ds 3Sum class $mo Q2: Which of the following is correctly formed constant (or literal) of type integer? 1 45 4. 12.3 34 ‘8’ “9” -6 12,5 Q3: Which of the following is correctly formed constant (or literal) of type char? ‘ab’ ‘a’ “A” ‘3’ “5” ‘?’ “%” z Q4: Which of the following is correctly formed constant (or literal) of type double? 5.32 5 4. 1,6 -7.5 “4.77” Q5: Decide if these identifiers follow the naming conventions or not, and if not try to rewrite them, so they follow the conventions: identifiers correction class hello class Hello int R int r char c class Test int markTotal final int numOfMonth = 12 final int NUM_OF_MONTH = 12 final double RENT_RATE = 10.5 double scoreaverage Double scoreAverage Q6: Correct the errors byte x = 130; // short x = 130; OR int x = 130; OR long x = 130; short y = 40000; // int y = 40000 OR long y = 40000; int z = 3000000000; // long z = 3000000000L; 1 Computer programming- II (CS 2300) Lab Session - #01 Q7: Which of the following variable declaration are correct? If a variable declaration is not correct, give the reason(s) and provide the correct variable declaration. n = 12 ;// not correct because the data type is missing int n = 12; char letter = ;// not correct because there is no character literal or variable at the right side of = , char letter = ‘r’; int one = 5, two; // correct double x, y, z ; // correct Q8: Write Java statements that accomplish the following. a. Declare int variables x and y. int x, y; b. Initialize an int variable x to 10 and a char variable ch to 'B'. int x = 10; char ch = ‘B’; c. Update the value of an int variable x by adding 5 to it. x = x + 5; d. Declare and initialize a double variable payRate to 12.50. double payRate = 12.50; e. Copy the value of an int variable firstNum into an int variable tempNum. tempNum = firstNum; f. Swap the contents of the int variables x and y. (Declare additional variables, if necessary.) int temp = x; x = y; y = temp; g. Declare a char variable grade and set the value of grade to 'A'. char grade = ‘A’; h. Declare int variables to store four integers. int x=1, y=2, z=3, w=4 ; Q9 .Find the errors in the following programs: (Each segment has 4 errors) a. public static void main( String args[] ) { int x, n, m=1 ,y+1 ; wrong declaration for y+1 m-2 = n; left side can’t be an expression y = x + m; x has no value System.out.println(" Sum is "+ y), ; not , } 2 Computer programming- II (CS 2300) Lab Session - #01 b. public class Errors { final int NUM=11; public static void main( String args[] ) { char ch = ‘m’; int N = 3 : t; , not : n = 5; n is not declared NUM = N+3; NUM is constant, change is not allowed!! System.out.println("ch=" + Ch+", NUM="+NUM )); ch not Ch } } Q10. What is the output of the following program? Program // IO example import java.util.Scanner; public class IOExample { public static void main(String[] args) { Scanner input = new Scanner(System.in); int i=3; System.out.println("Java is a powerful programming language"); System.out.print("Please enter an integer number: "); i = input.nextInt(); System.out.printf("%s%d","The number you entered is ", i); System.out.printf("%s%d%s%d","and two times of ", i, " is ", i*2); } } Output Java is a powerful programming language Please enter an integer number: 4 The number you entered is 4 and two times of 4 is 8 3 Computer programming- II (CS 2300) Lab Session - #01 Q11.What is output by the following program Program import javax.swing.JOptionPane; public class AreaRectangle { public static void main(String[] args) { int length, width, area; String string; string = JOptionPane.showInputDialog(null, "Enter the length: ", "Input", JOptionPane.INFORMATION_MESSAGE); length = Integer.parseInt(string); string = JOptionPane.showInputDialog(null, "Enter the width: ", "Input", JOptionPane.INFORMATION_MESSAGE); width = Integer.parseInt(string); area = length*width; JOptionPane.showMessageDialog(null, "Area = "+area, "Display Area", JOptionPane.INFORMATION_MESSAGE); } } Output 4 Computer programming- II (CS 2300) Lab Session - #01 Q12. Write a simple java program to display “ Welcome to Java “ ? Output Welcome to java Program public class SimpleProgram { public static void main(String[] args) { System.out.println("Welcom to Java"); } } Q13. Write a program that reads two numbers and print the sum of two numbers ? Output Enter the first number: 5 Enter the second number: 3 The sum = 8 Program import java.util.Scanner; public class Sum { public static void main(String[] args) { Scanner input = new Scanner(System.in); int num1,num2,sum; System.out.print("Enter the first num:"); num1 = input.nextInt(); System.out.print("Enter the second num:"); num2 = input.nextInt(); sum=num1+num2; System.out.println("Sum="+sum); } } 5