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

Some of the class here have already answered but I needed the code asap implemen

ID: 3910424 • Letter: S

Question

Some of the class here have already answered but I needed the code asap implementing the exception for JOptionPane, Exam Questions, Factorial, Number to Words, Pyramid and Address Book.

Only NetBeans project compressed in .rar or .zip format will be accepted.

1. ArrayIndexOutOfBoundsException <-- Question answered

Given the following code:

public class ExceptionTest{

public static void main( String[] args ){

   String nums[] = {“one”, “two”, “three”};

for( int i=0; i<=3; i++ ){

System.out.println(nums[i]);

}

}

}

Compile and run the TestExceptions program. The output should look like this:

one

two

three

Exception in thread "main"

java.lang.ArrayIndexOutOfBoundsException: 3

at ExceptionTest.main(1.java:4)

Modify the TestExceptions program to handle the exception. The output of the program after catching the exception should look like this:

one

two

three

Exception caught: Array Index 3 is out of bounds

2. 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 003 – Circle Calculator(Question answered for Circle Calculator) and JOptionPane

Lab Exercise 004 – Exam Questions, Factorial, Number to Words, Pyramid

Lab Exercise 007 – Address Book

Sample Output:

I don't have a code for JOptionPane but I also request for having the code for it implementing exception.

Here is my code for Exam Questions without implementing exception:

package Week6;

import java.util.Scanner;

public class examprogram {

public static void main(String[] args) {

      Scanner scan = new Scanner(System.in);

int score =0;

   double percentage;

System.out.println("Who is the first President of the Philippines?"

      + " 1.Emilio Aguinaldo"

      + " 2.Joseph Estrada"

      + " 3.Manuel Quezon"

      + " 4.GMA");

System.out.print("Enter [1-4]:");

      int Ans1 = scan.nextInt();

if(Ans1==1){

      System.out.println("Correct ");

   score ++ ;

   }

   else{

      System.out.println("Incorrect! The Correct answer is 1.Emilio Aguinaldo ");

   }

System.out.println("Hitler party which came into power in 1933 is known as?"

      + " 1.Labour"

      + " 2.Nazi Party"

      + " 3.Ku-Klux-Klan"

      + " 4.Democratic Party");

System.out.print("Enter [1-4]:");

      int Ans2 = scan.nextInt();

if(Ans2==2) {

      System.out.println("Correct ");

   score ++ ;

      }

      else {

      System.out.println("Incorrect! The Correct answer is 2.Nazi Party ");

      }

System.out.println("For which of the following discipline is Nobel Prize awarded?"

      + " 1.Physics and Chemistry"

      + " 2.Physiology or Medicine"

      + " 3.Literature, Peace and Economics"

      + " 4.All of the above");

  

      System.out.print("Enter [1-4]:");

      int Ans3 = scan.nextInt();

if(Ans3==4){

      System.out.println("Correct ");

      score ++ ;

      }

      else{

      System.out.println("Incorrect! The Correct answer is 4. All of the above ");

      }

System.out.println("What is 3*(2-1)?"

      + " 1. 1"

      + " 2. 2"

      + " 3. 3"

      + " 4. 4");

  

      System.out.print("Enter [1-4]:");

      int Ans4 = scan.nextInt();

  

      if(Ans4==3){

      System.out.println("Correct ");

      score ++ ;

      }

      else{

      System.out.println("Incorrect! The Correct answer is 3. 3 ");

      }

System.out.println("What is 1+1?"

      + " 1. 1"

      + " 2. 2"

      + " 3. 3"

      + " 4. 4");

System.out.print("Enter [1-4]:");

      int Ans5 = scan.nextInt();

if(Ans5==2){

      System.out.println("Correct ");

   score ++;

      }

else{

      System.out.println("Incorrect! The Correct answer is 2. 10 ");

      }

percentage = (score * 100)/5;

   System.out.println("Congratulations!, you got answers right! "+ score);

   System.out.println("That is a score of " + percentage + " percent.");

}

}

I don't have a code for Factorial but I also request for having the code for it 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 Number to Words without implementing exception:

package Week6;

import java.util.Scanner;

public class numbertowords {

