Results 1 to 20 of 33
- 06-14-2011, 02:06 AM #1
Member
- Join Date
- Jun 2011
- Posts
- 40
- Rep Power
- 0
- 06-14-2011, 02:15 AM #2code I have so far is mangled and won't compile
- 06-14-2011, 02:20 AM #3
Have you written the Person class? If so show your code.
Also, don't just dump your code and say ir doesn't work. That provides zero information. As asked above paste full and exact error messages. Most importantly, ask a specific question.
- 06-14-2011, 02:22 AM #4
Member
- Join Date
- Jun 2011
- Location
- San Diego, CA
- Posts
- 24
- Rep Power
- 0
Have you tried creating the Person class? If so, please paste that code as well.
Last edited by JDScoot; 06-14-2011 at 02:24 AM. Reason: Sorry, Junky already said this
- 06-20-2011, 03:11 AM #5
Member
- Join Date
- Jun 2011
- Posts
- 40
- Rep Power
- 0
I have just edited my post, I appreciate all the responses but wouldn't help a little if I posted all the code?
- 06-20-2011, 03:24 AM #6
Are you finished with this thread now? Without code or error messages there is nothing anyone can do.
- 06-20-2011, 03:44 AM #7
Member
- Join Date
- Jun 2011
- Posts
- 40
- Rep Power
- 0
Oh now I need a lot of help well really just a steer in the right direction I think i just need to figure these set methods out.
Here's the static code
Java Code:public class TestPerson { public static void main(String [] args) { Person pete = new Person("Townsend", 100, true); Person marie = new Person("Osmond", 500, false); pete.spend(10); pete.spend(5); pete.spend(5); System.out.println(pete.toString()); pete.earned(200); System.out.println(pete); pete.says(); System.out.println("Can Pete afford something that is $3?"); if (pete.canAfford(3)) System.out.println("Yes, Pete can afford that"); else System.out.println("Nope, too much!"); System.out.println("Can Pete afford something that is $500?"); if (pete.canAfford(500)) System.out.println("Yes, Pete can afford that"); else System.out.println("Nope, too much!"); if (pete.isHappy()) System.out.println("Yes"); else System.out.println("No"); marie.spend(-10); System.out.println(marie); marie.earned(-100); System.out.println(marie); marie.spend(20000); System.out.println(marie); marie.earned(30); marie.earned(30); int current = marie.getDollars(); System.out.printf("Marie currently has %d dollars\n", current); if (marie.isHappy()) System.out.println("Yes"); else System.out.println("No"); Person george = new Person("Bush", -10, true); String s = george.toString(); System.out.println(s); System.out.printf("Currently has: %d\n", george.getDollars()); george.setHappy(false); george.says(); } }
Java Code:public class Person extends Object { private String lastName; private double Money; private boolean mentalState; public Person(String last, double wealth, boolean happy) { lastName = last; setWealth( wealth ); mentalState = happy; } public void setLastName( String last ) { lastName = last; } public String getLastName() { return lastName; } public void setWealth( double wealth ) { Money = ( wealth < 0.0 ) ? 50 : wealth; } public void spend( double wealth ) { Money = wealth - spend; } @Override public String toString() { return String(lastName, " has ", Money, " dollars."); } }
- 06-20-2011, 03:50 AM #8
Do you have a question? Then ask it.
Do you have error messages? Then post them.
Java Code:public void spend( double wealth ) { Money = wealth - spend; }
By the way, variables should be lowercase.
- 06-20-2011, 03:52 AM #9
Member
- Join Date
- Jun 2011
- Posts
- 40
- Rep Power
- 0
Here's my question, in the TestPerson class there is a set method for spend which is pete.spend(10) how do I subtract that number from the total wealth?
The code you posted was exactly the code in questioned, I don't know how to do the calculation.
-
A couple of recommendations:
- Never add good code to bad. You should start with a small skeleton of your program, just the main method to start with and compile. Each time you add a little bit of code, a line perhaps, you compile. You must fix all compile errors before adding any more code. Otherwise you end up with nothing but a rat's nest of errors.
- Don't try to make things up such as try to use methods that don't exist. The compiler is strict and unforgiving. If you don't know what to do, check your text, notes, and tutorials, but don't type in random code and expect it to work.
I suggest that you scrap your current program and start over.
- 06-20-2011, 03:55 AM #11
Dear Lord!
I just explained it at the end of my post. It is a simple calculation that if you know how to do in real life then it is a simple matter to write the code.
- 06-20-2011, 03:59 AM #12
Member
- Join Date
- Jun 2011
- Posts
- 40
- Rep Power
- 0
The problem with only writing good code, is I have to write the entire program before I can even test is because if I try to compile a little of it it spits out all these no such methods. So I try to write code to just make it compile so that I can debug the code I am trying to make work.
Junky, obviously if I have 10 dollars and I need to spend 4 I am subtracting 4 from 10 to get 6. I don't know how to introduce the number called however.
when I call a method such as spend with an argument of 5 how do I get that to subtract correctly in the other file?Last edited by CGHMN; 06-20-2011 at 04:02 AM.
-
This reply doesn't hold water as you can always write a skeleton of the method (a method "stub") first if need be and flesh it out later, or write the method fully before using it. Your current code is full of compilation errors, almost too many to count, so you can't keep doing what you're doing. Trust me, if you plan out your program first and then incrementally create your code, you and your code will be much happier.
- 06-20-2011, 04:18 AM #14
Member
- Join Date
- Jun 2011
- Posts
- 40
- Rep Power
- 0
See the way I look at it, is my code as it is, is scratch and essentially concocted with method stubs. So I am building a skeleton but I am being sidetracted by this one issue that I can't seem to solve by just waiting it out. What I need to know for just now is how to pass a double argument to a seperate class.
This reason this bit of information is important to me is once I understand that. Then I can implent the canAfford method and the getDollars method which only leaves that stupid isHappy boolean method and the stupid says method which doesn't seem that complicated on the count it's just a string formatLast edited by CGHMN; 06-20-2011 at 04:22 AM.
- 06-20-2011, 04:47 AM #15
As I said it is a simple calculation.
As to what Fubar said, this is what he is getting at:
Java Code:class Person { public void spend(double value) { System.out.println("In spend method"); } public static void main(String[] args) { Person x = new Person(); x.spend(3.5); } }
Last edited by Junky; 06-20-2011 at 04:56 AM. Reason: Oops!. Now it will compile.
- 06-20-2011, 04:50 AM #16
x.spend();
vs
x.spend(50.0);
System.out.println("In spend method value = " + value); // show value passed to method
- 06-20-2011, 05:41 AM #17
Member
- Join Date
- Jun 2011
- Posts
- 40
- Rep Power
- 0
All that code is in the TestPerson class. it looks like pete.spend(50) I need to know to use that in the person class. how to program it.
And I have one more different question do I ever have to distinguish between people ever, in this class? Because it seems like it's just a constructor for the TestPerson to figure it out.
I know this will make you guys mad but I have added the following code taken in with your guy's advice.
Java Code:@Override public String toString() { return String.format(lastName, " has ", Money, " dollars."); } public String says() { return String.format("My name is ", lastName, mentalState); } public boolean canAfford(double wealth) { if(wealth >= canAfford) return true; else return false; } }
Last edited by CGHMN; 06-20-2011 at 06:00 AM. Reason: Just adding more info.
- 06-20-2011, 05:58 AM #18
Forget the TestPerson class for now. Do you see in the code I posted above I have added my own main method that tests the Person class? Well do that yourself.
Sheesh, I'll do it for you
Java Code:class Person { private String lastName; private double money; private boolean mentalState; public Person(String last, double wealth, boolean happy) { lastName = last; money = wealth; mentalState = happy; } public void spend(double value) { System.out.println("In spend method"); } public String toString() { return lastName + " has " + money + " dollars."; } public static void main(String [] args) { Person pete = new Person("Townsend", 100, true); pete.spend(10); System.out.println(pete); } }
- 06-20-2011, 06:34 AM #19
Member
- Join Date
- Jun 2011
- Posts
- 40
- Rep Power
- 0
Alright man that really helped out a lot. I even got the program running! Now it's just a matter of tinkering with it to make it work. Because the output isn't what it's suppose to be. Now I know it's annoying a rude to be posting long code every post but I don't know the vB code to make it into a shorter box that you can scroll but here's my almost finished code.
Java Code:public class Person extends Object { private String lastName; private int Money; private boolean mentalState; public Person(String last, int wealth, boolean happy) { lastName = last; setWealth( wealth ); mentalState = happy; } public void setLastName( String last ) { lastName = last; } public String getLastName() { return lastName; } public void setWealth( int wealth ) { Money = ( wealth < 0.0 ) ? 50 : wealth; } public void spend( int wealth ) { wealth = Money - wealth; } @Override public String toString() { return String.format(lastName, " has ", Money, " dollars."); } public String says() { return String.format("My name is ", lastName, mentalState); } public boolean canAfford(int wealth) { if(wealth >= Money) return true; else return false; } public void earned(int wealth) { wealth = Money + wealth; } public boolean isHappy() { if(Money>0) return true; else return false; } public int getDollars() { return Money; } public void setHappy(boolean happy) { mentalState = happy; } }
Townsend
Townsend
Can Pete afford something that is $3?
Nope, too much!
Can Pete afford something that is $500?
Yes, Pete can afford that
Yes
Osmond
Osmond
Osmond
Marie currently has 500 dollars
Yes
Bush
Currently has: 50
Townsend has 80 dollars
Townsend has 280 dollars
My name is Townsend and I am doing great!
Can Pete afford something that is $3?
Yes, Pete can afford that
Can Pete afford something that is $500?
Nope, too much!
Yes
Osmond has 500 dollars
Osmond has 500 dollars
Osmond has 0 dollars
Marie currently has 60 dollars
No
Bush has 50 dollars
Currently has: 50
My name is Bush and I could be better.
- 06-20-2011, 06:44 AM #20Java Code:
public void spend( int wealth ) { wealth = Money - wealth; }
Similar Threads
-
How to code a program to send messages to a chat program?
By josh2992 in forum New To JavaReplies: 2Last Post: 04-02-2011, 01:57 PM -
tricky initial value problem
By Black_Eye in forum New To JavaReplies: 4Last Post: 10-19-2010, 10:02 AM -
changing my program to array working program
By Chewart in forum New To JavaReplies: 39Last Post: 11-18-2009, 07:53 PM -
Tricky but very interesting problem
By ravjot28 in forum New To JavaReplies: 4Last Post: 06-26-2008, 02:43 PM -
tricky indexOf implementation -- Help!!
By definewebsites in forum New To JavaReplies: 3Last Post: 12-10-2007, 01:48 AM
Bookmarks