Results 1 to 2 of 2
  1. #1
    jimJohnson is offline Member
    Join Date
    Jan 2008
    Posts
    22
    Rep Power
    0

    Default trouble with program

    i am finishing a java program up and a little confused....I need a clear button and know that is with the action listener but already have an item listener and confused on how to put a second one with a deadline in a couple hours midnight to be exact

    output should be
    Initial screen:

    Enter the average number of hours you are home per week
    House Apartment dormitory

    Select your type of housing to determine the best pet

    Clear Fields Button

    Screen with valid Input


    Enter the average number of hours you are home per week
    House Apartment dormitory

    Select your type of housing to determine the best pet
    According to your requirements I would recommend a ....

    Clear Fields Button

    Invalid screen
    Enter the average number of hours you are home per week
    House Apartment dormitory

    Select your type of housing to determine the best pet
    Enter a valid number of hours between 0 and 168
    Clear Fields Button

    Java Code:
    //packages to import
    
    import java.awt.*;
    import java.applet.*;
    import java.awt.event.*;
    
    
    public class PetAdviceApplet extends Applet implements ItemListener
    {
    	//declare variables and construct components
    	int housing;
    	double hours;
    	String pet;
    
    
    	Label promptLabel = new Label ("Enter the average number of hours you are home per week ");
    	TextField hoursField = new TextField(15);
    
    	CheckboxGroup housingGroup = new CheckboxGroup();
    	Checkbox houseBox = new Checkbox("House", false, housingGroup);
    	Checkbox apartmentBox = new Checkbox("Apartment", false, housingGroup);
    	Checkbox dormitoryBox = new Checkbox("Dormitory", false, housingGroup);
    	Checkbox hiddenBox = new Checkbox("",true,housingGroup);
    
    	Label housingLabel = new Label ("Select your type of housing to determine the best pet ");
    
    	Label outputLabel = new Label (" ");
    
    	Button clearButton = new Button("Clear Fields");
    
    	public void init()
    	{
    		setBackground(Color.red);
    		setForeground(Color.black);
    		add(promptLabel);
    		add(hoursField);
    		hoursField.requestFocus();
    		hoursField.setForeground(Color.black);
    		add(houseBox);
    		houseBox.addItemListener(this);
    		add(apartmentBox);
    		apartmentBox.addItemListener(this);
    		add(dormitoryBox);
    		dormitoryBox.addItemListener(this);
    		add(housingLabel);
    		add(outputLabel);
    
    
    	} //end init()
    
    	//This method is triggered by the user clicking an option button
    	public void itemStateChanged(ItemEvent choice)
    	{
    		try
    		{
    			housing = getHousing();
    			hours = getHours()	;
    			pet = getPet(hours, housing);
    			output(pet);
    		} //end try
    
    	catch(NumberFormatException e)
    		{
    			outputLabel.setText("Enter a valid number for hours between 0 and 168 ");
    			hiddenBox.setState(true);
    			hoursField.setText("");
    			hoursField.requestFocus();
    		} //end catch
    	}
    
    	public double getHours()
    	{
    		double hours = Double.parseDouble(hoursField.getText());
    		if ((hours <= 0) || (hours >= 168)) throw new NumberFormatException();
    
    		return hours;
    	} //end getHours()
    
    	public int getHousing()
    	{
    		int housing = 0;
    		if (houseBox.getState()) housing = 1;
    		else
    		if (apartmentBox.getState()) housing = 2;
    		else
    		if (dormitoryBox.getState()) housing = 3;
    
    		return housing;
    	} //end getHousing()
    
    	public String getPet(double hours, int housing)
    				{
    					//declare method variables
    
    					String pet = "";
    
    
    					switch(housing)
    					{
    						case 1: //pot-bellied pig
    						if ((housing == 1) && (hours>=18))
    						{
    						pet = "Pot-bellied pig";
    						}
    						case 2: //dog
    						if((housing == 1) && (hours>=10) && (hours < 18))
    						{
    						pet = "Dog";
    						}
    
    						case 3: //snake
    						if ((housing == 1) && (hours < 10))
    						{
    						pet = "Snake";
    						}
    
    						case 4: //cat
    						if ((housing == 2) && (hours >= 10))
    						{
    						pet = "Cat";
    						}
    
    						case 5: //hamster
    						if ((housing == 2) && (hours < 10))
    						{
    						pet = "Hamster";
    						}
    
    						case 6: //fish
    						if ((housing == 3) && (hours >= 6))
    						{
    						pet = "Fish";
    						}
    
    						case 7: //ant farm
    						if ((housing == 3) && (hours < 6))
    						{
    						pet = "Ant farm";
    						}
    
    						break;
    
    					}//end switch
    
    					return pet;
    
    		}//end getComm
    	public void output(String pet)
    	{
    		outputLabel.setText ("According to your requirements, I would recommend a " + pet);
    		validate();
    	} //end output
    }//end class

    I hope I explained that clear enough...if not I will be back at 830 to explain it better

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Java Code:
    //  <applet code="PAA" width="400" height="400"></applet>
    import java.awt.*;
    import java.applet.*;
    import java.awt.event.*;
    
    public class PAA extends Applet implements ItemListener, ActionListener
    {
        //declare variables and construct components
        int housing;
        double hours;
        String pet;
    
        Label promptLabel = new Label ("Enter the average number " +
                              "of hours you are home per week ");
        TextField hoursField = new TextField(15);
    
        CheckboxGroup housingGroup = new CheckboxGroup();
        Checkbox houseBox = new Checkbox("House", false, housingGroup);
        Checkbox apartmentBox = new Checkbox("Apartment", false, housingGroup);
        Checkbox dormitoryBox = new Checkbox("Dormitory", false, housingGroup);
        Checkbox hiddenBox = new Checkbox("",true,housingGroup);
    
        Label housingLabel = new Label ("Select your type of housing " +
                                  "to determine the best pet ");
    
        Label outputLabel = new Label (" ");
    
        Button clearButton = new Button("Clear Fields");
    
        public void init()
        {
            setBackground(Color.red);
            setForeground(Color.black);
            clearButton.addActionListener(this);
            add(promptLabel);
            add(hoursField);
            hoursField.requestFocus();
            hoursField.setForeground(Color.black);
            add(houseBox);
            houseBox.addItemListener(this);
            add(apartmentBox);
            apartmentBox.addItemListener(this);
            add(dormitoryBox);
            dormitoryBox.addItemListener(this);
            add(housingLabel);
            add(outputLabel);
            add(clearButton);
        } //end init()
    
        //This method is triggered by the user clicking an option button
        public void itemStateChanged(ItemEvent choice)
        {
            try
            {
                housing = getHousing();
                hours = getHours();
                pet = getPet(hours, housing);
                output(pet);
            } //end try
            catch(NumberFormatException e)
            {
                outputLabel.setText("Enter a valid number for hours " +
                                    "between 0 and 168 ");
                hiddenBox.setState(true);
                hoursField.setText("");
                hoursField.requestFocus();
            } //end catch
        }
    
        public void actionPerformed(ActionEvent e)
        {
            hoursField.setText("");
            hiddenBox.setState(true);
            outputLabel.setText("");
        }
    
        public double getHours()
        {
            double hours = Double.parseDouble(hoursField.getText());
            if ((hours <= 0) || (hours >= 168))
                throw new NumberFormatException("hours must >= 0 and <= 168");
    
            return hours;
        } //end getHours()
    
        public int getHousing()
        {
            int housing = 0;
            if (houseBox.getState()) housing = 1;
            else if (apartmentBox.getState()) housing = 2;
            else if (dormitoryBox.getState()) housing = 3;
    
            return housing;
        } //end getHousing()
    
        public String getPet(double hours, int housing)
        {
            //declare method variables
            String pet = "";
            switch(housing)
            {
                case 1: // house
                    if (hours>=18)
                    {
                        pet = "Pot-bellied pig";
                    }
                    else if(hours>=10 && hours < 18)
                    {
                        pet = "Dog";
                    }
                    else if (hours < 10)
                    {
                        pet = "Snake";
                    }
                    break;
                case 2: // apartment
                    if (hours >= 10)
                    {
                        pet = "Cat";
                    }
                    else if (hours < 10)
                    {
                        pet = "Hamster";
                    }
                    break;
                case 3: // dorm
                    if (hours >= 6)
                    {
                        pet = "Fish";
                    }
                    else if (hours < 6)
                    {
                        pet = "Ant farm";
                    }
            }//end switch
            return pet;
        }//end getComm
    
        public void output(String pet)
        {
            outputLabel.setText ("According to your requirements, " +
                                 "I would recommend a " + pet);
            validate();
        } //end output
    }

Similar Threads

  1. compiling trouble
    By capacitator in forum CLDC and MIDP
    Replies: 4
    Last Post: 06-10-2008, 11:12 PM
  2. [SOLVED] trouble learnig swing
    By monir6464 in forum AWT / Swing
    Replies: 5
    Last Post: 05-08-2008, 07:01 AM
  3. Compile Trouble
    By adelgado0723 in forum New To Java
    Replies: 5
    Last Post: 04-21-2008, 03:02 AM
  4. Having trouble with array
    By ice22 in forum New To Java
    Replies: 3
    Last Post: 11-13-2007, 04:06 AM
  5. JTree trouble
    By Alantie Vala in forum AWT / Swing
    Replies: 3
    Last Post: 08-01-2007, 12:12 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •