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 2 of 2 FirstFirst 12
Results 26 to 28 of 28

Thread: Basic help with Constructors and Classes

  1. #26
    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 final code:

    public class PayrollClass
    {
     
    	private String name;
    	private int numberEmployees;
    	private float avgSalary;
     
    	String className;
    	int classNumber;
    	float classSalary;
     
    	public PayrollClass(String className, int classNumber, float classSalary)
    	{
    		name = className;
    		numberEmployees = classNumber;
    		avgSalary = classSalary;
    	}
     
     
    	public String getName() // get method for retreiving the department name
    	{
    		return name;
    	}
     
    	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 avgSalary * numberEmployees;
     
    	}
     
    }

    And the Main method class

    // 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 ", myPayrollClass.getName() );
    		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!");
     
    	}
    }

    I looked carefully at why it would give initial values. I traced it to the constructor in the PayrollClass. I noticed that I was giving them private values to pass onto to public ones. So I changed it to the publicly declared variables, and in the method set them equal to the private value... GOD this was a rough assignment.

    Thank you so much everyone for your help! Inlcuding jps for guiding me. I am going to see if I can find some practice examples in this subject because I need to understand it better...

  2. #27
    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

    //**could use a comment to describe the purpose and use of this class as a whole
    {
     
    	private String name;
    	private int numberEmployees;
    	private float avgSalary;
     
    	String className;//**what does this variable do?
    	int classNumber;//**what does this variable do?
    	float classSalary;//**what does this variable do?
     
    	public PayrollClass(String className, int classNumber, float classSalary)
    	{
    		name = className;
    		numberEmployees = classNumber;
    		avgSalary = classSalary;
    	}
     
     
    	public String getName() // get method for retreiving the department name
    	{
    		return name;
    	}
     
    	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 avgSalary * numberEmployees;
     
    	}
     
    }
    //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;//**what does this variable do?
     
    	System.out.println( "Welcome to the Payroll Program" );
     
    	while (!deptName.toLowerCase().equals("stop")) //while loop to ask for information repeatedly until "stop" is typed
               //**I still don't think all of this is necessary. Try while(true)
    	{
     
    		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 ", myPayrollClass.getName() );
    		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!");
     
    	}
    }

    I think one more go over would just about cover it. There are some variables here and there that I am not sure you need. A few places I personally would like to see a comment. Still not quite up to java par with the format. I was someone who refused to use java convention for a long time. Over time I have found looking at java code with java format keeps the brain on java easier than seeing old styles used in old languages.

  3. #28
    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
    //**could use a comment to describe the purpose and use of this class as a whole
    {
     
    	private String name;
    	private int numberEmployees;
    	private float avgSalary;
     
    	String className;//**what does this variable do?
    	int classNumber;//**what does this variable do?
    	float classSalary;//**what does this variable do?
     
    	public PayrollClass(String className, int classNumber, float classSalary)
    	{
    		name = className;
    		numberEmployees = classNumber;
    		avgSalary = classSalary;
    	}
     
     
    	public String getName() // get method for retreiving the department name
    	{
    		return name;
    	}
     
    	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 avgSalary * numberEmployees;
     
    	}
     
    }
    //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;//**what does this variable do?
     
    	System.out.println( "Welcome to the Payroll Program" );
     
    	while (!deptName.toLowerCase().equals("stop")) //while loop to ask for information repeatedly until "stop" is typed
               //**I still don't think all of this is necessary. Try while(true)
    	{
     
    		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 ", myPayrollClass.getName() );
    		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!");
     
    	}
    }

    I think one more go over would just about cover it. There are some variables here and there that I am not sure you need. A few places I personally would like to see a comment. Still not quite up to java par with the format. I was someone who refused to use java convention for a long time. Over time I have found looking at java code with java format keeps the brain on java easier than seeing old styles used in old languages.
    Well now that the assignment is turned and finish, I can work on cleaning up the code and fixing some things you pointed out that wasn't related directly to the assignment. I am going to start the code from scratch and no reference to the original, just so I know I got a hold on it. I'll post the finished code here when I'm done.

Page 2 of 2 FirstFirst 12

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