1. Not finding help here? Sign up for a free 30min tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Help Writing Java Class

  1. Mar 14, 2012 #1
    1. The problem statement, all variables and given/known data

    This is my assignment: (Sorry, it's a little long)

    You are required to write a UserID Java class to be used when generating and storing the user id and password for a new user of a system. At a minimum, each UserID object should store a first name, last name, user id, and password for a user. Your class should have no public instance variables and should provide the following public methods:

    UserID
    (String first, String last)
    Constructor to create a new UserID object, initialize the first and last
    names, and generate a user id and a password, using the rules specified in
    programming assignment 2.

    ______________________________________________________________

    UserID ( ) Constructor to create a new anonymous UserID object. The first name
    should be initialized to “User” and the last name to “Anonymous”. The user
    id and password should be generated as specified in programming
    assignment 2, except that the number which is part of the user id should be
    a random integer in the range 10-99 (instead of the sum of the lengths of
    the first and last names).

    _______________________________________________________________

    getID ( ) Accessor that returns the user id as a String object. The same user id
    should be returned each time this method is invoked for an object.

    _______________________________________________________________

    getName ( ) Accessor to return a single String containing the name with format “LAST,
    FIRST” (all uppercase)


    _______________________________________________________________

    getPassword ( ) Accessor to return (but not modify) the currently stored password.



    _______________________________________________________________

    setPassword
    (String newPassword)
    Void method to modify the stored password with the String passed as
    argument.


    _______________________________________________________________


    generatePassword ( ) Void method that generates and stores a new random password that
    follows the same format as the original password.

    _______________________________________________________________

    toString ( ) Returns a String containing the user’s last name, first name, user id, and
    password.





    2. Relevant equations



    3. The attempt at a solution

    This is my program so far. I know that there is something wrong in the first two UserID methods. I was initially getting the program to work, but that was only when I initialized the private String first = ""; But I shouldn't have to do that, right? We just learned about classes, and I'm trying really hard to understand. Any help/explanation is appreciated!



    Code (Text):
    import java.util.*;

    public class UserID
       {
            private String userID;
            private String password;
            private String first;
          private String last;
            int num;
            Random generator = new Random();
            Scanner scan = new Scanner (System.in);
         
          public UserID(String first, String last)
          {
                String firstName = first;
                String lastName = last;
           
                // For user ID     
                String lastFive;                                // First 5 letters of last name            
                String firstLetter;                         // First letter of first name
                int totalCharacters;                            // Total number of characters in first and last
       
           
                // For password
                Random generator = new Random();            // Generates random digits
                int num1, num2, num3, num4, num5;       // 5 random digits
                String secondLetter;                            // Second letter of last name                  
       

           
                // GENERATES USER ID
           
                    // Gets first five letters of last name and changes letters to lowercase
                    lastFive = lastName.substring(0, 5);
                    lastFive = lastFive.toLowerCase();
               
                    // Gets first letter of first name and changes it to lowercase
                    firstLetter = firstName.substring(0,1);
                    firstLetter = firstLetter.toLowerCase();
           
                    // Gets number of characters in the first and last name
                    totalCharacters = firstName.length() + lastName.length();
           
                    // Creates user ID
                    userID = lastFive + firstLetter + totalCharacters;
               
               
               
                // GENERATES PASSWORD  
           
                    // Gets first letter of first name and changes it to uppercase
                    firstLetter = firstLetter.toUpperCase();
               
                    // Generates 5 random digits
                    num1 = generator.nextInt(10);
                    num2 = generator.nextInt(10);
                    num3 = generator.nextInt(10);
                    num4 = generator.nextInt(10);
                    num5 = generator.nextInt(10);
               
                    // Gets second letter of last name and changes it to uppercase
                    secondLetter = lastName.substring(1, 2);
                    secondLetter = secondLetter.toUpperCase();
               
                    // Creates password
                    password = firstLetter + num1 + num2 + num3 + num4 + num5 + secondLetter;                  
                }
                   
                public UserID()
                {              
                    first = "User";
                    last = "Anonymous";
                       
                    // For user ID     
                    String lastFive;                                // First 5 letters of last name            
                    String firstLetter;                         // First letter of first name
                    int random;                                     // Random number between 10-99
       
           
                    // For password
                    Random generator = new Random();            // Generates random digits
                    int num1, num2, num3, num4, num5;       // 5 random digits
                    String secondLetter;                            // Second letter of last name                  
       

           
                    // GENERATES USER ID
                   
                        // Gets first five letters of last name and changes letters to lowercase
                        lastFive = last.substring(0, 5);
                        lastFive = lastFive.toLowerCase();
               
                        // Gets first letter of first name and changes it to lowercase
                        firstLetter = first.substring(0,1);
                        firstLetter = firstLetter.toLowerCase();
           
                        // Generates random number
                        random = generator.nextInt(90) + 10;
           
                        // Creates user ID
                        userID = lastFive + firstLetter + random;
               
               
               
                    // GENERATES PASSWORD  
           
                        // Gets first letter of first name and changes it to uppercase
                        firstLetter = firstLetter.toUpperCase();
               
                        // Generates 5 random digits
                        num1 = generator.nextInt(10);
                        num2 = generator.nextInt(10);
                        num3 = generator.nextInt(10);
                        num4 = generator.nextInt(10);
                        num5 = generator.nextInt(10);
               
                        // Gets second letter of last name and changes it to uppercase
                        secondLetter = last.substring(1, 2);
                        secondLetter = secondLetter.toUpperCase();
               
                        // Creates password
                        password = firstLetter + num1 + num2 + num3 + num4 + num5 + secondLetter;
                }
                   
                public String getID()
                    {
                        return userID;
                    }
                   
                public String getName()
                    {
                        return (first.toUpperCase() + "," + last.toUpperCase());
                    }
                   
                public String getPassword()
                    {
                        return password;
                    }
                   
                public void setPassword (String newPassword)
                    {
                        password = newPassword;
                    }
                   
                public void generatePassword()
                    {
                        password = first.substring(0,1) + num + last.substring(1,2);
                    }
                   
                public String toString()
                    {
                        return ("Last Name: " + last + " First Name: " + first + " User ID: " + userID + " Password: " + password);
                    }
            }


    And this is the program I am supposed to use to test it:
    Code (Text):
    import java.util.Scanner;
       
        /**
        * This program can be used to test the UserID class methods.
         *
         * @author Dr.C.
        */
       public class UserIDTest
       {

          public static void main(String[] args)
          {
           
             UserID myInfo = new UserID("John", "Lennon");
                System.out.println(myInfo.getID());
                System.out.println(myInfo.getName());
                System.out.println(myInfo.getPassword());
             System.out.println(myInfo);
           
             UserID unknown = new UserID();
                System.out.println(unknown.getID());
                System.out.println(unknown.getName());
                System.out.println(unknown.getPassword());
             System.out.println(unknown);
               
                myInfo.setPassword("Imagine!!");
                System.out.println(myInfo.getPassword());
             System.out.println(myInfo);
               
                unknown.generatePassword();
                System.out.println(unknown.getPassword());
             System.out.println(unknown);      
          }
       
       }

    I'm getting an error on the getName method.

    Exception in thread "main" java.lang.NullPointerException
    at UserID.getName(UserID.java:132)
    at UserIDTest.main(UserIDTest.java:16
     
  2. jcsd
  3. Mar 15, 2012 #2
    Thats because first and last both point to null in your program. You need to make sure they are set in the constructor.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Help Writing Java Class
  1. Java class Arrays (Replies: 1)

Loading...