Tell me what you think, the first program just converts amount of time in seconds into minutes hours day weeks years, and the other averages four different weighted test scores. This was my first time using GUI's so I hope they look alright.
Code:
//SecondConversion.java
//Michel Eber
//CS110 Section 7
//LAB Section 9
//LAB instructor Matt Williamson
//Last Modified 11/15/06
//This program takes an input of seconds from a user and calculates differnt units of time with a GUI display
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class SecondConversion extends JFrame
{ // Defines all variables
private JLabel Seconds, Years, Weeks, Days, Hours, Minutes;
private JTextField secs, yrs, wks, dys, hrs, mins;
private JButton calculateB, exitB;
private CalculateButtonHandler cbHandler;
private ExitButtonHandler ebHandler;
// Sets programs dimensions
private static final int WIDTH = 500;
private static final int HEIGHT = 400;
public SecondConversion()
{
// Create labels of text fields
Seconds = new JLabel("Enter an amount of time in seconds: ",
SwingConstants.RIGHT);
Years = new JLabel("Seconds in year(s): ",
SwingConstants.RIGHT);
Weeks = new JLabel("Seconds in week(s): ",
SwingConstants.RIGHT);
Days = new JLabel("Seconds in day(s): ",
SwingConstants.RIGHT);
Hours = new JLabel("Seconds in hour(s): ",
SwingConstants.RIGHT);
Minutes = new JLabel("Seconds in minute(s): ",
SwingConstants.RIGHT);
//Create input text fields
secs = new JTextField(10);
//create Calculate Button
calculateB = new JButton("Calculate");
cbHandler = new CalculateButtonHandler();
calculateB.addActionListener(cbHandler);
//Create Exit Button
exitB = new JButton("Exit");
ebHandler = new ExitButtonHandler();
exitB.addActionListener(ebHandler);
//Creat Answer Field
yrs = new JTextField(10);
wks = new JTextField(10);
dys = new JTextField(10);
hrs = new JTextField(10);
mins = new JTextField(10);
//Set the title of the window
setTitle("Converting Seconds to Different Units of Time");
//Get the container
Container pane = getContentPane();
//Set the layout
pane.setLayout(new GridLayout(10,1));
//Place all items created
pane.add(Seconds);
pane.add(secs);
pane.add(Years);
pane.add(yrs);
pane.add(Weeks);
pane.add(wks);
pane.add(Days);
pane.add(dys);
pane.add(Hours);
pane.add(hrs);
pane.add(Minutes);
pane.add(mins);
pane.add(calculateB);
pane.add(exitB);
//set the size of the window and display it
setSize(WIDTH,HEIGHT);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
private class CalculateButtonHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
double time, ans1, ans2, ans3, ans4, ans5;
// Gets time from user's input
time = Double.parseDouble(secs.getText());
// Calculations Required to output converted units of time
ans1 = time/60/60/24/365;
ans2 = time/60/60/24/7;
ans3 = time/60/60/24;
ans4 = time/60/60;
ans5 = time/60;
// Outputs the units of time
yrs.setText("" + ans1);
wks.setText("" + ans2);
dys.setText("" + ans3);
hrs.setText("" + ans4);
mins.setText("" + ans5);
}
}
private class ExitButtonHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
//Exits the Program
{
System.exit(0);
}
}
public static void main(String[] args)
{
SecondConversion score = new SecondConversion();
}
}