Programming Fundamentals Chapter 5.3 Quiz

¡Supera tus tareas y exámenes ahora con Quizwiz!

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


Conjuntos de estudio relacionados

Chapter 18 (Part 3)- Eating Disorders

View Set

Ch 20 Forming & Operating Partnership, Tax 332 Chapter 20, ACCT 4343-Tax of Business Ch.9

View Set

M13 Chapter 12 Retrofit and Future Trends AUTI 142

View Set

apush chapter 16 vocab, apush ch 17 vocab, apush ch 18 vocab, chapter 19 apush vocab (period 6)

View Set

General Biology Chapter 10 Quiz, General Bio Ch.9, Biology Study Guide 2 (3/3), Bio quiz 5,6,7,8

View Set

EIP 1 Midterm Study Unit 2: Research & Professional Writing

View Set

Chapter 26 NCLEX Style Review Questions

View Set