Solution to Quiz 1 of CMSC 131 Fall 2004, Quizzes of Computer Science

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

Pre 2010

Uploaded on 07/29/2009

koofers-user-1ng
koofers-user-1ng 🇺🇸

9 documents

1 / 1

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CMSC 131 Fall 2004 Quiz 1 Solution
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);
}
}

Partial preview of the text

Download Solution to Quiz 1 of CMSC 131 Fall 2004 and more Quizzes Computer Science in PDF only on Docsity!

CMSC 131 Fall 2004 Quiz 1 Solution

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); } }