-
Basic help with Constructors and Classes
I have been racking my brain trying to understand this fundamental concept and It is tearing me apart. Here is what I am supposed to do for my assignment:
Modify the Payroll Program so that it uses a class to store and retrieve the department’s name, the number of employees for the department, and the average salary per employee in the department. Use a constructor to initialize the department information, and a method within that class to calculate the total department salary amount (number of employees in the department times the average salary per employee). Once stop is entered as the department name, the application should terminate. Make sure the program maintains all the functionality required in previous assignments and your source code is readable and well documented. Use feedback you have received from the instructor to make any needed modifications.
Here is my code:
Code java:
// Thomas Harrald IT215
// Checkpoint Payroll Program Three
//Use a Class to Store and Retrieve Dept Name, Number of Employee, avg salary
//Use a Constructor to Initialize the Department information, and a method within in that class
// to calculate the total Department Salary Amount
import java.util.Scanner; //Import Scanner
public class PayrollProgramthree
{
public static void main( String[] args )
{
Scanner input = new Scanner (System.in);
String IName="null";
int INumber;
float ISalary;
System.out.println( "Welcome to the Payroll Program" );
while (!PayrollClass.PName.toLowerCase().equals("stop"))
{
System.out.println( "Please input the name of the department, type stop to end." );
IName = input.next();
PayrollClass.setName ( IName);
if(PayrollClass.PName.toLowerCase().equals( "stop"))
{
break;
}
System.out.println( "Please input the number of employees" );
INumber = input.nextInt();
while (INumber < 0 )
{
System.out.println ( "Please input a positive number");
INumber = input.nextInt();
}
PayrollClass.setNumber ( INumber);
System.out.println( "Please input the average employee salary" );
ISalary = input.nextFloat();
while ( ISalary < 0 )
{
System.out.println ( " Please input a positive number");
ISalary = input.nextFloat();
}
PayrollClass.setAvg ( ISalary);
PayrollClass.setTotal ();
System.out.printf( "The name of the department is %s\n ", PayrollClass.PName );
System.out.printf( "The total payroll is $ %s USD ", PayrollClass.totalDept);
}
System.out.println( "Thank you for using the Payroll Program!");
}
}
public class PayrollClass
{
private String deptName;
private int numberEmployees;
private float avgSalary;
float totalDept;
public void setName ( String PName )
{
deptName = PName;
}
public String getName ()
{
return deptName;
}
public void setNumber (int PNumber)
{
numberEmployees = PNumber;
}
public int getNumber()
{
return numberEmployees;
}
public void setAvg (float PSalary)
{
avgSalary = PSalary;
}
public float getAvg ()
{
return avgSalary;
}
public float setTotal ()
{
totalDept = PNumber * PSalary;
}
}
Any help would be appreciated! Thank you kindly!
-
Re: Basic help with Constructors and Classes
Do you have a specific question about your assignment?
-
Re: Basic help with Constructors and Classes
Quote:
Originally Posted by
awinston
Do you have a specific question about your assignment?
It's horribly wrong. I'm in the process of trying to figure it out. I think I'm nuking it...
I can't tell you specifically what's wrong because I don't really know what is wrong with it lol, if that makes any sense.
-
Re: Basic help with Constructors and Classes
1. How are you running this program?
2. Can you print out the errors that you receive upon execution of the program?
-
Re: Basic help with Constructors and Classes
Any place you see //** in the code is a comment I added
Code java:
// Thomas Harrald IT215 //** Shouldn't post real life names on public forums
// Checkpoint Payroll Program Three
//Use a Class to Store and Retrieve Dept Name, Number of Employee, avg salary
//Use a Constructor to Initialize the Department information, and a method within in that class
// to calculate the total Department Salary Amount
import java.util.Scanner; //Import Scanner
public class PayrollProgramthree //**the 't' in "three" should be caps
{
public static void main( String[] args )
{
Scanner input = new Scanner (System.in);
String IName="null";//**these three variables could be more localized to where they are used
int INumber;
float ISalary;
System.out.println( "Welcome to the Payroll Program" );
while (!PayrollClass.PName.toLowerCase().equals("stop")) //**what does !PayrollClass.PName.toLowerCase().equals("stop)) translate to on first run?
//**is it necessary to do all of that work here?
{
System.out.println( "Please input the name of the department, type stop to end." );
IName = input.next();
PayrollClass.setName ( IName); //**you have no PayrollClass OBJECT to setName in yet, and if you did this would not be the syntax to do so
if(PayrollClass.PName.toLowerCase().equals( "stop"))
{
break;
}
System.out.println( "Please input the number of employees" );
INumber = input.nextInt();
while (INumber < 0 )
{
System.out.println ( "Please input a positive number");
INumber = input.nextInt();
}
PayrollClass.setNumber ( INumber); //**you have not PayrollClass OBJECT to setNumber in yet, and if you did this would not be the syntax to do so
System.out.println( "Please input the average employee salary" );
ISalary = input.nextFloat();
while ( ISalary < 0 )
{
System.out.println ( " Please input a positive number");
ISalary = input.nextFloat();
}
PayrollClass.setAvg ( ISalary); //**you have not PayrollClass OBJECT to setAvg in yet, and if you did this would not be the syntax to do so
//**Now that you have gathered the three inputs, this is the time to call the constructor of your PayrollClass and initialize an object of the class
PayrollClass.setTotal ();//**what happens to this line once you have made your PayrollClass object?
System.out.printf( "The name of the department is %s\n ", PayrollClass.PName );//**you will need to use the created object for these
System.out.printf( "The total payroll is $ %s USD ", PayrollClass.totalDept);//**two lines here
}
System.out.println( "Thank you for using the Payroll Program!");
}
}
public class PayrollClass
{
//**would there be a way to create an object of this class and initialize the necessary variables at once with a constructor?
private String deptName;
private int numberEmployees;
private float avgSalary;
float totalDept;
public void setName ( String PName )
{
deptName = PName;
}
public String getName ()
{
return deptName;
}
public void setNumber (int PNumber)
{
numberEmployees = PNumber;
}
public int getNumber()
{
return numberEmployees;
}
public void setAvg (float PSalary)
{
avgSalary = PSalary;
}
public float getAvg ()
{
return avgSalary;
}
public float setTotal ()
{
totalDept = PNumber * PSalary;
}
}
The comments in your code + our previous discussion should get you facing forward and moving again
-
Re: Basic help with Constructors and Classes
Yep thanks for the tips.
I actually think the assignment is a lot easier than I was making it.
I am working on it currently.
-
Re: Basic help with Constructors and Classes
Ok, If I'm not mistaken this is a 2 part assignment. 2 separate files.
Here is one of them, the class that will be used to create an object.
Code java:
public class PayrollClass
{
private float totalDept;
private String name;
private int numberEmployees;
private float avgSalary;
public float getTotal () //method for multiplying the number of employees times the avg salary and sending the value for totalDept back to the calling method
{
totalDept = numberEmployees * avgSalary;
}
public void setName ( String PName ) //set method for setting the mainclass Pname to the PayrollClass name
{
name = PName;
}
public String getName () // get method for retreiving the department name
{
return name;
}
public void setNumber (int CNumber) // set method for setting the main class number to payrollClass numberEmployees
{
numberEmployees = CNumber;
}
public int getNumber() // get method for retrieving the number of employees
{
return numberEmployees;
}
public void setAvg (float CSalary) // Set method for setting the mainclass salary to PayrollClass avgSalary
{
avgSalary = CSalary;
}
public float getAvg () //get method for retreiving average salary
{
return avgSalary;
}
}
I get an error about missing a return statement.
-
Re: Basic help with Constructors and Classes
Fixed the Class that will be used in the main method.
Code java:
public class PayrollClass
{
private float totalDept;
private String name;
private int numberEmployees;
private float avgSalary;
public void getTotal () //method for multiplying the number of employees times the avg salary
{
totalDept = numberEmployees * avgSalary;
}
public void setName ( String PName ) //set method for setting the mainclass Pname to the PayrollClass name
{
name = PName;
}
public String getName () // get method for retreiving the department name
{
return name;
}
public void setNumber (int CNumber) // set method for setting the main class number to payrollClass numberEmployees
{
numberEmployees = CNumber;
}
public int getNumber() // get method for retrieving the number of employees
{
return numberEmployees;
}
public void setAvg (float CSalary) // Set method for setting the mainclass salary to PayrollClass avgSalary
{
avgSalary = CSalary;
}
public float getAvg () //get method for retreiving average salary
{
return avgSalary;
}
}
I modified the getTotal method to public void instead of public float.
-
Re: Basic help with Constructors and Classes
Here is the other part, the class with the main method.
Code java:
// Thomas Harrald IT215
// Checkpoint Payroll Program Three
//Use a Class to Store and Retrieve Dept Name, Number of Employee, avg salary
//Use a Constructor to Initialize the Department information, and a method within in that class
// to calculate the total Department Salary Amount
import java.util.Scanner; //Import Scanner
public class PayrollProgramthree // Main class that will store and retreive the information required by assignment
{
public static void main( String[] args )
{
Scanner input = new Scanner (System.in);
String deptName="null";
int numberofEmployees;
float Salary;
float totalofDept;
System.out.println( "Welcome to the Payroll Program" );
while (!deptName.toLowerCase().equals("stop")) //while loop to ask for information repeatedly until "stop" is typed
{
System.out.println( "Please input the name of the department type stop to end." ); //get name of dept
deptName = input.next();
if(deptName.toLowerCase().equals( "stop")) //breaks the while loop
{
break;
}
System.out.println( "Please input the number of employees" );
numberofEmployees = input.nextInt();
while (numberofEmployees < 0 ) //validates user input
{
System.out.println ( "Please input a positive number");
numberofEmployees = input.nextInt();
}
System.out.println( "Please input the average employee salary" );
Salary = input.nextFloat();
while ( Salary < 0 ) // validates user input
{
System.out.println ( " Please input a positive number");
Salary = input.nextFloat();
}
PayrollClass myPayrollClass = new PayrollClass ( String deptName int numberofEmployees float Salary); //constructor to initialize an object with the information inputted above
myPayrollClass.gettotal(); //method to get the total
System.out.printf( "The name of the department is %s\n ", deptName );
System.out.printf( "The total payroll is $%sUSD ", myPayrollClass.totalDept);
}
System.out.println( "Thank you for using the Payroll Program!");
}
}
I get three errors all on the constructor. Error: ";" expected.
-
Re: Basic help with Constructors and Classes
Modified the constructor to include commas since there are multiple parameters...
but now I get 6 errors. The three from before including three saying illegal start of expression.
-
Re: Basic help with Constructors and Classes
Modified the constructor yet again by removing the variable types in the parameters. That took care of that.
However I have 3 new errors.
error: Constructor PayrollClass in class PayrollClass cannot be applied to the given types.
The other two involve my method myPayrollClass.getTotal();
-
Re: Basic help with Constructors and Classes
Could you post the code as is now?
-
Re: Basic help with Constructors and Classes
Completely revamped code.
Here is the class:
Code java:
public class PayrollClass
{
private String name;
private int numberEmployees;
private float avgSalary;
String PName;
int PNumber;
float PSalary;
public PayrollClass (String name, int numberEmployees, float avgSalary)
{
PName = name;
PNumber = numberEmployees;
PSalary = avgSalary;
}
public String getName () // get method for retreiving the department name
{
return PName;
}
public int getNumber() // get method for retrieving the number of employees
{
return numberEmployees;
}
public float getAvg () //get method for retreiving average salary
{
return avgSalary;
}
public float getTotal () //method for multiplying the number of employees times the avg salary
{
return numberEmployees * avgSalary;
}
}
Next the main method:
Code java:
// Thomas Harrald IT215
// Checkpoint Payroll Program Three
//Use a Class to Store and Retrieve Dept Name, Number of Employee, avg salary
//Use a Constructor to Initialize the Department information, and a method within in that class
// to calculate the total Department Salary Amount
import java.util.Scanner; //Import Scanner
public class PayrollProgramPartThree // Main class that will store and retreive the information required by assignment
{
public static void main( String[] args )
{
Scanner input = new Scanner (System.in);
String deptName = "null";
int numberofEmployees;
float Salary;
float totalofDept;
System.out.println( "Welcome to the Payroll Program" );
while (!deptName.toLowerCase().equals("stop")) //while loop to ask for information repeatedly until "stop" is typed
{
System.out.println( "Please input the name of the department type stop to end." ); //get name of dept
deptName = input.next();
if(deptName.toLowerCase().equals( "stop")) //breaks the while loop
{
break;
}
System.out.println( "Please input the number of employees" );
numberofEmployees = input.nextInt();
while (numberofEmployees < 0 ) //validates user input
{
System.out.println ( "Please input a positive number");
numberofEmployees = input.nextInt();
}
System.out.println( "Please input the average employee salary" );
Salary = input.nextFloat();
while ( Salary < 0 ) // validates user input
{
System.out.println ( " Please input a positive number");
Salary = input.nextFloat();
}
PayrollClass myPayrollClass = new PayrollClass( deptName, numberofEmployees, Salary); //constructor to initialize an object with the information inputted above
System.out.printf( "The name of the department is %s\n ", deptName );
System.out.printf( "The total payroll is $%sUSD ", myPayrollClass.getTotal()); //method to get the total
}
System.out.println( "Thank you for using the Payroll Program!");
}
}
It works just fine! Not a problem and it displayed the correct total at the end.
I might close some credit by using just deptName at the end to display the name of the department, I think the professor wants me to run a getName() method, but when I initially tried that, I would just get "null" as the deptName. Any idea how I can fix that?
-
Re: Basic help with Constructors and Classes
Quote:
Originally Posted by
Harrald
... illegal start of expression...
This error means that a body has not ended before the start of an otherwise legal body has started.
What it means is you probably left out a } somewhere. As has been previously suggested, an IDE would quickly identify things like this with updates as fast as each key you type. Unless you are just the type who just wants to use a computer to do things, yet insist on doing them yourself anyway. But check the brackets either way.
-
Re: Basic help with Constructors and Classes
Quote:
Originally Posted by
Harrald
...I think the professor wants me to run a getName() method, but when I initially tried that, I would just get "null" as the deptName. Any idea how I can fix that?
Yes I noticed that and will answer that in a second. I have your new code up at this second. But I will post back on that in a few minutes.
-
Re: Basic help with Constructors and Classes
much better this round, but a few notes
Code java:
public class PayrollClass
{
//** Have you looked over all of these variables? What each one is for?
private String name;// name.. name of? person place or thing? I would go with
//**something more descriptive like you have below, numberEmployees. That is easy to figure out
private int numberEmployees;
private float avgSalary;// good name
String PName;//** PName huh? As a reader of the code I am not really sure what that P is for.
//** Is there a better name, perhaps more descriptive to its purpose in this class?
int PNumber;//**Again the P makes me wonder what it is for.
float PSalary;//**P...
//** could use some comment on the constructor, what it is used for, what the params mean
public PayrollClass (String name, int numberEmployees, float avgSalary)
{
PName = name;
PNumber = numberEmployees;
PSalary = avgSalary;
}
public String getName () // get method for retreiving the department name//**spelling
{
return PName;//**what is this returning and when the value get set?//edit - when did the value get set
}
public int getNumber() // get method for retrieving the number of employees
{
return numberEmployees;
}
public float getAvg () //get method for retreiving average salary//**spelling
{
return avgSalary;
}
public float getTotal () //method for multiplying the number of employees times the avg salary
//**getTotal what? total employees? total hours? I would say add to the name what it is a total of.
//**it is not such a big problem on a short project. Imagine code with even only 5 totals. Descriptive names are important
{
return numberEmployees * avgSalary;
}
//** formatting is better but still not java convention :/
}
-
Re: Basic help with Constructors and Classes
even fewer notes on more code
Code java:
//Thomas Harrald IT215
//Checkpoint Payroll Program Three
//Use a Class to Store and Retrieve Dept Name, Number of Employee, avg salary
//Use a Constructor to Initialize the Department information, and a method within in that class
// to calculate the total Department Salary Amount
import java.util.Scanner; //Import Scanner
public class PayrollProgramPartThree
// Main class that will store and retreive the information required by assignment//**:)
{
public static void main(String[] args) {//**thats the java touch
Scanner input = new Scanner(System.in);
String deptName = "null";
int numberofEmployees;
float Salary;
float totalofDept;//**what do you use this for?
System.out.println("Welcome to the Payroll Program");
while (!deptName.toLowerCase().equals("stop")) // while loop to ask for information repeatedly until "stop" is typed
//**what do you do with (!deptName.toLowerCase().equals("stop")) ?
//**Doesn't your break get you out of this loop?
{
System.out.println("Please input the name of the department type stop to end.");
// get name of dept
deptName = input.next();
if (deptName.toLowerCase().equals("stop")) // breaks the while loop//**you said it yourself...
{
break;
}
System.out.println("Please input the number of employees");
numberofEmployees = input.nextInt();
while (numberofEmployees < 0) // validates user input//**partially. Enter 3e as number of employees
{
System.out.println("Please input a positive number");
numberofEmployees = input.nextInt();
}
System.out.println("Please input the average employee salary");
Salary = input.nextFloat();
while (Salary < 0) // validates user input//**partially, try 3e again
{
System.out.println(" Please input a positive number");
Salary = input.nextFloat();
}
PayrollClass myPayrollClass = new PayrollClass(deptName, numberofEmployees, Salary);
// constructor to initialize an object with the information inputted above//**inputted?
System.out.printf("The name of the department is %s\n ", deptName);
System.out.printf("The total payroll is $%sUSD ", myPayrollClass.getTotal()); // method to get the total
//**method to get the total of what?
}
System.out.println("Thank you for using the Payroll Program!");
}
}//**not quite java convention on the formatting
-
Re: Basic help with Constructors and Classes
Quote:
Originally Posted by
jps
much better this round, but a few notes
Code java:
public class PayrollClass
{
//** Have you looked over all of these variables? What each one is for?
private String name;// name.. name of? person place or thing? I would go with
//**something more descriptive like you have below, numberEmployees. That is easy to figure out
private int numberEmployees;
private float avgSalary;// good name
String PName;//** PName huh? As a reader of the code I am not really sure what that P is for.
//** Is there a better name, perhaps more descriptive to its purpose in this class?
int PNumber;//**Again the P makes me wonder what it is for.
float PSalary;//**P...
//** could use some comment on the constructor, what it is used for, what the params mean
public PayrollClass (String name, int numberEmployees, float avgSalary)
{
PName = name;
PNumber = numberEmployees;
PSalary = avgSalary;
}
public String getName () // get method for retreiving the department name//**spelling
{
return PName;//**what is this returning and when the value get set?//edit - when did the value get set
}
public int getNumber() // get method for retrieving the number of employees
{
return numberEmployees;
}
public float getAvg () //get method for retreiving average salary//**spelling
{
return avgSalary;
}
public float getTotal () //method for multiplying the number of employees times the avg salary
//**getTotal what? total employees? total hours? I would say add to the name what it is a total of.
//**it is not such a big problem on a short project. Imagine code with even only 5 totals. Descriptive names are important
{
return numberEmployees * avgSalary;
}
//** formatting is better but still not java convention :/
}
The P was supposed to be for PayrollClass, as in the variable of name that is in the PayrollClass, PName. Guess it only made sense to me lol.
I thought the PName was set in the constructor? PName = name, name being assigned by being passed into the constructor in the main class.
I thought the while (stop) thing was redundant, but when I take it out and leave while(), I get an error.
-
Re: Basic help with Constructors and Classes
Oh and nevermind. I get a value of 0.00USD for the total... I'm missing something in the variable dance...
-
Re: Basic help with Constructors and Classes
Quote:
Originally Posted by
Harrald
The P was supposed to be for PayrollClass, as in the variable of name that is in the PayrollClass, PName. Guess it only made sense to me lol.
I thought the PName was set in the constructor? PName = name, name being assigned by being passed into the constructor in the main class.
look it over again and see what gets set and what gets returned
Quote:
Originally Posted by
Harrald
I thought the while (stop) thing was redundant, but when I take it out and leave while(), I get an error.
what is the correct syntax for a while loop?
Code java:
while(condition) {
//some way to exit the loop, we hope..
}
as an addon to above: If your variable names were all as descriptive as numberEmployees, you might not have had one of the errors you have. Just my opinion
-
Re: Basic help with Constructors and Classes
The initial values get returned...
-
Re: Basic help with Constructors and Classes
HA!
A simple of swapping the variables looks like it worked. instead of className = name; name = className;
-
Re: Basic help with Constructors and Classes
Nevermind... That didn't work. It is still returning the initial values...
-
Re: Basic help with Constructors and Classes
-
Re: Basic help with Constructors and Classes