Results 1 to 4 of 4
  1. #1
    Slone is offline Member
    Join Date
    Dec 2010
    Posts
    1
    Rep Power
    0

    Default Really need method help

    No matter how many times I've been explained the theory behind methods, I can't determine how to write them to apply to programs. Here is a simple program that work, but I need to apply 4 different methods to it.


    setUsername() A mutator method that sets the username for the associated account. (Use any
    8 letters to represent the initial username.)

    getPassword() - An accessor method that asks for the password associated with the account.

    isUsernameValid() -This method checks whether the username entered meets the required rules
    for usernames.


    isPasswordValid() - This method checks whether the string entered is valid.




    //Using the JOptionPane class to promt the user for input
    import javax.swing.JOptionPane;
    class JavaAssignment5

    {

    public static void main(String[]args){

    //Promting the user to enter a user name and assigning it to 'x'
    String x = JOptionPane.showInputDialog(null,"Enter User Name: ", "Login", JOptionPane.QUESTION_MESSAGE);

    //Assigning 'y' as the password entered
    String y = JOptionPane.showInputDialog(null,"Password: ", "Login", JOptionPane.QUESTION_MESSAGE);

    //Setting the correct user name
    String user = new String("muzukashi");

    //Setting the correct password
    String pass = new String("Ja57pan");

    //Boolean expression to check if what the person entered is correct for both username and password
    if(user.equals(x) && pass.equals(y)){

    //Displays a message to inform them that they entered both correctly
    JOptionPane.showMessageDialog(null,"Valid user Name","Good job!", JOptionPane.WARNING_MESSAGE);
    }
    else{
    //Informs the user that they entered something wrong, but not actually telling them
    JOptionPane.showMessageDialog(null,"Invalid user Name","Dumbass", JOptionPane.WARNING_MESSAGE);
    }


    }

    }

  2. #2
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    Please use CODE tags (the # icon in the editor) when posting code.

    Java Code:
    //Using the JOptionPane class to promt the user for input
    import javax.swing.JOptionPane;
    
    class JavaAssignment5
    {
    
        public static void main(String[] args) {
    
            // Promting the user to enter a user name and assigning it to 'x'
            String x = JOptionPane.showInputDialog(null, "Enter User Name: ", "Login", JOptionPane.QUESTION_MESSAGE);
    
            // Assigning 'y' as the password entered
            String y = JOptionPane.showInputDialog(null, "Password: ", "Login", JOptionPane.QUESTION_MESSAGE);
    
            // Setting the correct user name
            String user = new String("muzukashi"); [COLOR="Blue"]// there's no need for [I][B]new String()[/B][/I] here -- [I][B]String user = "muzukashi";[/B][/I] is fine[/COLOR]
    
            // Setting the correct password
            String pass = new String("Ja57pan"); [COLOR="blue"]// same here[/COLOR]
    
            // Boolean expression to check if what the person entered is correct for both username and password
            if (user.equals(x) && pass.equals(y)) {
    
                // Displays a message to inform them that they entered both correctly
                JOptionPane.showMessageDialog(null, "Valid user Name","Good job!", JOptionPane.WARNING_MESSAGE);
            }
            else{
                // Informs the user that they entered something wrong, but not actually telling them
                JOptionPane.showMessageDialog(null, "Invalid user Name", "Dumbass", JOptionPane.WARNING_MESSAGE);
            }
        }
     }
    OK, so you know some basics of Java syntax, but you're confounded by methods. Let's take them one by one.


    setUsername() A mutator method that sets the username for the associated account. (Use any
    8 letters to represent the initial username.)


    A mutator method is one that changes the value of an instance variable or field of the Object. In this case, we seem to want a method that will set the value of a username field, so we'd better create the field first.
    Java Code:
    class JavaAssignment5
    {
    [COLOR="Blue"]    private String username;
    [/COLOR]
        public static void main(String[] args) {
    Now we can write the method. Since it's meant to update a field, and not to get any information from the Object, its return type should be void. It's likely that we'll want other code to be able to invoke this method, so we'll make it public. We'll need to know what value to set the username field to, so we will need a String argument.

    Java Code:
    class JavaAssignment5
    {
    [COLOR="Blue"]    private String username;
    
        public void setUsername(String username) {
            this.username = username;
        }
    [/COLOR]
        public static void main(String[] args) {
    The body of the method says to assign the contents of the username argument that was passed in to the method to the username field of the Object. We need to say this.username to indicate we mean the Object's field, or Java will assume we mean the argument. (Some people advise giving either the field or the argument a different name, as they find this syntax confusing. I think it's more confusing to start giving different names to what is essentially the same piece of information.)

    OK, that's one example. Try writing one of the other methods, and post what you come up with. Don't try invoking the methods yet, as there's another point we need to clear up before that will make sense.

    -Gary-

  3. #3
    SmilingKey is offline Member
    Join Date
    Dec 2010
    Posts
    19
    Rep Power
    0

    Default

    I think these are basic methods:

    getPassword(){
    return password;
    }

    browse for any example of get, set, is(return boolean)

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,926
    Rep Power
    8

Similar Threads

  1. Thread problem, calling method in run method
    By majk in forum Threads and Synchronization
    Replies: 4
    Last Post: 09-27-2010, 11:40 AM
  2. Replies: 2
    Last Post: 03-26-2010, 05:12 PM
  3. ArrayLists compareTo method, equals method
    By random0munky in forum New To Java
    Replies: 2
    Last Post: 10-26-2009, 07:20 PM
  4. Replies: 29
    Last Post: 09-25-2008, 07:55 PM
  5. cannot call private method from static method
    By jon80 in forum New To Java
    Replies: 3
    Last Post: 05-07-2008, 08:37 AM

Posting Permissions

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