Use eclipse or an equivalent IDE to compile and run the Java code below. Write a
ID: 3559197 • Letter: U
Question
Use eclipse or an equivalent IDE to compile and run the Java code below. Write a program that will get the user to type in two decimal numbers and the program will calculate the average and output this result. Use JOptionPane for I/O.
import java.util.Scanner;
import javax.swing.JOptionPane;
public class Comp274Wk1_1 {
public static void main(String[] args) {
// scanner example
Scanner myIn;
System.out.println("please type an integer number");
int n;
myIn = new Scanner(System.in);
n = myIn.nextInt();
// for verification
System.out.printf("you typed the number %d", n);
String s = JOptionPane.showInputDialog("type a decimal number");
float f = Float.parseFloat(s);
String output = "the number you input was " + f +
", and this number multiplied by six is " + 6*f;
JOptionPane.showMessageDialog(null, output);
System.exit(0);
}
}
Explanation / Answer
import java.util.Scanner;
import javax.swing.JOptionPane;
public class Comp274Wk1_1 {
public static void main(String[] args) {
// scanner example
Scanner myIn;
int n,m;
System.out.println("please type an integer number");
myIn = new Scanner(System.in);
m = myIn.nextInt();
System.out.println("please type an integer number");
n = myIn.nextInt();
// for verification
System.out.printf("you typed the number %d ", m);
System.out.printf("you typed the number %d ", n);
String s1 = JOptionPane.showInputDialog("type a decimal number");
String s2 = JOptionPane.showInputDialog("type a decimal number");
float f1 = Float.parseFloat(s1);
float f2 = Float.parseFloat(s2);
String output = "the numbers you input were " + f1 + " and " + f2 +
", and their average is " + (f1+f2)/2;
JOptionPane.showMessageDialog(null, output);
System.exit(0);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.