Starting from:
$30

$24

Lab Assignment 04 Solution

PURPOSE: The purpose of this lab is to reinforce the ability to write if-else statements and loops.




You must complete all five problems to receive full credit for this assignment. The number of points that each problem is worth is given in parenthesis after the problem number. You may implement all of these problems in the same Java file. Save your work in a zip file and upload both your zipped project file and a notepad (.txt) file of your work to your Pilot drop box before the lab due date. Ask your TA for help if you have any questions.







Problem 1 (3 points):




Fix the mistakes in the following if-else statement so that the sentences it prints to the console are all correct (i.e. true). Do not delete or change any of the System.out.println statements. At the point in the code that reads "YOUR CODE HERE", add code to display either "a is greater than b" or "b is greater than a", as appropriate. Note: there are more mistakes than just the missing code.







Scanner scanner = new Scanner(System.in); System.out.print(“Enter a double value for a “); double a = scanner.nextDouble(); System.out.print(“Enter a double value for b “); double b = scanner.nextDouble();







System.out.println("a = " + a);




System.out.println("b = " + b);




if (a != b) {




System.out.println("The values of a and b are not equal."); System.out.println("Let's check whether a or b is greater."); // YOUR CODE HERE




}




else




System.out.println("The values of a and b are equal"); System.out.println("In other words, a = b.");

Problem 2 (3 points):




Fix the mistakes in the following if-else statement so that the sentence it prints to the console is correct (i.e. true). Do not delete or change any of the System.out.println statements.







System.out.print(“Enter an int value for x “);




int x = scanner.nextInt();




System.out.print(“Enter an int value for y “);




int y = scanner.nextInt();




System.out.print(“Enter a char value for charVar “);




char charVar = scanner.next().charAt(0);
















System.out.println("x = " + x);




System.out.println("y = " + y);




System.out.println("charVar = " + charVar);




if ((x 0 && y 0) && charVar == 'z') {




System.out.println("The values of x and y are positive"); System.out.println("The value of charVar is z");




} else {




System.out.println("The values of x and y are negative"); System.out.println("The value of charVar is z");

}







Problem 3 (4 points):




The do-while loop below is intended to count by two from 2 up to 200 (e.g. 2, 4, 6, ..., 198, 200). Rewrite this loop as a for loop.




int counter = 2;




do {




System.out.println(counter);




counter += 2;




} while (counter <= 200);







Problem 4 (4 points):




The for loop below is intended to roll a simulated 6-sided die until the number 3 is rolled and count how many time this takes. This code is particularly bad because it uses a large and arbitrary upper bound for the loop control variable because that is the only way to be very confident that a 3 will be rolled before the loop terminates. Rewrite this loop as a do-while loop.

int count = 0;




for (int i=0; i<1000000; i++) {







int numberRolled = (int) (Math.random() * 6 + 1); System.out.println(numberRolled); count++;




if (numberRolled == 3) {




break;




}




}




System.out.println("It took " + count + " tries to roll a 3.");







Problem 5 (6 points):




A prime number is a number that is only divisible by itself and 1. For example, the first five prime numbers are 2, 3, 5, 7, and 11. Write a program that prompts the user for a number n and then displays the first n prime numbers. Hint: you probably want to use nested loops.

More products