• Welcome to Overclockers Forums! Join us to reply in threads, receive reduced ads, and to customize your site experience!

Simple Java Programs I created for class

Overclockers is supported by our readers. When you click a link to make a purchase, we may earn a commission. Learn More.

updawg

StarCraft II Fanatic
Joined
May 19, 2006
Location
NOVA
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();
    }
}
 
Code:
//WeightedAverage.java
//Michel Eber
//CS110 Section 7
//LAB Section 9
//LAB instructor Matt Williamson
//Last Modified 11/15/06
//This program takes grade inputs and their weight through JTextFields and outputs a weighted average

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class WeightedAverage extends JFrame
{         // Defines all variables
    private JLabel Test1, Test2,
           Test3, Test4, Weight1, Weight2, Weight3, Weight4, ansa;
    
    private JTextField testa, testb, testc, testd, weighta, weightb, weightc, weightd, ans;

    private JButton calculateB, exitB;

    private CalculateButtonHandler cbHandler;

    private ExitButtonHandler ebHandler;
          // Sets programs dimensions
    private static final int WIDTH = 450;
    private static final int HEIGHT = 500;

    public WeightedAverage()
    {
          // Create labels of text fields
     Test1 = new JLabel("Enter test score one: ",
                            SwingConstants.RIGHT);
     Weight1 = new JLabel("Enter weight of test score one: ",
                            SwingConstants.RIGHT);
     
     Test2 = new JLabel("Enter test score two: ",
                       SwingConstants.RIGHT);
     Weight2 = new JLabel("Enter weight of test score two: ",
                       SwingConstants.RIGHT);
     
     Test3 = new JLabel("Enter test score three: ",
                        SwingConstants.RIGHT);
     Weight3 = new JLabel("Enter weight of test score three: ",
                        SwingConstants.RIGHT);
     
     Test4 = new JLabel("Enter test score four: ",
                        SwingConstants.RIGHT);
     Weight4 = new JLabel("Enter weight of test score four: ",
                        SwingConstants.RIGHT);
     
     
      ansa = new JLabel("The weighted average is: ",
                        SwingConstants.RIGHT);
     

            //Create input text fields
     testa = new JTextField(10);
     weighta = new JTextField(10);
     testb = new JTextField(10);
     weightb = new JTextField(10);
     testc = new JTextField(10);
     weightc = new JTextField(10);
     testd = new JTextField(10);
     weightd = 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
     ans = new JTextField(10);

            //Set the title of the window
     setTitle("Weighted Average of four test scores");

            //Get the container
     Container pane = getContentPane();

            //Set the layout
     pane.setLayout(new GridLayout(10,1));

            //Place all items created
     pane.add(Test1);
     pane.add(testa);
     pane.add(Weight1);
     pane.add(weighta);
     
     pane.add(Test2);
     pane.add(testb);
     pane.add(Weight2);
     pane.add(weightb);
     
     pane.add(Test3);
     pane.add(testc);
     pane.add(Weight3);
     pane.add(weightc);
     
     pane.add(Test4);
     pane.add(testd);
     pane.add(Weight4);
     pane.add(weightd);
     
     pane.add(calculateB);
     pane.add(exitB);
     pane.add(ansa);
     pane.add(ans);
    
             

          //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 t1, t2, t3, t4, w1, w2, w3, w4, avg1, avg2, avg3, avg4, favg;

         t1 = Double.parseDouble(testa.getText());
         w1 = Double.parseDouble(weighta.getText());
         t2 = Double.parseDouble(testb.getText());
         w2 = Double.parseDouble(weightb.getText());
         t3 = Double.parseDouble(testc.getText());
         w3 = Double.parseDouble(weightc.getText());
         t4 = Double.parseDouble(testd.getText());
         w4 = Double.parseDouble(weightd.getText());
         
         
         // Calculations Required to output weighted average
         avg1 = t1 * w1;
         avg2 = t2 * w2;
         avg3 = t3 * w3;
         avg4 = t4 * w4;
         favg = avg1 + avg2 + avg3 + avg4;
         // Outputs the weighted Average
         ans.setText("" + favg);
         
     }
    }

    private class ExitButtonHandler implements ActionListener
    {
         // Exits the program
     public void actionPerformed(ActionEvent e)
     {
      System.exit(0);
     }
    }

    public static void main(String[] args)
    {
     WeightedAverage weight = new WeightedAverage();
    }
}
 
Back