Write a program that uses input dialog boxes to read three test marks, each out of 100. The program discards your lowest mark and shows the average of the two higher marks in a message dialog box.
This is how far i got and i dont know where to do from here, any help would be appreciated:
import javax.swing.JOptionPane;
public class Average {
public static void main (String [] args){
String test1, test2, test3, avg;
test1= JOptionPane.showInputDialog("Please input mark for test 1: ");
test2= JOptionPane.showInputDialog("Please input mark for test 2: ");
test3= JOptionPane.showInputDialog("Please input mark for test 3: ");
}
}
The JOptionPane displays the dialog boxes with one of the four standard icons (question, information, warning, and error) or the custom icons specified by the user.
JDialog(Window o, String t) : creates an empty dialog with a specified window as its owner and specified title. JDialog(Dialog o, String s) : creates an empty dialog with a specified dialog as its owner and specified title.
Why to annoy the user with three different Dialog Boxes to enter things, why not do all this in one go in a single Dialog and save time, instead of testing the patience of the USER ?
You can add everything in a single Dialog, by putting all the fields on your JPanel
and then adding this JPanel
to your JOptionPane
. Below code can clarify a bit more :
import java.awt.*;
import java.util.*;
import javax.swing.*;
public class AverageExample
{
private double[] marks;
private JTextField[] marksField;
private JLabel resultLabel;
public AverageExample()
{
marks = new double[3];
marksField = new JTextField[3];
marksField[0] = new JTextField(10);
marksField[1] = new JTextField(10);
marksField[2] = new JTextField(10);
}
private void displayGUI()
{
int selection = JOptionPane.showConfirmDialog(
null, getPanel(), "Input Form : "
, JOptionPane.OK_CANCEL_OPTION
, JOptionPane.PLAIN_MESSAGE);
if (selection == JOptionPane.OK_OPTION)
{
for ( int i = 0; i < 3; i++)
{
marks[i] = Double.valueOf(marksField[i].getText());
}
Arrays.sort(marks);
double average = (marks[1] + marks[2]) / 2.0;
JOptionPane.showMessageDialog(null
, "Average is : " + Double.toString(average)
, "Average : "
, JOptionPane.PLAIN_MESSAGE);
}
else if (selection == JOptionPane.CANCEL_OPTION)
{
// Do something here.
}
}
private JPanel getPanel()
{
JPanel basePanel = new JPanel();
//basePanel.setLayout(new BorderLayout(5, 5));
basePanel.setOpaque(true);
basePanel.setBackground(Color.BLUE.darker());
JPanel centerPanel = new JPanel();
centerPanel.setLayout(new GridLayout(3, 2, 5, 5));
centerPanel.setBorder(
BorderFactory.createEmptyBorder(5, 5, 5, 5));
centerPanel.setOpaque(true);
centerPanel.setBackground(Color.WHITE);
JLabel mLabel1 = new JLabel("Enter Marks 1 : ");
JLabel mLabel2 = new JLabel("Enter Marks 2 : ");
JLabel mLabel3 = new JLabel("Enter Marks 3 : ");
centerPanel.add(mLabel1);
centerPanel.add(marksField[0]);
centerPanel.add(mLabel2);
centerPanel.add(marksField[1]);
centerPanel.add(mLabel3);
centerPanel.add(marksField[2]);
basePanel.add(centerPanel);
return basePanel;
}
public static void main(String... args)
{
SwingUtilities.invokeLater(new Runnable()
{
@Override
public void run()
{
new AverageExample().displayGUI();
}
});
}
}
import java.util.SortedSet;
import java.util.TreeSet;
import javax.swing.JOptionPane;
import javax.swing.JFrame;
public class Average {
public static void main(String [] args) {
String test1= JOptionPane.showInputDialog("Please input mark for test 1: ");
String test2= JOptionPane.showInputDialog("Please input mark for test 2: ");
String test3= JOptionPane.showInputDialog("Please input mark for test 3: ");
int int1 = Integer.parseInt(test1);
int int2 = Integer.parseInt(test2);
int int3 = Integer.parseInt(test3);
SortedSet<Integer> set = new TreeSet<>();
set.add(int1);
set.add(int2);
set.add(int3);
Integer [] intArray = set.toArray(new Integer[3]);
JFrame frame = new JFrame();
JOptionPane.showInternalMessageDialog(frame.getContentPane(), String.format("Result %f", (intArray[1] + intArray[2]) / 2.0));
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With