In: Computer Science
You are to modify the Pez class so that all errors are handled by exceptions. The constructor must be able to handle invalid parameters. The user should not be able to get a piece of candy when the Pez is empty. The user should not be able to put in more candy than the Pez can hold. I would like you to do 3 types of exceptions. One handled within the class itself, one handled by the calling method and one custom made exception. Be sure to comment in the program identifying each type of exception. You have the freedom of choosing which exception style you use where. You will need to submit 3 files, the Pez class, the custom exception class, and a tester program demonstrating that all of your exceptions work properly.
public class Pez { private int candy; private final int MAX_PIECES = 12; public Pez() { candy = 0; } public Pez(int pieces) //this will need to be error-proofed { candy = pieces; } public int getCandy() { return candy; } public void setCandy(int pieces) //this will need to be error-proofed { candy = pieces; } public void getAPieceOfCandy() //this will need to be error-proofed { candy = candy - 1; } public int fillPez() { int piecesNeeded = MAX_PIECES - candy; candy = MAX_PIECES; return piecesNeeded; } public int emptyPez() { int currentPieces = candy; candy = 0; return currentPieces; } public void addPieces(int pieces) //this will need to be error-proofed { candy = candy + pieces; } public String toString() { String thisPez = "This Pez has " + candy + " pieces of candy"; return thisPez; } public boolean equals(Pez pez2) { if(this.candy == pez2.candy) return true; return false; } }
Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks
Note: Details about the three kinds of exception are mentioned in comments.
// Pez.java
public class Pez {
private int candy;
private final int MAX_PIECES = 12;
public Pez() {
candy = 0;
}
public Pez(int pieces) {
/**
* This constructor handles the exception by itself. the setCandy()
* method throws IllegalArgumentException if the parameter pieces is
* invalid. If that exception occur, this constructor will set the value
* of candy to 0
*/
// trying to set the pieces of candy
try {
setCandy(pieces);
// set successfully
} catch (IllegalArgumentException e) {
// error occurred, alerting user and using default value for candy
System.out.println("Invalid number of candies detected."
+ " Using a default value of 0");
candy = 0;
}
}
public int getCandy() {
return candy;
}
public void setCandy(int pieces) {
/**
* throwing exception if the 'pieces' is invalid. This should be handled
* by the method/object calling this method
*/
if (pieces < 0 || pieces > MAX_PIECES) {
throw new IllegalArgumentException("Invalid number of candies");
}
candy = pieces;
}
public void getAPieceOfCandy() throws InvalidOperationException {
/**
* throwing custom exception if the candy is empty.This should be
* handled by the method/object calling this method
*/
if (candy == 0) {
throw new InvalidOperationException(
"ERROR: There are no candies left");
}
candy = candy - 1;
}
public int fillPez() {
int piecesNeeded = MAX_PIECES - candy;
candy = MAX_PIECES;
return piecesNeeded;
}
public int emptyPez() {
int currentPieces = candy;
candy = 0;
return currentPieces;
}
public void addPieces(int pieces) throws InvalidOperationException {
/**
* throwing custom exception if the candy+pieces exceeds the limit.This
* should be handled by the method/object calling this method
*/
if ((candy + pieces) > MAX_PIECES) {
throw new InvalidOperationException(
"ERROR: Number of candies exceed the Maximum value");
}
//also throwing exception if the pieces are negative
if (pieces < 0) {
throw new InvalidOperationException(
"ERROR: Invalid Number of pieces");
}
candy = candy + pieces;
}
public String toString() {
String thisPez = "This Pez has " + candy + " pieces of candy";
return thisPez;
}
public boolean equals(Pez pez2) {
if (this.candy == pez2.candy)
return true;
return false;
}
}
// custom exception class to handle exceptions occur while using
// getAPieceOfCandy and addPieces methods
class InvalidOperationException extends Exception {
public InvalidOperationException(String ex) {
super(ex);
}
}
// Test.java (a class to test the updated Pez class)
public class Test {
public static void main(String[] args) {
/**
* below line will cause exception, but it will be handled by the
* constructor itself. the number of candy will be set to 0 instead of
* -5 and a message will be displayed
*/
Pez pez = new Pez(-5);
/**
* testing getAPieceOfCandy method
*/
try{
System.out.println("\nTrying to get a candy when there are none.");
pez.getAPieceOfCandy();
}catch (InvalidOperationException ex) {
System.out.println(ex.getMessage());
System.out.println("Exception thrown, test success");
}
/**
* testing addPieces method with legal values
*/
try{
System.out.println("\nTrying to add 12 pieces of candies.");
pez.addPieces(12);
System.out.println("Added without exception. Success");
}catch (InvalidOperationException ex) {
System.out.println(ex.getMessage());
System.out.println("Exception thrown, test failed");
}
/**
* testing addPieces method when candies exceed maximum
*/
try{
System.out.println("\nTrying to add 2 pieces of candies when full.");
pez.addPieces(2);
System.out.println("Added without exception. Error");
}catch (InvalidOperationException ex) {
System.out.println(ex.getMessage());
System.out.println("Exception thrown, test success");
}
System.out.println("Emptying candies");
pez.emptyPez();
/**
* testing addPieces method with negative value
*/
try{
System.out.println("\nTrying to add -5 pieces of candies.");
pez.addPieces(-5);
System.out.println("Added without exception. Error");
}catch (InvalidOperationException ex) {
System.out.println(ex.getMessage());
System.out.println("Exception thrown, test success");
}
}
}
/*OUTPUT*/
Invalid number of candies detected. Using a default value of 0
Trying to get a candy when there are none.
ERROR: There are no candies left
Exception thrown, test success
Trying to add 12 pieces of candies.
Added without exception. Success
Trying to add 2 pieces of candies when full.
ERROR: Number of candies exceed the Maximum value
Exception thrown, test success
Emptying candies
Trying to add -5 pieces of candies.
ERROR: Invalid Number of pieces
Exception thrown, test success