Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Java: Please help with the following assignment. Thank you. Student Learning Out

ID: 3754406 • Letter: J

Question

Java: Please help with the following assignment. Thank you.

Student Learning Outcomes. After completion of this lab, students will be able to write a Java program that

Uses exception handling

uses a user defined exception.

Activity - Implement a program prompts a user for their name, age, salary and email address and develops a hacker name from the data.

Catches the following exceptions

NumberFormatException for the salary

User defined MalformedEmailAddressException (See below)

Allows the user to try again in either case.

User Class

Stores user information in instance variables name, age, salary and email address in the appropriate data type.

Default constructors only

Accessor and mutator methods including:

void setSalary(String newSalary)

receives a string and converts it to a number

Throws a NumberFormatException in case the conversion fails.

void 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.

void 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 see http://forums.windrivers.com/showthread.php?38547-Good-hacker-names (Links to an external site.)Links to an external site. for some ideas.

String getHackerName() method for the name that was generated

Exception handling

Email Address

Check to see if an email address is valid

Valid address is simply contains an @ symbol between the first and last characters of the address.

If email address is malformed it throws a the User Defined MalformedEmailAddressException.(see below)

The MalformedEmailAddressException should be caught and the user given a chance to correct the address. (Think about this as it’s a little trickier than you think)

MalformedEmailAddressException class - Is created with a message explaining the exception.

UserTest

Test all methods in the User class including exception.

Do this by creating a User instance and ask for the properties, Repeating this for each property until it is accepted.

Generate and print the generated hacker name.

Explanation / Answer

package ExceptionHandling;

import java.io.IOException;

import java.util.Scanner;

// Defines a class MalformedEmailAddressException to handle email exception

class MalformedEmailAddressException

{

// Method to return true if valid email otherwise retrun false

boolean checkEmail(String email)

{

// Initial status is false

boolean status = false;

// try block begins

try

{

// Loops till end of the email id

for(int x = 0; x < email.length(); x++)

// Checks if character at x index position is '@' symbol

// and x value is greater than zero and not equals to length then return true

if(email.charAt(x) == '@' && x > 0 && x != email.length())

status = true;

// Checks if status is false

if(status == false)

// Throws an exception

throw new IllegalArgumentException();

}// End of try block

// Catch block to handle IllegalArgumentException

catch(IllegalArgumentException s)

{

System.out.print(" Missing @ symbol in email address" + email);

s.printStackTrace();

}// End of catch block

// Returns status

return status;

}// End of method

}// End of class

// Defines a class for Hacker

public class Hacker

{

// Instance variable to store data

String name;

int age;

double salary;

String email;

// Scanner class object created

Scanner sc = new Scanner(System.in);

// Default constructor definition

Hacker()

{

name = "";

age = 0;

salary = 0.0;

email = "";

}// End of default constructor

// Method to validate and set age

void setAge(String newAge)

{

// try block begins

try

{

// Checks if the age does not contains alphabet

if(newAge.contains("[a-zA-Z]+") == false)

// Convert the age to integer and store

age = Integer.parseInt(newAge);

// Otherwise throws an exception

else

throw new NumberFormatException();

}// End of try block

// Catch block to handle NumberFormatException

catch(NumberFormatException ne)

{

// Displays the error message

System.out.println(" Invalid Age - " + newAge);

// Displays the error track

ne.printStackTrace();

// Accepts new age

System.out.println(" Enter Age: ");

newAge = sc.next();

// Calls the method to validate age

setAge(newAge);

}// End of catch block

}// End of method

// Method to validate and set salary

void setSalary(String newSalary)

{

// try block begins

try

{

// Checks if the salary does not contains alphabet

if(newSalary.contains("[a-zA-Z]+") == false)

// Convert the salary to double and store

salary = Double.parseDouble(newSalary);

// Otherwise throws an exception

else

throw new NumberFormatException();

}// End of try block

// Catch block to handle NumberFormatException

catch(NumberFormatException ne)

{

// Displays the error message

System.out.println(" Invalid Salary - " + newSalary);

// Displays the error track

ne.printStackTrace();

// Accepts new salary

System.out.println(" Enter Salary: ");

newSalary = sc.next();

// Calls the method to validate salary

setSalary(newSalary);

}// End of catch block

}// End of method

// Method to validate and set email id

void setEmail(String newEmail)

{

// Loops till valid email id

do

{

// Calls the function to validate email id

if(new MalformedEmailAddressException().checkEmail(newEmail))

// If valid then break

break;

// Otherwise asks to reenter email id

else

{

System.out.println(" Enter a Email address: ");

newEmail = sc.next();

}// End of else

}while(true); // End of do - while loop

// Assigns the valid email id

email = newEmail;

}// End of method

// Method to validate and set name

void setHackerName(String newName)

{

// try block begins

try

{

// Checks if name is null then throws an exception

if(newName == "");

throw new NullPointerException();

}// End of try block

// Catch block to handle NullPointerException

catch(NullPointerException ne)

{

// Displays the error message

System.out.println(" Invalid Name! Name cannot be null.");

// Displays the error track

ne.printStackTrace();

// Accepts name

System.out.println(" Enter a Name: ");

newName = sc.next();

}// End of catch block

// Assigns the valid name

name = newName;

}// End of method

// Method to return hacker information

String getHackerName()

{

return " Name: " + name + " Age: " + age + " Salary: $" + salary + " Email ID: " + email;

}// End of method

// main method definition

public static void main(String[] args)

{

// Creates Hacker class object using default constructor

Hacker hc = new Hacker();

// Calls the methods to validate and set

hc.setHackerName("");

hc.setAge("a2");

hc.setSalary("RS2000");

hc.setEmail("pms.sahu");

// Displays information

System.out.println(hc.getHackerName());

}// End of main method

}// End of class

