Page 1 of 2 12 LastLast
Results 1 to 20 of 33
  1. #1
    CGHMN is offline Member
    Join Date
    Jun 2011
    Posts
    40
    Rep Power
    0

    Default Need some help with tricky program

    Sorry for my original post guys, Heres my question I need to initiate a spend money method from the static class to the Object class but I am not sure how to use the variables correctly
    Last edited by CGHMN; 06-20-2011 at 02:10 AM. Reason: First post sucked

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    code I have so far is mangled and won't compile
    Please copy and paste here the full text of the error messages.

  3. #3
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,759
    Rep Power
    7

    Default

    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.

  4. #4
    JDScoot is offline Member
    Join Date
    Jun 2011
    Location
    San Diego, CA
    Posts
    24
    Rep Power
    0

    Default

    Have you tried creating the Person class? If so, please paste that code as well.
    Last edited by JDScoot; 06-14-2011 at 01:24 AM. Reason: Sorry, Junky already said this

  5. #5
    CGHMN is offline Member
    Join Date
    Jun 2011
    Posts
    40
    Rep Power
    0

    Default

    I have just edited my post, I appreciate all the responses but wouldn't help a little if I posted all the code?

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    Are you finished with this thread now? Without code or error messages there is nothing anyone can do.

  7. #7
    CGHMN is offline Member
    Join Date
    Jun 2011
    Posts
    40
    Rep Power
    0

    Default

    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();
       }
    }
    As you can see There's a lot of methods and variables I have to account for here is the code I have so far which compiles everything except for the spend variable which Is what I have the most trouble with.

    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.");
      }
    }
    and I am pretty much on track for the most part. But the spend and toString method is confusing me. And for a better understanding of the assignment I'll add the rubric for kicks.
    Attached Files Attached Files

  8. #8
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,759
    Rep Power
    7

    Default

    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;
      }
    I assume you get a "cannot find symbol" error on that code. What is spend? Think about it in real life. If you had $10.00 and spent $3.50, how much money would you have left? How did you work that out?

    By the way, variables should be lowercase.

  9. #9
    CGHMN is offline Member
    Join Date
    Jun 2011
    Posts
    40
    Rep Power
    0

    Default

    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.

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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.

  11. #11
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,759
    Rep Power
    7

    Default

    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.

  12. #12
    CGHMN is offline Member
    Join Date
    Jun 2011
    Posts
    40
    Rep Power
    0

    Default

    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 03:02 AM.

  13. #13
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by CGHMN View Post
    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.
    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.

  14. #14
    CGHMN is offline Member
    Join Date
    Jun 2011
    Posts
    40
    Rep Power
    0

    Default

    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 format
    Last edited by CGHMN; 06-20-2011 at 03:22 AM.

  15. #15
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,759
    Rep Power
    7

    Default

    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);
        }
    }
    There are two things to note. A lot of the code has been omitted which goes towards what Fubar said: only add a bit at a time and test it. The other is that the above code will compile and run. OK it doesn't do what you need but it is a start. Once you are satisfied it does compile and run then change it slightly. Compile and run again. Repeat this process as many times as needed to finish the entire assignment
    Last edited by Junky; 06-20-2011 at 03:56 AM. Reason: Oops!. Now it will compile.

  16. #16
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    x.spend();
    vs
    x.spend(50.0);


    System.out.println("In spend method value = " + value); // show value passed to method

  17. #17
    CGHMN is offline Member
    Join Date
    Jun 2011
    Posts
    40
    Rep Power
    0

    Default

    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;	
    	 
    	 
      }
      
    }
    I need to figure out what the variable canAfford has to be greater then but I think the whole boolean is like dead on to what i need
    Last edited by CGHMN; 06-20-2011 at 05:00 AM. Reason: Just adding more info.

  18. #18
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,759
    Rep Power
    7

    Default

    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);
        }
    }
    Now once you have that compiled and it runs successfully you can work on the spend method. I assume you now know that you have to do a subtraction. Once again do not think about Java, just think about how you would do it in real life.

  19. #19
    CGHMN is offline Member
    Join Date
    Jun 2011
    Posts
    40
    Rep Power
    0

    Default

    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;
      }
    }
    And here is the output
    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
    and the required out put is

    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.

  20. #20
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,759
    Rep Power
    7

    Default

    Java Code:
    public void spend( int wealth )
      {
        wealth = Money - wealth;
      }
    How much Money will you have after this? Once again if I have $10 (Money) and I spend $3.50 (wealth), Money - wealth => 10.0 - 3.5 => 7.50, you then assign that to wealth: Money = 10.0 and wealth = 7.5. Hmmmm still doesn't work.

Page 1 of 2 12 LastLast

Similar Threads

  1. Replies: 2
    Last Post: 04-02-2011, 12:57 PM
  2. tricky initial value problem
    By Black_Eye in forum New To Java
    Replies: 4
    Last Post: 10-19-2010, 09:02 AM
  3. changing my program to array working program
    By Chewart in forum New To Java
    Replies: 39
    Last Post: 11-18-2009, 06:53 PM
  4. Tricky but very interesting problem
    By ravjot28 in forum New To Java
    Replies: 4
    Last Post: 06-26-2008, 01:43 PM
  5. tricky indexOf implementation -- Help!!
    By definewebsites in forum New To Java
    Replies: 3
    Last Post: 12-10-2007, 12:48 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
  •