?
Solved

Need help modifying a code

Posted on 2008-01-27
7
Medium Priority
?
278 Views
Last Modified: 2013-11-23
I was wondering if anyone can help me modify my code. I need to use a class to store and retrieve employee name, hourly rate, and number of hours worked. I am guessing I will need to use a constructor to initialize the information but I also need a method within the class to calculate the weekly pay. I also want to be able to have the user type STOP which will completely terminate the program. Thanks!
import java.util.*;
public class Payroll
{
	// main method begins execution of Java application
	public static void main( String args[] )
	{
		
		// create Scanner to obtain input from command window
		Scanner input = new Scanner( System.in );
		
		String name = ""; // employee's name
		int number1; // hourly rate
		int number2; // number of hours worked for the week
		int product; // product of number1 and number2
 
		while(name!=null && !name.equalsIgnoreCase("stop"))
 {
	 	number1 =-1;
		number2 = -1;
	 
		System.out.print( "Enter employee's name: "); //prompt user to input name
		name = input.nextLine(); //read employee's name from user's input
		while(number1<0)
		{
		System.out.print( "Enter hourly rate: " ); // prompt user for employee's hourly rate
		number1 = input.nextInt(); // read hourly rate from user's input
		}
		while(number2<0)
		{	
		System.out.print( "Enter hours worked for the week: " ); // prompt user to enter number of hours worked for the week
		number2 = input.nextInt(); // read number of weeks from user's input
		}
		
		product = number1 * number2; // multiply numbers
		
		System.out.print( "The Employee " + name ); // displays employee's name
		System.out.println( " weekly pay is $%d\n"  + product ); // displays weekly pay
 }
 
	} // end method main
 
} // end class Payroll

Open in new window

0
Comment
Question by:pogibear77
  • 4
  • 3
7 Comments
 
LVL 9

Expert Comment

by:matthew016
ID: 20753921
Create a class Employee and as field :

private String name;
private int hourlyRate;
private int hoursWorked;
private int weeklyPay;
// need something better than "number1" and "number2"

then you create a constructor, getters, and setters for all of the fields.

The input from the user can stay in the Payroll class and once you have all of the fields information, you call the constructor.
Or can you create an empty Employee object, and call the setter methods as you get the input.

When user types STOP, just do a System.exit(0);
0
 

Author Comment

by:pogibear77
ID: 20754076
Hey matthew016,
What do you mean // need something better than number1 and number2?
0
 
LVL 9

Expert Comment

by:matthew016
ID: 20754093
Do not call your variable names "number1", "number2", "product",
it's not very oriented object programming.
Give them a name in the context
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 

Author Comment

by:pogibear77
ID: 20755526
Could you help start me off with the code? I still get confused when adding multiple classes. Thanks
0
 

Author Comment

by:pogibear77
ID: 20755830
This is what i did so far. what u think?
import java.util.Scanner;
 
public class Employee //class for Employee which will hold the employee data
{
	private String employeeName; // name of employee
	
	// constructor initializes employeeName
	public Employee( String name )
	{
		employeeName = name; // initializes employeeName
	} // end constructor
	
	// method to set the employee name
	public void setEmployeeName( String name )
	{
		employeeName = name; // store the employee's name
	} // end method setEmployeeName
	
	// method to retrieve the employee name
	public String getEmployeeName
	{
		return employeeName;
	} // end method getEmployeeName
	
	private int hourlyRate; // employee's hourly rate
	
	// constructor initializes hourlyRate
	public Employee( String name )
	{
		Hourly Rate = int hourlyRate; // initializes hourlyRate
	} // end constructor
	
	// method to set the hourly rate
	public void setHourlyRate()
	{
		

Open in new window

0
 
LVL 9

Accepted Solution

by:
matthew016 earned 2000 total points
ID: 20757675
Remove the Scanner import.
Your constructor should initialize all of the necessary fields.


public class Employee //class for Employee which will hold the employee data
{
        private String employeeName; // name of employee
        private int hourlyRate; // employee's hourly rate
        private int hoursWorked; 
        private int weeklyPay; 
        
        // constructor initializes employeeName
        public Employee( String name, int hourlyRate, int hoursWorked, int weeklyPay )
        {
                employeeName = name; // initializes employeeName
                this.hourlyRate = hourlyRate;
                this.hoursWorked = hoursWorked;
                this.weeklyPay = weeklyPay ;
        } // end constructor
        
        // method to set the employee name
        public void setEmployeeName( String name )
        {
                employeeName = name; // store the employee's name
        } // end method setEmployeeName
        
        // method to retrieve the employee name
        public String getEmployeeName()
        {
                return employeeName;
        } // end method getEmployeeName
        
        // method to set the hourly rate
        public void setHourlyRate(int hourlyRate)
        {
          this.hourlyRate = hourlyRate;
        }
        public void getHourlyRate()
        {
          return hourlyRate;
        }
        public void setHoursWorked (int hourlyRate)
        {
          this.hoursWorked = hoursWorked ;
        }
        public void getHoursWorked ()
        {
          return hoursWorked ;
        }
 
        public void getWeeklyPay ()
        {
          return hoursWorked * hourlyRate ;
        }
        
}

Open in new window

0
 
LVL 9

Expert Comment

by:matthew016
ID: 20757686
Forget the weeklyPay in the constructor, I forgot this was calculated  :-)

should be:
        public Employee( String name, int hourlyRate, int hoursWorked)
        {
                employeeName = name; // initializes employeeName
                this.hourlyRate = hourlyRate;
                this.hoursWorked = hoursWorked;
        } // end constructor

and I correct setHoursWorked from my snippet:

        public void setHoursWorked (int hoursWorked )
        {
          this.hoursWorked = hoursWorked ;
        }
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This video teaches viewers about errors in exception handling.
Suggested Courses
Course of the Month7 days, 11 hours left to enroll

608 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question