Sample Output:

Invalid Name! Name cannot be null.

java.lang.NullPointerException

Enter a Name:

at ExceptionHandling.Hacker.setHackerName(Hacker.java:151)

at ExceptionHandling.Hacker.main(Hacker.java:182)

Mohan

Invalid Age - a2

java.lang.NumberFormatException: For input string: "a2"

Enter Age:

at java.lang.NumberFormatException.forInputString(Unknown Source)

at java.lang.Integer.parseInt(Unknown Source)

at java.lang.Integer.parseInt(Unknown Source)

at ExceptionHandling.Hacker.setAge(Hacker.java:71)

at ExceptionHandling.Hacker.main(Hacker.java:183)

b2

Invalid Age - b2

java.lang.NumberFormatException: For input string: "b2"

at java.lang.NumberFormatException.forInputString(Unknown Source)

at java.lang.Integer.parseInt(Unknown Source)

at java.lang.Integer.parseInt(Unknown Source)

at ExceptionHandling.Hacker.setAge(Hacker.java:71)

at ExceptionHandling.Hacker.setAge(Hacker.java:88)

at ExceptionHandling.Hacker.main(Hacker.java:183)

Enter Age:

22

Invalid Salary - RS2000

java.lang.NumberFormatException: For input string: "RS2000"

at sun.misc.FloatingDecimal.readJavaFormatString(Unknown Source)

at sun.misc.FloatingDecimal.parseDouble(Unknown Source)

at java.lang.Double.parseDouble(Unknown Source)

at ExceptionHandling.Hacker.setSalary(Hacker.java:101)

at ExceptionHandling.Hacker.main(Hacker.java:184)

Enter Salary:

R2000

Invalid Salary - R2000

java.lang.NumberFormatException: For input string: "R2000"

Enter Salary:

at sun.misc.FloatingDecimal.readJavaFormatString(Unknown Source)

at sun.misc.FloatingDecimal.parseDouble(Unknown Source)

at java.lang.Double.parseDouble(Unknown Source)

at ExceptionHandling.Hacker.setSalary(Hacker.java:101)

at ExceptionHandling.Hacker.setSalary(Hacker.java:118)

at ExceptionHandling.Hacker.main(Hacker.java:184)

2000

Missing @ symbol in email addresspms.sahujava.lang.IllegalArgumentException

Enter a Email address:

at ExceptionHandling.MalformedEmailAddressException.checkEmail(Hacker.java:26)

at ExceptionHandling.Hacker.setEmail(Hacker.java:129)

at ExceptionHandling.Hacker.main(Hacker.java:185)

pms@ab

Name: Mohan

Age: 22

Salary: $2000.0

Email ID: pms@ab

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote