How Should I Implement the UserID and Password Generation in Java?

In summary: A simple way to do that would be to change your constructor to this:public UserID(String first, String last){ this.first = first; this.last = last; // Rest of your code}
  • #1
csgirl504
18
0

Homework Statement



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.

Homework Equations


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:
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:
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
 
Physics news on Phys.org
  • #2
Thats because first and last both point to null in your program. You need to make sure they are set in the constructor.
 

What is a Java class?

A Java class is a template or blueprint that defines the properties and behavior of objects in Java programming. It contains variables, methods, and constructors, which are used to create and manipulate objects.

How do I create a Java class?

To create a Java class, you need to use the keyword "class" followed by the name of the class and a pair of curly braces. Inside the curly braces, you can define the variables, methods, and constructors for the class.

What is the purpose of a constructor in a Java class?

A constructor is a special method that is used to initialize objects of a class. It is called automatically when an object is created and is used to set initial values for the object's variables.

How do I add variables and methods to a Java class?

To add variables and methods to a Java class, you can use the respective keywords "int" for integer variables, "double" for floating-point variables, "void" for methods that do not return a value, and "return" for methods that return a value. You can also specify access modifiers such as "public" or "private" to control the visibility of these variables and methods.

What is the difference between a class and an object in Java?

A class is a template or blueprint that defines the properties and behavior of objects, while an object is an instance of a class. In other words, a class is used to create objects, and each object has its own unique set of properties and behaviors defined by its class.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Engineering and Comp Sci Homework Help
2
Replies
37
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
12
Views
3K
  • Programming and Computer Science
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
2K
  • Programming and Computer Science
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
4K
  • Programming and Computer Science
Replies
1
Views
1K
Back
Top