Results 21 to 36 of 36
Thread: Simple java program help.
- 07-15-2011, 05:44 AM #21
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.
- 07-15-2011, 05:49 AM #22
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
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 05:54 AM.
- 07-15-2011, 06:02 AM #23
Member
- Join Date
- Jun 2011
- Posts
- 40
- Rep Power
- 0
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" ); } }
- 07-15-2011, 06:34 AM #24
Clearly you don't.
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!Java Code:LemonadeStand stand1 = new LemonadeStand(); LemonadeStand stand2 = new LemonadeStand(10);
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
That provides zero information. I don't even know what it means.but it's getting caught at the sell method.
- 07-15-2011, 06:55 AM #25
Member
- Join Date
- Jun 2011
- Posts
- 40
- Rep Power
- 0
Hey I got this working ( sort of ) it doesn't give the right output but who cares.
Now I have to create a class that extends this that adds a different person and how much their stand sold.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" ); } }
- 07-15-2011, 07:03 AM #26
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???Java Code:public void sell(double sales) { lemonSales = sales+sales+sales; }
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?
- 07-15-2011, 07:05 AM #27
Member
- Join Date
- Jun 2011
- Posts
- 40
- Rep Power
- 0
I would write down the numbers then add them up and tell you that number.
- 07-15-2011, 07:09 AM #28
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
- 07-15-2011, 07:14 AM #29
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
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)
- 07-15-2011, 07:15 AM #30
Member
- Join Date
- Jun 2011
- Posts
- 40
- Rep Power
- 0
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"; } }
- 07-15-2011, 07:20 AM #31
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
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
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.Java Code:lemonSales = lemonSales + sales; or lemonSales += sales;
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.
- 07-15-2011, 07:21 AM #32
FFS
Are you just trolling.
You have to add each new sales to the old total and then update the total.Java Code:lemonSales = lemonSales + sales; // or lemonSales += sales;
- 07-15-2011, 07:24 AM #33
Member
- Join Date
- Jun 2011
- Posts
- 40
- Rep Power
- 0
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
- 07-15-2011, 07:38 AM #34
Whatever!
I'm done helping this doofus.
- 07-15-2011, 11:40 AM #35
A minor setback in improving the world, huh?
- 07-15-2011, 12:31 PM #36
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
Going to the compilation error:
Compare that (that it is complaingin about) with your constructor declaration:Java Code:LemonadeStand stand1 = new LemonadeStand();
The first one is calling a constructor which has no parameters.Java Code:public LemonadeStand(double sales)
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.
Similar Threads
-
First Java Program-Has Errors-simple GUI
By cc11rocks in forum New To JavaReplies: 1Last Post: 01-04-2011, 12:15 AM -
Simple java program, need help
By cliffh in forum New To JavaReplies: 1Last Post: 10-21-2010, 03:32 AM -
Simple Java program
By Rolle in forum New To JavaReplies: 3Last Post: 10-26-2009, 04:05 PM -
help with simple program in java
By katie in forum New To JavaReplies: 2Last Post: 08-06-2007, 08:03 PM -
help with simple java program
By leonard in forum New To JavaReplies: 3Last Post: 07-30-2007, 09:40 AM


3Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks