- Edit, compile and run the following program.
public class Loop1{
public static void main(String [] args){
final int HIGH = 20;
int i = 1;
while(i <= HIGH){
System.out.print(i + " ");
i += 1;
}
System.out.println();
}
}
- Study the above example, make sure you understand each statement. Now
modify the above program to print out only even integers
2 4 6 8 ... 20
- Edit, compile and run the following program.
import javax.swing.JOptionPane;
public class Loop4{
public static void main(String [] args){
final int STARTING_POINT = 1;
String input = JOptionPane.showInputDialog("How high can you count ?");
int high = Integer.parseInt(input);
int i = STARTING_POINT;
while(i <= high){
System.out.print(i + " ");
i += 1;
}
System.exit(0);
}
}
- Edit, compile and run the following program. Use 10 as the input. The output should be.
10
9
8
7
6
5
4
3
2
1
Blast Off.
THE PROGRAM
import javax.swing.JOptionPane;
public class BlastOff{
public static void main(String [] args){
String input = JOptionPane.showInputDialog("Enter a positive integer");
int start = Integer.parseInt(input);
while(start > 0){
System.out.println(start);
start += 1;
}
System.out.println("Blast Off");
System.exit(0);
}
}
- The BlassOff program has a logic error, correct the above program and run it twice.
The first time use 10 as the input, the second time use -10.
- Finding the minimum of a sequence of non-negative integers
Write the Java code to find the minimum of a sequence of non-negative integers.
The basic idea is to use an integer variable min initialized to the first value.
Get the next integer from the user and compare that integer to the current min.
If that integer is less than the min, assign to min that integer.
If not leave the min alone. Repeat until the user enters a negative value.
Use the following pseudo code to develop your Java code.
Declare variables
Initialize min to first value
while (value is non negative)
if next integer is less than min
Assign to min that integer
get next value
END WHILE
if min is non negative
Print out the min
else
Print out "No integers entered"
- Write a program that prompt the user to enter a decimal number and then outputs this number rounded to two places. See PrintfEx1.java
- Write a program that uses while loops to perform the following steps.
- Prompt the user to input two integers: firstNum and secondNum (firstNum must be less than secondNum).
- Output all numbers between firstNum and secondNum
- Edit, compile and run the following program
import javax.swing.JOptionPane;
class AdditionTest{
public static void main(String [] args){
int correct = 0, incorrect = 0;
int response = JOptionPane.showConfirmDialog( null,
"Ready To Test You Arithmetic? ",
"Confirmation",
JOptionPane.YES_NO_OPTION);
while(response == JOptionPane.YES_OPTION){
int operand1 = (int)(Math.random() * 100);
int operand2 = (int)(Math.random() * 100);
int sum = operand1 + operand2;
String display = operand1 + " + " + operand2 + " = ";
String input = JOptionPane.showInputDialog(display);
if(input == null || input.length() == 0){
System.out.println("Invalid input");
}
else{
int answer = Integer.parseInt(input);
if(answer == sum){
System.out.println("Correct! :)");
++correct;
}else{
System.out.println("Incorrect! :(");
++incorrect;
}
System.out.println(display + sum);
}
response = JOptionPane.showConfirmDialog( null,
"Try Another Computation ? ",
"Ready To Test You Arithmetic?",
JOptionPane.YES_NO_OPTION);
}
System.out.println("Correct -> "
+ correct + "\nIncorrect -> " + incorrect);
System.exit(0);
}
}