Question

Intro to Java. Creating random SSN

  1. Create a Java program which includes the following:

 Uses a random value as well as manipulates string and character objects in order to make an SS# (xxx-xx-xxxx).

  1. First 3 Numbers: Create a three digit random integer named FirstThree in the range of 100- 999. (10 Points)

  2. Second 2 Numbers: Create a two digit number named SecondTwo by using FirstThree as follows: (10 Points)

     If FirstThree is 550 or less then create a 2 digit string using the name StringFirstThree from the first 2 digits of FirstThree otherwise create a 2 digit string named StringFirstThree using the last 2 digits of FirstThree.

  3. Last 4 Numbers: Use the user’s name to generate a four place number string as follows: (10 Points)

    •   Ask the user to enter their first name. Change their input to uppercase and find the length named FirstNameLength. If the length is less than 10 then add a 0 in front of the number and call it FirstLastTwo.

    •   Ask the user for their last name. Change their input to uppercase and find the length named LastNameLength. If the length is less than 10, then add a 0 after the number and call it SecondLastTwo.

    •   Combine FirstLastTwo and SecondLastTwo into a string named LastFour.

  4. SSN: Create a string named YourSSN that combines StringFirstThree, SecondTwo and

    LastFour with dashes where the output is like this 123-45-9884. (10 Points)

  5. Output: Tell the user their new SS number. (10 Points)


0 0
Add a comment Improve this question Transcribed image text
✔ Recommended Answer
Answer #1

import java.util.Scanner;


//TestBlock.java


public class TestBlock {

  

  

  

  public static void main(String[] args) {

    

    //Scanner Used to get the input from user

    

    Scanner scan = new Scanner(System.in);

    

    //Using Math.random to calculate first three numbers

    

    int rand = (int)(Math.random() * (999 - 100 + 1) + 100);

    

    Integer firstThree = rand % 1000;

    

    

    

    Integer secondTwo = 0;

    

    

    

    if(firstThree<=550) {

      

      secondTwo= firstThree/10;

      

    }

    

    else {

      

      secondTwo= firstThree%100;

      

    }

    

    

    

    String second = secondTwo.toString();

    

    if(secondTwo<10) {

      

      second = "0"+second;

      

    }

    

    //Getting the first name and Last name to calculate last 4 numbers

    

    System.out.print("Enter the First Name: ");

    

    String firstName = scan.nextLine();

    

    Integer len = firstName.length();

    

    String firstLastTwo = len.toString();

    

    if(len < 10) {

      

      firstLastTwo = "0"+firstLastTwo;

      

    }

    

    

    

    System.out.print("Enter the Last Name: ");

    

    String lastName = scan.nextLine();

    

    len = lastName.length();

    

    String secondLastTwo = len.toString();

    

    if(len < 10) {

      

      secondLastTwo = secondLastTwo + "0";

      

    }

    

    //Printing out the SSN

    

    String ssn = firstThree.toString()+"-"+second+"-"+firstLastTwo+secondLastTwo;

    

    System.out.println("Your SSN: "+ssn);

    

    

    

    scan.close();

    

  }  

  

}


answered by: codegates
Add a comment
Know the answer?
Add Answer to:
Intro to Java. Creating random SSN
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT