Programming Fundamentals Chapter 5.3 Quiz
How many times will the following code print "Welcome to Java"? int count = 0;while (count++ < 10) { System.out.println("Welcome to Java"); }
10
Goal: Learn to write while loops with sentinels. Assignment: Assume a Scanner stdin has been declared (i.e. Scanner stdin = new Scanner(System.in); is given). Write a loop that continuously prompts the user for a username. The loop will keep running until the user inputs a username of 8 characters or longer. Your program will print the final username. Q: What is the code for getting the user input with the given being that stdin is scanner variable?
String userInput = stdin.nextLine();
Assignment: In a data analysis application for environmental studies, temperature readings from a sensor are recorded in a sequence. Sudden temperature spikes or drops are significant and are marked when the difference between one reading and the next exceeds 5 degrees. Write code that reads a sequence of temperature readings (as integers) from standard input, terminated by a negative number. The code should identify and count occurrences where the absolute difference between consecutive readings exceeds 5 degrees. After processing, print the count of such significant changes. For example, given the sequence "12 15 7 7 22 16 -1", the code should print "2" because there are two instances where consecutive readings differ by more than 5 degrees (15 to 7 and 7 to 22). Assume a Scanner stdin object for standard input is provided. Q: What is the code for the loop that enables the logic(printing of instances)?
if (previousReading != Integer.MAX_VALUE) { // Calculate the absolute difference between consecutive readings if (Math.abs(currentReading - previousReading) > 5) { significantChangeCount++; } }
Assignment: In a data analysis application for environmental studies, temperature readings from a sensor are recorded in a sequence. Sudden temperature spikes or drops are significant and are marked when the difference between one reading and the next exceeds 5 degrees. Write code that reads a sequence of temperature readings (as integers) from standard input, terminated by a negative number. The code should identify and count occurrences where the absolute difference between consecutive readings exceeds 5 degrees. After processing, print the count of such significant changes. For example, given the sequence "12 15 7 7 22 16 -1", the code should print "2" because there are two instances where consecutive readings differ by more than 5 degrees (15 to 7 and 7 to 22). Assume a Scanner stdin object for standard input is provided. Q: What variables are to be declared when coding the example above?
int previousReading = Integer.MAX_VALUE; // Initially, set to a large number int currentReading; int significantChangeCount = 0;
Assignment: Assume a Scanner stdin has been declared (i.e. Scanner stdin = new Scanner(System.in); is given). Write a loop that continuously prompts the user for a username. The loop will keep running until the user inputs a username of 8 characters or longer. Your program will print the final username. Q: What is the code for getting the userLength?
userInput.length()
Assignment: In a data analysis application for environmental studies, temperature readings from a sensor are recorded in a sequence. Sudden temperature spikes or drops are significant and are marked when the difference between one reading and the next exceeds 5 degrees. Write code that reads a sequence of temperature readings (as integers) from standard input, terminated by a negative number. The code should identify and count occurrences where the absolute difference between consecutive readings exceeds 5 degrees. After processing, print the count of such significant changes. For example, given the sequence "12 15 7 7 22 16 -1", the code should print "2" because there are two instances where consecutive readings differ by more than 5 degrees (15 to 7 and 7 to 22). Assume a Scanner stdin object for standard input is provided. Q: What is the code for the loop that reads to through the sequence?
while (true) { currentReading = stdin.nextInt();
Assignment: Assume a Scanner stdin has been declared (i.e. Scanner stdin = new Scanner(System.in); is given). Write a loop that continuously prompts the user for a username. The loop will keep running until the user inputs a username of 8 characters or longer. Your program will print the final username. What is the code for constructing the loop?
while (userInput.length() < 8){ System.out.print("State username:"); userInput = stdin.nextLine(); if (userInput.length() >= 8){ System.out.print(userInput); } }
What is the output of the following code? int x = 0; while (x < 4) { x = x + 1; } System.out.println("x is " + x);
x is 4