Page 1 of 2 12 LastLast
Results 1 to 20 of 36
Like Tree3Likes

Thread: Simple java program help.

  1. #1
    CGHMN is offline Member
    Join Date
    Jun 2011
    Posts
    40
    Rep Power
    0

    Default Simple java program help.

    Hi I have this neighborhood class and I am suppose to right a lemonadestand class for it that keep tracks of the amount of sales at the stand.

    This is the neighborhood class
    Java Code:
    public class Neighborhood
    {
      public static void main(String [] args)
      {
        LemonadeStand stand1 = new LemonadeStand();
    	 stand1.sell(3);
    	 stand1.sell(2);
    	 stand1.sell(1);
    	 System.out.println(stand1.toString());
    	 
    	 LemonadeStand stand2 = new LemonadeStand(10);
    	 stand2.sell(5);
    	 System.out.println(stand2);
      }
    }
    this is my code right underneath it.
    Java Code:
    public class LemonadeStand();
    {
      private double lemonSales;
      public class sell(double sales)
      {
        lemonSales = sales;
    	 
      }
    }
    my compiler errors are:
    Java Code:
    Neighborhood.java:17: '{' expected
    public class LemonadeStand();
                              ^
    Neighborhood.java:20: '{' expected
      public class sell(double sales)
                       ^
    Neighborhood.java:20: ';' expected
      public class sell(double sales)
                                    ^
    Neighborhood.java:25: reached end of file while parsing
    }

  2. #2
    Iron Lion is offline Senior Member
    Join Date
    Nov 2010
    Posts
    210
    Rep Power
    4

    Default

    You seem to be confusing classes with methods.

    Java Code:
    public class LemonadeStand();
    Lose the brackets and the semicolon.

    Java Code:
      public class sell(double sales)
      {
        lemonSales = sales;
    	 
      }
    Pretty sure you mean "void" instead of "class".

    You'll need to override toString() for your program to produce anything meaningful though.

  3. #3
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    Java Code:
    public class sell(double sales) {
        lemonSales = sales;
    }
    Another problem: your LemonadeStand class will only hold the last value passed to the sell method. So in your example it will hold 1 and the previous calls of 3 and 2 will be lost. Shouldn't it be keeping a total of all sales made?

  4. #4
    CGHMN is offline Member
    Join Date
    Jun 2011
    Posts
    40
    Rep Power
    0

    Default

    Okay I have changed up the code so far to this but it is still giving me errors.

    New code
    Java Code:
    class LemonadeStand
    {
      private double lemonSales;
      public void sell(double sales)
      {
        
        lemonSales = sales;
    	
      }
      @Override	
    	public	String	toString()
    	{
    		return String.format( "Stan sold "+sales.sell()+" cups" );
    	}
    
    }
    my errors.
    Java Code:
    Neighborhood.java:11: cannot find symbol
    symbol  : constructor LemonadeStand(int)
    location: class LemonadeStand
    	 LemonadeStand stand2 = new LemonadeStand(10);
    	                        ^
    Neighborhood.java:29: cannot find symbol
    symbol  : variable sales
    location: class LemonadeStand
    		return String.format( "Stan sold "+sales.sell()+" cups" );
    		                                   ^
    2 errors

  5. #5
    dlorde is offline Senior Member
    Join Date
    Jun 2008
    Posts
    339
    Rep Power
    7

    Default

    The compiler is telling you that it can't find a constructor for LemonadeStand that takes an int argument. So I guess you should write one.

    It's also telling you that it can't find a variable called 'sales' in the toString() method (this will be because it's not a class variable or declared in the toString() method).

    Writing constructors is pretty basic stuff - we can't really teach Java here, we're basically here to help people who get stuck. We can help, but you really ought to know about constructors already.
    Last edited by dlorde; 07-15-2011 at 02:03 AM.

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    Quote Originally Posted by CGHMN View Post
    Okay I have changed up the code
    So you decided to introduce more errors into your code before fixing all the other errors already pointed out to you.

  7. #7
    CGHMN is offline Member
    Join Date
    Jun 2011
    Posts
    40
    Rep Power
    0

    Default

    Considering that I did this :

    Java Code:
    public class LemonadeStand(int lemonSales)
    {
      private int lemonSales;
    
    }
    
      public void sell(int sales)
      {
        
        lemonSales = sales;
    	
      }
      
      @Override	
    	public	String	toString()
    	{
    		return String.format( "Stan sold "+sales.sell()+" cups" );
    	}
    now I get

    Java Code:
    Neighborhood.java:17: '{' expected
    public class LemonadeStand(int lemonSales);
                              ^
    Neighborhood.java:17: ';' expected
    public class LemonadeStand(int lemonSales);
                                             ^
    Neighborhood.java:19: illegal start of expression
      private int lemonSales;
      ^
    Neighborhood.java:34: reached end of file while parsing
    	}
    	 ^
    4 errors

  8. #8
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    Compare the LemonStand class to the Neighborhood class. Do they look the same. What is different about them. Stop guessing and making random changes to your code. You need to fully understand what you are doing. If you don't then go back and review the basics. Stop writing code until you do understand.
    sunde887 and CGHMN like this.

  9. #9
    CGHMN is offline Member
    Join Date
    Jun 2011
    Posts
    40
    Rep Power
    0

    Default

    I have taken that into consideration and I am splitting the files.

  10. #10
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    While I am a big advocate for having each class in its own file, splitting them has nothing to do with your problems.

  11. #11
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    As junky said, splitting the files is fine, but not entirely necessary. If the files are split, You should have each class containing a similar structure. Containing state(variables) and behavior(methods), also the compiler messages will make sense to you if you make efforts to understand and properly modify the code. Don't hesitate to read your textbook or tutorials if you need clarification.

  12. #12
    CGHMN is offline Member
    Join Date
    Jun 2011
    Posts
    40
    Rep Power
    0

    Default

    I've been going through the book and the packets of information. Also I'm going through my old code and this should work. I think it has a problem interpreting the variables. It seems I have to run a tokenizer up the String balancer

  13. #13
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    What on earth are you talking about?
    Java Code:
    public class LemonadeStand(int lemonSales)
    {
    That is not valid syntax and has nothing to do with a StringTokenizer. Where did you get that from?

  14. #14
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Ya, you are confusing some things.

    The entire class should have the following: one instance variable, two methods, and two constructors. Look those up on google by typing "java tutorial <keyword>"

  15. #15
    CGHMN is offline Member
    Join Date
    Jun 2011
    Posts
    40
    Rep Power
    0

    Default

    I have made some progress and now the LemonadeStand.java will compile but for some reason the set method from the Neighborhood class cannot find the LemonadeStand method that is clearly defined in the other class. Unless my constructor is completely off. well here is my code which is now it's own separate file.
    Java Code:
    public class LemonadeStand extends Neighborhood
    {
      private double lemonSales;
      
      public LemonadeStand(double sales)
      {
            lemonSales = sales;
      }
      public void sell(double sales)
      {
        
        lemonSales = sales;
    	
      }
      /*public void getSales(int sales);
      {
        lemonSales = sales;
        return sales;
      }*/
      
      @Override	
    	public	String	toString()
    	{
    		return String.format( "Stan sold "+" cups" );
    	}
    }
    Heres the error
    Java Code:
    Neighborhood.java:5: cannot find symbol
    symbol  : constructor LemonadeStand()
    location: class LemonadeStand
        LemonadeStand stand1 = new LemonadeStand();
                               ^
    1 error

  16. #16
    CGHMN is offline Member
    Join Date
    Jun 2011
    Posts
    40
    Rep Power
    0

    Default

    I'm trying to just get the skeleton up and running so then I go through and so the simple task it want's from me. I don't understand why it cannot call to the LemonadeStand method.

  17. #17
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    You should not be using inheritance. If you do use inheritance you should think to yourself, "is a lemonade stand a neighborhood?", if it isn't you should never use inheritance. I think it's a safe statement to say that you are still in the beginning stages and should not be using inheritance for a while.

    Next, you should not have a setter method, the only methods you should have is toString() and sell(int x).

    Then you need to look up constructors and overloading them.
    Fubarable likes this.

  18. #18
    CGHMN is offline Member
    Join Date
    Jun 2011
    Posts
    40
    Rep Power
    0

    Default

    Well inheritance doesn't sound too off. I think I am really far behind in this class which is sad because it's intro to java 2. Maybe I need to figure out why my constructor isn't doing it's job perhaps the parameters are wrong.

  19. #19
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by CGHMN View Post
    Well inheritance doesn't sound too off.
    Yeah, it does. Really.

  20. #20
    CGHMN is offline Member
    Join Date
    Jun 2011
    Posts
    40
    Rep Power
    0

    Default

    You know what taking that into consideration I have eliminated all traces of inheritance.

Page 1 of 2 12 LastLast

Similar Threads

  1. First Java Program-Has Errors-simple GUI
    By cc11rocks in forum New To Java
    Replies: 1
    Last Post: 01-04-2011, 12:15 AM
  2. Simple java program, need help
    By cliffh in forum New To Java
    Replies: 1
    Last Post: 10-21-2010, 03:32 AM
  3. Simple Java program
    By Rolle in forum New To Java
    Replies: 3
    Last Post: 10-26-2009, 04:05 PM
  4. help with simple program in java
    By katie in forum New To Java
    Replies: 2
    Last Post: 08-06-2007, 08:03 PM
  5. help with simple java program
    By leonard in forum New To Java
    Replies: 3
    Last Post: 07-30-2007, 09:40 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
  •