Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 1 of 1

Thread: School Assignment help

  1. #1
    Junior Member
    Join Date
    Oct 2013
    Posts
    26
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default School Assignment help

    I have an assignment project for school, and I've gotten a lot of it done, but I know it's not correct, and needing a little guidance. I will post the assignment, and then I'll list my code below. I have the GUI fully built, with the exception of the buttons doing anything. I started the action event listener, but that's where I got stuck. I also have to move the attributes into the appliance class and the 2 appliance sub-classes.

    Introduction to GUIs (+ some inheritance)
    For this assignment, you are going to create a user interface that interacts with the setters and getters of some classes that you will create. First, create an abstract class called Appliance. This abstract class should have two attributes (dealing with household appliances) and two abstract methods called turnOn() and turnOff(). These methods should return void. Then, create two subclasses of Appliance that represent household appliances (like a Refrigerator or Stove ((don't use those!))). These subclasses should have two attributes that are specific to the various appliance. Each subclass should implement the turnOn() and turnOff() methods. These methods should print to the command line some information about the appliance as it turns on and off.
    Now, the fun part! Create a GUI interface!
    Your window should have two panels: one for each appliance subclass. Each panel should have 4 textboxes (with appropriate labels) to receive/display information that correspond to the 4 attributes (2 from Appliance and 2 from the subclass) for each subclass. You also need 2 buttons on each panel: A Get button and a Set button. When the Get button is pressed, the text boxes should be filled with the information from the instantiated object of the appropriate subclass. When the Set button is pressed, the object should then contain the information contained that the user has altered. In your main method, you should create an object of each subclass, and prefill it with information (either using the constructor or the setters), then display your GUI. You should now be able to get and set the information for your objects from the GUI.
    Notes:
    At least one of your attributes for each subclass should be numeric
    Note that you will need to handle incorrectly formatted input (You can use exception handling to do this if you want to. Wrapper classes also will work)
    If there is text in the boxes when the "Get" button is pressed, it should be overwritten by what is in the object
    Remember that these two panels should both be on screen at the same time. You don't need 2 different windows, one window: 2 panels.


    import javax.swing.*;
     
    import java.awt.BorderLayout;
    import java.awt.FlowLayout;
    import java.awt.GridLayout;
    import java.awt.event.*;
     
     
    public class ApplianceGUI extends JFrame
    {
    	private JLabel coffeeTitle;
    	private JLabel machineTitle;
    	private JPanel fullPanel;
    	private JPanel coffeeMaker;
    	private JPanel coffeeGrid;
    	private JPanel coffeeNorth;
    	private JPanel coffeeAttribute1;
    	private JPanel coffeeAttribute2;
    	private JPanel coffeeAttribute3;
    	private JPanel coffeeAttribute4;
    	private JPanel machineAttribute1;
    	private JPanel machineAttribute2;
    	private JPanel machineAttribute3;
    	private JPanel machineAttribute4;
    	private JPanel washingMachine;
    	private JPanel machineGrid;
    	private JPanel machineNorth;
    	private JLabel coffeeLabel1;
    	private JLabel coffeeLabel2;
    	private JLabel coffeeLabel3;
    	private JLabel coffeeLabel4;
    	private JLabel machineLabel1;
    	private JLabel machineLabel2;
    	private JLabel machineLabel3;
    	private JLabel machineLabel4;
    	private JTextField coffeeWeight;
    	private JTextField coffeeColor;
    	private JTextField coffeeCups;
    	private JTextField coffeeRoast;
    	private JTextField machineWeight;
    	private JTextField machineColor;
    	private JTextField machineCups;
    	private JTextField machineLoadSize;
    	private JButton getCoffee;
    	private JButton setCoffee;
    	private JButton getMachine;
    	private JButton setMachine;
    	private final int WIDTH = 600;
    	private final int HEIGHT = 200;
     
     
    	public ApplianceGUI()
    	{
    		setTitle("Appliances");
    		setSize(WIDTH, HEIGHT);
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
    		buildFullPanel();
     
    		add(fullPanel);
     
    		setVisible(true);
    	}
     
    	private void buildFullPanel()
    	{
     
    		fullPanel = new JPanel();
     
    		fullPanel.setLayout(new BorderLayout());
     
    		buildCoffeeMaker();
    		buildWashingMachine();
     
    		fullPanel.add(coffeeMaker, BorderLayout.WEST);
    		fullPanel.add(washingMachine, BorderLayout.EAST);
     
    	}
     
     
    	private void buildCoffeeMaker()
    	{
     
    		coffeeMaker = new JPanel();
    		coffeeMaker.setLayout(new BorderLayout());
     
    		buildCoffeeNorth();	
    		buildCoffeeGrid();
     
    		coffeeMaker.add(coffeeNorth, BorderLayout.NORTH);
    		coffeeMaker.add(coffeeGrid, BorderLayout.WEST);
    	}
     
    	private void buildCoffeeNorth()
    	{
    		coffeeTitle = new JLabel("Coffee Maker");
     
    		coffeeNorth = new JPanel();
    		coffeeNorth.setLayout(new BorderLayout());
     
    		coffeeNorth.add(coffeeTitle, BorderLayout.CENTER);
    	}
     
    	private void buildCoffeeGrid()
    	{
    		getCoffee = new JButton("Get");
    		setCoffee = new JButton("Set");
     
    		getCoffee.addActionListener(new ApplianceButtonListener());
    		setCoffee.addActionListener(new ApplianceButtonListener());
     
    		coffeeGrid = new JPanel();
    		coffeeGrid.setLayout(new GridLayout(6,1));
     
    		buildCoffeeAttribute1();
    		buildCoffeeAttribute2();
    		buildCoffeeAttribute3();
    		buildCoffeeAttribute4();
     
    		coffeeGrid.add(coffeeAttribute1);
    		coffeeGrid.add(coffeeAttribute2);
    		coffeeGrid.add(coffeeAttribute3);
    		coffeeGrid.add(coffeeAttribute4);
     
    		coffeeGrid.add(getCoffee);
    		coffeeGrid.add(setCoffee);
    	}
     
    	private void buildCoffeeAttribute1()
    	{
    		coffeeLabel1 = new JLabel("Weight: ");
    		coffeeWeight = new JTextField(7);
     
    		coffeeAttribute1 = new JPanel();
    		coffeeAttribute1.setLayout(new FlowLayout());
     
    		coffeeAttribute1.add(coffeeLabel1);
    		coffeeAttribute1.add(coffeeWeight);
    	}
     
    	private void buildCoffeeAttribute2()
    	{
    		coffeeLabel2 = new JLabel("Color: ");
    		coffeeColor = new JTextField(7);
     
    		coffeeAttribute2 = new JPanel();
    		coffeeAttribute2.setLayout(new FlowLayout());
     
    		coffeeAttribute2.add(coffeeLabel2);
    		coffeeAttribute2.add(coffeeColor);
     
    	}
     
    	private void buildCoffeeAttribute3()
    	{
    		coffeeLabel3 = new JLabel("Number of Cups: ");
    		coffeeCups = new JTextField(7);
     
    		coffeeAttribute3 = new JPanel();
    		coffeeAttribute3.setLayout(new FlowLayout());
     
    		coffeeAttribute3.add(coffeeLabel3);
    		coffeeAttribute3.add(coffeeCups);
    	}
     
    	private void buildCoffeeAttribute4()
    	{
    		coffeeLabel4 = new JLabel("Type of Roast: ");
    		coffeeRoast = new JTextField(7);
     
    		coffeeAttribute4 = new JPanel();
    		coffeeAttribute4.setLayout(new FlowLayout());
     
    		coffeeAttribute4.add(coffeeLabel4);
    		coffeeAttribute4.add(coffeeRoast);
    	}
     
    	private void buildWashingMachine()
    	{
    		washingMachine = new JPanel();
    		washingMachine.setLayout(new BorderLayout());
     
    		buildMachineNorth();
    		buildMachineGrid();
     
    		washingMachine.add(machineNorth, BorderLayout.NORTH);
    		washingMachine.add(machineGrid, BorderLayout.EAST);
     
    	}
     
    	private void buildMachineNorth()
    	{
    		machineTitle = new JLabel("Washing Machine");
     
    		machineNorth = new JPanel();
    		machineNorth.setLayout(new BorderLayout());
     
    		machineNorth.add(machineTitle, BorderLayout.CENTER);
    	}
     
    	private void buildMachineGrid()
    	{
    		getMachine = new JButton("Get");
    		setMachine = new JButton("Set");
     
    		getMachine.addActionListener(new ApplianceButtonListener());
    		setMachine.addActionListener(new ApplianceButtonListener());
     
    		machineGrid = new JPanel();
    		machineGrid.setLayout(new GridLayout(6,1));
     
    		buildMachineAttribute1();
    		buildMachineAttribute2();
    		buildMachineAttribute3();
    		buildMachineAttribute4();
     
    		machineGrid.add(machineAttribute1);
    		machineGrid.add(machineAttribute2);
    		machineGrid.add(machineAttribute3);
    		machineGrid.add(machineAttribute4);
     
    		machineGrid.add(getMachine);
    		machineGrid.add(setMachine);
    	}
     
    	private void buildMachineAttribute1()
    	{
    		machineLabel1 = new JLabel("Weight: ");
    		machineWeight = new JTextField(7);
     
    		machineAttribute1 = new JPanel();
    		machineAttribute1.setLayout(new FlowLayout());
     
    		machineAttribute1.add(machineLabel1);
    		machineAttribute1.add(machineWeight);
    	}
     
    	private void buildMachineAttribute2()
    	{
    		machineLabel2 = new JLabel("Color: ");
    		machineColor = new JTextField(7);
     
    		machineAttribute2 = new JPanel();
    		machineAttribute2.setLayout(new FlowLayout());
     
    		machineAttribute2.add(machineLabel2);
    		machineAttribute2.add(machineColor);
    	}
     
    	private void buildMachineAttribute3()
    	{
    		machineLabel3 = new JLabel("Cups of Detergent:  ");
    		machineCups = new JTextField(7);
     
    		machineAttribute3 = new JPanel();
    		machineAttribute3.setLayout(new FlowLayout());
     
    		machineAttribute3.add(machineLabel3);
    		machineAttribute3.add(machineCups);
    	}
     
    	private void buildMachineAttribute4()
    	{
    		machineLabel4 = new JLabel("Load Size: ");
    		machineLoadSize = new JTextField(7);
     
    		machineAttribute4 = new JPanel();
    		machineAttribute4.setLayout(new FlowLayout());
     
    		machineAttribute4.add(machineLabel4);
    		machineAttribute4.add(machineLoadSize);
    	}
     
    	private class ApplianceButtonListener implements ActionListener
    	{
    		public void actionPerformed(ActionEvent e)
    		{
    			if(e.getActionCommand().equals(getCoffee))
    			{
    				coffeeWeight.setText();
    				coffeeColor.setText();
    				coffeeCups.setText();
    				coffeeRoast.setText();
    			}
    			else if(e.getActionCommand().equals(setCoffee))
    			{
    				coffeeWeight.setText();
    				coffeeColor.setText();
    				coffeeCups.setText();
    				coffeeRoast.setText();
    			}
     
    			if (e.getActionCommand().equals(getMachine))
    			{
    				machineWeight.setText();
    				machineColor.setText();
    				machineCups.setText();
    				machineLoadSize.setText();
    			}
    		}
     
    	}
     
    	public static void main(String[] args)
    	{
    		new ApplianceGUI();
    	}
     
     
     
     
     
     
     
    }

    public abstract class Appliance
    {
    	private int weight;
    	private int color;
     
    	public abstract void turnOn();
    	{
     
    	}
     
    	public abstract void  turnOff();
    	{
     
    	}
    }

     
    public class CoffeeMaker extends Appliance
    {
    	private int cups;
    	private String roast;
     
    	public void turnOn()
    	{
    		System.out.println("Cofee Maker Powering On...");
    	}
     
    	public void turnOff()
    	{
    		System.out.println("Cofee Maker Powering Off...");
    	}
     
    }

    [code=java]
    public class WashingMachine extends Appliance
    {
    private int cupsOfDetergent;
    private String loadSize;


    public void turnOn()
    {
    System.out.println("Washing Machine Powering On...");
    }

    public void turnOff()
    {
    System.out.println("Washing Machine Powering On...");
    }

    }
    [/java]
    Last edited by mattmattmatt; April 17th, 2014 at 06:40 PM. Reason: correcting code


Similar Threads

  1. Help with school assignment/arrays
    By d0mlnance in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 14th, 2013, 09:00 PM
  2. Java assignment school
    By Ur Nerd in forum Paid Java Projects
    Replies: 3
    Last Post: September 7th, 2013, 03:22 AM
  3. Problem with School Assignment.
    By SaltSlasher in forum What's Wrong With My Code?
    Replies: 11
    Last Post: February 17th, 2012, 04:12 PM
  4. School Assignment AHH!
    By Europa in forum Loops & Control Statements
    Replies: 8
    Last Post: January 20th, 2012, 08:19 AM
  5. Java program for to implement supermarket menu
    By 5723 in forum AWT / Java Swing
    Replies: 1
    Last Post: April 14th, 2009, 03:14 AM