180 likes | 288 Views
Problem Solving 2 Abstract Classes, Interfaces and Exception Handling. ICS-201 Introduction to Computing II Semester 071. Abstract Classes.
E N D
Problem Solving 2 Abstract Classes, Interfaces and Exception Handling ICS-201 Introduction to Computing II Semester 071
Abstract Classes • Design and implement an abstract class called SolidObject. A SolidObject has a surface area and volume which can be calculated. Provide the following methods in your class: • public abstract double getVolume() • public abstract double getSurfaceArea() • public String toString() //prints the Surface Area and the Volume
Solution abstract class SolidObject { public abstract double getSurfaceArea(); public abstract double getVolume(); public String toString() { return "Surface Area " + getSurfaceArea() + " Volume " + getVolume(); } }
Interfaces • Design and implement an interface called Usable. A Usable interface has only one method • public void use() //This method will print the use of a solid object.
Solution: interface Usable { public void use(); }
We may modify our abstract class to implement the interface Usable. Note that since the use() method is abstract, we need not provide an implementation for use() at this stage. abstract class SolidObject implements Usable { public abstract double getSurfaceArea(); public abstract double getVolume(); public String toString() { return "Surface Area " + getSurfaceArea() + " Volume " + getVolume(); } } Modify the abstract class
The Cube Class • Design and implement a class called Cube which is a subclass of SolidObject. A cube has only one instance variable side representing its side and has the following formulae: • Surface Area: 6 side*side • Volume: side*side*side • Cubes are used as dice. Use the use() method to print the message “Used as dice”.
Solution class Cube extends SolidObject implements Usable { private double x; public Cube(double side) { x = side; } public double getSurfaceArea() { return 6 * x * x; } public double getVolume() { return x * x * x; } public void use() { System.out.println("Used as dice"); } public String toString() { return "Cube " + super.toString(); } }
The Sphere Class • Design and implement a class called Sphere which is a subclass of SolidObject. A sphere has only one instance variable radius representing its radius and has the following formulae: • Surface Area: 4 π radius2 • Volume: 4/3 π radius3 • Spheres are used as balls. Use the use() method to print the message “Used as balls”.
Solution: class Sphere extends SolidObject implements Usable { private double r; public Sphere(double radius) { r = radius; } public double getSurfaceArea() { return 4*Math.PI*r*r; } public double getVolume() { return 4.0/3.0*Math.PI*r*r*r; } public void use() { System.out.println("Used as a ball"); } public String toString() { return "Sphere " + super.toString(); } }
NegativeValueException class • Design and implement a class NegativeValueException which generates an exception with a message “Negative value”. The purpose of this class is not to allow an initialization of a SolidObject with a negative value.
Solution: class NegativeValueException extends Exception { public NegativeValueException(String message) { super(message); } public NegativeValueException() { this("Negative Value, Solid cannot be initialized"); } }
Throwing Exceptions • Modify your classes Cube and Sphere so that their constructor throws a NegativeValueException. Use the constructor of these classes to throw the NegativeValueException with the appropriate argument.
Modified Cube Class class Cube extends SolidObject implements Usable { private double x; public Cube(double side) throws NegativeValueException { x = side; if(x < 0) throw(new NegativeValueException("Negative Side Length for the Cube!")); } //..Rest of the class is the same.
Modified Sphere Class class Sphere extends SolidObject implements Usable { private double r; public Sphere(double rad) throws NegativeValueException { r = rad; if(r < 0) throw(new NegativeValueException("Negative Value for the Sphere")); } //..Rest of the class is the same.
Test Class • Write a test class that creates two Usable objects, a Cube and a Sphere. It handles (catches) the NegativeValueException generated by these two classes if an attempt is made to initialize these objects with a negative value.
The Test Class public class SolidObjectTest { public static void main(String[] args) { Usable mycube, mysphere; try { mycube = new Cube(6); mysphere = new Sphere(-7); System.out.println(mycube); mycube.use(); System.out.println(mysphere); mysphere.use(); } catch(NegativeValueException e) { System.out.println(e.getMessage()); System.exit(0); } } }
Output Cube Surface Area 216.0 Volume 216.0 Used as dice Sphere Surface Area 615.7521601035994 Volume 1436.755040241732 Used as a ball