Modify the code below so it will use proper GUI components, such as labels, text
ID: 3812936 • Letter: M
Question
Modify the code below so it will use proper GUI components, such as labels, text fields, and buttons (Submit, Clear, and Exit) to perform the same task as below. All output messages must be displayed by showMessageDialog() of JOptionPane.
*Call your operation class TestScoreGUI and driver class TestScoreGUIApp*
****************************************************************
class InvalidTestScore extends Exception
{
public InvalidTestScore(int n)
{
super ("Error: Number must be between 0 - 100 i.e" +n);
}
}
*****************************************************
class TestScore
{
private int score [] = new int [5];
public TestScore (int test []) throws InvalidTestScore
{
for (int i=0; i<5; i++)
{
if (test[i]<0||test[i]>100)
throw new InvalidTestScore (test[i]);
else score[i]=test[i];
}
System.out.println("Average is: "+Average ());
}
public double Average ()
{
int sum = 0;
double avg;
for(int i=0; i<5; i++)
{
sum+=score[i];
}
avg=sum/5;
return avg;
}
}
************************************************
import java.util.Scanner;
class TestScoreApp
{
public static void main (String args[]) throws Exception
{
Scanner kb = new Scanner (System.in);
int array[] = new int[5];
System.out.println("Enter Test Scores");
for (int i=0; i<5; i++)
array[i]=kb.nextInt();
try
{
TestScore t1=new TestScore(array);
}
catch (InvalidTestScore e)
{
System.out.println(e.getMessage());
}
System.exit(0);
}
}
Explanation / Answer
import java.awt.*;
import java.awt.event.*;
import javax.swing.JOptionPane;
class InvalidTestScore extends Exception
{
public InvalidTestScore(int n)
{
super ("Error: Number must be between 0 - 100 i.e" +n);
}
}
class TestScore extends Frame
{
private int score [] = new int [5];
public TestScore (int test []) throws InvalidTestScore
{
for (int i=0; i<5; i++)
{
if (test[i]<0||test[i]>100)
throw new InvalidTestScore (test[i]);
else score[i]=test[i];
}
JOptionPane.showMessageDialog(this,"Average is: "+Average ());
}
public double Average ()
{
int sum = 0;
double avg;
for(int i=0; i<5; i++)
{
sum+=score[i];
}
avg=sum/5;
return avg;
}
}
class TestScoreApp extends Frame implements ActionListener
{
Label l1,l2,l3,l4,l5;
TextField tf1,tf2,tf3,tf4,tf5;
Button bt1,bt2,bt3;
public TestScoreApp(){
l1=new Label("1 Test");
l2=new Label("2 Test");
l3=new Label("3 Test");
l4=new Label("4 Test");
l5=new Label("5 Test");
tf1=new TextField();
tf2=new TextField();
tf3=new TextField();
tf4=new TextField();
tf5=new TextField();
bt1=new Button("Submit");
bt2=new Button("Clear");
bt3=new Button("Exit");
bt1.addActionListener(this);
bt2.addActionListener(this);
bt3.addActionListener(this);
setLayout(null);
l1.setBounds(150,50,70,20);
l2.setBounds(150,100,70,20);
l3.setBounds(150,150,70,20);
l4.setBounds(150,200,70,20);
l5.setBounds(150,250,70,20);
tf1.setBounds(250,50,70,20);
tf2.setBounds(250,100,70,20);
tf3.setBounds(250,150,70,20);
tf4.setBounds(250,200,70,20);
tf5.setBounds(250,250,70,20);
bt1.setBounds(120,300,70,30);
bt2.setBounds(220,300,70,30);
bt3.setBounds(320,300,70,30);
add(l1);
add(l2);
add(l3);
add(l4);
add(l5);
add(tf1);
add(tf2);
add(tf3);
add(tf4);
add(tf5);
add(bt1);
add(bt2);
add(bt3);
JOptionPane.showMessageDialog(this,"Enter The Test Scores");
}
public void actionPerformed(ActionEvent ae){
String str=ae.getActionCommand();
if(str.equals("Submit")){
int array[] = new int[5];
try
{
array[0]=Integer.parseInt(tf1.getText());
array[1]=Integer.parseInt(tf2.getText());
array[2]=Integer.parseInt(tf3.getText());
array[3]=Integer.parseInt(tf4.getText());
array[4]=Integer.parseInt(tf5.getText());
TestScore t1=new TestScore(array);
}
catch (InvalidTestScore e)
{
JOptionPane.showMessageDialog(bt1,e.getMessage());
}catch (NumberFormatException ne)
{
JOptionPane.showMessageDialog(bt1,"Check Values"+ne.getMessage());
}
}else if(str.equals("Clear")){
tf1.setText("");
tf2.setText("");
tf3.setText("");
tf4.setText("");
tf5.setText("");
}else{
System.exit(0);
}
}
public static void main (String args[]) throws Exception
{
TestScoreApp ta=new TestScoreApp();
ta.setSize(500,500);
ta.setVisible(true);
}
}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author SuNiL SiNgH
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.JOptionPane;
class InvalidTestScore extends Exception
{
public InvalidTestScore(int n)
{
super ("Error: Number must be between 0 - 100 i.e" +n);
}
}
class TestScore extends Frame
{
private int score [] = new int [5];
public TestScore (int test []) throws InvalidTestScore
{
for (int i=0; i<5; i++)
{
if (test[i]<0||test[i]>100)
throw new InvalidTestScore (test[i]);
else score[i]=test[i];
}
JOptionPane.showMessageDialog(this,"Average is: "+Average ());
}
public double Average ()
{
int sum = 0;
double avg;
for(int i=0; i<5; i++)
{
sum+=score[i];
}
avg=sum/5;
return avg;
}
}
class TestScoreApp extends Frame implements ActionListener
{
Label l1,l2,l3,l4,l5;
TextField tf1,tf2,tf3,tf4,tf5;
Button bt1,bt2,bt3;
public TestScoreApp(){
l1=new Label("1 Test");
l2=new Label("2 Test");
l3=new Label("3 Test");
l4=new Label("4 Test");
l5=new Label("5 Test");
tf1=new TextField();
tf2=new TextField();
tf3=new TextField();
tf4=new TextField();
tf5=new TextField();
bt1=new Button("Submit");
bt2=new Button("Clear");
bt3=new Button("Exit");
bt1.addActionListener(this);
bt2.addActionListener(this);
bt3.addActionListener(this);
setLayout(null);
l1.setBounds(150,50,70,20);
l2.setBounds(150,100,70,20);
l3.setBounds(150,150,70,20);
l4.setBounds(150,200,70,20);
l5.setBounds(150,250,70,20);
tf1.setBounds(250,50,70,20);
tf2.setBounds(250,100,70,20);
tf3.setBounds(250,150,70,20);
tf4.setBounds(250,200,70,20);
tf5.setBounds(250,250,70,20);
bt1.setBounds(120,300,70,30);
bt2.setBounds(220,300,70,30);
bt3.setBounds(320,300,70,30);
add(l1);
add(l2);
add(l3);
add(l4);
add(l5);
add(tf1);
add(tf2);
add(tf3);
add(tf4);
add(tf5);
add(bt1);
add(bt2);
add(bt3);
JOptionPane.showMessageDialog(this,"Enter The Test Scores");
}
public void actionPerformed(ActionEvent ae){
String str=ae.getActionCommand();
if(str.equals("Submit")){
int array[] = new int[5];
try
{
array[0]=Integer.parseInt(tf1.getText());
array[1]=Integer.parseInt(tf2.getText());
array[2]=Integer.parseInt(tf3.getText());
array[3]=Integer.parseInt(tf4.getText());
array[4]=Integer.parseInt(tf5.getText());
TestScore t1=new TestScore(array);
}
catch (InvalidTestScore e)
{
JOptionPane.showMessageDialog(bt1,e.getMessage());
}catch (NumberFormatException ne)
{
JOptionPane.showMessageDialog(bt1,"Check Values"+ne.getMessage());
}
}else if(str.equals("Clear")){
tf1.setText("");
tf2.setText("");
tf3.setText("");
tf4.setText("");
tf5.setText("");
}else{
System.exit(0);
}
}
public static void main (String args[]) throws Exception
{
TestScoreApp ta=new TestScoreApp();
ta.setSize(500,500);
ta.setVisible(true);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.