Results 1 to 9 of 9
- 09-20-2010, 09:20 PM #1
Member
- Join Date
- Sep 2010
- Posts
- 61
- Rep Power
- 0
I need to create a comlicated java class
Write a class Person that correctly compiles and runs with the following TestPerson code. You cannot change a single thing in the TestPerson class. Your Person class should keep track of a person's name, how much money that have on them (in dollars), and whether or not they are happy. The Person class needs to implement the methods used in the code below. Here are a few special notes on some of the methods:
* The Person constructor sets the amount of dollars the person has to $50 if the initial dollar amount is invalid (i.e., less than zero).
* The spend method does nothing if the value they try to spend is less than zero. If they try to spend more than they currently have, then all that person's money is spent (i.e., if the person has $10 and they try to spend $30, then they spend all of the $10 and the amount they have left is $0).
* The canAfford method just returns true or false, indicating whether or not the person can afford something of the requested amount. The amount of money the person has does not change when this method is called.
* When the says method is called, the person "says" (prints to the console) one of two phrases -- either "My name is X and I am doing great!" if they are currently happy, or else "My name is X and I could be better." if they are not currently happy (X is replaced with their name).
TestPerson
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();
}
}
Required Output
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.
- 09-20-2010, 09:36 PM #2
Senior Member
- Join Date
- Nov 2009
- Posts
- 235
- Rep Power
- 4
Well, good luck with that; let us know if you need any help.
- 09-20-2010, 10:01 PM #3
Member
- Join Date
- Sep 2010
- Posts
- 61
- Rep Power
- 0
Well why don't you try.
- 09-20-2010, 10:07 PM #4
Senior Member
- Join Date
- Nov 2009
- Posts
- 150
- Rep Power
- 4
because its your homework ;)
we can do it but we can't do your exam. just do it yourself and come back with real problems
- 09-20-2010, 10:08 PM #5
Member
- Join Date
- Sep 2010
- Posts
- 61
- Rep Power
- 0
well I am asking help because I got stock. Not because I am lazy.
- 09-20-2010, 10:14 PM #6
Where are you stuck? What part of the assignment is stumping you? Are you getting errors? (If so, what are the EXACT errors received, along with line numbers and such?) If the code is not working as expected, what is it doing versus what should it be doing?
The more specific you are in your particular issues, the better we can help.
- 09-20-2010, 10:16 PM #7
Member
- Join Date
- Sep 2010
- Posts
- 61
- Rep Power
- 0
I am sorry I won't be able to explain that now. I have a night class I am going now. I will work on it tomorrow and once I am done I will post the solution.
Thanks for your concern anyways.
Best Regards
- 09-20-2010, 10:17 PM #8
- 10-07-2010, 05:50 PM #9
Member
- Join Date
- Sep 2010
- Posts
- 61
- Rep Power
- 0
Solution:
public class Person
{
public static int DEFAULT_DOLLARS = 50;
public String name;
public int dollars;
public boolean happy;
public Person(String name, int dollars, boolean happy)
{
this.name = name;
setDollars(dollars);
this.happy = happy;
}
private void setDollars(int amount)
{
if (amount >= 0)
dollars = amount;
else
dollars = DEFAULT_DOLLARS;
}
public void spend(int amount)// spend class
{
if (amount >= 0)
{
if (amount <= dollars)
dollars -= amount;
else
dollars = 0;
}
}
public void earned(int amount)//earned class
{
if (amount >= 0)
dollars += amount;
}
public boolean canAfford(int amount)//canAfford class
{
return amount <= dollars;
}
public int getDollars()
{
return dollars;
}
public boolean isHappy()
{
return happy;
}
public String toString()
{
return String.format("%s has %d dollars", name, dollars);
}
public void says()// says class
{
System.out.printf("My name is %s and I ", name);
if (happy)
System.out.println("am doing great!");
else
System.out.println("could be better.");
}
public void setHappy(boolean mood)
{
happy = mood;
}
}
Similar Threads
-
Create a java class for a bank account!!?
By singh345 in forum New To JavaReplies: 1Last Post: 03-17-2010, 04:26 PM -
[SOLVED] When should one create a new class?
By porchrat in forum New To JavaReplies: 3Last Post: 04-29-2009, 01:44 AM -
How to create class from a C structure
By jcardozo in forum New To JavaReplies: 8Last Post: 12-06-2008, 10:47 AM -
How to create a class in java?
By pawankumarom in forum New To JavaReplies: 2Last Post: 09-05-2008, 07:47 AM -
How to create main class link to another two class?
By pearllymary78 in forum New To JavaReplies: 6Last Post: 07-16-2008, 11:02 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks