• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Jeanne Boyarsky
  • Tim Cooke
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
Bartenders:

Passing data to methods

 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
author & internet detective
Posts: 41763
887
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
RJ Cavender
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why don't you have the calculateCommission() method accept a parameter that represents the weekly sales.

 
RJ Cavender
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:

 
RJ Cavender
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
RJ Cavender
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I GOT IT!!! (whew! Finally done).
 
New rule: no elephants at the chess tournament. Tiny ads are still okay.
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic