Page 2 of 2 FirstFirst 12
Results 21 to 36 of 36
Like Tree3Likes

Thread: Simple java program help.

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

    Default

    Neighborhood.java:5: cannot find symbol
    symbol : constructor LemonadeStand()
    location: class LemonadeStand
    LemonadeStand stand1 = new LemonadeStand();
    ^
    1 error

    You have fallen foul of the constructor rules.
    - All classes have at least one constructor
    - If you do not write a constructor then the compiler will provide a default (no arg) constructor.
    - If you do write a comstructor then the compile WILL NOT provide a default (no arg) constructor

    Since you provided a constructor that takes a double parameter, the no arg constructor does not exist.

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

    Default

    Id like to give a simple explanation why inheritance is not the way to go, rather than just telling you to blindly listening to me. There are a few reasons why inheritance is bad in this case(and in many other cases), because people tend to inherit incorrectly, as you did. The one thing to test is if one class IS-A of the other class. So if a lemonade stand IS-A neighborhood, than by all means, use inheritance. But in my world I don't think a lemonade stand, there could very well be imaginary worlds where this relationship fits, but in ours it does not. So that's one reason why inheritance is bad in this situation.

    The other situation I must pose as a question, what is lemonade stand inheriting from neighborhood?

    Think about it, all neighborhood has is a main method, therefore it has neither state or behavior, simply a main method. So the inheritance doesn't actually inherit anything from the superclass, so what's the point? It's like if a kid had nothing inherited from parents at all.

    Using inheritance in this situation is analogous to using a bar of soap to fish. It's totally worthless.

    I highly suggest if you are stuck you spend some study time, and also supplement class learning with the tutorials provided by oracle, which can be found here: The Really Big Index

    Make sure you understand each step before moving on.

    The final thing I want to say is once again, he lemonade stand should have the following things:

    2 Methods:
    1) sell
    2) toString

    1 instance variable:
    1) sales

    2 constructors:
    1) default(no arg) constructor
    2) 1 parameter constructor which takes an int



    edit:

    To really push my anti inheritance sentiment, you should generally favor composition over inheritance and save inheritance for only when you are absolutely sure it is being used correctly.

    You can google Liskov substitution principle, but it's probably a bit more than you need to read.

    Composition relies on using instances of one class inside of the other. It's probably one of those things you are unfamiliar with still.
    Last edited by sunde887; 07-15-2011 at 06:54 AM.

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

    Default

    I understand now the Neighborhood class isn't sending any argument and I keep making one that says to take a double when i should leave it plain. and under that I should define a int and then make a sell() method that accepts an integer as an argument and keeps it as a variable object. I believe is what I have to do now. I can not seem to figure out how to define the sell method to work.

    I have put my code back inside the neighborhood class and now at lease it can pass that LemonStand but it's getting caught at the sell method.

    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);
      }
    }
    
    public class LemonadeStand
    {
    
    public class LemonadeStand()
    {
     private int lemonSales;
     public class sell(int sales)
     {
       lemonSales = sales;
     }
    }
      @Override	
    	public	String	toString()
    	{
    		return String.format( "Stan sold "+" cups" );
    	}
    	
    }

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

    Default

    Quote Originally Posted by CGHMN View Post
    I understand
    Clearly you don't.
    Java Code:
    LemonadeStand stand1 = new LemonadeStand();
    LemonadeStand stand2 = new LemonadeStand(10);
    The first line is calling the no arg constructor. The second line is calling the constructor with an int parameter. Do both constructors exist? NO!

    You have several choices:
    - provide both constructors
    - provide only the no arg constructor, then both lines above must have no parameters
    - provide only the int arg constructor, then both lines above must have an int parameter

    but it's getting caught at the sell method.
    That provides zero information. I don't even know what it means.

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

    Default

    Hey I got this working ( sort of ) it doesn't give the right output but who cares.

    Java Code:
    public class LemonadeStand 
    {
      private double lemonSales;
    
      public LemonadeStand() {
      
    }
    
      public LemonadeStand(double sales)
      {
    
      if(sales >= 3)
        lemonSales = sales+sales;
    
      }
    
    public void sell(double sales)
      {
    
       lemonSales = sales+sales+sales;
    
    }
    
    
    @Override    
        public    String    toString()
        {
      return String.format( "Stan sold "+ lemonSales + " cups" );
        }
    }
    Now I have to create a class that extends this that adds a different person and how much their stand sold.

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

    Default

    Java Code:
    public void sell(double sales) {
        lemonSales = sales+sales+sales;
    }
    If you sell 5 glasses, you total sales is going to be 15??? Then if you sell another 2 glasses you throw away 15 and make total sales 6???

    It is simple mathematics. If you sold 5 glasses and 2 glasses what would your total sales be? Forgetting about Java for a moment, if I called out to you 4 numbers and wanted you to tell me what the sum was, how would you do it?

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

    Default

    I would write down the numbers then add them up and tell you that number.

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

    Default

    OK but what you have said is a higher level of what happens. At a lower level what happens is:

    I call out a number
    You remember that number
    I call out a second number
    You then take that number, add it to the value you already remembered and then remember the new value
    etc

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

    Default

    Exactly, so the constructor should set the sales to be the input number, and the sell method should add the input to the current sales. The sales variable is supposed to be a running total. Every call to sales(1) should add 1 to the total. Extrapolate this for sales(2), sales(3), sales(4) .... sales(n)

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

    Default

    So if I wanted to translate that into java it would be

    lemonSales = sales;
    lemonSales = sales+sales; ?

    Anyway I was suppose to turn this in an hour ago. So I did the output is a little off now I am working on the second half of the assignment which is to make a class that extends the last one.

    which is here yet when I compile and run if gives the wrong output n
    Java Code:
    public class NamedStand extends LemonadeStand
    {
      private String guysName;
      private int lemonSales;
      public NamedStand(String name, int sales)
      {
        guysName = name;
    	 lemonSales = sales;
      }
      
      public void sell(int sales)
      {
       lemonSales = sales;
      }
      public void setName(String name)
      {
       name = "Robert";
      }
      
    }

  11. #31
    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 are getting close. Except lemonSales = sales+sales, just sets the lemonSales to twice the input. Instead you want to add lemonSales to the input, or
    Java Code:
    lemonSales = lemonSales + sales;
    
    or
    
    lemonSales += sales;
    People think on a much higher level than the language/computer so the concept of doing lemonSales = lemonSales + sales may seem strange, but it makes perfect sense to for the computer if you think about it.

    I really suggest you get this one working before moving on, you are quite close.

    There are things wrong with your new code, but building on broken code will hurt you more than anything. Figure one thing out, understand it, then move on.

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

    Default

    FFS

    Are you just trolling.

    Java Code:
    lemonSales = lemonSales + sales;
    // or
    lemonSales += sales;
    You have to add each new sales to the old total and then update the total.

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

    Default

    You know whats crazy, I can just pass stuff as String literals! No need to do anything at all if Im only returning one stupid line of text anyway! everything worked out great

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

    Default

    Whatever!

    I'm done helping this doofus.

  15. #35
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,457
    Rep Power
    20

    Default

    A minor setback in improving the world, huh?

  16. #36
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default

    Going to the compilation error:
    Java Code:
    LemonadeStand stand1 = new LemonadeStand();
    Compare that (that it is complaingin about) with your constructor declaration:
    Java Code:
    public LemonadeStand(double sales)
    The first one is calling a constructor which has no parameters.
    However the only constructor is one that takes a double. So it can't compile.

    Since you are creatig a new LemonadeStand it would (to me) make sense that you won't have sold anything yet, so just get rid of the parameter in that constructor.

Page 2 of 2 FirstFirst 12

Similar Threads

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