• Post Reply Bookmark Topic Watch Topic
  • New Topic

Overloaded Methods  RSS feed

 
Nana Mikamura
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello!

I am new to Java. I am having some problems understanding some concepts in Java. Some concepts I understand, and others I do not. I am trying to figure out how to return an overloaded method. If anyone could help me that would be great! ^_^
 
Ganish Patil
Ranch Hand
Posts: 529
19
Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to CodeRanch!
Nana Mikamura wrote: I am trying to figure out how to return an overloaded method
Pardon me, would you please elaborate little more. Do you want an overloaded method to return a value?
 
Nana Mikamura
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, I am trying to overload 3 methods and return the value of all 3 of them.
 
Jason Bullers
Ranch Hand
Posts: 115
11
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could you provide an example maybe, or some pseudo code of what you are trying to do or how you expect it to work? I'm not entirely clear on what problem you are trying to solve...
 
Ganish Patil
Ranch Hand
Posts: 529
19
Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nana Mikamura wrote:Yes, I am trying to overload 3 methods and return the value of all 3 of them.
Whatever you have tried yet, just post that code using Code tag (To get it, click Code button in menu and put your code in it), so we can understand what precisely you want to know..
 
Nana Mikamura
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is the problem.

create a class named billing that includes three overloaded computebill() methods for a photo book store. when computebill() receives a single parameter, it represents the price of one photo book ordered. add 8% tax, and return the total due.
when computebill receives two parameters, they represent the price of a photo book and the quantity ordered. multiply the two values, add 8% tax, and return total.
then when computebill receives three parameters, they represent the price of a book, quantity, and a coupon value. multiply quantity and price, reduce result by coupon value, and then add 8% tax and return total.
then write a main method that tests all methods.[/size]

And this is the code I have so far:

public class Billing {

double Price = 0.0;
int q = 0;
double Tax = 0.008;
int c = 0;
double Total = 0.0;

public double computeBill(double Price)
System.out.println("Total including tax is" + Price + Tax);
return Total;


I want to know if I am on the right track or totally wrong.
 
Carey Brown
Bartender
Posts: 3011
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Before trying to overload two additional methods I suggest you work on getting what you have so far to compile. Then add a main() method to run it. Only after you have that working should you go back and create two more overloaded methods.

Variable names should start with a lower-case character.

Use descriptive variable names. 'c' and 'q' are not helpful.
 
Nana Mikamura
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Carey!!! ^_^
 
Ganish Patil
Ranch Hand
Posts: 529
19
Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nana Mikamura wrote:double Price
First character of filed should be small letter. Please read this naming convention link
As I understood your passing price, order quantity and coupon value as parameters to respective methods, where they may vary or relies on your input values. Only double Tax = 0.008; is fixed value. So just declare final double tax = 0.008; because it is the constant value so you can make it final field if it's fix for all. All other price, order quantity and coupon values will be stored in method's local variables, when you call these methods by passing arguments in it.
Nana Mikamura wrote:public double computeBill(double Price)
System.out.println("Total including tax is" + Price + Tax);
return Total;
Here your not calculating total, only returning Total variable in your case which is initialized 0.0, you must assign calculation value of Price + Tax like Total = Price + Tax. Actually you can directly return total bill amount without storing in separate variable which is Total in your code. You can return it likeI think you should read this first Method Overloading
 
Knute Snortum
Sheriff
Posts: 4087
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You probably want that code to go in a method. (The other places code can go are constructors and initializers.) You can only have declarations and initializations outside of a method, constructor or initializer.
 
Nana Mikamura
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is what I got. I went to a tutor, but we ran out of time before he could fully explain it to me. I still do not understand how to return the values.

 
Carey Brown
Bartender
Posts: 3011
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You want tax to be a constant, therefore add this just inside the class declaration:

The word 'static' is the key to making accessible to all methods. Constants should be in all caps.

Forget pTax. Always compute the tax using the TAX constant.

You don't need the member variables (aka fields). You have

inside your class declaration. Nowhere in your code are you making any use of these, so remove them.

Your main() method is static so it can only call other static methods, therefore you will have to declare your other methods static as well. This is not necessarily the most object-oriented approach but will suffice for your assignment.

As it stands your program won't compile without errors. Make these changes first and then concentrate on getting a clean compile. Then work on expanding your main() to test your other methods.
 
Knute Snortum
Sheriff
Posts: 4087
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You almost got the code tags right -- I edited your post to correct them. Follow this link: UseCodeTags ...to learn more.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!