Problem solving 2 abstract classes interfaces and exception handling
Download
1 / 18

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


  • 136 Views
  • Uploaded 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

PowerPoint Slideshow about 'Problem Solving 2 Abstract Classes, Interfaces and Exception Handling' - amberly


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
Problem solving 2 abstract classes interfaces and exception handling l.jpg

Problem Solving 2 Abstract Classes, Interfaces and Exception Handling

ICS-201 Introduction to Computing II

Semester 071


Abstract classes l.jpg
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 l.jpg
Solution

abstract class SolidObject

{

public abstract double getSurfaceArea();

public abstract double getVolume();

public String toString() {

return "Surface Area " + getSurfaceArea()

+ " Volume " + getVolume();

}

}


Interfaces l.jpg
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.


Solution5 l.jpg
Solution:

interface Usable

{

public void use();

}


Modify the abstract class l.jpg

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 l.jpg
The Cube Class Usable. Note that since the use() method is abstract, we need not provide an implementation for use() at this stage.

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


Solution8 l.jpg
Solution Usable. Note that since the use() method is abstract, we need not provide an implementation for use() at this stage.

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 l.jpg
The Sphere Class Usable. Note that since the use() method is abstract, we need not provide an implementation for use() at this stage.

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


Solution10 l.jpg
Solution: Usable. Note that since the use() method is abstract, we need not provide an implementation for use() at this stage.

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 l.jpg
NegativeValueException class Usable. Note that since the use() method is abstract, we need not provide an implementation for use() at this stage.

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


Solution12 l.jpg
Solution: Usable. Note that since the use() method is abstract, we need not provide an implementation for use() at this stage.

class NegativeValueException extends Exception

{

public NegativeValueException(String message)

{

super(message);

}

public NegativeValueException() {

this("Negative Value, Solid cannot be initialized");

}

}


Throwing exceptions l.jpg
Throwing Exceptions Usable. Note that since the use() method is abstract, we need not provide an implementation for use() at this stage.

  • 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 l.jpg
Modified Cube Class Usable. Note that since the use() method is abstract, we need not provide an implementation for use() at this stage.

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 l.jpg
Modified Sphere Class Usable. Note that since the use() method is abstract, we need not provide an implementation for use() at this stage.

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 l.jpg
Test Class Usable. Note that since the use() method is abstract, we need not provide an implementation for use() at this stage.

  • Write a test class that creates two Usable object, 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 l.jpg
The Test Class Usable. Note that since the use() method is abstract, we need not provide an implementation for use() at this stage.

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 l.jpg
Output Usable. Note that since the use() method is abstract, we need not provide an implementation for use() at this stage.

Cube Surface Area 216.0 Volume 216.0

Used as dice

Sphere Surface Area 615.7521601035994 Volume 1436.755040241732

Used as a ball


ad