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

Thread: Hot Dog Stand Code

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

    Default Hot Dog Stand Code

    All right, since I don't want to bother my teacher (I'm wondering if he'll eventually get annoyed since I'm having to ask him for help every weekend now...), can anyone help me with my code?

    The question is: You operate several hot dog stands distributed throughout town. Define a class named HotDogStand that has an instance variable for the hot dog stand's ID number and an instance variable for how many hot dogs the stand has sold that day. Create a constructor that allows a user of the class to initialize both values.

    Also create a method named justSold that increments by one the number of hot dogs the stand has sold. The idea is that this method will be invoked each time the stand sells a hot dog so that you can track the total number of hot dogs sold by the stand. Add another method that returns the value in this variable.

    Finally, ad a static variable that tracks the total number of hot dogs sold by all hot dog stands and a static method that returns the value in this variable.

    This is my class definition...
    public class HotDogStand {
     
    	// instance variables
     
    	private String hotDogStandID;
    	private int hotDogsSold;
    	private static int totalSold;
     
    	// mutators
     
    	public void setHotDogStandID(String standID) {
    		hotDogStandID = standID; }
     
    	public void setHotDogsSold(int sold) {
    		hotDogsSold = sold; }
     
    	public void setAll(String standID, int sold) {
    		hotDogStandID = standID;
    		hotDogsSold = sold; }
     
    	// constructors
     
    	public HotDogStand() {
    		hotDogStandID = "A Hot Dog Stand";
    		hotDogsSold = 0; }
     
    	public HotDogStand(String standID, int sold) {
    		hotDogStandID = standID;
    		if(hotDogsSold >= 0) {
    			hotDogsSold = sold;
    		} else {
    			System.out.println("Error! Stop giving away free hot dogs!");
    			System.exit(0); }; }
     
    	// accessors
     
    	public String getHotDogStandID() {
    		return hotDogStandID; }
     
    	public int getHotDogsSold() {
    		return hotDogsSold; }
     
    	public int getTotalSold() {
    		return totalSold; }
     
    	// other methods
     
    	public int justSold(int sold) {
    		return sold++; }
     
    	public static int totalSold(int sold) {
    		return totalSold++; }
     
    }

    And this is my driver/tester/demo:
    import javax.swing.JOptionPane;
    public class StandDemo {
    	public static void main(String[] args) {
     
    		String nameStandOne = JOptionPane.showInputDialog("What is the first stand's name?");
    		String inputFirstSold = JOptionPane.showInputDialog("How many hot dogs have been sold so far for "+nameStandOne+"?");
    		int firstSold = Integer.parseInt(inputFirstSold);
    		HotDogStand firstStand = new HotDogStand(nameStandOne, firstSold);
     
    		String nameStandTwo = JOptionPane.showInputDialog("What is the second stand's name?");
    		String inputSecondSold = JOptionPane.showInputDialog("How many hot dogs have been sold so far for "+nameStandTwo+"?");
    		int secondSold = Integer.parseInt(inputSecondSold);
    		HotDogStand secondStand = new HotDogStand(nameStandOne, firstSold);
     
    		System.out.println("The third hot dog stand has just opened!\n");
    		HotDogStand thirdStand = new HotDogStand();
     
    		int k = 1;
    		while(k != 0) {
    			String choiceWord = JOptionPane.showInputDialog("Enter 1 to sell a hot dog! Enter 0 for the next stand.");
    			int choice = Integer.parseInt(choiceWord);
    			if (choice == 1) {
    				System.out.println("Hot dogs sold: "+firstStand.justSold(firstSold));
    			} else if(choice == 0) {
    				break; }
    		}
     
     
     
    	}
    }

    Obviously, the code isn't done yet. I'm stopping a moment to see if part of it works, but I've hit a roadblock. You see, whenever I try to increase the amount of hot dogs sold by the first stand, nothing actually happens. When, for the first hot dog stand, I enter 1 as the current amount of hot dogs sold and increment it, nothing happens. It remains at one and I'm not sure why.

    I'm not very great at class definitions yet (I'm bumbling through them), but I'm hoping to get a bit of help.

    The attached file shows the error.
    Error.jpg
    EDIT: or... it's too small. Nonetheless, the amount of hot dogs sold isn't incrementing and I'm not sure why. The program is compiling, but I have a logical error somewhere in there.
    Last edited by Powerbomb; October 26th, 2013 at 08:36 PM. Reason: Thumbnail is too small


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Hot Dog Stand Code

    public int justSold(int sold) {
    		return sold++; }
    You are increasing the parameter value and not the instance variable that tracks how many hotdogs have been sold. Other problems:

    Why are you passing in a parameter at all? If you have sold 10 hotdogs so far today and then another one gets sold all that should happen is that the total increases by one.
    Why is the method returning a value?
    Improving the world one idiot at a time!

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

    Default Re: Hot Dog Stand Code

    Quote Originally Posted by Junky View Post
    public int justSold(int sold) {
    		return sold++; }
    You are increasing the parameter value and not the instance variable that tracks how many hotdogs have been sold. Other problems:

    Why are you passing in a parameter at all? If you have sold 10 hotdogs so far today and then another one gets sold all that should happen is that the total increases by one.
    Why is the method returning a value?
    If I only return the total wouldn't that carry over for each stand? Also, why wouldn't I pass in a parameter? Don't I need some starting value on the driver, or should that be on the class definition?

    The method is returning a value because... isn't that what it's supposed to do? JCreator won't compile the code if there isn't a return value...

    I changed a few things. Class definition:
    public class HotDogStand {
     
    	// instance variables
     
    	private String hotDogStandID;
    	private int hotDogsSold;
    	private static int totalSold = 0;
     
    	// mutators
     
    	public void setHotDogStandID(String standID) {
    		hotDogStandID = standID; }
     
    	public void setHotDogsSold(int sold) {
    		hotDogsSold = sold; }
     
    	public void setAll(String standID, int sold) {
    		hotDogStandID = standID;
    		hotDogsSold = sold; }
     
    	// constructors
     
    	public HotDogStand() {
    		hotDogStandID = "A Hot Dog Stand";
    		hotDogsSold = 0; }
     
    	public HotDogStand(String standID, int sold) {
    		hotDogStandID = standID;
    		if(hotDogsSold >= 0) {
    			hotDogsSold = sold;
    		} else {
    			System.out.println("Error! Stop giving away free hot dogs!");
    			System.exit(0); } }
     
    	// accessors
     
    	public String getHotDogStandID() {
    		return hotDogStandID; }
     
    	public int getHotDogsSold() {
    		return hotDogsSold; }
     
    	public int getTotalSold() {
    		return totalSold; }
     
    	// other methods
     
    	public int justSold(int sold) {
    		sold = hotDogsSold;
    		hotDogsSold = hotDogsSold+1; // ++ won't increase the value for some reason
    		return hotDogsSold; }
     
    	public String generateJustSold(String standID, int sold) {
    		return "You have sold "+sold+" hot dogs for stand "+standID+"!"; }
     
    	public static int totalSold() {
    		return totalSold++; }
     
    }

    Tester:
    import javax.swing.JOptionPane;
    public class StandDemo {
    	public static void main(String[] args) {
     
    		String nameStandOne = JOptionPane.showInputDialog("What is the first stand's name?");
    		String inputFirstSold = JOptionPane.showInputDialog("How many hot dogs have been sold so far for "+nameStandOne+"?");
    		int firstSold = Integer.parseInt(inputFirstSold);
    		HotDogStand firstStand = new HotDogStand(nameStandOne, firstSold);
     
    		String nameStandTwo = JOptionPane.showInputDialog("What is the second stand's name?");
    		String inputSecondSold = JOptionPane.showInputDialog("How many hot dogs have been sold so far for "+nameStandTwo+"?");
    		int secondSold = Integer.parseInt(inputSecondSold);
    		HotDogStand secondStand = new HotDogStand(nameStandOne, firstSold);
     
    		System.out.println("The third hot dog stand has just opened!\n");
    		HotDogStand thirdStand = new HotDogStand("Third Stand", 0);
     
    		int k = 1;
    		while(k != 0) {
    			String choiceWord = JOptionPane.showInputDialog("Enter 1 to sell a hot dog! Enter 0 for the next stand.");
    			int choice = Integer.parseInt(choiceWord);
    			if (choice == 1) {
    				System.out.println("Hot dogs sold: "+firstStand.justSold(firstSold));
    			} else if(choice == 0) {
    				break; } }
     
    		System.out.println(firstStand.generateJustSold(nameStandOne, firstSold));
     
    		while(k != 0) {
    			String choiceWord = JOptionPane.showInputDialog("Enter 1 to sell a hot dog! Enter 0 for the next stand.");
    			int choice = Integer.parseInt(choiceWord);
    			if (choice == 1) {
    				System.out.println("Hot dogs sold: "+secondStand.justSold(secondSold));
    			} else if(choice == 0) {
    				break; } }
     
    		System.out.println(secondStand.generateJustSold(nameStandTwo, secondSold));
     
    		while(k != 0) {
    			String choiceWord = JOptionPane.showInputDialog("Enter 1 to sell a hot dog! Enter 0 to show how many total hot dogs you've sold.");
    			int choice = Integer.parseInt(choiceWord);
    			if (choice == 1) {
    				System.out.println("Hot dogs sold: "+thirdStand.justSold(0));
    			} else if (choice == 0) {
    				break; } }
     
    		System.out.println(thirdStand.generateJustSold("Third Stand", 0));
     
    		System.out.println(HotDogStand.totalSold());		
    	}
    }

  4. #4
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Hot Dog Stand Code

    If you were blindfolded and it was your job to count how many hotdogs I sold and I called out "justSold" every time I sold a hotdog how would you keep track? Would you require me to give your a number each time I sold a hotdog? Before you answer "what if you sold 3 hotdogs?" then I would call out "justSold justSold justSold". there is no need for me to give you a number.


    "If I only return the total wouldn't that carry over for each stand? "

    I'm not sure what you are asking here but each object of HotDogStand would have its own instance variable keeping track of the number of hotdogs sold.
    Improving the world one idiot at a time!

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

    Default Re: Hot Dog Stand Code

    What I mean is that if I'm using a static variable to count the amount of hot dogs sold for each stand wouldn't that value carry over once I finished with the input for stand 1?

    For example, let's say Stand 1 sells 5 hot dogs. Because I'm using a static variable, that value counts for all of the objects, right? Or am I misunderstanding something?

    EDIT: The problem is also asking me to keep track of each stand's individual hot dog count. How would I make a static method that keeps track of the total amount of hot dogs sold work? Sorry, I'm confused.

    public class HotDogStand {
     
    	// instance variables
     
    	private String hotDogStandID;
    	private int hotDogsSold;
    	private static int totalSold = 0;
     
    	// mutators
     
    	public void setHotDogStandID(String standID) {
    		hotDogStandID = standID; }
     
    	public void setHotDogsSold(int sold) {
    		hotDogsSold = sold; }
     
    	public void setAll(String standID, int sold) {
    		hotDogStandID = standID;
    		hotDogsSold = sold; }
     
    	// constructors
     
    	public HotDogStand() {
    		hotDogStandID = "A Hot Dog Stand";
    		hotDogsSold = 0; }
     
    	public HotDogStand(String standID, int sold) {
    		hotDogStandID = standID;
    		if(hotDogsSold >= 0) {
    			hotDogsSold = sold;
    		} else {
    			System.out.println("Error! Stop giving away free hot dogs!");
    			System.exit(0); } }
     
    	// accessors
     
    	public String getHotDogStandID() {
    		return hotDogStandID; }
     
    	public int getHotDogsSold() {
    		return hotDogsSold; }
     
    	public int getTotalSold() {
    		return totalSold; }
     
    	// other methods
     
    	public int justSold(int sold) {
    		sold = hotDogsSold;
    		hotDogsSold = hotDogsSold+1;
    		return hotDogsSold; }
     
    	public String generateJustSold(String standID, int sold) {
    		sold = hotDogsSold;
    		return "You have sold "+hotDogsSold+" hot dogs for stand "+standID+"!"; }
     
    	public static int totalSold() {
    		return totalSold++; }
     
    }

    import javax.swing.JOptionPane;
    public class StandDemo {
    	public static void main(String[] args) {
     
    		String nameStandOne = JOptionPane.showInputDialog("What is the first stand's name?");
    		String inputFirstSold = JOptionPane.showInputDialog("How many hot dogs have been sold so far for "+nameStandOne+"?");
    		int firstSold = Integer.parseInt(inputFirstSold);
    		HotDogStand firstStand = new HotDogStand(nameStandOne, firstSold);
     
    		String nameStandTwo = JOptionPane.showInputDialog("What is the second stand's name?");
    		String inputSecondSold = JOptionPane.showInputDialog("How many hot dogs have been sold so far for "+nameStandTwo+"?");
    		int secondSold = Integer.parseInt(inputSecondSold);
    		HotDogStand secondStand = new HotDogStand(nameStandTwo, secondSold);
     
    		String nameStandThree = JOptionPane.showInputDialog("What is the third stand's name?");
    		String inputThirdSold = JOptionPane.showInputDialog("How many hot dogs have been sold so far for "+nameStandThree+"?");
    		int thirdSold = Integer.parseInt(inputThirdSold);
    		HotDogStand thirdStand = new HotDogStand(nameStandThree, thirdSold);
     
    		int k = 1;
    		while(k != 0) {
    			String choiceWord = JOptionPane.showInputDialog("Enter 1 to sell a hot dog! Enter 0 for the next stand.");
    			int choice = Integer.parseInt(choiceWord);
    			if (choice == 1) {
    				HotDogStand.totalSold();
    				System.out.println("Hot dogs sold: "+firstStand.justSold(firstSold));
    			} else if(choice == 0) {
    				break; } }
     
    		System.out.println(firstStand.generateJustSold(nameStandOne, firstSold));
     
    		while(k != 0) {
    			String choiceWord = JOptionPane.showInputDialog("Enter 1 to sell a hot dog! Enter 0 for the next stand.");
    			int choice = Integer.parseInt(choiceWord);
    			if (choice == 1) {
    				HotDogStand.totalSold();
    				System.out.println("Hot dogs sold: "+secondStand.justSold(secondSold));
    			} else if(choice == 0) {
    				break; } }
     
    		System.out.println(secondStand.generateJustSold(nameStandTwo, secondSold));
     
    		while(k != 0) {
    			String choiceWord = JOptionPane.showInputDialog("Enter 1 to sell a hot dog! Enter 0 to show how many total hot dogs you've sold.");
    			int choice = Integer.parseInt(choiceWord);
    			if (choice == 1) {
    				HotDogStand.totalSold();
    				System.out.println("Hot dogs sold: "+thirdStand.justSold(0));
    			} else if (choice == 0) {
    				break; } }
     
    		System.out.println(thirdStand.generateJustSold(nameStandThree, thirdSold));
     
    		System.out.println("Total amount of hot dogs sold is: "+HotDogStand.totalSold());		
    	}
    }

    In any case, here's the code so far. The static variable works, and the logic is sound, I guess. I don't suppose you have any tips for trimming it? It looks pretty bulky.

  6. #6
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Hot Dog Stand Code

    Quote Originally Posted by Powerbomb View Post
    What I mean is that if I'm using a static variable to count the amount of hot dogs sold for each stand wouldn't that value carry over once I finished with the input for stand 1?
    You should be able to answer that question. Why is is static?

    --- Update ---

    Ahhh!

    You have 2 variables:
    totalsold which I assume is for all stands then that does need to be static.
    hotDogsSold which is for one hotdog stand and should be an instance variable.

    You have made your code much harder than it needs to be.
    public void justSold() {
        hotDogsSold++;
    }
    That's it!
    Improving the world one idiot at a time!

  7. The Following User Says Thank You to Junky For This Useful Post:

    Powerbomb (October 26th, 2013)

  8. #7
    Junior Member
    Join Date
    Oct 2013
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Hot Dog Stand Code

    All right, I got it. Thanks for the help and patience, Junky.

Similar Threads

  1. Stand Alone Classes
    By Andrew R in forum Java Theory & Questions
    Replies: 4
    Last Post: August 18th, 2013, 08:04 PM
  2. Dog Tester Class
    By mwardjava92 in forum Object Oriented Programming
    Replies: 4
    Last Post: February 11th, 2012, 05:33 PM
  3. Dog Class
    By mwardjava92 in forum Object Oriented Programming
    Replies: 1
    Last Post: February 11th, 2012, 02:21 PM
  4. Old Dog Learning New Tricks!
    By Max Peck in forum Member Introductions
    Replies: 2
    Last Post: August 29th, 2011, 03:51 PM