Re: Constructor Help Needed
- From: "Mike" <mparham1@xxxxxxxxxxxxx>
- Date: 16 Jul 2006 19:30:33 -0700
Here is an example where I tried to put them in but it seems everytime
I make a change more errors popup. I haveing a hard time on figuring
how to implement it into the code. Does it have to be after cvertain
events. The variable transfers are confusing to.
// Payroll.java
//This calls the external class scanner
import java.util.Scanner;//program uses scanner
public class PR5
{
//main method begins program execution
public static void main( String args[] )
Employee payroll3 = new Employee(String name, double hours, double
rate );
payroll3.determineWage();
public class Employee
{
private String name;
private double hours;
private double rate;
// if the constructor uses parameter names identical to instance
variable names the "this" reference is required to distinguish between
names
public Employee(String name, double hours, double rate) //constructor
{
this.name = name; // set "this" object's name
this.hours = hours; // set "this" object's hours
this.rate = rate; // set "this" object's rate
}
public void determineWage()
{ // Start Rate Method
// create Scanner to obtain input from command window
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
// public class Calculation
//public Employee( rate, hours); {
//}
//{
double pay = rate * hours;
//}
// 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\n", rate );
System.out.printf( "\nHours worked: $%.2f\n", 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 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 check value back to calling method
} // End class paycheck
} // End of Payroll Class
.
- Follow-Ups:
- Re: Constructor Help Needed
- From: Mark Space
- Re: Constructor Help Needed
- From: Chris Smith
- Re: Constructor Help Needed
- References:
- Constructor Help Needed
- From: Mike
- Re: Constructor Help Needed
- From: Lionel
- Constructor Help Needed
- Prev by Date: Re: Flexible data transfer & manipulation
- Next by Date: Re: Flexible data transfer & manipulation
- Previous by thread: Re: Constructor Help Needed
- Next by thread: Re: Constructor Help Needed
- Index(es):
Relevant Pages
|