I have never posted anything in here for java so if I don'tuse the code tags right I apologize...anyways I compiled an assignment in text pad and have a ton or errors and I was wanting to know if it is something minor or I am really that stupid...thanks
Here is what the assignment says:
Write an application (PetAdvice.java) that recommends a pet for a user based on the user’s lifestyle. Prompt the user to enter whether he or she lives in an apartment, house, or dormitory (1, 2, or 3… 4 to quit) and the average number or hours the user is home per week.
Additional hints/requirements:
1. Do not use the default values for the title bar caption – see example below.
2. For your icons, use the question for input, the information “I” for output, and the error for error message dialog boxes.
3. You are required to use IF statements and case statements at some point in the program. I combined some IF statements with my case statements to come up with the pet in the getPet() method.
4. You should allow decimals for the hours input.
5. Make sure you provide reasonableness checks when necessary.
6. If the user makes an invalid entry, allow them to re-enter their data (loop back). However, you don’t need to loop back to the beginning at the end of your program. Once your user clicks the last OK, you can terminate.
7. You will be required to use multiple methods for this application (like we were doing with our Chapter 4 in-class exercises). You don’t have to use my exact methods, but here is what I used: getHousing(), getHours(), getPet(), output(), and finish().
8. Termination: program your cancel buttons to terminate in addition to terminating when your user selects 4 on the first screen.
Print your recommendation based on the following table:
Residence Hours Home Recommendations
House 18 or more Pot-bellied pig
House 10 to <18 Dog
House Fewer than 10 Snake
Apartment 10 or more Cat
Apartment Fewer than 10 Hamster
Dormitory 6 or more Fish
Dormitory Fewer than 6 Ant farm
this is what I have so far and if the code tags dont come out right please let me know
/*
Programmer: Jim Johnson
Date: 3/18/08
Filename: PetAdvice.java
Purpose:
*/
//packages to import
import javax.swing.JOptionPane;
public class PetAdvice
{
public static void main(String[] args)
{
//declare class variables
int housing;
double hours;
String pet;
//call method
housing = getHousing();
hours = getHours();
pet = getPet();
output();
finish();
}//end main()
//the output() method displays the pet recommended
public static void output(String pet)
{
;
//display output
JOptionPane.showMessageDialog(null, "According to your requirements, I would recommend a ", + pet, "Pet Advice: Recommendation", JOptionPane.INFORMATION_MESSAGE);
}// end output
//the getPet() method accepts the housing and hours and returns a pet
public static String getPet(String pet)
{
//declare method variables
String pet;
switch(housing)
{
case 1: //pot-bellied pig
if ((housing = 1) && (hours>=18))
pet = "pot-bellied pig";
else
break;
case 2: //dog
if ((housing = 1) && (hours>=10) && (hours < 18))
pet = "dog";
break;
case 3: //snake
if ((housing = 1) && (hours < 10))
pet = "snake";
break;
case 4: //cat
if ((housing = 2) && (hours >= 10))
pet = "cat";
break;
case 5: //hamster
if ((housing = 2) && (hours < 10))
pet = "hamster";
break;
case 6: //fish
if ((housing = 3) && (hours >= 6))
pet = fish;
break;
case 7: //ant farm
if ((housing = 3) && (hours < 6))
pet = "ant farm";
break;
}//end switch
//.return value to the calling method
return commission;
}//end getComm
//the getHours() method asks the user to input hours and validate it
public static double getHours()
{
//declare method variables
double hours = 0.0;
boolean done = false;
//loop while not done
while(!done)
{
String answer = JOptionPane.showInputDialog(null, "Enter the average number of hours\nyou are home per week:");
//See if the cancel buttom was clicked....this will only work if the input is brought in as a a string
if(answer == null) finish();
//validate the input
try
{
//is answer a valid double
hours = Double.parseDouble(answer);
//is sales reasonable?
if(hours <= 0) throw new NumberFormatException();
//if the value is valid and reasonable, get out of the while loop
else done = true;
} //end try
catch(NumberFormatException e)
{
JOptionPane.showMessageDialog(null, "Your entry was not in the proper format.\nPlease enter a positive number for hours", "Error", JOptionPane. ERROR_MESSAGE);
} //end catch
} //end while
return hours;
} //end getHours()
//the getHousing() method retries a housing option from the user and validates it.
public static int getHousing()
{
//declare method variables
int housing = 0;
boolean done = false;
//loop while not done
while(!done)
{
try
{
String message = "Enter your type of residence:" + "\n\n1) House\n2) Apartment\n3) Dormitory\n4) Quit the Application\n";
code = Integer.parseInt(JOptionPane.showInputDialog(null,message));
//test for valid codes 1, 2, 3, or 4
if(code<1 || code>4) throw new NumberFormatException();
else done = true;
}
catch(NumberFormatException e)
{
JOptionPane.showMessageDialog(null,"Please enter a 1, 2, 3, or 4.", "Error", JOptionPane.INFORMATION_MESSAGE);
}
}
return code;
}
//the finish() method successfully terminates the application
public static void finish()
{
System.exit(0);
} //end finish()
} //end class