Exception Handling - Java Add scanner for user input for the objects and impleme
ID: 3699092 • Letter: E
Question
Exception Handling - Java
Add scanner for user input for the objects and implement exception handling for them.
Implement the following program with exception handling.
------------------------------Circle.java------------------------------------------
public class Circle extends Shape { //Extension of new subclass
private long centerX; //Private variable
private long centerY; //Private variable
private double radius; //Private variable
public Circle(long x, long y, double r) //Constructor
{
this.centerX = x; //Initializing
this.centerY = y; //Initializing
this.radius = r; //Initializing
calcArea(); //Initializing
}
@Override public void calcArea() //Override function to share the function to the extended class
{
this.area = (3.14519 * this.radius * this.radius); //Area of a circle by multiplying pi by radius^2
}
@Override public String toString() //Override function to share the function to the extended class
{
return "The Area of the Circle is: " + this.getArea() + " Radius: " + this.radius + " X-Coordinate: " + this.centerX + " Y-Coordinate: " + this.centerY;
} //Printing of information for a circle
public void setCenterX(long centerX) //Set method
{
this.centerX = centerX;
}
public void setCenterY(long centerY) //Set method
{
this.centerY = centerY;
}
public void setRadius(double radius) //Set Method
{
this.radius = radius;
}
public long getCenterX() //Get method
{
return centerX;
}
public long getCenterY() //Get method
{
return centerY;
}
public double getRadius() //Get method
{
return radius;
}
}
---------------------------------------Rectangle.java----------------------------------------
public class Rectangle extends Shape { //Abstract class with extension
private long width; //PRivate variable
private long length; //Private variable
public Rectangle(long width, long length) //Constructor
{
this.width = width; //Initializing
this.length = length; //Initializing
calcArea(); //Initializing
}
public void setWidth(long width) //Set method
{
this.width = width;
}
public void setLength(long length) //Set method
{
this.length = length;
}
public long getWidth() //Get method
{
return width;
}
public long getLength() //Get method
{
return length;
}
@Override public void calcArea() //Override function to use the function in a subclass
{
this.area = (this.length * this.width); //Finding the area length * width
}
@Override public String toString() //Override function to use the function in a subclass
{
return "Area of Rectangle is: " + this.getArea() + " Length: " + this.length + " Width: " + this.width;
}
}
-------------------------------Shape.java------------------------------------------------------------
public abstract class Shape { //Abstract class that will be extended to the other subclasses
protected double area; //Protected
public double getArea; //Public
public String toString; //Public
public abstract void calcArea(); //Abstract method used in all classes
public double getArea() //Returns the value
{
return this.area;
}
public String toString() //Reports the shape area
{
calcArea();//Abstract method
return "The Area is " + this.area;
}
}
-----------------------------------------ShapeTest.java-----------------------------------------------------------
import java.util.ArrayList; //Importing of Array List
public class ShapeTest { //Tester File
public static void main(String[] args) {
ArrayList<Shape> shapeList = new ArrayList<Shape>(); //New ArrayList
Shape circle = new Circle(2,2,2); //New circle object
Shape rectangle = new Rectangle(4,4); //New rectangle object
shapeList.add(circle);
shapeList.add(rectangle);
for(int i = 0; i < shapeList.size(); i++) //loop to call toString method
{
System.out.println(shapeList.get(i).toString()); //Printing of information from toString method
}
}
}
Explanation / Answer
I am assuming you wanted scanner in your main method. I have implemented the same as well as exception handling. Please let me know in comments if you have any issue.
Circle.java
public class Circle extends Shape { // Extension of new subclass
private long centerX; // Private variable
private long centerY; // Private variable
private double radius; // Private variable
public Circle(long x, long y, double r) // Constructor
{
this.centerX = x; // Initializing
this.centerY = y; // Initializing
this.radius = r; // Initializing
calcArea(); // Initializing
}
@Override
public void calcArea() // Override function to share the function to the
// extended class
{
this.area = (3.14519 * this.radius * this.radius); // Area of a circle
// by multiplying pi
// by radius^2
}
@Override
public String toString() // Override function to share the function to the
// extended class
{
return "The Area of the Circle is: " + this.getArea() + " Radius: " + this.radius + " X-Coordinate: "
+ this.centerX + " Y-Coordinate: " + this.centerY;
} // Printing of information for a circle
public void setCenterX(long centerX) // Set method
{
this.centerX = centerX;
}
public void setCenterY(long centerY) // Set method
{
this.centerY = centerY;
}
public void setRadius(double radius) // Set Method
{
this.radius = radius;
}
public long getCenterX() // Get method
{
return centerX;
}
public long getCenterY() // Get method
{
return centerY;
}
public double getRadius() // Get method
{
return radius;
}
}
Rectangle.java
public class Rectangle extends Shape { // Abstract class with extension
private long width; // PRivate variable
private long length; // Private variable
public Rectangle(long width, long length) // Constructor
{
this.width = width; // Initializing
this.length = length; // Initializing
calcArea(); // Initializing
}
public void setWidth(long width) // Set method
{
this.width = width;
}
public void setLength(long length) // Set method
{
this.length = length;
}
public long getWidth() // Get method
{
return width;
}
public long getLength() // Get method
{
return length;
}
@Override
public void calcArea() // Override function to use the function in a
// subclass
{
this.area = (this.length * this.width); // Finding the area length *
// width
}
@Override
public String toString() // Override function to use the function in a
// subclass
{
return "Area of Rectangle is: " + this.getArea() + " Length: " + this.length + " Width: " + this.width;
}
}
Shape.java
public abstract class Shape { // Abstract class that will be extended to the
// other subclasses
protected double area; // Protected
public double getArea; // Public
public String toString; // Public
public abstract void calcArea(); // Abstract method used in all classes
public double getArea() // Returns the value
{
return this.area;
}
public String toString() // Reports the shape area
{
calcArea();// Abstract method
return "The Area is " + this.area;
}
}
ShapeTest.java
public class ShapeTest { // Tester File
public static void main(String[] args) {
ArrayList<Shape> shapeList = new ArrayList<Shape>(); // New ArrayList
Scanner scan = new Scanner(System.in);
long x,y;
double r;
long length, width;
//Getting input from user
try {
System.out.println("Enter the value of x for Circle: ");
x = Long.parseLong(scan.next());
System.out.println("Enter the value of y for Circle: ");
y = Long.parseLong(scan.next());
System.out.println("Enter the value of r for Circle: ");
r = Double.parseDouble(scan.next());
System.out.println("Enter the value of length for Rectangle: ");
length = Long.parseLong(scan.next());
System.out.println("Enter the value of width for Rectangle: ");
width = Long.parseLong(scan.next());
Shape circle = new Circle(x, y, r); // New circle object
Shape rectangle = new Rectangle(width, length); // New rectangle object
shapeList.add(circle);
shapeList.add(rectangle);
for (int i = 0; i < shapeList.size(); i++) // loop to call toString
// method
{
System.out.println(shapeList.get(i).toString()); // Printing of
// information
// from toString
// method
}
//Exception catching
} catch (NumberFormatException e) {
System.out.println("Number format is not proper. Please enter a numerical value.");
} catch (Exception e) {
System.out.println("An exception occured. Please try again.");
}
}
}
Output
Sample Output 1
Enter the value of x for Circle:
2
Enter the value of y for Circle:
2
Enter the value of r for Circle:
2
Enter the value of length for Rectangle:
32
Enter the value of width for Rectangle:
32
The Area of the Circle is: 12.58076
Radius: 2.0
X-Coordinate: 2
Y-Coordinate: 2
Area of Rectangle is: 1024.0
Length: 32
Width: 32
Sample output 2
Enter the value of x for Circle:
2
Enter the value of y for Circle:
2
Enter the value of r for Circle:
2
Enter the value of length for Rectangle:
3
Enter the value of width for Rectangle:
df
Number format is not proper. Please enter a numerical value.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.