Results 1 to 7 of 7
Like Tree1Likes
  • 1 Post By jim829

Thread: Gettters and Setters

  1. #1
    tsing520 is offline Member
    Join Date
    Oct 2013
    Posts
    9
    Rep Power
    0

    Default Gettters and Setters

    I'm very new to programming and I'm trying to write a programme using BlueJ. This is my 'User' class. I am trying to make it so this class has a Username, Name and Usertype (which is either admin, user or editor). I have a constructor which takes these 3 as paramaters but I'm struggling on the code for the getUsername, getUserType, getName and setUserType methods. For setUserType how do I make it so one of the 3 options (admin, editor or user) has to be chosen? Also should the set method be void? from examples I have looked up that seems to be the norm but surely I want to return the set value? Thanks in advance for any help!

    Java Code:
    public class User{
    	
    	public String id;
    	public String userPermissions;
    	public String actualName;
    	
    	public User(String username, String userType, String name){
    		id = username;
    		userPermissions = userType;
    		actualName= name;
    	}
    
    	public String getUsername(){
    		return id;
    	}
    		
    	public String getUserType(){
    		return userPermissions;
    	}		
    
    	public String getName(){
    		return actualName;
    	}
    
    	public String setUserType(){
    	
    	
    	}
    }
    Last edited by tsing520; 11-09-2013 at 06:02 PM.

  2. #2
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,058
    Rep Power
    6

    Default Re: Gettters and Setters

    Option #1: Store the user type as an integer and create some constants in your class to provide mnemonics for the values.

    Java Code:
    public static final int USER_TYPE_ADMIN = 1;
    public static final int USER_TYPE_USER = 2;
    public static final int USER_TYPE_EDITOR = 3;
    I like to avoid using 0 for these kinds of constants since 0 is the default value of an int. Then in your setter, do something like this:

    Java Code:
    if(arg == USER_TYPE_ADMIN || arg == USER_TYPE_USER || arg == USER_TYPE_EDITOR) {
       userType = arg;
    }
    else {
       throw new IllegalArgumentException("invalid user type: " + arg);
    }
    This way of doing it is fine if you only have a few allowed values. If you had a lot of values, it would be better to create a UserType enum and store the user type as a UserType. Callers can't pass an invalid value because there are no invalid values; all UserTypes must come from the enum.
    Get in the habit of using standard Java naming conventions!

  3. #3
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,058
    Rep Power
    6

    Default Re: Gettters and Setters

    BTW, you should perform these kinds of checks in your constructor, too. To avoid duplicating code, you could call your setter from your constructor.
    Get in the habit of using standard Java naming conventions!

  4. #4
    tsing520 is offline Member
    Join Date
    Oct 2013
    Posts
    9
    Rep Power
    0

    Default Re: Gettters and Setters

    I am very new to java so there are some things there which I am struggling to understand:
    1)what does the final keyword do?
    2)when I run it i get the error cannot find symbol- variable arg- why is this?
    3)how would i call my setter from my constructor?
    4) I have attempted to make an enum from googling it but I am unsure what to do- I have:
    [CODE]
    public enum UserType{
    admin, editor, user
    }
    [CODE]
    but should I be referencing this in the setter? how would I go about doing this?
    Last edited by tsing520; 11-09-2013 at 06:15 PM.

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    2,949
    Rep Power
    4

    Default Re: Gettters and Setters

    IMO, enums are the way to go. Your example is fine as is except I would recommend the types be uppercase:

    Java Code:
    enum UserType {EDITOR, ADMIN, USER};
    
    // Then in your class you can have
    
    UserType currentType;
    
    // And the method:
    
    setType(UserType type) {
        currentType = type;
    }
    Say you want to set the type to USER. Then a method call
    Java Code:
    setType(UserType.USER);
    will do it. The nice thing about this is that only a type of UserType may be passed to the setType method. And if you want to add
    additional types later on you can and you don't have to check to ensure it is a valid type because it will be part of the enum
    declaration.

    Regards,
    Jim
    SurfMan likes this.
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  6. #6
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,058
    Rep Power
    6

    Default Re: Gettters and Setters

    1) In this case, it makes it so you can't change the value. It has a few other meanings in other contexts.
    2) You need to include arg in your method's parameter list. Or better yet, give it a descriptive name.
    3) Sounds like you need to review how parameters and method calls work. Passing Information to a Method or a Constructor (The Java™ Tutorials > Learning the Java Language > Classes and Objects)

    One thing I forgot to mention about enums... you still need to check if the value is null if you don't want to allow nulls.
    Get in the habit of using standard Java naming conventions!

  7. #7
    tsing520 is offline Member
    Join Date
    Oct 2013
    Posts
    9
    Rep Power
    0

    Default Re: Gettters and Setters

    Thanks for the help- my user class all works now!! I now have a brand new problem- one which I am sure is disgustingly basic but I can't seem to find the answer. I am now making an arraylist of users in another class called UserGroup. I need to instantiate this in a constructor in UserGroup and write a method called .addSampleData() which creates 10 Users and using the ArrayList's add() method, puts the 10 new User objects into the ArrayList. I can't figure out how to do this at all!! Please help!

Similar Threads

  1. Getters and setters
    By Erre in forum New To Java
    Replies: 10
    Last Post: 10-25-2012, 08:30 PM
  2. EasyMock or Bean setters?
    By Jason1 in forum New To Java
    Replies: 0
    Last Post: 02-13-2012, 11:34 PM
  3. Confused about getters and setters
    By jnjh in forum New To Java
    Replies: 8
    Last Post: 05-03-2011, 07:12 AM
  4. Getters and Setters
    By lheviathan in forum New To Java
    Replies: 4
    Last Post: 11-02-2009, 01:47 AM
  5. Getters and Setters
    By Charliestons in forum New To Java
    Replies: 10
    Last Post: 09-12-2008, 10:57 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •