1 Attachment(s)
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.
Attachment 1463
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!
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.
Quote:
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?
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()
{
}
}
Re: Beginner Programming Help
public static double plus()
{
}
SHOULD BE
public int plus()
{
}
I think...
Re: Beginner Programming Help
Quote:
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.
Code java:
public class Sample {
public Sample() {
this.color = "pretty color";
}
private String color;
}
Note the highlights and formatting.
Quote:
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.
Quote:
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?