Question: IN JAVA WILL LIKE AND SUPPORT! Here are files that you can use for writing the runtime exception class for the Barrel class. The Barrel

IN JAVA WILL LIKE AND SUPPORT!

Here are files that you can use for writing the runtime exception class for the Barrel class. The Barrel exception class is almost exactly like this example. Put the runtime exception class for the Barrel class in the project with the Barrel class. The assignment requires you to change the code in the Barrel class that requires a call to the System.exit() method. Your code should now throw an exception. Make sure that you send a sentence that explains the exception to the constructor of the exception class. Note that the read method can throw an exception that can be caught in a catch block and the client can enter a new name.

I have the barrel class ,but need help implementing a runtime exception class in the Barrel class.

Barrel class:

import java.util.Scanner;

public class Barrel {

private int numberOfApples;

public static final int MINIMUM = 0;

public static final int MAXIMUM = 1000;

public Barrel() {

this.numberOfApples = 0;

}

public Barrel (Barrel barrel) {

this.numberOfApples = barrel.get();

}

public Barrel(int n) {

while (nMINIMUM || n>MAXIMUM) {

System.out.println("Please enter a new value of apples: ");

Scanner scan = new Scanner (System.in);

n = scan.nextInt();

}

this.numberOfApples = n;

}

public void read() {

Scanner scan = new Scanner (System.in);

int n;

System.out.print("Enter Number of Apples: ");

n = scan.nextInt();

while (nMINIMUM || n>MAXIMUM) {

System.out.print("Enter Number of Apples between " + MINIMUM +" and "+ MAXIMUM + ": ");

n = scan.nextInt();

}

this.numberOfApples = n;

}

public void set (Barrel barrel) {

this.numberOfApples = barrel.numberOfApples;

}

public int get() {

return this.numberOfApples;

}

public Barrel add(Barrel b) {

Barrel barrel = new Barrel();

int n = this.numberOfApples + b.numberOfApples;

if (n>=MINIMUM && nMAXIMUM)

barrel.numberOfApples = n;

else {

System.out.print("Result exceeds set bounds");;

System.exit(0);

}

return barrel;

}

public Barrel subtract(Barrel b) {

Barrel barrel = new Barrel();

int n = this.numberOfApples - b.numberOfApples;

if (n>=MINIMUM && nMAXIMUM)

barrel.numberOfApples = n;

else {

System.out.print("Result exceeds set bounds");;

System.exit(0);

}

return barrel;

}

public Barrel multiply(Barrel b) {

Barrel barrel = new Barrel();

int n = this.numberOfApples * b.numberOfApples;

if (n>=MINIMUM && nMAXIMUM)

barrel.numberOfApples = n;

else {

System.out.print("Result exceeds set bounds");;

System.exit(0);

}

return barrel;

}

public Barrel divide(Barrel b) {

Barrel barrel = new Barrel();

int n = this.numberOfApples / b.numberOfApples;

if (n>=MINIMUM && nMAXIMUM)

barrel.numberOfApples = n;

else {

System.out.print("Result exceeds set bounds");;

System.exit(0);

}

return barrel;

}

public Barrel add(int nApples) {

Barrel barrel = new Barrel();

int n = this.numberOfApples + nApples;

if (n>=MINIMUM && nMAXIMUM)

barrel.numberOfApples = n;

else {

System.out.print("Result exceeds set bounds");;

System.exit(0);

}

return barrel;

}

public Barrel subtract(int nApples) {

Barrel barrel = new Barrel();

int n = this.numberOfApples - nApples;

if (n>=MINIMUM && nMAXIMUM)

barrel.numberOfApples = n;

else {

System.out.print("Result exceeds set bounds");;

System.exit(0);

}

return barrel;

}

public Barrel multiply(int nApples) {

Barrel barrel = new Barrel();

int n = this.numberOfApples * nApples;

if (n>=MINIMUM && nMAXIMUM)

barrel.numberOfApples = n;

else {

System.out.print("Result exceeds set bounds");;

System.exit(0);

}

return barrel;

}

public Barrel divide (int nApples) {

Barrel barrel = new Barrel();

int n = 0;

if (nApples>0)

n = this.numberOfApples / nApples;

else {

System.out.print("Cannot divide by zero");;

System.exit(0);

}

if (n>=MINIMUM && nMAXIMUM)

barrel.numberOfApples = n;

else {

System.out.print("Result exceeds set bounds");;

System.exit(0);

}

return barrel;

}

public boolean equals(Barrel b) {

if(this.numberOfApples == b.numberOfApples)

return true;

else

return false;

}

public String toString() {

return "Barrel with " + this.numberOfApples+ " apples";

}

public static void main(String[] args) {

Barrel Barrel_1 = new Barrel();

Barrel Barrel_2 = new Barrel();

Barrel Barrel_3 = new Barrel();

Barrel Barrel_4 = new Barrel();

Barrel Barrel_5 = new Barrel();

Barrel_1.read();

System.out.println("Barrel_1 is a " + Barrel_1.toString());

Barrel_3 = Barrel_1.multiply(2);

Barrel_2 = Barrel_3.add(Barrel_1);

System.out.println("Barrel_2 is a " + Barrel_2.toString());

System.out.println("Barrel_3 is a " + Barrel_3.toString());

Barrel_4 = Barrel_2.subtract(Barrel_1);

System.out.println("Barrel_4 is a " + Barrel_4.toString());

Barrel_5 = Barrel_4.divide(2);

System.out.println("Barrel_5 is a " + Barrel_5.toString());

System.out.print("Barrel_5 is ");

if (!Barrel_5.equals(Barrel_1))

System.out.print(" not");

System.out.print(" equal to Barrel_1");

}

}

Demo for barrel class:

import java.util.Scanner;

// test driver for the Barrel class

public class Barreldemo

{

static Scanner scan = new Scanner(System.in);

public static void main(String[] args)

{

int x;

Barrel Barrel_1 = new Barrel();

Barrel Barrel_2 = new Barrel();

Barrel Barrel_3 = new Barrel();

Barrel Barrel_4 = new Barrel();

Barrel Barrel_5 = new Barrel();

System.out.println("please enter a number for Barrel_1:");

Barrel_1.read();

System.out.println("Barrel_1 is this value " + Barrel_1 + " .");

Barrel_3 = Barrel_3.add(Barrel_1);

Barrel_3 = Barrel_3.add(Barrel_2);

Barrel_3 = Barrel_3.divide(2);

System.out.println("The 2 Barrel average is: " + Barrel_3 + " .");

System.out.print("Subtracting Barrel1 from Barrel_2 is: " );

Barrel_3 = Barrel_2.subtract(Barrel_1);

System.out.println( Barrel_3);

Barrel_3 = Barrel_2.divide(Barrel_1);

System.out.println("Dividing Barrel_2 with Barrel_1 is: " + Barrel_3 + ".");

if (Barrel_1.equals(Barrel_2))

{

System.out.println("Barrel_1 and Barrel _2 are equal.");

}

else

{

System.out.println("Barrel_1 and Barrel_2 are not equal.");

}

System.out.println("Barrel_4 is now given the value of 10 with the set() method.");

Barrel_4.set(Barrel_1);

System.out.println("The value of Barrel_4 is " + Barrel_4 + ".");

System.out.println("Barrel_4 is now multipled with Barrel_1. The value is placed in " +

"Barrel_5.");

Barrel_5 = Barrel_1.multiply(Barrel_4);

System.out.println("The value of Barrel_5 is " + Barrel_5 + ".");

System.out.println("Enter an integer to add to the value Barrel_1 has.");

System.out.println("The sum will be put in Barrel_3.");

x = scan.nextInt();

Barrel_3 = Barrel_1.add(x);

System.out.println("Adding your number " + x +

" to Barrel_1 gives a new Barrel with " + Barrel_3 + " in it.");

System.out.print("Adding the number " + Barrel_2 + " which is the number" +

" in Barrel_2 to the number in ");

Barrel_2 = Barrel_1.add(Barrel_2);

System.out.println("Barrel_1 which is " + Barrel_1 +" gives " + Barrel_2 + ".");

}

}

Here are files that you can use for writing the runtime exception class for the Barrel class:

person.java

IN JAVA WILL LIKE AND SUPPORT! Here are files that you can

use for writing the runtime exception class for the Barrel class. The

Barrel exception class is almost exactly like this example. Put the runtime

ExceptionHandlingDemo.java

exception class for the Barrel class in the project with the BarrelAgeOutOfBoundsException.java:

class. The assignment requires you to change the code in the Barrel

Thanks for your help!

import java.util.Scanner; public class Person { static Scanner scan = new Scanner(System.in); Il private members of class: private String name; private int age ; private static final int MAX_VALUE = 90; private static final int MIN_VALUE = 0; // CLASSINV: age >= MIN_VALUE && age MAX_VALUE) { throw new AgeOutOfBoundsException ("constructor: age MAX_VALUE"); } this.name = name; this. age = age; } public void read() { String name; int age = 0; System.out.println("Please enter a name."); name = scan.nextLine(); this.name = name; boolean goodAge = false; while(!goodAge) { try { System.out.println("Please enter an age."); age = scan.nextInt(); if(age MAX_VALUE) { throw new AgeOutOfBoundsException ("in read method: age value MAX_VALUE"); } goodAge = true; } catch(AgeOutofBoundsException e) { System.out.println(e); } } this.age = age; } public void setAge(int age) { if(age MAX_VALUE) { throw new AgeOutofBoundsException ("set: age value MAX_VALUE"); } this.age = age; } public void setName(String name) { this.name = name; } public int getAge() // - // access method to get private data // - { return age; } public boolean equals(Person other) return(this.age == other.age && this.name.equalsIgnorecase (other.name)); } public String toString() // print to screen { return "TI + "Name is " } } II + name + age is " + age; import java.util.Scanner; public class ExceptionHandlingDemo { static Scanner scan = new Scanner(System.in); public static void main(String[] args) { Person person = new Person(); System.out.println("Please enter a person's data."); person.read(); System.out.println(person); } public class AgeOutofBoundsException extends RuntimeException { private static final long serialversionUID = 1L; public AgeOutOfBoundsException() { super(); } public AgeOutOfBoundsException(String message) { super (message); } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!