Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Page 1 of 2 12 LastLast
Results 1 to 25 of 28

Thread: Basic help with Constructors and Classes

  1. #1
    Member
    Join Date
    Jul 2012
    Posts
    56
    Thanks
    15
    Thanked 0 Times in 0 Posts

    Default 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:

    // 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!


  2. #2
    Junior Member awinston's Avatar
    Join Date
    Jul 2012
    Location
    United States
    Posts
    10
    My Mood
    Fine
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Basic help with Constructors and Classes

    Do you have a specific question about your assignment?
    "Success is not final, failure is not fatal: it is the courage to continue that counts." - Winston Churchill

  3. #3
    Member
    Join Date
    Jul 2012
    Posts
    56
    Thanks
    15
    Thanked 0 Times in 0 Posts

    Default Re: Basic help with Constructors and Classes

    Quote Originally Posted by awinston View Post
    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.

  4. #4
    Junior Member awinston's Avatar
    Join Date
    Jul 2012
    Location
    United States
    Posts
    10
    My Mood
    Fine
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default 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?
    "Success is not final, failure is not fatal: it is the courage to continue that counts." - Winston Churchill

  5. #5
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Basic help with Constructors and Classes

    Any place you see //** in the code is a comment I added

    // 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

  6. The Following User Says Thank You to jps For This Useful Post:

    Harrald (August 1st, 2012)

  7. #6
    Member
    Join Date
    Jul 2012
    Posts
    56
    Thanks
    15
    Thanked 0 Times in 0 Posts

    Default 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.

  8. #7
    Member
    Join Date
    Jul 2012
    Posts
    56
    Thanks
    15
    Thanked 0 Times in 0 Posts

    Default 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.

    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.
    Last edited by Harrald; August 1st, 2012 at 08:37 PM.

  9. #8
    Member
    Join Date
    Jul 2012
    Posts
    56
    Thanks
    15
    Thanked 0 Times in 0 Posts

    Default Re: Basic help with Constructors and Classes

    Fixed the Class that will be used in the main method.

    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.

  10. #9
    Member
    Join Date
    Jul 2012
    Posts
    56
    Thanks
    15
    Thanked 0 Times in 0 Posts

    Default Re: Basic help with Constructors and Classes

    Here is the other part, the class with the main method.

    // 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.

  11. #10
    Member
    Join Date
    Jul 2012
    Posts
    56
    Thanks
    15
    Thanked 0 Times in 0 Posts

    Default 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.

  12. #11
    Member
    Join Date
    Jul 2012
    Posts
    56
    Thanks
    15
    Thanked 0 Times in 0 Posts

    Default 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();

  13. #12
    Member
    Join Date
    Jul 2012
    Posts
    69
    My Mood
    Relaxed
    Thanks
    1
    Thanked 6 Times in 6 Posts

    Default Re: Basic help with Constructors and Classes

    Could you post the code as is now?

  14. #13
    Member
    Join Date
    Jul 2012
    Posts
    56
    Thanks
    15
    Thanked 0 Times in 0 Posts

    Default Re: Basic help with Constructors and Classes

    Completely revamped code.

    Here is the class:

    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:

    // 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?

  15. #14
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Basic help with Constructors and Classes

    Quote Originally Posted by Harrald View Post
    ... 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.

  16. #15
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Basic help with Constructors and Classes

    Quote Originally Posted by Harrald View Post
    ...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.

  17. #16
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Basic help with Constructors and Classes

    much better this round, but a few notes
    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   :/
    }
    Last edited by jps; August 1st, 2012 at 10:33 PM.

  18. The Following User Says Thank You to jps For This Useful Post:

    Harrald (August 1st, 2012)

  19. #17
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Basic help with Constructors and Classes

    even fewer notes on more code
    //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

  20. The Following User Says Thank You to jps For This Useful Post:

    Harrald (August 1st, 2012)

  21. #18
    Member
    Join Date
    Jul 2012
    Posts
    56
    Thanks
    15
    Thanked 0 Times in 0 Posts

    Default Re: Basic help with Constructors and Classes

    Quote Originally Posted by jps View Post
    much better this round, but a few notes
    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.

  22. #19
    Member
    Join Date
    Jul 2012
    Posts
    56
    Thanks
    15
    Thanked 0 Times in 0 Posts

    Default 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...

  23. #20
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Basic help with Constructors and Classes

    Quote Originally Posted by Harrald View Post
    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 View Post
    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?
    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

  24. The Following User Says Thank You to jps For This Useful Post:

    Harrald (August 1st, 2012)

  25. #21
    Member
    Join Date
    Jul 2012
    Posts
    56
    Thanks
    15
    Thanked 0 Times in 0 Posts

    Default Re: Basic help with Constructors and Classes

    The initial values get returned...

  26. #22
    Member
    Join Date
    Jul 2012
    Posts
    56
    Thanks
    15
    Thanked 0 Times in 0 Posts

    Default Re: Basic help with Constructors and Classes

    HA!

    A simple of swapping the variables looks like it worked. instead of className = name; name = className;

  27. #23
    Member
    Join Date
    Jul 2012
    Posts
    56
    Thanks
    15
    Thanked 0 Times in 0 Posts

    Default Re: Basic help with Constructors and Classes

    Nevermind... That didn't work. It is still returning the initial values...

  28. #24
    Member
    Join Date
    Jul 2012
    Posts
    56
    Thanks
    15
    Thanked 0 Times in 0 Posts

    Default Re: Basic help with Constructors and Classes

    I'm stumped...

  29. #25
    Member
    Join Date
    Jul 2012
    Posts
    56
    Thanks
    15
    Thanked 0 Times in 0 Posts

    Default Re: Basic help with Constructors and Classes

    FIXED IT!!!! WOOT!!!!

Page 1 of 2 12 LastLast

Similar Threads

  1. Replies: 3
    Last Post: July 8th, 2012, 03:44 PM
  2. Need help with classes and constructors!!
    By rayp4993 in forum What's Wrong With My Code?
    Replies: 8
    Last Post: October 14th, 2011, 01:02 PM
  3. Constructors
    By av8 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: July 19th, 2011, 06:40 PM
  4. constructors in servlets
    By the light in forum Java Servlet
    Replies: 3
    Last Post: June 27th, 2011, 04:13 AM
  5. [SOLVED] Overloading constructors(Multiple Constructors)
    By chronoz13 in forum Java Theory & Questions
    Replies: 2
    Last Post: May 11th, 2011, 12:55 PM