# Java Methods

Page 1 of 1

## 5 Replies - 1706 Views - Last Post: 01 November 2006 - 11:30 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=20231&amp;s=9c9ff8c76a294dab2d89466fe6e6de7e&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 roniz

Reputation: 0
• Posts: 4
• Joined: 29-October 06

# Java Methods

Posted 29 October 2006 - 03:54 PM

My understanding of methods is shaky as it is but I have succeeded in creating one and now I need to take the output from that method and use in to calculate another method.

This is my code so far
```

double amount;
int years;
double ans;
double extra = 400;

System.out.print("Please enter the number of years the sales person has been with the company:  ");

double commission = commission(amount);
double bonus = bonus(years);

System.out.println("Your weekly commission is \$" + bonus  );
}
public static double commission(double amount)
{

if  (amount >= 5000) { //Calculate sales if amount entered is greater than or equal to 10001
return (amount * 0.10)  + 400;

}

else if  (amount >= 1000 ){
return (amount * .08);

}

else if  (amount >= 1000){
return (amount * 0.08);

}

else if   (amount >= 500 ){
return(amount * .05);

}

else { return 0.0;

}
}

public static double bonus(double commission, int years)

{

if  (years >= 2) {
return (commission() baseSalary* years/100);

}

else { return 0.0;
}

}
}

```

I need to write three methods to 1st to calculate the commission which i've done and second to calculate bonus which checks for how man years the sales person has been there and they add the number of years they've been there to get weekly sales for instance

``` if  (amount >= 5000 && years < 2){
ans =  (amount * 0.10) + (baseSalary + 400);
```

But since I've already calculate commission which is half of this statement, how do I get commission and use it in my bonus method?

Is This A Good Question/Topic? 0

## Replies To: Java Methods

### #2 Jayman

• Student of Life

Reputation: 419
• Posts: 9,532
• Joined: 26-December 05

## Re: Java Methods

Posted 29 October 2006 - 07:09 PM

You need to pass it into your method as a parameter. Currently you are set up to do that with public static double bonus(double commission, int years) but you are only passing one parameter currently in this statement double bonus = bonus(years);

Modify to the following:
```double bonus = bonus(commission, years);

```

### #3 roniz

Reputation: 0
• Posts: 4
• Joined: 29-October 06

## Re: Java Methods

Posted 29 October 2006 - 07:50 PM

jayman9, on 29 Oct, 2006 - 07:09 PM, said:

You need to pass it into your method as a parameter. Currently you are set up to do that with public static double bonus(double commission, int years) but you are only passing one parameter currently in this statement double bonus = bonus(years);

Modify to the following:
```double bonus = bonus(commission, years);

```

OH! thank you so much! I spent they day trying to figure this out.

### #4 roniz

Reputation: 0
• Posts: 4
• Joined: 29-October 06

## Re: Java Methods

Posted 31 October 2006 - 06:48 PM

Now when I run it I'm getting ';' expected. When I insert it, I get a boat load of errors. What's wrong with it?

I think i figured it out

This post has been edited by roniz: 31 October 2006 - 06:55 PM

### #5 Jayman

• Student of Life

Reputation: 419
• Posts: 9,532
• Joined: 26-December 05

## Re: Java Methods

Posted 31 October 2006 - 07:08 PM

Well if you are still having problems post your updated code and someone will be happy to help you.

### #6 roniz

Reputation: 0
• Posts: 4
• Joined: 29-October 06

## Re: Java Methods

Posted 01 November 2006 - 11:30 AM

For the last part of my problem I need to create another method called payroll that takes no parameters and loop it 4 times and in each pass, it should ask for sales amount for the week then calculate sales for the month by calling on bonus and commission?

I'm not sure how to go about it and I don't understand why payroll shouldn't have any parameters. How can it call on commission and bonus without them are parameters?

Also what kind of loop will be best suited for it?

```

public class Project4 {

public static void main(String[] args) {

double amount;
static int years;
double extra = 400;
static double  baseSalary = 500.00;

System.out.println("Your monthly salary is \$" + payroll()  );
}
double payroll = payroll();
public static double commission(double amount)
{

if  (amount >= 5000) { //Calculate sales if amount entered is greater than or equal to 10001
return (amount * 0.10)  + 400;

}

else if  (amount >= 1000 ){
return (amount * .08);

}

else if  (amount >= 1000){
return (amount * 0.08);

}

else if   (amount >= 500 ){
return(amount * .05);

}

else { return 0.0;

}

}

public static double bonus(int years, double amount, double baseSalary)

{

if  (amount >= 5000 && years >= 2) {
return (baseSalary + amount * years/100);

}

else if  (amount >= 1000 && years >= 2){
return (baseSalary + amount * years/100);

}

else if  (amount >= 500 && years >= 2){
return (baseSalary + amount  * years/100);

}

else if   (amount < 500&& years >= 2){
return(baseSalary + amount  * years/100);

}

else { return baseSalary;
}

}

public static double payroll()

{
int numberOfWeeks = 4;
double totalSum = 0.0;

System.out.print("Please enter the number of years the sales person has been with the company:  ");

for(int i = 1; i <= numberOfWeeks; i++){
System.out.print("Please enter the amount of sales per week :\$  ");

totalSum += commission(amount) + bonus(years,amount,baseSalary);

}

}
}

```

That is my code so far.

I have my loop going but I'm getting an error "non-static variable amount cannot be referenced from static context"

This post has been edited by roniz: 02 November 2006 - 01:49 AM