
Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
The solution to quiz 1 of the cmsc 131 fall 2004 course. It includes the java code for a program that calculates the average of two user inputs and assigns a status message based on the average. The program uses joptionpane for user input and message display, and swing for graphics and swing objects. The code demonstrates the use of if statements and the need for import statements and system.exit(0) when using joptionpane.
Typology: Quizzes
1 / 1
This page cannot be seen from the preview
Don't miss anything!

This is one possible solution. Note that the JOptionPane returns a string value, and so it must be converted to a floating type using either Double.parseDouble or Float.parseFloat. We initialized the message string to “Satisfactory” and changed it if the average is less than 70. We could have achieved the same result using an if-else construct. The initial import statement and the final System.exit(0) are needed because we are using JOptionPane. (In fact this is true whenever graphics and swing objects are used.)
import javax.swing.*; public class ComputeStatus {
public static void main(String[] args) { String strVal1 = JOptionPane.showInputDialog("Enter Value"); String strVal2 = JOptionPane.showInputDialog("Enter Value"); double val1 = Double.parseDouble(strVal1); double val2 = Double.parseDouble(strVal2); double avg = (val1 + val2)/2;
String message = "Satisfactory"; if (avg < 70.0) { message = "Unsatisfactory"; }
JOptionPane.showMessageDialog(null, message); System.exit(0); } }