Results 1 to 9 of 9
  1. #1
    globo is offline Member
    Join Date
    Sep 2010
    Posts
    61
    Rep Power
    0

    Default 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.

  2. #2
    collin389 is offline Senior Member
    Join Date
    Nov 2009
    Posts
    235
    Rep Power
    5

    Default

    Well, good luck with that; let us know if you need any help.

  3. #3
    globo is offline Member
    Join Date
    Sep 2010
    Posts
    61
    Rep Power
    0

    Default

    Well why don't you try.

  4. #4
    dinosoep is offline Senior Member
    Join Date
    Nov 2009
    Posts
    150
    Rep Power
    5

    Default

    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

  5. #5
    globo is offline Member
    Join Date
    Sep 2010
    Posts
    61
    Rep Power
    0

    Default

    well I am asking help because I got stock. Not because I am lazy.

  6. #6
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    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.

  7. #7
    globo is offline Member
    Join Date
    Sep 2010
    Posts
    61
    Rep Power
    0

    Default

    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

  8. #8
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    Fair enough; best of luck.

  9. #9
    globo is offline Member
    Join Date
    Sep 2010
    Posts
    61
    Rep Power
    0

    Default

    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

  1. Create a java class for a bank account!!?
    By singh345 in forum New To Java
    Replies: 1
    Last Post: 03-17-2010, 05:26 PM
  2. [SOLVED] When should one create a new class?
    By porchrat in forum New To Java
    Replies: 3
    Last Post: 04-29-2009, 02:44 AM
  3. How to create class from a C structure
    By jcardozo in forum New To Java
    Replies: 8
    Last Post: 12-06-2008, 11:47 AM
  4. How to create a class in java?
    By pawankumarom in forum New To Java
    Replies: 2
    Last Post: 09-05-2008, 08:47 AM
  5. How to create main class link to another two class?
    By pearllymary78 in forum New To Java
    Replies: 6
    Last Post: 07-17-2008, 12:02 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
  •