M
Mike
I am very new to Java programming so this may seam like the basics to
some of you. I have an assignment where I am to take my code from a
previous week and add a constructor. I have been trying to figure out
how to add one for the past few days. Every time I tried to add one, I
got errors somewhere in my program. I was wondering if someone could
shed some light on how to properly put a constrcutor in your code. The
code I created for the class is in 2 parts:
Pat 1
/**
Payroll Part 3
Mike Parham
Week 4 Day 7
This program calls Payroll3 class to determine the amount of a
paycheck for employees.
**/
public class PayCheckFinder
{ // Start class PayCheckFinder
public static void main( String args[] )
{ // Start Main
Payroll3 payroll3 = new Payroll3();
payroll3.determineWage();
} // End Main
} // End class PayCheckFinder
Part 2
/** Payroll Part 3
Mike Parham
Week 4 Day 7
This program gets the name, rate and hours of an
employee. It
calculates the the pay of
the employee and loops to get another employee and
information
until "stop" is entered as
the employee name.
New to this version:
A class is created to retrieve and store the information
A constructor is used to initialize multiple parameters
A method within the class is used to do calculations
**/
// This calls the external class scanner
import java.util.Scanner;
public class Payroll3
{ // Start of main Payroll3 Class
public void determineWage()
{ // Start Rate Method
// Create a scanner to get input from keyboard
Scanner input = new Scanner( System.in );
// Variable Declaration
double rate; // Hourly Rate
double hours; // Hours Worked
//double pay; // Weekly Pay ( Rate * Hours)
//double check;
// Get User Inputs
System.out.printf( "Enter the employee's name (Enter stop when
finished): " );
String name = input.next();
while(!name.equals("stop"))
{ // Start user input loop until "stop" is entered
// Get hourly rate
System.out.printf( "Enter a positive hourly pay rate: " );
rate = input.nextDouble();
while ( rate <= 0 )
{ // Start hourly rate loop until positive number is entered
System.out.printf( "\nHourly rate must be positive: " );
rate = input.nextDouble();
} // End hourly rate loop
// Get hours worked
System.out.printf( "Enter a positive number of hours worked: "
);
hours = input.nextDouble();
while ( hours <= 0 )
{ // Start hours loop until positive number is entered
System.out.printf( "\nThe number of hours worked must be
positive: " );
hours = input.nextDouble();
} // End hours loop
// Return the amount of pay using hours and rate
double pay = paycheck( rate, hours );
// Display results
System.out.printf ( "\nEmployee: %s", name );
System.out.printf( "\nHourly rate: $%.2f", rate );
System.out.printf( "\nHours worked: $%.2f", hours );
System.out.printf( "\nWeekly paycheck: $%.2f\n", pay);
// Get name to check if loop should continue
System.out.printf( "\n\nEnter the employee's name (Enter stop
when finished): " );
name = input.next();
} // End User input loop
// Returns the paycheck amount based on 2 doubles, rate and hours
}// End Rate Method
public double paycheck( double x, double y)
{ // Start class paycheck
double check = x * y; // Calulate paycheck
return check; // Send the value of check back to calling
method
} // End class paycheck
some of you. I have an assignment where I am to take my code from a
previous week and add a constructor. I have been trying to figure out
how to add one for the past few days. Every time I tried to add one, I
got errors somewhere in my program. I was wondering if someone could
shed some light on how to properly put a constrcutor in your code. The
code I created for the class is in 2 parts:
Pat 1
/**
Payroll Part 3
Mike Parham
Week 4 Day 7
This program calls Payroll3 class to determine the amount of a
paycheck for employees.
**/
public class PayCheckFinder
{ // Start class PayCheckFinder
public static void main( String args[] )
{ // Start Main
Payroll3 payroll3 = new Payroll3();
payroll3.determineWage();
} // End Main
} // End class PayCheckFinder
Part 2
/** Payroll Part 3
Mike Parham
Week 4 Day 7
This program gets the name, rate and hours of an
employee. It
calculates the the pay of
the employee and loops to get another employee and
information
until "stop" is entered as
the employee name.
New to this version:
A class is created to retrieve and store the information
A constructor is used to initialize multiple parameters
A method within the class is used to do calculations
**/
// This calls the external class scanner
import java.util.Scanner;
public class Payroll3
{ // Start of main Payroll3 Class
public void determineWage()
{ // Start Rate Method
// Create a scanner to get input from keyboard
Scanner input = new Scanner( System.in );
// Variable Declaration
double rate; // Hourly Rate
double hours; // Hours Worked
//double pay; // Weekly Pay ( Rate * Hours)
//double check;
// Get User Inputs
System.out.printf( "Enter the employee's name (Enter stop when
finished): " );
String name = input.next();
while(!name.equals("stop"))
{ // Start user input loop until "stop" is entered
// Get hourly rate
System.out.printf( "Enter a positive hourly pay rate: " );
rate = input.nextDouble();
while ( rate <= 0 )
{ // Start hourly rate loop until positive number is entered
System.out.printf( "\nHourly rate must be positive: " );
rate = input.nextDouble();
} // End hourly rate loop
// Get hours worked
System.out.printf( "Enter a positive number of hours worked: "
);
hours = input.nextDouble();
while ( hours <= 0 )
{ // Start hours loop until positive number is entered
System.out.printf( "\nThe number of hours worked must be
positive: " );
hours = input.nextDouble();
} // End hours loop
// Return the amount of pay using hours and rate
double pay = paycheck( rate, hours );
// Display results
System.out.printf ( "\nEmployee: %s", name );
System.out.printf( "\nHourly rate: $%.2f", rate );
System.out.printf( "\nHours worked: $%.2f", hours );
System.out.printf( "\nWeekly paycheck: $%.2f\n", pay);
// Get name to check if loop should continue
System.out.printf( "\n\nEnter the employee's name (Enter stop
when finished): " );
name = input.next();
} // End User input loop
// Returns the paycheck amount based on 2 doubles, rate and hours
}// End Rate Method
public double paycheck( double x, double y)
{ // Start class paycheck
double check = x * y; // Calulate paycheck
return check; // Send the value of check back to calling
method
} // End class paycheck