problem solving 2 abstract classes interfaces and exception handling n.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
Problem Solving 2 Abstract Classes, Interfaces and Exception Handling PowerPoint Presentation
Download Presentation
Problem Solving 2 Abstract Classes, Interfaces and Exception Handling

Loading in 2 Seconds...

play fullscreen
1 / 18

Problem Solving 2 Abstract Classes, Interfaces and Exception Handling - PowerPoint PPT Presentation


  • 77 Views
  • Updated on

Problem Solving 2 Abstract Classes, Interfaces and Exception Handling. ICS-201 Introduction to Computing II Semester 071. Abstract Classes.

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

Problem Solving 2 Abstract Classes, Interfaces and Exception Handling


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
    Presentation Transcript
    1. Problem Solving 2 Abstract Classes, Interfaces and Exception Handling ICS-201 Introduction to Computing II Semester 071

    2. 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

    3. Solution abstract class SolidObject { public abstract double getSurfaceArea(); public abstract double getVolume(); public String toString() { return "Surface Area " + getSurfaceArea() + " Volume " + getVolume(); } }

    4. 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.

    5. Solution: interface Usable { public void use(); }

    6. 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

    7. 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”.

    8. 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(); } }

    9. 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”.

    10. 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(); } }

    11. 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.

    12. Solution: class NegativeValueException extends Exception { public NegativeValueException(String message) { super(message); } public NegativeValueException() { this("Negative Value, Solid cannot be initialized"); } }

    13. 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.

    14. 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.

    15. 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.

    16. 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.

    17. 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); } } }

    18. Output Cube Surface Area 216.0 Volume 216.0 Used as dice Sphere Surface Area 615.7521601035994 Volume 1436.755040241732 Used as a ball