Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Java: Please help with my following code and tester. Thank you. Instructions for code: Activity - Implement a program prompts a user for their name,

Java: Please help with my following code and tester. Thank you.

Instructions for code:

Activity - Implement a program prompts a user for their name, age, salary, hackername and email address.

  1. Catches the following exceptions
    1. NumberFormatException for the salary
    2. User defined Malformed email address (See below)
    3. Allows the user to try again in either case.
  2. User Class
    1. Stores user information in instance variables name, age, salary, hacker name and email address in the appropriate data type.
    2. Default constructors only
    3. Accessor and mutator methods including:
      1. boolean setSalary(String newSalary)
        1. receives a string and converts it to a number
        2. Handles the NumberFormatException.
        3. Returns true if conversion is successful and false if not.
      2. boolean setEmail(String newEmail)
        1. receives a String
        2. Tests for Malformed email address and throws MalformedEmailAddress Exception if it is malformed.
        3. Returns true if address is correctly formed, false if not.
    4. setHackerName method
      1. Use your creativity to create an algorithm that uses the data the create a hacker nickname,
      2. The nickname does not necessary have to be a pronounceable word, but that would be nice.
      3. You may want to store a bank of words to use as part of your hacker name see http://forums.windrivers.com/showthread.php?38547-Good-hacker-names for some ideas.
    5. toString method - Displays all the instance variable data in a neatly formated output

    1. Exception handling
      1. Email Address
        1. Check to see if an email address is valid
        2. Valid address is simply contains an @ symbol between the first and last characters of the address.
        3. If email address is malformed it throws a the User Defined MalformedEmailAddress Exception.(see below)
        4. The MalformedEmailAddress should be caught and the user given a chance to correct the address. (Think about this as its a little trickier than you think)
  1. MalformedEmailAddress Exception class - Creates an error message explaining the exception.
  2. UserTest
    1. Test all methods in the User class including the exceptions.
    2. To simplify the process read all data in from a Scanner as a String.

Code:

import java.net.MalformedURLException; import java.util.Random; /** * @author Admin * */ public class User { /** * instance variable age */ private int age; /** * Instance variable salary */ private int salary; /** * instance variable hackerName */ private String hackerName; /** * Instance variable emailAddress */ private String emailAddress; /** * Default constructors only */ public User() { this.age = 0; this.salary = 0; this.hackerName = " "; this.emailAddress = " "; } /** Methods */ /** * Mutator * @param age */ public void setAge(int age) { this.age = age; } /** * Accessor * @return age */ public int getAge() { return age; } /** * Mutator * boolean setSalary(String newSalary) * Receives a string and converts it to a number. * Handles the NumberFormatException. * Returns true if conversion is successful and false if not. * @param newSalary * @return */ public boolean setSalary(String newSalary) { try { this.salary = Integer.parseInt(newSalary); } catch (NumberFormatException ex) { System.out.println("please enter in numericals"); return false; } return true; } /** * Accessor * @return salary */ public int getSalary() { return salary; }

/** * Mutator * boolean setEmail(String newEmail) * Receives a String. * Tests for Malformed email address and throws * MalformedEmailAddress Exception if it is malformed. * Returns true if address is correctly formed, false if not. */ public boolean setEmail(String newEmail) { int count = 0; for (int i = 0; i < newEmail.length(); i++) { if (newEmail.charAt(i) == '@') { count++; } } if (count > 1) { try { throw new MalformedURLException(); } catch (MalformedURLException e) { return false; } } return true; } /** * Accessor * @return email */ public String getEmail() { return emailAddress; } /** * Mutator * setHackerName method: Use your creativity to create an algorithm * that uses the data the create a hacker nickname. * The nickname does not necessary have to be a pronounceable word, * but that would be nice. * You may want to store a bank of words to use as part of your * hacker name * @param hackerName * @return */ public String setHackerName(String hackerName) { String[] Beginning = { "Kr", "Ca", "Ra", "Mrok", "Cru", "Ray", "Bre", "Zed", "Drak", "Mor", "Jag", "Mer", "Jar", "Mjol", "Zork", "Mad", "Cry", "Zur", "Creo", "Azak", "Azur", "Rei", "Cro", "Mar", "Luk" }; String[] Middle = { "air", "ir", "mi", "sor", "mee", "clo", "red", "cra", "ark", "arc", "miri", "lori", "cres", "mur", "zer", "marac", "zoir", "slamar", "salmar", "urak" }; String[] End = { "d", "ed", "ark", "arc", "es", "er", "der", "tron", "med", "ure", "zur", "cred", "mur" }; Random rand = new Random(); return Beginning[rand.nextInt(Beginning.length)] + Middle[rand.nextInt(Middle.length)] + End[rand.nextInt(End.length)]; } /** * Accessor * @return hackerName */ public String getHackerName() { return hackerName; } /** * toString method - Displays all the instance variable * data in a neatly formated output. */ @Override public String toString() { return "User [salary=" + salary + ", age=" + age + ", hackerName=" + hackerName + ", newEmail=" + emailAddress + "]"; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + age; result = prime * result + ((emailAddress == null) ? 0 : emailAddress.hashCode()); result = prime * result + ((hackerName == null) ? 0 : hackerName.hashCode()); result = prime * result + salary; return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; User other = (User) obj; if (age != other.age) return false; if (emailAddress == null) { if (other.emailAddress != null) return false; } else if (!emailAddress.equals(other.emailAddress)) return false; if (hackerName == null) { if (other.hackerName != null) return false; } else if (!hackerName.equals(other.hackerName)) return false; if (salary != other.salary) return false; return true; }

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Concepts

Authors: David Kroenke, David Auer, Scott Vandenberg, Robert Yoder

10th Edition

0137916787, 978-0137916788

More Books

Students also viewed these Databases questions

Question

5. What is regression analysis? When would you use it?

Answered: 1 week ago

Question

explain what is meant by redundancy

Answered: 1 week ago