NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

CSY2030 Exceptions Lab Solutions
Q1.
public class ParseIntError
{
public static void main(String[] args)
{
String str = "abcde";
int number;
try
{
number = Integer.parseInt(str);
}
catch (NumberFormatException e)
{
System.out.println("Conversion error: " + e.getMessage());
}
}
}
Q2.
public class TestScores
{
// Variable to reference an array holding test scores
private double[] scores;
/**
The constructor initializes an object with an array of scores. If the array contains
an invalid value (less than 0 or greater than 100) an exception is thrown.
@param s The array of test scores.
@exception IllegalArgumentException When the argument array contains an invalid
value.
*/
public TestScores(double[] s) throws IllegalArgumentException
{
// Create an array to hold the scores passed
// as an argument.
scores = new double[s.length];
// Copy the scores passed as an argument into
// the new array. Check for illegal values as
// they are copied.
for (int i = 0; i < s.length; i++)
{
if (s[i] < 0 || s[i] > 100)
throw new IllegalArgumentException("Element: " + i + " Score: " +
s[i]);
else
scores[i] = s[i];
}
}
/**
The getAverage method returns the average
of the object's test scores.
@return The average of the object's test scores.
*/
public double getAverage()
{
double total = 0.0; // Accumulator
// Accumulate the sum of the scores.
for (int i = 0; i < scores.length; i++)
total += scores[i];
// return the average.
return (total / scores.length);
}
}
We also need another program to test the above class i.e:
public class TestScoresDemo
{
public static void main(String[] args)
{
// An array with test scores.
// Notice that element 3 contains an invalid score.
double[] badScores = {97.5, 66.7, 88.0, 101.0, 99.0 };
// Another array with test scores.
// All of these scores are good.
double[] goodScores = {97.5, 66.7, 88.0, 100.0, 99.0 };

// Create a TestScores object initialized with badScores.
try
{
TestScores tBad = new TestScores(badScores);
// The following statement should not execute.
System.out.println("The average of the bad scores is " + tBad.getAverage());
}
catch (IllegalArgumentException e)
{
System.out.println("Invalid score found.n" + e.getMessage());
}

// Create a TestScores object initialized with goodScores.
try
{
TestScores tGood = new TestScores(goodScores);
System.out.println("The average of the good scores is " +
tGood.getAverage());
}
catch (IllegalArgumentException e)
{
System.out.println("Invalid score found.n" + e.getMessage());
}
}
}
Q3.
Firstly the EmployeeException class:
public class EmployeeException extends Exception
{
public EmployeeException(String s)
{
super(s);
}
}
Secondly the Employe class:
public class Employee
{
int idNum;
double hourlyWage;
final double MIN = 6.00;
final double MAX = 50.00;
public Employee(int id, double wage) throws EmployeeException
{
idNum = id;
hourlyWage = wage;
if((hourlyWage < MIN) || (hourlyWage > MAX))
throw (new EmployeeException(idNum + " " + hourlyWage));
}
}
Thirdly the ThrowEmploye class which establishes at least three Employees with hourlyWages that
are above, below, and within the allowed range:
public class ThrowEmployee
{
public static void main(String[] args)
{
int num;
double sal;
try
{
num = 101;
sal = 88.99;
Employee first = new Employee(num, sal);
System.out.println("Employee #" + num + " is ok");
}
catch(EmployeeException error)
{
System.out.println("Employee Error: " + error.getMessage());
}
try
{
num = 102;
sal = 5.99;
Employee second = new Employee(num, sal);
System.out.println("Employee #" + num + " is ok");
}
catch(EmployeeException error)
{
System.out.println("Employee Error: " + error.getMessage());
}
try
{
num = 103;
sal = 12.99;
Employee third = new Employee(num, sal);
System.out.println("Employee #" + num + " is ok");
}
catch(EmployeeException error)
{
System.out.println("Employee Error: " + error.getMessage());
}
}
}
Q4.
Firstly the DataEntryException class:
public class DataEntryException extends Exception
{
public DataEntryException(int num)
{
super("DataEntryException - " + num);
}
}
Secondly the GetIDAndAge class:
import java.util.*;
public class GetIDAndAge
{
public static void main(String[] args)
{
int id;
int age;
final int QUIT = 0;
int returnVal = QUIT + 1;
Scanner keyboard = new Scanner(System.in);
while(returnVal != QUIT)
{
try
{
System.out.print("Enter ID ");
id = keyboard.nextInt();
System.out.print("Enter age ");
age = keyboard.nextInt();
returnVal = check(id, age);
}
catch(DataEntryException ex)
{
showStatus("Invalid age or ID - " + ex.getMessage());
}
catch(InputMismatchException ex)
{
showStatus("Invalid data type");
keyboard.nextLine();
}
}
}

public static int check(int idNum, int ageNum) throws DataEntryException
{
final int HIGHID = 999;
final int HIGHAGE = 119;
final int QUIT = 0;
int returnVal = 1;
if (idNum < QUIT || idNum > HIGHID)
throw (new DataEntryException(idNum));
if (ageNum < QUIT || ageNum > HIGHAGE)
throw (new DataEntryException(ageNum));
if (idNum == QUIT && ageNum == QUIT)
returnVal = QUIT;
else
{
showStatus("ID and Age OK");
}
return returnVal;
}
public static void showStatus(String msg)
{
System.out.println(msg);
}
}
     
 
what is notes.io
 

Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 12 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.