In: Computer Science
In this question, use your new GUI classes to create a small application. You have 2 options:
Write any small application of your choice, as long as it follows the rules that follow.
Write the small UnitConversion application, which will be described for you.
Option 1: Write your own application
You can write any small application that you like, as long as:
It uses your GUI classes, including:
at least one checkbox,
at least one group of 3 radio buttons,
at least two buttons, one of which is labelled “Quit” and terminates the program. (You can use System.exit(0); to do this anywhere in a program.)
At least one input text box, and at least one output text box.
It must do some sort of calculation or processing that uses the input, and the options specified by the checkboxes and radio buttons, to produce some output that is displayed.
It must be self-explanatory, so that the marker can run it and test it without knowing anything about it other than what is displayed in the window.
Option 2: Write a UnitConverter application
If you have no idea of your own, you can use this one:
Write a simple application that will do conversions between units of temperature, weight, and length, with a GUI interface that looks approximately like the one below. (You don’t have to put things in the same places.)
The type of conversion is specified by the radio buttons on the right. If the Reverse checkbox is highlighted, then it should do the conversion in the opposite order (F to C instead of C to F for example). It does not have to re-label the radio buttons. The user should be able to type a number into the top box. (Error handling would be nice, but is not required.) When the Calculate button is pressed the answer should appear in the lower text box, with a suitable message as shown. The Quit button should terminate the program.
You can use code from TestA3Q1 or TestA3Q2 as a starting point.
/*
* Converter
*/
import java.awt.*;
import java.util.Scanner;
import java.awt.event.*;
class Converter
{
Converter()
{
Frame f=new Frame("Converter");
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e) {
f.dispose();
}
});
Label cl=new Label();
cl.setBounds(700,700,50,100);
cl.setVisible(false);
f.add(cl);
Label l1=new Label("enter input:");
l1.setBounds(50,50,200,50);
f.add(l1);
TextField t1=new TextField();
t1.setBounds(50,100, 200,30);
f.add(t1);
Label l2=new Label("enter convertion type:");
l2.setBounds(50,150,200,50);
f.add(l2);
CheckboxGroup cbg = new CheckboxGroup();
Checkbox checkBox1 = new Checkbox("Temperature(0c->F)", cbg, false);
checkBox1.setBounds(50,200, 150,50);
checkBox1.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
cl.setText("temperature");
}
});
Checkbox checkBox2 = new Checkbox("Weight(kg->gm)", cbg, false);
checkBox2.setBounds(200,200, 150,50);
checkBox2.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
cl.setText("weight");
}
});
Checkbox checkBox3 = new Checkbox("length(km->m)", cbg, false);
checkBox3.setBounds(350,200, 150,50);
checkBox3.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
cl.setText("length");
}
});
f.add(checkBox1);
f.add(checkBox2);
f.add(checkBox3);
Label l3=new Label("output:");
l3.setBounds(50,250,200,50);
f.add(l3);
TextField t2=new TextField();
t2.setBounds(50,300, 200,30);
f.add(t2);
Button b1=new Button("submit");
b1.setBounds(50, 350, 50, 25);
b1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
double input =Integer.valueOf(t1.getText());
String choice=cl.getText();
if(choice.equals("temperature"))
t2.setText(Double.toString(input * 1.8 + 32) );
else if(choice.equals("weight"))
t2.setText(Double.toString(input * 1000) );
else if(choice.equals("length"))
t2.setText(Double.toString(input *1000) );
}
});
f.add(b1);
Button b2=new Button("exit");
b2.setBounds(110, 350, 50, 25);
b2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
f.dispose();
}
});
f.add(b2);
f.setSize(800,800);
f.setLayout(null);
f.setVisible(true);
}
public static void main(String[] args) {
new Converter();
}
}