   public static void main(String[] args) {

      Scanner input =new Scanner(System.in);

      System.out.print(" Enter number: ");

      int num =input.nextInt();

  

   String s1[]={ " One"," Two"," Three"," Four"," Five"," Six"," Seven"," Eight",   " Nine", " Ten", " Eleven",   " Twelve",  

   " Thirteen",   " Fourteen",   " Fifteen",   " Sixteen",  

   " Seventeen",   " Eighteen",   " Nineteen"};

String[] s2 = {" ten",

   " Twenty",

   " Thirty",

   " Forty",

   " Fifty",

   " Sixty",

   " Seventy",

   " Eighty",

   " Ninety"

   };

   String[] s3={" Hundred",

   " Thousand",

   " Ten thousand",

   " Hundred thousand"};

int i=num;while(i>0)

   {

   if(i>=1&&i<=19)

   {System.out.println(s1[i-1]);break;}

   else if(i>=20&&i<=99)

   {

   int l=i%10;

   i=i-l;

   i=i/10;

   System.out.println(s2[i-1]+""+s1[l-1]);break;

   }  

   else if(i>=100&&i<=999){

   int l=i%100;

   int k=i-l;

   k=k/100;

   System.out.print(s1[k-1]+""+" Hundred");

   i=i-(k*100);

   }

   else if(i>=1000&&i<=9999){

   int l=i%1000;

   int k=i-l;

   k=k/1000;

   System.out.print(s1[k-1]+""+" Thousand");

   i=i-(k*1000);

   }

   else if(i>=10000&&i<=99999){

   int l=i%10000;

   int k=i-l;

   k=k/10000;

   System.out.print(s2[k-1]);

   i=i-(k*10000);

   }

   if(num>=100001){

   System.out.println(" Number is out of range");break;

   }

   else if(num==100000){

   System.out.println(" One Hundred Thousand"); break;

   }

   }

   }

   }

Here is my code for Pyramid without implementing exception:

package Week6;

import java.util.Scanner;

public class PrintHalfPyramid {

   public static void main(String[] args) {

Scanner input =new Scanner(System.in);

      System.out.print("Enter number: ");

      int num =input.nextInt();

for (int i = 0; i < num; i++) {

      for (int j = 0; j < i - i; j++) {

      System.out.print(j+1);

      }

      for (int k = 0; k <= i; k++) {

      System.out.print("* ");

      }

      System.out.println();

      }

      }

}

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 :)

Lab9Blanco_Maria Source Packages · ?·?1abexerdse9 AddressBook·java ? ExamProgram.java ?ExceptionTestjava ?Factorial.java Halfpyramid.java JOptionPane,Java ? NumberToWords.java do

Explanation / Answer

All the required parts (Exceptions and JOptionPane wherever required) in the class files has been implemented and posted below in the tables.

public class ExceptionTest{

public static void main( String[] args ){

String nums[] = {"one", "two", "three"};

for( int i=0; i<=3; i++ ){

try

{

System.out.println(nums[i]);

}

catch(ArrayIndexOutOfBoundsException ex)

{

System.out.println("Exception caught: Array Index " +

i +" is out of bounds");

}

}

}

}

import java.util.InputMismatchException;

import java.util.Scanner;

public class examprogram {

public static void main(String[] args) {

Scanner scan = new Scanner(System.in);

int score =0;

double percentage;

System.out.println("Who is the first President of the Philippines?"

+ " 1.Emilio Aguinaldo"

+ " 2.Joseph Estrada"

+ " 3.Manuel Quezon"

+ " 4.GMA");

System.out.print("Enter [1-4]:");

int Ans1 = 0;

try

{

Ans1 = scan.nextInt();

}

catch(InputMismatchException ex)

{

System.out.println("Exception caught: Not a number");

scan.nextLine();

}

if(Ans1==1){

System.out.println("Correct ");

score ++ ;

}

else{

System.out.println("Incorrect! The Correct answer is 1.Emilio Aguinaldo ");

}

System.out.println("Hitler party which came into power in 1933 is known as?"

+ " 1.Labour"

+ " 2.Nazi Party"

+ " 3.Ku-Klux-Klan"

+ " 4.Democratic Party");

System.out.print("Enter [1-4]:");

int Ans2 = 0;

try

{

Ans2 = scan.nextInt();

}

catch(InputMismatchException ex)

{

System.out.println("Exception caught: Not a number");

scan.nextLine();

}

if(Ans2==2) {

System.out.println("Correct ");

score ++ ;

}

else {

System.out.println("Incorrect! The Correct answer is 2.Nazi Party ");

}

System.out.println("For which of the following discipline is Nobel Prize awarded?"

+ " 1.Physics and Chemistry"

+ " 2.Physiology or Medicine"

+ " 3.Literature, Peace and Economics"

+ " 4.All of the above");

  

System.out.print("Enter [1-4]:");

int Ans3 = 0;

try

{

Ans3 = scan.nextInt();

}

catch(InputMismatchException ex)

{

System.out.println("Exception caught: Not a number");

scan.nextLine();

}

if(Ans3==4){

System.out.println("Correct ");

score ++ ;

}

else{

System.out.println("Incorrect! The Correct answer is 4. All of the above ");

scan.nextLine();

}

System.out.println("What is 3*(2-1)?"

+ " 1. 1"

+ " 2. 2"

+ " 3. 3"

+ " 4. 4");

  

System.out.print("Enter [1-4]:");

int Ans4 = 0;

try

{

Ans4 = scan.nextInt();

}

catch(InputMismatchException ex)

{

System.out.println("Exception caught: Not a number");

scan.nextLine();

}

  

if(Ans4==3){

System.out.println("Correct ");

score ++ ;

}

else{

System.out.println("Incorrect! The Correct answer is 3. 3 ");

}

System.out.println("What is 1+1?"

+ " 1. 1"

+ " 2. 2"

+ " 3. 3"

+ " 4. 4");

System.out.print("Enter [1-4]:");

int Ans5 = 0;

try

{

Ans5 = scan.nextInt();

}

catch(InputMismatchException ex)

{

System.out.println("Exception caught: Not a number");

scan.nextLine();

}

if(Ans5==2){

System.out.println("Correct ");

score ++;

}

else{

System.out.println("Incorrect! The Correct answer is 2. 10 ");

}

percentage = (score * 100)/5;

System.out.println("Congratulations!, you got answers right! "+ score);

System.out.println("That is a score of " + percentage + " percent.");

}

}

