aspose file tools*
The moose likes Beginning Java and the fly likes Passing data to methods Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Passing data to methods" Watch "Passing data to methods" New topic
Author

Passing data to methods

RJ Cavender
Ranch Hand

Joined: Jul 26, 2006
Posts: 51
Im always afraid to ask a question that will come off like im asking for the answer, im not. In this program, im modifying one that i did previously that only had one main method. It asked for your name, weekly sales, are you salary or hourly, commission or not, and would print a sales report at the end. NOW, i have to use that same program I wrote and break it up into methods. I think I have most of it, but as to the matter of commission, im coming up snake eyes on how to pass "weeklySales" down to a "calculate_commission" method (not written yet). Is it as simple as making an integer equal weekly sales? One thats present in that method?

Thanks for any help you can give.
Ray


EDIT by mw: Added Code Tags.
[ February 18, 2007: Message edited by: marc weber ]
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 29287
    
140

Originally posted by RJ Cavender:
I think I have most of it, but as to the matter of commission, im coming up snake eyes on how to pass "weeklySales" down to a "calculate_commission" method (not written yet). Is it as simple as making an integer equal weekly sales? One thats present in that method?

It looks like a double in your code, so you can pass that to the new method.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
RJ Cavender
Ranch Hand

Joined: Jul 26, 2006
Posts: 51
Yes, I convert weeklySales to a double, but if I put a method in called "calculate_commission" and try to use "weeklySales", i get told it hasn't been identified.
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
Why don't you have the calculateCommission() method accept a parameter that represents the weekly sales.



Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
RJ Cavender
Ranch Hand

Joined: Jul 26, 2006
Posts: 51
Thanks.....i got it. Now im having trouble figuring out how to pass a string like "empName" AND doubles to the print_payroll function. If i declare things like ftax,stax, and ltax as doubles, I dont know how to pull a string like empName in as well.

package acmeCarSales;
import javax.swing.JOptionPane;
public class acmeCarSales
{

public static void main(String[] args){

double ltax,stax,ftax,npay,empname,type;
double commission = 0;
double gpay=0;
String comAnswer = "";
String payType = "";
String empName = "";
String pay = "";






//prompt user for his or her name
String employeeNameString=JOptionPane.showInputDialog(
"Enter your full name:");

empName=employeeNameString;



//Call to pay type method
int ptype=read_pay_type();

//Call to Yes/No commission method
int ctype=read_comm_type();



//prompt for salary for hourly OR salaried employee
if(ptype==1)
pay="Salary";
else
if(ptype==2)
pay="Hourly";
if(ptype==1){
String weeklySalaryString=JOptionPane.showInputDialog(
"Please enter your weekly salary:");
//salaried pay
gpay=Double.parseDouble(weeklySalaryString);
}else{
String hoursWorkedString=JOptionPane.showInputDialog(
"Please enter the hours worked:");
String ratePerHourString=JOptionPane.showInputDialog(
"Please enter your hourly rate:");
double hours=Double.parseDouble(hoursWorkedString);
double rate=Double.parseDouble(ratePerHourString);
}


if(ctype==1)
comAnswer="Yes";
else
if(ctype==2)
comAnswer="No";

//prompt user for the weekly sales
String weeklySalesString=JOptionPane.showInputDialog(
"Enter your sales for the week:");

//Convert weekly sales to a double
double weeklySales=Double.parseDouble(weeklySalesString);

if(ctype==1)
commission=calculate_commission(weeklySales);

gpay = gpay + commission;
ltax = gpay * .01;
stax = gpay * .06;
ftax = gpay * .20;
npay = gpay - (ltax + stax + ftax);
print_payroll(empName);
print_payroll(gpay,ftax,stax,ltax);




}//END MAIN METHOD

//Method to take in salary or hourly pay type
public static int read_pay_type(){
int result=0;
String payRateString=JOptionPane.showInputDialog (
"Enter 1 for salary or 2 for hourly:");
//convert answer to integer
int answer=Integer.parseInt(payRateString);
return result;
}


//Method to read in if commission or not
public static int read_comm_type(){
int result=0;
String commissionStatusString=JOptionPane.showInputDialog(
"Are you on commission? 1 for yes, 2 for no:");
//convert answer to integer

int CommStatus=Integer.parseInt(commissionStatusString);
return result;
}


//Method to calculate commission based on weeklysales
public static double calculate_commission(double weeklySales){

double comm=0;
if(weeklySales < 50000)
comm = weeklySales * .05;
else if(weeklySales >=50000)
comm = weeklySales * .10;
else if(weeklySales < 100000)
comm = weeklySales * .10;
else if(weeklySales >= 100000)
comm = weeklySales * .15;
return comm;

}




//Method to print the payroll information
public static void print_payroll(gpay,ftax,stax,ltax){
String output ="ACME CAR DEALERSHIP";
System.out.println("Gross pay = "+gpay);
System.out.println("Federal tax = "+ftax);
System.out.println("State tax = "+stax);
System.out.println("Local tax = "+ltax);

}









}//End of the Acme Car class
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 10925
    
  12

