Results 1 to 20 of 36
Thread: Simple java program help.
- 07-14-2011, 11:41 PM #1
Member
- Join Date
- Jun 2011
- Posts
- 40
- Rep Power
- 0
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
this is my code right underneath it.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); } }
my compiler errors are:Java Code:public class LemonadeStand(); { private double lemonSales; public class sell(double sales) { lemonSales = sales; } }
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 }
- 07-15-2011, 12:16 AM #2
Senior Member
- Join Date
- Nov 2010
- Posts
- 210
- Rep Power
- 3
You seem to be confusing classes with methods.
Lose the brackets and the semicolon.Java Code:public class LemonadeStand();
Pretty sure you mean "void" instead of "class".Java Code:public class sell(double sales) { lemonSales = sales; }
You'll need to override toString() for your program to produce anything meaningful though.
- 07-15-2011, 12:31 AM #3
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?Java Code:public class sell(double sales) { lemonSales = sales; }
- 07-15-2011, 01:54 AM #4
Member
- Join Date
- Jun 2011
- Posts
- 40
- Rep Power
- 0
Okay I have changed up the code so far to this but it is still giving me errors.
New code
my errors.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" ); } }
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
- 07-15-2011, 02:00 AM #5
Senior Member
- Join Date
- Jun 2008
- Posts
- 339
- Rep Power
- 5
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.
- 07-15-2011, 02:10 AM #6
- 07-15-2011, 02:14 AM #7
Member
- Join Date
- Jun 2011
- Posts
- 40
- Rep Power
- 0
Considering that I did this :
now I getJava 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" ); }
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
- 07-15-2011, 02:22 AM #8
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.
- 07-15-2011, 02:45 AM #9
Member
- Join Date
- Jun 2011
- Posts
- 40
- Rep Power
- 0
I have taken that into consideration and I am splitting the files.
- 07-15-2011, 02:46 AM #10
While I am a big advocate for having each class in its own file, splitting them has nothing to do with your problems.
- 07-15-2011, 03:04 AM #11
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
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.
- 07-15-2011, 03:47 AM #12
Member
- Join Date
- Jun 2011
- Posts
- 40
- Rep Power
- 0
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
- 07-15-2011, 03:55 AM #13
What on earth are you talking about?
That is not valid syntax and has nothing to do with a StringTokenizer. Where did you get that from?Java Code:public class LemonadeStand(int lemonSales) {
- 07-15-2011, 04:13 AM #14
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
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>"
- 07-15-2011, 04:51 AM #15
Member
- Join Date
- Jun 2011
- Posts
- 40
- Rep Power
- 0
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.
Heres the errorJava 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" ); } }Java Code:Neighborhood.java:5: cannot find symbol symbol : constructor LemonadeStand() location: class LemonadeStand LemonadeStand stand1 = new LemonadeStand(); ^ 1 error
- 07-15-2011, 04:57 AM #16
Member
- Join Date
- Jun 2011
- Posts
- 40
- Rep Power
- 0
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.
- 07-15-2011, 04:57 AM #17
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
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.
- 07-15-2011, 05:04 AM #18
Member
- Join Date
- Jun 2011
- Posts
- 40
- Rep Power
- 0
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.
-
- 07-15-2011, 05:22 AM #20
Member
- Join Date
- Jun 2011
- Posts
- 40
- Rep Power
- 0
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