In: Computer Science
Given the following TestDriver.java
public class TestDriver {
public static void main(String args[]) {
Point point = new Point(10, 5);
Circle circle = new Circle(10.5, 20, 19);
Cylinder cylinder = new Cylinder(12, 3.6, 20, 20);
System.out.println("Point");
System.out.println(point.toString()) ;
System.out.println("Circle");
System.out.println(circle.toString());
System.out.println("Cylinder");
System.out.println(cylinder.toString());
}
}
You will get the following result once you execute the TestDriver.java (after compiling it)
Point
(10, 5)
Circle
Center = (20, 19); Radius = 10.5
Cylinder
Center = (20, 20); Radius = 3.6; Height = 12.0
Please fill out the missing part of the following java programs for testDriver is working as above explained.
Please note that you can use multiple statements for an answer for each question.
public class Shape extends 1. _________(5pt) {
public double area() {
return 0;
}
public double volumn() {
return 0.0;
}
}
public class Point extends Shape{
private int x,y;
public Point() {
setPoint(0,0);
}
public Point(int x, int y) {
setPoint(x,y);
}
public void setPoint(int x, int y) {
this.x = x;
this.y = y;
}
public int getX() {
return x;
}
public String toString() {
return "(" + x + ", " + y + ")";
}
}
public class Circle extends Point{
protected double radius;
public Circle(){
setRadius(0);
}
public Circle(double cr, int x, int y) {
2.__________(10pt)
}
public void setRadius(double cr) {
radius = (cr >=0 ? cr:0);
}
public double getradius() {
return radius;
}
public double area() {
return Math.PI* radius*radius;
}
public String toString() {
return "Center = " + super.toString() + "; Radius = " + radius;
}
}
public class Cylinder extends Circle{
protected double height;
public Cylinder() {
setHeight(0);
}
public Cylinder(double h, double radius, int x, int y) {
3. __________(10pt)
}
public void setHeight(double h) {
height = (h >= 0 ? h:0);
}
public double area() {
return 4.__________________________(5 pt);
//Assuming the area = 2*PI*r2 +2*Pi*r*height
}
public String toString() {
return super.toString() + "; Height = " + height;
}
}
Assuming that the toString of Circle is missing (completely deleted from Circle class)
What will be output with the following testDriver
5._______________________________________________________(10pt)
public class TestDriver {
public static void main(String args[]) {
Circle circle = new Circle(10.5, 20, 19);
Cylinder cylinder = new Cylinder(12, 3.6, 20, 20);
System.out.println("Circle");
System.out.println(circle.toString());
System.out.println("Cylinder");
System.out.println(cylinder.toString());
}
}
What will be output when the following JVMException is being executed. i.e. what will be prionted as a result of the following JVMExceptuon.jva
6.__________________________________________(10 pt)
public class JVMException {
public static void main(String[] args){
try{
faultyMethod();
} catch (OutOfMemoryError e){
System.out.println(" I am here at Catch");
e.printStackTrace();
fixMemoryLeak();
}
}
public static void faultyMethod(){
System.out.println(" I am here FaultyMethod");
throw new OutOfMemoryError("JVM is out of memory! Fix memory leak!");
}
public static void fixMemoryLeak(){
System.out.println("Fixing memory leak...");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
finally {
System.out.println("The memory leak is fixed!");
}
}
}
public class TestDriver {
public static void main(String args[]) {
Point point = new Point(10, 5);
Circle circle = new Circle(10.5, 20, 19);
Cylinder cylinder = new Cylinder(12, 3.6, 20, 20);
System.out.println("Point");
System.out.println(point.toString()) ;
System.out.println("Circle");
System.out.println(circle.toString());
System.out.println("Cylinder");
System.out.println(cylinder.toString());
}
}
class Shape extends Object {
public double area() {
return 0;
}
public double volumn() {
return 0.0;
}
}
class Point extends Shape{
private int x,y;
public Point() {
setPoint(0,0);
}
public Point(int x, int y) {
setPoint(x,y);
}
public void setPoint(int x, int y) {
this.x = x;
this.y = y;
}
public int getX() {
return x;
}
public String toString() {
return "(" + x + ", " + y + ")";
}
}
class Circle extends Point{
protected double radius;
public Circle(){
setRadius(0);
}
public Circle(double cr, int x, int y) {
setPoint(x, y);
radius=cr;
}
public void setRadius(double cr) {
radius = (cr >=0 ? cr:0);
}
public double getradius() {
return radius;
}
public double area() {
return Math.PI* radius*radius;
}
public String toString() {
return "Center = " + super.toString() + "; Radius = " + radius;
}
}
class Cylinder extends Circle{
protected double height;
public Cylinder() {
setHeight(0);
}
public Cylinder(double h, double radius, int x, int y) {
super(radius, x, y);
height=h;
}
public void setHeight(double h) {
height = (h >= 0 ? h:0);
}
public double area() {
return 2 * Math.PI * radius * radius + 2 * Math.PI * radius * height;
//Assuming the area = 2*PI*r2 +2*Pi*r*height
}
public String toString() {
return super.toString() + "; Height = " + height;
}
}
As per policy we can answer 1 question per post. Please post the remianing questions as separate post.Thanks