I've not read your code, so I'm not giving you the answer, but...

when you write a method, you list all the parameters you want to pass it. you name what type they are, and what you will call them in the method:


that method must now be called by passing it four variables - two ints, one String, and one MyClass:



There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
RJ Cavender
Ranch Hand

Joined: Jul 26, 2006
Posts: 51
Wow, Fred "IS" a good guy! Thanks you. Now if its not too much trouble, since im so close (ive solved the last one myself) can you tell me why in my final "print_payroll" method "pay" and "comAnswer" arent printing anything? It's just blank. Earlier in the program in the main method i give them values, can you tell me why those values arent being passed to the print method?

package acmeCarSales;
import javax.swing.JOptionPane;
public class acmeCarSales
{

public static void main(String[] args){

double ltax,stax,ftax,npay,empname,type;
double commission = 0;
double gpay=0;
String comAnswer = "";
String payType = "";
String empName = "";
String pay="";







//prompt user for his or her name
String employeeNameString=JOptionPane.showInputDialog(
"Enter your full name:");

empName=employeeNameString;



//Call to pay type method
int ptype=read_pay_type();

//Call to Yes/No commission method
int ctype=read_comm_type();



//prompt for salary for hourly OR salaried employee
if(ptype==1)
pay="Salary";
else
if(ptype==2)
pay="Hourly";
if(ptype==1){
String weeklySalaryString=JOptionPane.showInputDialog(
"Please enter your weekly salary:");
//salaried pay
gpay=Double.parseDouble(weeklySalaryString);
}else{
String hoursWorkedString=JOptionPane.showInputDialog(
"Please enter the hours worked:");
String ratePerHourString=JOptionPane.showInputDialog(
"Please enter your hourly rate:");
double hours=Double.parseDouble(hoursWorkedString);
double rate=Double.parseDouble(ratePerHourString);
}


if(ctype==1)
comAnswer="Yes";
else
if(ctype==2)
comAnswer="No";

//prompt user for the weekly sales
String weeklySalesString=JOptionPane.showInputDialog(
"Enter your sales for the week:");

//Convert weekly sales to a double
double weeklySales=Double.parseDouble(weeklySalesString);

if(ctype==1)
commission=calculate_commission(weeklySales);

gpay = gpay + commission;
ltax = gpay * .01;
stax = gpay * .06;
ftax = gpay * .20;
npay = gpay - (ltax + stax + ftax);
print_payroll(empName,pay,comAnswer,gpay,ftax,stax,ltax,commission,npay);




}//END MAIN METHOD

//Method to take in salary or hourly pay type
public static int read_pay_type(){
int result=0;
String payRateString=JOptionPane.showInputDialog (
"Enter 1 for salary or 2 for hourly:");
//convert answer to integer
int answer=Integer.parseInt(payRateString);
return result;
}


//Method to read in if commission or not
public static int read_comm_type(){
int result=0;
String commissionStatusString=JOptionPane.showInputDialog(
"Are you on commission? 1 for yes, 2 for no:");
//convert answer to integer

int CommStatus=Integer.parseInt(commissionStatusString);
return result;
}


//Method to calculate commission based on weeklysales
public static double calculate_commission(double weeklySales){

double comm=0;
if(weeklySales < 50000)
comm = weeklySales * .05;
else if(weeklySales >=50000)
comm = weeklySales * .10;
else if(weeklySales < 100000)
comm = weeklySales * .10;
else if(weeklySales >= 100000)
comm = weeklySales * .15;
return comm;

}




//Method to print the payroll information
public static void print_payroll(String empName,String pay,String comAnswer,double gpay,double ftax,double stax,double ltax, double commission, double npay){


String output ="ACME CAR DEALERSHIP"+
"\n"+
"\n"+"Employee Name = "+empName+
"\n"+"Pay Type = "+pay+
"\n"+"Gets Commission = "+comAnswer+
"\n"+"Gross Pay = "+gpay+
"\n"+"Federal Tax = "+ftax+
"\n"+"State Tax = "+stax+
"\n"+"Local Tax = "+ltax+
"\n"+"Commisson = "+commission+
"\n"+"Net Pay = "+npay;

JOptionPane.showMessageDialog(null,output);


}









}//End of the Acme Car class
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39576
    
  27
Here's a hint: Those variables get assigned something only if ptype and ctype are 1 or 2. Work your way backwards from there, and you'll find the problem.

You're probably worrying about other aspects of the program right now, but I just wanted to point out that making all methods static is not good OO design.


Ping & DNS - updated with new look and Ping home screen widget
RJ Cavender
Ranch Hand

Joined: Jul 26, 2006
Posts: 51
I know what you're saying but allowances for no reply are not a requirement at this stage. I have to assume a 1 or 2 "will" be selected. There is no allowance for no answer.
RJ Cavender
Ranch Hand

Joined: Jul 26, 2006
Posts: 51
I GOT IT!!! (whew! Finally done).
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Passing data to methods
 
Similar Threads
cannot understand the point of downcasting in the following code
Program Output not working
Help with Methods please
how to test/run a simple inheritance hierarchy in Eclipse
Needs help in editing