d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Java - Method Overloading Help
Add Reply New Topic New Poll
Member
Posts: 3,457
Joined: Jul 22 2012
Gold: 30,884.00
Feb 10 2018 01:20am
Stuck on basic assignment on overloading, I need to create two overload methods which are print() and print(boolean kg). First one is suppose to print the weight in pounds out while the other one is suppose to convert pounds to kg if true is passed in and print out kg weight instead of pounds. How would the finish code look like in the ???????

public class Weight
{
private double pounds;

public Weight(double thePounds)
{
pounds = thePounds;
}

public void print()
{
System.out.println(pounds);
}

public void print(boolean kg)
{
???????????
}
}


This post was edited by RYce on Feb 10 2018 01:25am
Member
Posts: 1,039
Joined: Jul 8 2008
Gold: 1,939.50
Feb 10 2018 01:00pm
You have everything you need to solve this problem. Take some time and work through it step by step.

Using this method signature:

public void print(boolean kg)

We know that if kg is true you want to print out the kg weight. If kg is false you want to print out the lb weight. Simple as that.

So we can start with the case you already have. kg is false. Just print out pounds.
Code
public void print(boolean kg) {
if (kg == false) {
System.out.println(pounds);
}
}


Now we have to handle the case where kg is true. This is slightly more complex, but if you go step by step again it isn't too difficult. Think about what you want to do.
If kg is true print out the kg weight.
if kg is true
Code
if (kg == true) {
//calculate kg weight
//print out kg weight
}


Now to calculate the weight in kg we multiply lbs by a conversion ratio. Roughly 0.45359237.
Code
double kgWeight = pounds * 0.45359237;
System.out.println(kgWeight);


Put all of this together and we get the following method:

Code
public void print(boolean kg) {
if (kg == false) {
System.out.println(pounds);
}
if (kg == true) {
double kgWeight = pounds * 0.45359237;
System.out.println(kgWeight);
}
}


Now we can simplify this slightly by using an if/else statement
Code
public void print(boolean kg) {
if (kg == false) {
System.out.println(pounds);
} else {
double kgWeight = pounds * 0.45359237;
System.out.println(kgWeight);
}
}


CONTINUED...

This post was edited by waraholic on Feb 10 2018 01:03pm
Member
Posts: 1,039
Joined: Jul 8 2008
Gold: 1,939.50
Feb 10 2018 01:02pm
d2jsp doesn't like me having so many code tags. Continued:

We can simplify this more by inlining the kgWeight local variable.
Code
public void print(boolean kg) {
if (kg == false) {
System.out.println(pounds);
} else {
System.out.println(pounds * 0.45359237);
}
}


We can simplify this one more time by calling the print function you've written. Duplicate code is never a good thing and this eliminates some of it.
Code
public void print(boolean kg) {
if (kg == false) {
print();
} else {
System.out.println(pounds * 0.45359237);
}
}


There are other simplifications and different, more elegant ways to do this, but the last 4 code blocks all do the same thing.

This post was edited by waraholic on Feb 10 2018 01:04pm
Member
Posts: 3,457
Joined: Jul 22 2012
Gold: 30,884.00
Feb 10 2018 02:08pm
Thank you mate, very educational :thumbsup:
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll