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 5 of 5

Thread: Trying to wrap my head around Java

  1. #1
    Member
    Join Date
    Oct 2012
    Posts
    33
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Thumbs up Trying to wrap my head around Java

    Write, so I have very little programming experience, and so I'm trying to wrap my head around my Java class, I'm working at it, but I'm struggling on some points. Basically I try to follow the assignment instructions given by my programming professor but like any program, a bit of it is open to interpretation. My latest assignment is as follows....
    [highlight-instruction]
    The purpose of this assignment is to create and use dialog boxes and selection statements. It is based on a problem from Java Programming by Joyce Farrell. This application will recommend a pet to the user based on the type of dwelling the user lives in and the number of hours per day the user is at home.

    Your application will consist of one class, PetAdvice.
    Use an input dialog box to request the type of dwelling the program user lives in. Your input dialog must look like Figure 1 below.

    Figure 1

    Edit the value entered into the input dialog box. The value must be A, a, H, h, D, or d. Display a message dialog box indicating an invalid value was entered if A, a, H, h, D, or d was not entered. Your message dialog must look like Figure 2 below. Stop the application after displaying the error message dialog if invalid data was entered. Continue processing if a valid value was entered.

    Figure 2

    The program logic to display the input dialog box and edit the input for type of dwelling must be in its own method and separate from main().
    Use an input dialog box to request the number of hours the program user is at home during the day. Your input dialog must look like Figure 3 below.

    Figure 3

    Edit the value entered into the input dialog box. The value must be A, a, B, b, C, c, D, d, E or e. Display a message dialog box indicating an invalid value was entered if A, a, B, b, C, c, D, d, E or e was not entered. Your message dialog must look like Figure 4 below. Stop the application after displaying the error message dialog if invalid data was entered. Continue processing if a valid value was entered.

    Figure 4

    The program logic to display the input dialog box and edit the input for hours at home must be in its own method and separate from main().
    Based on the type of dwelling and the number of hours at home, use this table to determine what type of pet the user should get. Display this recommendation in a message dialog box. Your message dialog box must look like Figure 5 below. Keep in mind the recommended pet will change based on the user input.

    Figure 5

    The program logic to determine the pet and display the message dialog box with the recommend pet must be in its own method and separate from main().
    You may not use any "global" variables, that is variables declared outside a method.
    You do not need to program a response when the user clicks the Cancel button or clicks the X in the upper right corner of the dialog. I will not test these features when I grade your application.
    Based on the requirements above, you will have a minimum of four methods:

    main( )
    a method to process the type of dwelling
    a method to process the number of hours at home
    a method to determine and display the recommended pet.

    This project is worth a maximum of 40 points. Check the class syllabus for the penalties for submitting an assignment late.
    [/highlight]
    I've gotten some code written for it, but I'm at a loss as to what I'm supposed to fill my main() method with. I've got no errors in the code that I've written thus far, but when I compile and run it does absolutely nothing, it's like it's going through the motions but never getting anywhere. The code I have is as follows:
    import javax.swing.JOptionPane;
    public class PetAdvice
    {
    	String Domicile;
    	String Hours;
    	public static void main(String[] args)
    	{	
     
    	}
    	public void Domicile()
    	{
    		Domicile = JOptionPane.showInputDialog(null, "Input A(Apartment), H(House), D(Dorm)");
    		switch(Domicile)
    		{
    			case "A":
    			case "a":
    				Domicile = "A";
    				break;
    			case "H":
    			case "h":
    				Domicile = "H";
    				break;
    			case "D":
    			case "d":
    				Domicile = "D";
    				break;
    			default:
    			{
    				JOptionPane.showMessageDialog(null, "You have entered an invalid code; the application is now ending");
    				System.exit(-1);
    			}
    		}
    	}
    	public void Hours()
    	{
    		Hours= JOptionPane.showInputDialog(null, "Input A(18+ Hours), B(10-17 Hours), C(8-9 Hours), D(6-7 Hours), E(0-5 Hours).");
    		switch(Domicile)
    		{
    			case "A":
    			case "a":
    				Hours = "a";
    				break;
    			case "B":
    			case "b":
    				Hours = "b";
    				break;
    			case "C":
    			case "c":
    				Hours = "c";
    				break;
    			case "D":
    			case "d":
    				Hours = "d";
    				break;
    			case "E":
    			case "e":
    				Hours = "e";
    				break;				
    			default:
    			{
    				JOptionPane.showMessageDialog(null, "You have entered an invalid code; the application is now ending");
    				System.exit(-1);
    			}
    		}
    	}
    		public void petDetermination()
    		{
    			if (Domicile == "H")
    				switch(Hours)
    				{
    					case "a":
    						JOptionPane.showMessageDialog(null, "You should get a Pot-Bellied Pig.");
    						break;
    					case "b":
    						JOptionPane.showMessageDialog(null, "You should get a Dog.");
    						break;
    					case "c":
    					case "d":
    					case "e":
    						JOptionPane.showMessageDialog(null, "You should get a Snake.");
    						break;
    				}
    			if (Domicile == "A")
    				switch(Hours)
    				{
    					case "a":
    					case "b":
    						JOptionPane.showMessageDialog(null, "You should get a Cat.");
    						break;
    					case "c":
    					case "d":
    					case "e":
    						JOptionPane.showMessageDialog(null, "You should get a Hamster.");
    						break;
    				}		
    			if (Domicile ==  "D")
    				switch (Hours)
    				{
    					case "a":
    					case "b":
    					case "c":
    					case "d":
    						JOptionPane.showMessageDialog(null, "You should get a Fish.");
    						break;
    					case "e":
    						JOptionPane.showMessageDialog(null, "You should get an Ant Farm.");
    						break;
    				}
    		}
    }
    I imagine the code could look a little neater or better, but it's what I've got so far with my very limited skills. I did take a C++ class about 9 years ago, and didn't do very well then either, but I also wasn't in the mind set of actually trying, older and wiser now, and I really want to understand this what I'm doing wrong, and get into that programmer mindset. Programming isn't my major, but it's still something that interests me.

    Edit:
    Also tried to use the spoiler tags as you can see, guess they don't work >.< or they're incorrect, is there a different code I should use?
    Last edited by Sylis; October 10th, 2012 at 08:11 AM. Reason: Fixed


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Trying to wrap my head around Java

    Quote Originally Posted by Sylis View Post
    I've got no errors in the code that I've written thus far, but when I compile and run it does absolutely nothing, it's like it's going through the motions but never getting anywhere.
    public static void main(String[] args)
    	{//This marks the start of your program	
                  //This is what your program does while it is running. So it does exactly what you told it to do, nothing.
    	}//This marks the end of your program



    Quote Originally Posted by Sylis View Post
    I've gotten some code written for it, but I'm at a loss as to what I'm supposed to fill my main() method with.
    It should contain code which leads the user along the path.
    public static void main(String[] args)
    	{//This marks the start of your program
                 //figure out the user's dwelling type
                 //figure out the user's number of hours at home
                 //Use this information to determine the type of pet the user should get
                 //Display that information to the user according to the instructions
    	}//This marks the end of your program

  3. #3
    Member
    Join Date
    Oct 2012
    Posts
    33
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Trying to wrap my head around Java

    I wrote out an entire reply and ended up losing it somehow. Anyway basically I fixed the problem, thank you very much for your help. I understand what you're saying, regardless of how many methods I write in conjunction with main in the class, they won't properly run unless I've called on them in the main() method.

    I have a new problem, in JGrasp, compiling and running the program works, but when I input H(or h) for House type, and any number of hours A-E, it comes back with an error saying I've given an invalid input as it should if I typed anything other than H, A, or D, and anything but A-E for the hours. So inputting an H shouldn't be a problem, and yet it gives an error.

    I also have Eclipse and I've cut and pasted the code into this also, but it gives me an error at the switch lines for hour and domicile saying that string type is an incorrect value type for the switch. I'm assuming that this has something to do with the above problem even though JGrasp has no problem running it? Any thoughts on this, or pointers on where I should look to solve this problem? We haven't really gotten that in depth with value types. So I'm at a loss here.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Trying to wrap my head around Java

    string type is an incorrect value type for the switch
    Strings in switch() statements came with java 1.7
    Check which version of the JDK you are using.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Oct 2012
    Posts
    33
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Trying to wrap my head around Java

    I did end up fixing it. There was apparently something wrong with the code when I transcribed it to a new computer. Thank you all for your help.
    I enjoy that you guys point students in the right direction vice just doing it for them.
    This is a great place to learn from mistakes.

Similar Threads

  1. how to wrap a pre-generics library
    By frumious in forum Object Oriented Programming
    Replies: 2
    Last Post: April 9th, 2012, 09:58 PM
  2. The Concept of Entities -- Can't wrap my mind around it.
    By RaustBD in forum Java Theory & Questions
    Replies: 2
    Last Post: November 21st, 2011, 08:42 PM
  3. Need urgent help regarding java word wrap function.. URGENT
    By coldice in forum What's Wrong With My Code?
    Replies: 3
    Last Post: August 16th, 2011, 05:43 AM
  4. Why wont the Button wrap?
    By Taylorleemusic in forum What's Wrong With My Code?
    Replies: 4
    Last Post: October 1st, 2010, 12:03 AM
  5. Simple Game In Java (Head and Tails).
    By drkossa in forum Java Theory & Questions
    Replies: 5
    Last Post: November 28th, 2009, 11:40 AM