Java Methods

Using Methods

Page 1 of 1

5 Replies - 1583 Views - Last Post: 01 November 2006 - 11:30 AM Rate Topic: -----

#1 roniz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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;
 

		//Ask for amount
		System.out.print("Please enter sales amount for the week:  "); //Ask for amount
		amount = MyInput.readDouble(); //Store amount
		
		//Ask for years
		System.out.print("Please enter the number of years the sales person has been with the company:  "); 
		years = MyInput.readInt(); //Store amount
		
		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  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • 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);


Was This Post Helpful? 0
  • +
  • -

#3 roniz  Icon User is offline

  • New D.I.C Head

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

Re: Java Methods

Posted 29 October 2006 - 07:50 PM

View Postjayman9, 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.
Was This Post Helpful? 0
  • +
  • -

#4 roniz  Icon User is offline

  • New D.I.C Head

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

Re: Java Methods

Post icon  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

Was This Post Helpful? 0
  • +
  • -

#5 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#6 roniz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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:  "); 
	int years = (MyInput.readInt());
	
	for(int i = 1; i <= numberOfWeeks; i++){
		System.out.print("Please enter the amount of sales per week :$  "); 
		double amount = MyInput.readDouble();
	
		totalSum += commission(amount) + bonus(years,amount,baseSalary);

	}
	return totalSum;
	
}
}




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

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1