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

Exception Handing Programming Lab 4 Student Learning Outcomes. After completion

ID: 3588888 • Letter: E

Question

Exception Handing Programming Lab 4

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 that prompts a user for their name, age, salary and email address and develops a hacker name from the data. The program stores the information (name, age and email) along with the hackers name to a file, and displays their hacker name. Use the same file for every user.

Catches the following exceptions

NumberFormatException for the salary

User defined Malformed email address (See below)

Allows the user to try again.

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:

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.

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.

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 HYPERLINK "http://forums.windrivers.com/showthread.php?38547-Good-hacker-names" http://forums.windrivers.com/showthread.php?38547-Good-hacker-names for some ideas.

Exception handling

Email Address

Check to see if an email address is valid

Valid address is HYPERLINK "mailto:xxxx@xxxx.xxx" xxxx@xxxx.xxx (where x is any character or number)

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

The MalformedEmailAddress 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)

MalformedEmailAddress Exception class  - Creates an error message explaining the exception.

UserTest

Test all methods in the User class including exception.

To simplify the process read all data in from a Scanner as a String.

Explanation / Answer

package com.as.test;

import java.net.MalformedURLException;

import java.util.Random;

public class User {

private int salary;

private int age;

private String hackerName;

private String emailId;

public int getSalary() {

return salary;

}

public boolean setSalary(String sal) {

try {

this.salary = Integer.parseInt(sal);

} catch (NumberFormatException ex) {

System.out.println("please enter in numericals");

return false;

}

return true;

}

public int getAge() {

return age;

}

public void setAge(int age) {

this.age = age;

}

public String getHackerName() {

return hackerName;

}

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)];

}

public String getEmailId() {

return emailId;

}

public boolean setEmailId(String emailId) {

int count = 0;

for (int i = 0; i < emailId.length(); i++) {

if (emailId.charAt(i) == '@') {

count++;

}

}

if (count > 1) {

try {

throw new MalformedURLException();

} catch (MalformedURLException e) {

return false;

}

}

return true;

}

// constructor

public User() {

// TODO Auto-generated constructor stub

}

@Override

public String toString() {

return "User [salary=" + salary + ", age=" + age + ", hackerName="

+ hackerName + ", emailId=" + emailId + "]";

}

@Override

public int hashCode() {

final int prime = 31;

int result = 1;

result = prime * result + age;

result = prime * result + ((emailId == null) ? 0 : emailId.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 (emailId == null) {

if (other.emailId != null)

return false;

} else if (!emailId.equals(other.emailId))

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;

}

}