Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Beginner Programming Help

  1. #1
    Junior Member
    Join Date
    Oct 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Beginner Programming Help

    Hello, I am stuck on an assignment that is asking me to create a class (USMoney), and inside that class create a method that will use an object of that class as an input parameter for the method call. I completely understand the rest of the assignment, I just cannot figure out the plus method...

    Here is a snapshot of the assignment.

    USMoney.jpg


    Here is my code so far...

    public class USMoney {

    private int dollars;
    private int cents;

    public USMoney(int first,int second)
    {
    dollars = first;
    cents = second;
    }

    public int getDollars()
    {
    return dollars + cents / 100;
    }

    public int getCents()
    {
    return cents % 100;
    }

    public int plus()
    {

    }

    }

    Can someone please steer me in the right direction for the plus method? Thanks!


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 262 Times in 231 Posts

    Default Re: Beginner Programming Help

    Please see the announcements page for help on the use of code tags, and then use code tags for your code.

    Using "first" and "second" as parameter names for a public method is discouraged. The names should reflect their use or the values they represent.


    I just cannot figure out the plus method...

    public int plus()
    {

    }
    it looks like the body of the method is missing. What should it do?

  3. #3
    Junior Member
    Join Date
    Oct 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Beginner Programming Help

    I have changed my code tags. The reason I have left the plus method blank is because I do not know where to start. How will it recognize objects that are created in the tester class?

    public class USMoney {

    private int dollars;
    private int cents;

    public USMoney(int dollr,int cent)
    {
    dollars = dollr;
    cents = cent;
    }

    public int getDollars()
    {
    return dollars + cents / 100;
    }

    public int getCents()
    {
    return cents % 100;
    }

    public static double plus()
    {

    }

    }

  4. #4
    Junior Member
    Join Date
    Oct 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Beginner Programming Help

    public static double plus()
    {

    }

    SHOULD BE

    public int plus()
    {

    }

    I think...

  5. #5
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 262 Times in 231 Posts

    Default Re: Beginner Programming Help

    I have changed my code tags.
    Have you? Looks to me like the code is being treated like text still. When the tags are correct your code will look like code instead of text.
    public class Sample {
       public Sample() {
          this.color = "pretty color";
       }
       private String color;
    }
    Note the highlights and formatting.



    How will it recognize objects that are created in the tester class
    Where in the instructions does it say anything about recognizing objects created in the tester class? According to what I read the plus method is to accept a USMoney object as a parameter, and then create and return a new USMoney object. So if I have a USMoney object as such:
    USMoney personalSavings;
    and a USMoney object as such:
    USMoney personalSpending;
    and using the plus method I would be able to see what I am worth in US Cash as such:
    USMoney myValueInUSMoney = personalSavings.plus(personalSpending);
    in a way that my personalSavings has not been modified, nor has my personalSpending. The USMoney class does not need to know what the test class does with this myValueInUSMoney object. It just has to know how to add the balance of the personalSpending to the balance of the personalSavings and create a new USMoney object using the sum of the personal objects.



    public static double plus()
    {

    }

    SHOULD BE

    public int plus()
    {

    }

    I think...
    What makes you think that? (Not that you are right or wrong) There are valid reasons to make a method static, does this method qualify as static or non-static? Why?

Similar Threads

  1. Blackjack programming error (programmer is new to programming)
    By JSingh in forum What's Wrong With My Code?
    Replies: 4
    Last Post: September 27th, 2012, 09:13 PM
  2. Replies: 0
    Last Post: December 12th, 2011, 02:17 PM
  3. For-looping, if-else statements, charAt(), etc. Beginner programming problem
    By ayelleeeecks in forum Loops & Control Statements
    Replies: 11
    Last Post: October 3rd, 2011, 12:54 PM
  4. Beginner programming- strange problem
    By jkkruse in forum What's Wrong With My Code?
    Replies: 1
    Last Post: August 23rd, 2011, 05:39 AM
  5. Programming beginner
    By LatinaC09 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 15th, 2010, 01:33 PM