Constructor Help Needed

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
 
C

Chris Smith

Mike said:
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:

I don't see any code to even try to declare a constructor there.
Instead of saying that you get errors, please post the code you tried to
compile, and what errors you get, and why you think they shouldn't be
there. We understand you've probably tried many things; please post the
one that you most expected to work.

(Just as an aside, it actually makes your code harder to read when you
put comments on the same line with open and close braces. The structure
of your code is one of the most important things to understand, and
people reading your code will expect to see those nearly empty lines
with braces to delineate it. It was made more noticable here because
the line wrap feature in your newsreader obscured the indentation, which
is the other commonly used clue to structure.)
 
L

Lionel

Mike said:
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

All that Chris said is correct. As a pointer here is an example:

public class A {

//here is the constructor. No return type is defined or it won't
//compile
public A(/*list of parameters here*/) {
//do some initialisation
}
}
 
M

Mike

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
 
C

Chris Smith

Mike said:
Here is an example where I tried to put them in but it seems everytime
I make a change more errors popup.
public class PR5
{
//main method begins program execution
public static void main( String args[] )

Employee payroll3 = new Employee(String name, double hours, double
rate );

For one thing, you seem to be missing an open brace for your main method
here.

Secondly, you've got something confused in calling the constructor.
When you define the constructor in the class, you'll include a formal
parameter list like you've got above. But when you use the constructor
to create an object, you need actual values there. Something like:

Employee bob = new Employee("Bob Bobswell", 40.0, 22.50);
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
}

This looks fine. Just fix your use of the class.
 
M

Mark Space

Just to add a little to what Chris said, but in more general terms.

What you have here is an example of debugging a program. Your program
does something unexpected, and you have to figure out why. It's a good
idea to take every opportunity to practice this skill. Of course,
asking for help is valid also, but at some point in your career you have
to develop some techniques for effective debugging.

One thing I notice about your code is that it's littered with lots of
sections that are commented out. It's really hard to read. And the
basic problem here was that you were missing a brace, which can be hard
to spot, not to mention hard to spot in a program that's also hard to read.

One technique I've seen people use is to completely remove a difficult
problem, then approach it carefully in small steps, make certain each
small step is correct.

For example, one friend in college used to start his programming by
making all files, with nothing in them besides comments. Then he'd
compile that, just to make sure he had everything correct in the
structure of the program. This let him know that he wasn't missing a
start comment like /* somewhere.

Then he'd carefully add some code, usually just the class names and
methods, with no body at all. Fore example, something like this:
// Payroll.java
import java.lang.String;
public class PR5
{
public static void main( String args[] )
{
}

public class Employee
{
public Employee(String name, double hours, double rate)
{
}
}

public void determineWage()
{
}

public double paycheck( double x, double y)
{
} // End class paycheck

} // End of Payroll Class

Then compile that. I notice immediately now that I did this, that you
have a comment "end class paycheck" but paycheck seems to lack a class
keyword. It's a method, not a class. So maybe there is something to
look at. Also, I'm not really sure if determineWage() is part of PR5 or
Employee, your code is really hard to read. You should look at that
then correct it if needed.

Anyway, next you should add some variable declarations, then compile
again. Then add some objects and basic loops, then compile again.
Eventually if you have a syntax error somewhere, it'll be in the last
small thing you added, and much easier to find.

It's a good idea to pay careful attention to braces in indentation.
Make the code easy to read as possible. Then any nesting errors, like
missing a brace, will jump right out at you.

So this is just one little trick you can use when nothing makes any
sense. It takes a bit of time, but if you are really stuck, it'll help
you find your own errors, usually faster than if you wait for someone
else to get back to you with an answer.

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top