1/11
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced | Call with Kai | Chat |
|---|
No analytics yet
Send a link to your students to track their progress
Write a Java loop that will display the phrase One more time four times.
Also give any declarations or initializing statements that are needed.
for(int repeat = 1; repeat
Write a Java loop that will set the variable result equal to 2^5. Initialize the value of result to 1 and then multiply it by 2 for each of five loop iterations.
Also give any declarations or initializing statements that are
needed.
int result = 2;
for(int repeat = 0; repeat < 4; repeat++){
result *= 2;
}
System.out.println(result);
Write a Java loop that will read a list of numbers of type double and then display their average. The numbers are all greater than or equal to 1.0. The input data ends with a sentinel value, which you must specify. Also give any declarations or initializing statements that are needed.
Scanner keyScan = new Scanner(System.in);
System.out.println("Enter each students score");
System.out.println("When done, enter a negative number");
double classScore = keyScan.nextDouble();
double allScore = 0;
double students = 0;
boolean stillMore = true;
while(stillMore){
if(classScore < 1){
stillMore = false;
}else{
allScore += classScore;
students++;
classScore = keyScan.nextDouble();
}
}
double classAverage = allScore / students;
System.out.println("The class average is " + classAverage);
What output is produced by the following code?
for (int n = 1; n
One
Two
Three
After the loop
What output is produced by the following code?
for (int n = 1; n
Hello
Hello
After the loop
What output is produced by the following code?
for (int n = 1; n
hello
hello
Revise the loop shown in Listing 4.6 to use a break statement instead of the boolean variable areMore.
Comment on how your loop compares
with the original one.
import java.util.Scanner;
public class BooleanDemo2 {
public static void main(String[] args){
System.out.println("Enter nonnegative numbers.");
System.out.println("Place a negative number at the end");
System.out.println("to serve as end marker");
int sum = 0;
boolean areMore = true;
Scanner keyboard = new Scanner(System.in);
while(areMore){
int next = keyboard.nextInt();
if(next < 0){
break;
}else{
sum = sum + next;
}
}
System.out.println("The sum of the numbers is " + sum);
}
}
What is the bug in the code in the section "Tracing Variables"?
While the balance is a negative value, the following changes should be made:
while (balance < 0)
{
balance = balance - penalty;
balance = balance + deposit;
count++;
}
Add some suitable output statements to the following code, so that all variables are traced:
int sum = 0;
for (int n = 1; n < 10; n++)
sum = sum + n;
System.out.println("1 + 2 + ... + 9 + 10 == " + sum);
int sum = 0;
System.out.println("sum == " + sum); //*
for (int n = 1; n < 10; n++)
sum = sum + n;
System.out.println("n == " + n); //*
System.out.println("sum == " + sum); //*
System.out.println("1 + 2 + ... + 9 + 10 == " + sum);
What is the bug in the code in the previous question?
For the code to work, and add 1 all to 10 the following change needs to be made:
int sum = 0;
for (int n = 1; n
What is an assertion? Give some examples of assertions.
• Assert Boolean_Expression;
• Assert a == a;
• Assert a == 90;
Assert is used to check if a program is running correctly, at every interval with a assert, the program will check and see if the assertion is true. If the assertion is true, the the program carries on, but if it is false, the program stops and a error message is given.
Suppose that you did not have assertion checking in Java (Earlier versions of Java did not.). Write some code to simulate the following assertion check, where balance is a variable of type double:
assert balance > 0;
double balanceCheck = balance;
if(balanceCheck == 0.0){
System.out.println("");
}else{
System.out.println("error");
}