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
//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