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 9 of 9

Thread: How do I get rid of the "this."

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

    Default How do I get rid of the "this."

    Ok I have a class in OOP and instructor marked me off for have
     this.employeeId  this.HourlyRate  this.firstName this.lastName

    Anyways here is my code, I tried to fix it, but every time the whole thing collapses and get errors, any help would be greatly appreciated

     
     
    /*
     * To change this license header, choose License Headers in Project Properties.
     * To change this template file, choose Tools | Templates
     * and open the template in the editor.
     */
     
    package coursework;
     
    public class Employee { 
     
    	private String firstName; 
    	private String lastName; 
    	private int employeeId; 
    	private double hourlyRate; 
    	private Timecard timeCard;
     
    	public Employee(String firstNameInput, String lastNameInput, int idInput, double rateInput, int[] daysIn) {
    		setFirstName(firstNameInput); 
    		setLastName(lastNameInput); 
    		setEmployeeID(idInput); 
    		setHourlyRate(rateInput); 
    		setTimeCard(new Timecard(daysIn));
    	} 
     
    	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; 
    		} 
    	} 
     
    	private void setTimeCard(Timecard newtimeCard) {
     
    		if (newtimeCard == null) {
    			System.out.print("time card cannot be null");
    			System.exit(-1);
    		}
    		timeCard = newtimeCard;
    	}
     
    	public Timecard getTimeCard() {
    		return timeCard;
    	}
     
    	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; 
    	} 
    }


  2. #2
    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: How do I get rid of the "this."

    Quote Originally Posted by zng690 View Post
    Ok I have a class in OOP and instructor marked me off for have
     this.employeeId  this.HourlyRate  this.firstName this.lastName
    What is you question?? Please be precise whether you want to use the above code in your program or what to do with that??.

    Thanks,

  3. #3
    Junior Member
    Join Date
    Jan 2014
    Posts
    11
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: How do I get rid of the "this."

    Quote Originally Posted by Ganeprog View Post
    What is you question?? Please be precise whether you want to use the above code in your program or what to do with that??.

    Thanks,
    Hi, thanks for the reply, I am looking to keep the employeeId, firstName lastName and such, I just need to get rid of the "this." how do I modify the code to get rid of the "this." and keep the rest

  4. #4
    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: How do I get rid of the "this."

    k k fine. You just change the declared variable name or argument name. If you use same name you should differentiate using this keyword. k now use different name for the variables. For your reference,
    private void setFirstName (String fName) { 
    		// 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 = fName.length(); 
     
    		if (nameLength < 1) { 
    			System.out.println("First Name length is too small"); 
    			System.out.println(firstName + ": " + fName.length());
    		} else if (nameLength > 20) { 
    			System.out.println("First Name length is too long"); 
    			System.out.println(firstName + ": " + fName.length());
    		} else { 
    			firstName = fName; 
    		} 
    	}

    Here i just changed the argument name. Now you don't want to use "this" keyword.

    Try it....

    Thanks,

  5. #5
    Member
    Join Date
    Feb 2014
    Location
    India
    Posts
    47
    My Mood
    Bored
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Default Re: How do I get rid of the "this."

    When you use the arguments in a method with the same name as the variables of the class/method, then to differentiate between those variables we use "this" keyword, which refers to the current class . So to get rid of them you can change the name of the variables as suggested by Ganeprog

  6. #6
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    4,483
    My Mood
    Mellow
    Thanks
    151
    Thanked 609 Times in 596 Posts

    Default Re: How do I get rid of the "this."

    The use of 'this' is often entirely appropriate and consistent with standard coding practices, even sometimes required in order for the code to interface properly with other technologies, JSP/JavaBeans for instance. So, eliminating 'this' simply for the sake of eliminating 'this' may not be a good idea.

    If you have a specific need to remove 'this' and then find you have errors you can't correct, then please post the specific cases, the justification for removing the 'this' in each case, and the errors you receive when taking those actions. With that detail, we can give you better answers.

  7. #7
    Junior Member
    Join Date
    Jan 2014
    Posts
    11
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: How do I get rid of the "this."

    thank you for the advice, the instructor took a few points off my grade on the last assignment, he was anamant about not using "this."

  8. #8
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,608
    Thanks
    24
    Thanked 316 Times in 295 Posts

    Default Re: How do I get rid of the "this."

    In the context of your program, I disagree with your instructor. There was nothing syntactically or stylistically wrong with the use of the this keyword. Now, if it was mentioned it in your assignment instructions, that is another story. If not, and you feel brave enough to fight for your points back, refer your instructor to this oracle page: Using the this Keyword (The Java™ Tutorials > Learning the Java Language > Classes and Objects)
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  9. #9
    Member
    Join Date
    Feb 2014
    Location
    India
    Posts
    47
    My Mood
    Bored
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Default Re: How do I get rid of the "this."

    you should not leave it. Get to the core, where you were lagging so that next time you do not face much problems with "this"

Similar Threads

  1. Understanding layers of system logic. Clarity for what "SDK" & "JDK" mean
    By MilkWetGhost in forum Java Theory & Questions
    Replies: 1
    Last Post: October 3rd, 2013, 12:25 PM
  2. Replies: 2
    Last Post: June 22nd, 2013, 10:30 AM
  3. Replies: 3
    Last Post: December 7th, 2011, 01:03 AM
  4. Replies: 7
    Last Post: August 13th, 2011, 01:22 AM
  5. "java.lang.NoSuchMethodError: main" and "fatal exception occured."
    By joachim89 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 10th, 2010, 07:35 AM

Tags for this Thread