My output is showing duplicating "The Surface Area(SA) of Square:". How can I display only one but get the same results? Below is my code. There is also an attached picture for question reference.   Square.java public class Square { public double height; public double width; public double surfaceArea; //getter setter surfaceArea public void computeSurfaceArea(){ surfaceArea = height * width; System.out.println("The Surface Area(SA) of Square: " + surfaceArea); } public double getSurfaceArea(){ return surfaceArea; } //setter for height and width public void setHeight(double height){ this.height = height; } public void setWidth(double width){ this.width = width; } }   Cube.java public class Cube extends Square { private double depth; //computation for surface area override public void computeSurfaceArea() { super.computeSurfaceArea(); System.out.println("The Surface Area(SA) of Cube: " + ((2 * super.getSurfaceArea()) + (2 * width * depth) + (2 * height * depth))); } //setter for depth public void setDepth(double depth) { this.depth = depth; } }   DemoSquare.java public class DemoSquare { public static void main(String[] args) { Square s = new Square(); Cube c = new Cube(); //set values for square s.setHeight(2); s.setWidth(3); s.computeSurfaceArea(); //set values for cube c.setHeight(4); c.setWidth(5); c.setDepth(66); c.computeSurfaceArea(); } }

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question
100%

My output is showing duplicating "The Surface Area(SA) of Square:". How can I display only one but get the same results? Below is my code. There is also an attached picture for question reference.

 

Square.java

public class Square {
public double height;
public double width;
public double surfaceArea;

//getter setter surfaceArea
public void computeSurfaceArea(){
surfaceArea = height * width;
System.out.println("The Surface Area(SA) of Square: " + surfaceArea);
}
public double getSurfaceArea(){
return surfaceArea;
}
//setter for height and width
public void setHeight(double height){
this.height = height;
}
public void setWidth(double width){
this.width = width;
}
}

 

Cube.java

public class Cube extends Square {
private double depth;
//computation for surface area override
public void computeSurfaceArea() {
super.computeSurfaceArea();
System.out.println("The Surface Area(SA) of Cube: " + ((2 * super.getSurfaceArea()) + (2 * width * depth) + (2 * height * depth)));
}
//setter for depth
public void setDepth(double depth) {
this.depth = depth;
}
}

 

DemoSquare.java

public class DemoSquare {
public static void main(String[] args) {
Square s = new Square();
Cube c = new Cube();

//set values for square
s.setHeight(2);
s.setWidth(3);
s.computeSurfaceArea();

//set values for cube
c.setHeight(4);
c.setWidth(5);
c.setDepth(66);
c.computeSurfaceArea();
}
}

 

 

Create a class named Square that contains data fields for height, width, and
surfaceArea, and a method named computeSurfaceArea(). Create a child class named
Cube. Cube contains an additional data field named depth, and a
computeSurfaceArea() method that overrides the parent method. Write an application
that instantiates a Square object and a Cube object and displays the surface areas of
the objects. Save the files as Cube.java, Square.java, and DemoSquare.java.
Transcribed Image Text:Create a class named Square that contains data fields for height, width, and surfaceArea, and a method named computeSurfaceArea(). Create a child class named Cube. Cube contains an additional data field named depth, and a computeSurfaceArea() method that overrides the parent method. Write an application that instantiates a Square object and a Cube object and displays the surface areas of the objects. Save the files as Cube.java, Square.java, and DemoSquare.java.
The Surface Area(SA) of Square: 6.0
The Surface Area(SA) of Square: 20.0
The Surface Area(SA) of Cube: 1228.0
Process finished with exit code 0
The Surface Area(SA) of Square: 6.0
The Surface Area(SA) of Cube: 1228.0
Transcribed Image Text:The Surface Area(SA) of Square: 6.0 The Surface Area(SA) of Square: 20.0 The Surface Area(SA) of Cube: 1228.0 Process finished with exit code 0 The Surface Area(SA) of Square: 6.0 The Surface Area(SA) of Cube: 1228.0
Expert Solution
steps

Step by step

Solved in 2 steps with 1 images

Blurred answer
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education