Some of the class here have already answered but I needed the code asap implemen
ID: 3891769 • Letter: S
Question
Some of the class here have already answered but I needed the code asap implementing the exception for Factorial and Address Book.
Only NetBeans project compressed in .rar or .zip format will be accepted.
Catching Exceptions
Implement Exception handling for the programs you have written in the laboratory exercises as listed below. Note that all data to be used for processing must be input data (data entry from the user). When an invalid data is entered, the program should loop back to data entry until the input becomes valid.
Lab Exercise 004 – Factoria
Lab Exercise 007 – Address Book
I don't have a code for Factorial but I request for the code implementing exception.
Even the instruction says specifically for negative values, I also needed the code for any invalid input like letters etc.
Here is my code for Address Book without implementing exception:
package lab7_nunez_maryaileen;
import java.util.Scanner;
class AddressBook
{
//attributes
private String Name;//Name of the person in the address book
private String Address;//Address of the person
private String Mobile_Number;//Mobile number of the person
private String Email_Address;//Email address of the person
//constructors
AddressBook()
{
}
AddressBook(String n,String a,String m,String e)
{
Name = n;
Address = a;
Mobile_Number = m;
Email_Address = e;
}
//Accessors
String get_Name()
{
return Name;
}
String get_Address()
{
return Address;
}
String get_Mobile_Number()
{
return Mobile_Number;
}
String get_Email_Address()
{
return Email_Address;
}
//mutators
void set_Name(String n)
{
Name=n;
}
void set_Address(String a)
{
Address=a;
}
void set_Mobile_Number(String m)
{
Mobile_Number=m;
}
void set_Email_Address(String e)
{
Email_Address=e;
}
}
package lab7_nunez_maryaileen;
import java.util.Scanner;
public class AddressBookTest {
public static void main(String argv[])
{
AddressBook book[] = new AddressBook[100];//Instantiating an array of AddressBook objects of 100 entries
for(int i=0;i<100;i++)book[i]=new AddressBook();
int size=0,n=0;
int c=0;
Scanner sc = new Scanner(System.in);
String s="";
while(true)
{
System.out.print(" Main Menu 1:Add Entry 2:Delete Entry 3:View All Entries 4:Update an Entry 5:Exit Choose one option:");
c =sc.nextInt();//reading choice
if(c==1)
{
//adding entry
System.out.print("Enter Name of the person in the address book :");
s =sc.next();
book[n].set_Name(s);
System.out.print("Enter Address of the person:");
s =sc.next();
book[n].set_Address(s);
System.out.print("Enter Mobile number of the person:");
s =sc.next();
book[n].set_Mobile_Number(s);
System.out.print("Enter Email address of the person:");
s =sc.next();
book[n].set_Email_Address(s);
size++;
n++;
}
else if(c==2 && size !=0)//checking entry added or not
{
int m;
s="";
System.out.print("Enter index to delete :");
m = sc.nextInt();
book[n].set_Name(s);
book[n].set_Address(s);
book[n].set_Mobile_Number(s);
book[n].set_Email_Address(s);
size--;
}
else if(c==3&& size !=0)
{
int i;
System.out.println(" Entries in book:");
for(i=0;i<n;i++)
{
System.out.println(i);
System.out.println("Name:"+book[i].get_Name());
System.out.println("Address:"+book[i].get_Address());
System.out.println("Mobile Number:"+book[i].get_Mobile_Number());
System.out.println("Email Address:"+book[i].get_Email_Address());
System.out.println(" -------------------------");
}
}
else if(c==4&& size !=0)
{
int m;
s="";
System.out.print("Enter index to update :");
m = sc.nextInt();
System.out.print("Enter Name of the person in the address book :");
s =sc.next();
book[m].set_Name(s);
System.out.print("Enter Address of the person:");
s =sc.next();
book[m].set_Address(s);
System.out.print("Enter Mobile number of the person:");
s =sc.next();
book[m].set_Mobile_Number(s);
System.out.print("Enter Email address of the person:");
s =sc.next();
book[m].set_Email_Address(s);
}
else if(c==5)
{
System.out.println(" Thank you.......");
break;
}
else
{
System.out.println(" Enter Correct Choice ");
}
}
}
}
Thank you so much to someone who will answer the question :)
2. Factorial Create a program that computes for the factorial of a number. Factorial is the product of an integer and all the integers below it; e.g., factorial four 4! is equal to 24 (4*3*2"1-24). The factorial of 0 and 1 is 1. Negative values are invalid input. The program should display an error message and should loop back to data entry when an invalid input is encountered. Sample Output: run Enter nunber: 4 The tactorial of 4 is 24 tun Enter number: 5 The factorial of 5 is 120Explanation / Answer
Solution AddressBook:
AddressBook.java
package lab7_nunez_maryaileen;
import java.util.Scanner;
class AddressBook
{
//attributes
private String Name;//Name of the person in the address book
private String Address;//Address of the person
private String Mobile_Number;//Mobile number of the person
private String Email_Address;//Email address of the person
//constructors
public AddressBook()
{
}
public AddressBook(String n,String a,String m,String e)
{
Name = n;
Address = a;
Mobile_Number = m;
Email_Address = e;
}
//Accessors
public String get_Name()
{
return Name;
}
public String get_Address()
{
return Address;
}
public String get_Mobile_Number()
{
return Mobile_Number;
}
public String get_Email_Address()
{
return Email_Address;
}
//mutators
public void set_Name(String n)
{
Name=n;
}
public void set_Address(String a)
{
Address=a;
}
public void set_Mobile_Number(String m)
{
Mobile_Number=m;
}
public void set_Email_Address(String e)
{
Email_Address=e;
}
}
AddressBookTest.java
package lab7_nunez_maryaileen;
import java.util.InputMismatchException;
import java.util.Scanner;
public class AddressBookTest {
public static void main(String argv[])
{
AddressBook book[] = new AddressBook[100];//Instantiating an array of AddressBook objects of 100 entries
for(int i=0;i<100;i++)book[i]=new AddressBook();
int size=0,n=0;
int c=0;
String s="";
while(true)
{
Scanner sc = new Scanner(System.in);
System.out.print("Main Menu 1:Add Entry 2:Delete Entry 3:View All Entries 4:Update an Entry 5:Exit Choose one option: ");
try {
c =sc.nextInt();//reading choice
sc.nextLine();
if(c==1)
{
//adding entry
System.out.print("Enter Name of the person in the address book: ");
try {
s =sc.nextLine();
book[n].set_Name(s);
}catch(InputMismatchException ime) {
System.out.println("Input must be a string. ");
}
System.out.print("Enter Address of the person: ");
try {
s =sc.nextLine();
book[n].set_Address(s);
}catch(InputMismatchException ime) {
System.out.println("Input must be a string. ");
}
System.out.print("Enter Mobile number of the person:");
try {
s =sc.nextLine();
book[n].set_Mobile_Number(s);
}catch(InputMismatchException ime) {
System.out.println("Input must be a string. ");
}
System.out.print("Enter Email address of the person: ");
try {
s =sc.nextLine();
book[n].set_Email_Address(s);
size++;
n++;
}catch(InputMismatchException ime) {
System.out.println("Input must be a string. ");
}
}
else if(c==2 && size !=0)//checking entry added or not
{
int m;
s="";
System.out.print("Enter index to delete: ");
try {
m =sc.nextInt();
book[m].set_Name(s);
book[m].set_Address(s);
book[m].set_Mobile_Number(s);
book[m].set_Email_Address(s);
size--;
}catch(InputMismatchException ime) {
System.out.println("Input must be an int. ");
}
}
else if(c==3&& size !=0)
{
int i;
System.out.println(" Entries in book: ");
for(i=0;i<n;i++)
{
System.out.println(i+1);
System.out.println("Name:"+book[i].get_Name());
System.out.println("Address:"+book[i].get_Address());
System.out.println("Mobile Number:"+book[i].get_Mobile_Number());
System.out.println("Email Address:"+book[i].get_Email_Address());
System.out.println(" -------------------------");
}
}
else if(c==4&& size !=0)
{
int m=0;
s="";
System.out.print("Enter index to update: ");
try {
m =sc.nextInt();
sc.nextLine();
}catch(InputMismatchException ime) {
System.out.println("Input must be an int. ");
}
System.out.print("Enter Name of the person in the address book: ");
try {
s =sc.nextLine();
book[m].set_Name(s);
}catch(InputMismatchException ime) {
System.out.println("Input must be a string. ");
}
System.out.print("Enter Address of the person: ");
try {
s =sc.nextLine();
book[m].set_Address(s);
}catch(InputMismatchException ime) {
System.out.println("Input must be a string. ");
}
System.out.print("Enter Mobile number of the person: ");
try {
s =sc.nextLine();
book[m].set_Mobile_Number(s);
}catch(InputMismatchException ime) {
System.out.println("Input must be a string. ");
}
System.out.print("Enter Email address of the person: ");
try {
s =sc.nextLine();
book[m].set_Email_Address(s);
}catch(InputMismatchException ime) {
System.out.println("Input must be a string. ");
}
}
else if(c==5)
{
System.out.println("Thank you....... ");
break;
}
else
{
System.out.println("Enter Correct Choice. ");
}
}catch(InputMismatchException ime) {
System.out.println("Input must be an int type. ");
}
}
}
}
Sample Run:
Main Menu
1:Add Entry
2:Delete Entry
3:View All Entries
4:Update an Entry
5:Exit
Choose one option:
asd
Input must be an int type.
Main Menu
1:Add Entry
2:Delete Entry
3:View All Entries
4:Update an Entry
5:Exit
Choose one option:
1
Enter Name of the person in the address book:
Subrat
Enter Address of the person:
India
Enter Mobile number of the person:9738075712
Enter Email address of the person:
subratbehera94@gmil.cm
Main Menu
1:Add Entry
2:Delete Entry
3:View All Entries
4:Update an Entry
5:Exit
Choose one option:
1
Enter Name of the person in the address book:
Sub
Enter Address of the person:
Ind
Enter Mobile number of the person:8660882973
Enter Email address of the person:
subratkumar95@gmil.com
Main Menu
1:Add Entry
2:Delete Entry
3:View All Entries
4:Update an Entry
5:Exit
Choose one option:
3
Entries in book:
1
Name:Subrat
Address:India
Mobile Number:9738075712
Email Address:subratbehera94@gmil.cm
-------------------------
2
Name:Sub
Address:Ind
Mobile Number:8660882973
Email Address:subratkumar95@gmil.com
-------------------------
Main Menu
1:Add Entry
2:Delete Entry
3:View All Entries
4:Update an Entry
5:Exit
Choose one option:
4
Enter index to update:
0
Enter Name of the person in the address book:
Subrat Kumar
Enter Address of the person:
India
Enter Mobile number of the person:
+919738075712
Enter Email address of the person:
subratbehera94@gmil.com
Main Menu
1:Add Entry
2:Delete Entry
3:View All Entries
4:Update an Entry
5:Exit
Choose one option:
3
Entries in book:
1
Name:Subrat Kumar
Address:India
Mobile Number:+919738075712
Email Address:subratbehera94@gmil.com
-------------------------
2
Name:Sub
Address:Ind
Mobile Number:8660882973
Email Address:subratkumar95@gmil.com
-------------------------
Main Menu
1:Add Entry
2:Delete Entry
3:View All Entries
4:Update an Entry
5:Exit
Choose one option:
2
Enter index to delete:
1
Main Menu
1:Add Entry
2:Delete Entry
3:View All Entries
4:Update an Entry
5:Exit
Choose one option:
3
Entries in book:
1
Name:Subrat Kumar
Address:India
Mobile Number:+919738075712
Email Address:subratbehera94@gmil.com
-------------------------
2
Name:
Address:
Mobile Number:
Email Address:
-------------------------
Main Menu
1:Add Entry
2:Delete Entry
3:View All Entries
4:Update an Entry
5:Exit
Choose one option:
5
Thank you.......
Solution Factorial:
Factorial.java
package lab4_nunez_maryaileen;
import java.util.Scanner;
//Factorial class
public class Factorial{
//Main method
public static void main(String[] args){
//Asking for the continuous input if user types invalid number
while(true) {
try {
System.out.println("Enter an integer to find the factorial. ");
Scanner scan = new Scanner(System.in);
int number = scan.nextInt();
//If number is negative shwoing error message and asking for the input again.
if ( number < 0 ) {
System.out.print("Number should be natural number(non-negative). ");
continue;
}//If number is non-negative then computing factorial
else
{
int factorial = number;
for(int i = (number - 1); i > 1; i--)
{
factorial = factorial * i;
}
System.out.println("Factorial of a number is " + factorial+" ");
break;
}
}catch (Exception e){//If user types other than integer then showing the exception and asking for the input again
System.out.println("Entered number should be an integer. ");
continue;
}
}
}
}
Sample run:
Enter an integer to find the factorial.
edee
Entered number should be an integer.
Enter an integer to find the factorial.
-2
Number should be natural number(non-negative).
Enter an integer to find the factorial.
5
Factorial of a number is 120
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.