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.

Results 1 to 4 of 4

Thread: Driver Issue

  1. #1
    Junior Member
    Join Date
    Jan 2014
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Driver Issue

    Hi, below is my code, I am new to Java, is my driver in the wrong place??

    package coursework;
     
    public class Employee { 
     
    	private String firstName; 
    	private String lastName; 
    	private int employeeId; 
    	private double hourlyRate; 
     
    	public Employee(String firstName, String lastName, int employeeId, double hourlyRate) 
    	{ 
    		setFirstName(firstName); 
    		setLastName(lastName); 
    		setEmployeeID(employeeId); 
    		setHourlyRate(hourlyRate); 
    	} 
     
    	public void printEmpDetails() 
    	{ 
    		System.out.println("Name: " + firstName + " " + lastName); 
    		System.out.println("Emp. Id: " + employeeId); 
    		System.out.println("Hourly Rate: $" + hourlyRate); 
    		System.out.println(); 
    	} 
     
    	private void setFirstName (String firstName) { 
    		// Here we are setting our class variable firstName to the passed in variable "name" 
    		// But only if it has a length greater than 0 and less than or equal to 20 
     
    		int nameLength = firstName.length(); 
     
    		if (nameLength < 1) { 
    			System.out.println("First Name length is too small"); 
    			System.out.println(firstName + ": " + firstName.length());
    		} else if (nameLength > 20) { 
    			System.out.println("First Name length is too long"); 
    			System.out.println(firstName + ": " + firstName.length());
    		} else { 
    			this.firstName = firstName; 
    		} 
    	} 
     
    	private void setLastName (String lastName) { 
    		// Here we are setting our class variable firstName to the passed in variable "name" 
    		// But only if it has a length greater than 0 and less than or equal to 20 
     
    		int nameLength = lastName.length(); 
     
    		if (nameLength < 1) { 
    			System.out.println("Last Name length is too small"); 
    			System.out.println(firstName + ": " + lastName.length());
    		} else if (nameLength > 20) { 
    			System.out.println("Last Name length is too long"); 
    			System.out.println(firstName + ": " + lastName.length());
    		} else { 
    			this.lastName = lastName; 
    		} 
    	} 
     
    	private void setEmployeeID (int employeeId) { 
    		// Here we are setting our class variable firstName to the passed in variable "name" 
    		// But only if it has a length greater than 0 and less than or equal to 20 
     
    		if (employeeId > 9999) { 
    			System.out.println("Number length error: this should be less than 9999: " + employeeId); 
    		} else if (employeeId < 1000) { 
    			System.out.println("Number length error: this should be more than 1000: " + employeeId); 
    		} else { 
    			this.employeeId = employeeId; 
    		} 
    	} 
     
    	private void setHourlyRate (double hourlyRate) { 
    		// Here we are setting our class variable firstName to the passed in variable "name" 
    		// But only if it has a length greater than 0 and less than or equal to 20 
     
    		if(hourlyRate < 0)
    		{
    			System.out.println("Incorrect value, number must be greater than zero: " + hourlyRate);
    		}
    		else{
    			this.hourlyRate = hourlyRate; 
    		} 
    	} 
     
    	public String getFirstName() { 
    		// Here we are getting our class variable firstName 
    		return firstName; 
    	} 
     
    	public String getLastName() { 
    		// Here we are getting our class variable lastName 
    		return lastName; 
    	} 
     
     
    	public int getEmployeeId() { 
    		// Here we are getting our class variable firstName 
    		return employeeId; 
    	} 
     
    	public double getHourlyRate() { 
    		// Here we are getting our class variable firstName 
    		return hourlyRate; 
    	} 
    } 
     
    package coursework;
     
    import java.util.Scanner;
     
    public class Driver {
    	public static void main(String[] args) 
    	{ 
    		// First we variable of type Employee. 
    		// We always set this to null as good practice. 
    		Employee e = null; 
     
    		// Declare variables to hold user inputs needed 
    		// by the Employee constructor 
    		String firstNameInput; 
    		String lastNameInput; 
    		int idInput; 
    		double rateInput; 
     
    		// Get input data values from the user (via the keyboard) 
    		Scanner userInput = new Scanner(System.in); 
     
    		System.out.println("Employee First Name: "); 
    		firstNameInput = userInput.next(); 
     
    		System.out.println("Employee Last Name: "); 
    		lastNameInput = userInput.next(); 
     
    		System.out.println("Employee Id: " ); 
    		idInput = userInput.nextInt(); 
     
    		System.out.println("Employee Hourly Rate: "); 
    		rateInput = userInput.nextDouble(); 
     
    		// Now allocate a new instance of an Employee, 
    		// passing test data to the constructor. 
    		e = new Employee(firstNameInput, lastNameInput, 
    				idInput, rateInput); 
     
    		// Print out the data with nice titles 
    		System.out.println(); 
    		System.out.println("Name: " + e.getFirstName() + " " 
    				+ e.getLastName()); 
    		System.out.println( "Emp. Id: " + e.getEmployeeId()); 
    		System.out.println("Hourly Rate: $" + e.getHourlyRate()); 
     
    		// We're done with this employee, so now free up 
    		// the space we allocated for that employee by setting 
    		// the "e" variable back to null 
     
    		e = null; 
     
    		System.out.println(); 
    	}
    }


  2. #2
    Junior Member
    Join Date
    Jan 2014
    Posts
    23
    My Mood
    Mellow
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Driver Issue

    Yes it is in working condition.
    y are u getting any error.. if yes please specify ???

  3. #3
    Member Ganeprog's Avatar
    Join Date
    Jan 2014
    Location
    Chennai,India
    Posts
    80
    My Mood
    Love
    Thanks
    3
    Thanked 6 Times in 6 Posts

    Default Re: Driver Issue

    Hi,

    The above program is working fine. What is the problem in that.?? If you facing any difficulties,errors or doubts post here.

    thanks,

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    4,459
    My Mood
    Mellow
    Thanks
    148
    Thanked 607 Times in 594 Posts

    Default Re: Driver Issue

    Welcome to the Forum! Thanks for taking the time to learn to post code correctly.

    What do you mean by, "is my driver in the wrong place?" It looks good from here, at least posted with your other code for efficiency, but your requirements, either in your programming environment or according to the assignment, may be different.

Similar Threads

  1. regarding driver identification
    By chinnu&manu in forum What's Wrong With My Code?
    Replies: 4
    Last Post: January 17th, 2014, 07:10 AM
  2. Class Driver help
    By Kseidel in forum Object Oriented Programming
    Replies: 2
    Last Post: November 2nd, 2012, 01:06 PM
  3. Java Issue / Cache issue
    By VisualPK in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 14th, 2012, 08:43 PM
  4. Implementing a Driver to following code
    By Twoacross in forum What's Wrong With My Code?
    Replies: 4
    Last Post: November 11th, 2011, 10:17 PM