import javax.swing.JOptionPane;

public class PersonalInfo {

public static void main(String args[])

{

String name;

String age_str;

int age = -1;

String address;

String contact;

name = JOptionPane.showInputDialog(null, "Name:");

age_str = JOptionPane.showInputDialog(null, "Age:");

try

{

age = Integer.parseInt(age_str);

}

catch(NumberFormatException ex)

{

JOptionPane.showMessageDialog(null, "Not a number!", "Invalid Age", JOptionPane.ERROR_MESSAGE);

}

address = JOptionPane.showInputDialog(null, "Address:");

contact = JOptionPane.showInputDialog(null, "Contact:");

String msg = "User's Personal Information ";

msg += "Name: " + name + " " + "Age: " + age +" ";

msg += "Address: " + address + " ";

msg += "Contact Number: " + contact;

JOptionPane.showMessageDialog(null, msg);

}

}

import java.util.InputMismatchException;

import java.util.Scanner;

public class numbertowords {

public static void main(String[] args) {

Scanner input =new Scanner(System.in);

System.out.print("Enter number: ");

int num = 0;

try

{

num = input.nextInt();

}

catch(InputMismatchException ex)

{

System.out.println("Exception caught: Not a number!");

return;

}

  

String s1[]={ " One"," Two"," Three"," Four"," Five"," Six"," Seven"," Eight", " Nine", " Ten", " Eleven", " Twelve",  

" Thirteen", " Fourteen", " Fifteen", " Sixteen",  

" Seventeen", " Eighteen", " Nineteen"};

String[] s2 = {" ten",

" Twenty",

" Thirty",

" Forty",

" Fifty",

" Sixty",

" Seventy",

" Eighty",

" Ninety"

};

String[] s3={" Hundred",

" Thousand",

" Ten thousand",

" Hundred thousand"};

int i=num;while(i>0)

{

if(i>=1&&i<=19)

{System.out.println(s1[i-1]);break;}

else if(i>=20&&i<=99)

{

int l=i%10;

i=i-l;

i=i/10;

System.out.println(s2[i-1]+""+s1[l-1]);break;

}  

else if(i>=100&&i<=999){

int l=i%100;

int k=i-l;

k=k/100;

System.out.print(s1[k-1]+""+" Hundred");

i=i-(k*100);

}

else if(i>=1000&&i<=9999){

int l=i%1000;

int k=i-l;

k=k/1000;

System.out.print(s1[k-1]+""+" Thousand");

i=i-(k*1000);

}

else if(i>=10000&&i<=99999){

int l=i%10000;

int k=i-l;

k=k/10000;

System.out.print(s2[k-1]);

i=i-(k*10000);

}

if(num>=100001){

System.out.println(" Number is out of range");break;

}

else if(num==100000){

System.out.println(" One Hundred Thousand"); break;

}

}

}

}

import java.util.InputMismatchException;

import java.util.Scanner;

public class PrintHalfPyramid {

public static void main(String[] args) {

Scanner input =new Scanner(System.in);

System.out.print("Enter number: ");

int num = 0;

  

try

{

num = input.nextInt();

if(num > 99999)

{

System.out.println("Number is out of range");

return;

}

}

  

catch(InputMismatchException ex)

{

System.out.println("Exception Caught: Not a number");

}

for (int i = 0; i < num; i++) {

for (int j = 0; j < i - i; j++) {

System.out.print(j+1);

}

for (int k = 0; k <= i; k++) {

System.out.print("* ");

}

System.out.println();

}

}

}

Note: According to chegg policies we are advised to attempt only 1 question per post. But I have attempted here as much as possible within the limited time provided to us. Please post the remaining question in another question thread.

ExceptionTest.java

public class ExceptionTest{

public static void main( String[] args ){

String nums[] = {"one", "two", "three"};

for( int i=0; i<=3; i++ ){

try

{

System.out.println(nums[i]);

}

catch(ArrayIndexOutOfBoundsException ex)

{

System.out.println("Exception caught: Array Index " +

i +" is out of bounds");

}

}

}

}

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