I'm having trouble with fixing this error. Can someone please help? My prompt and the code is posted below.
Write a super class encapsulating a rectangle. A rectangle has two attributes representing the width and the height of the rectangle. It has methods returning the perimeter and the area of the rectangle. This class has a subclass, encapsulating a parallelepiped, or box. A parallelepiped has a rectangle as its base, and another attribute, its length. It has two methods that calculate and return its area and volume. You also need to include a client class to test these two classes.
public class Rectangle1
{
protected double width;
protected double height;
public Rectangle1(double width, double height){
this.width = width;
this.height = height;
}
public double getWidth(){
return width;
}
public void setWidth(double width) {
this.width = width;
}
public double getHeight(){
return height;
}
public void setHeight(double height){
this.height = height;
}
public double getArea(){
return width * height;
}
public double getPerimeter(){
return 2 * (width + height);
}
}
public class Box extends Rectangle1 {
protected double length;
public Box(double length){
this.length = length;
}
public double getLength(){
return length;
}
public void setLength(double length){
this.length = length;
}
public double getVolume(){
return width * height * length;
}
}
public class TestRectangle {
public static void main(String[] args) {
Rectangle1 rectangle = new Rectangle1(2,4);
Box box = new Box(5);
System.out.println("\nA rectangle " + rectangle.toString());
System.out.println("The area is " + rectangle.getArea());
System.out.println("The perimeter is " +rectangle.getPerimeter());
System.out.println("The volume is " + box.getVolume());
}
}
The error is at
public Box(double length){
this.length = length;
}
The error message in Eclipse IDE is as follows:
Implicit super constructor Rectangle1() is undefined. Must explicitly invoke another constructor.
And when I try to run it, it gives me:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
Implicit super constructor Rectangle1() is undefined. Must explicitly invoke another constructor
at Box.<init>(Box.java:4)
at TestRectangle.main(TestRectangle.java:7)
Can someone please advise me on how to fix this error?
Your base class Rectangle1 has a constructor:
public Rectangle1(double width, double height) {
this.width = width;
this.height = height;
}
Because you wrote a constructor, the default no aruments constructor will not exist, so super() will not find the right constructor. You should write: super(0, 0) in your Box constructor, which will match Rectangle1 constructor.
Firstly, every subclass must call super(...) as the first statement in every constructor. This is a bit of a pain, so Java adds a call to super() at the start of any constructor that doesn't have a call to super(...). Since Rectangle1 doesn't have a constructor with no arguments, Java's attempt to call super() doesn't work and you need to add your own. Peter and Maroun covered this.
A bigger problem is that you haven't thought about what a Box is. What is a Box(5)? A Rectangle1 has a width and a height, while a Box has a width, a height and a depth. What is shape is a Box(5)? Your Box constructor should be something like
public Box (double width, double height, double depth)
{
super (width, height);
this.depth = depth;
}
In this constructor you can see that the arguments tell you everything you need to know about the Box and the call to super(height, width) takes care of delegating all the rectangle stuff to the base class.
You have to call the super class constructor which you define. The default constructor only exists when you haven't defined one.
Also you should not attempt to initialise fields which are initialised by the parent as this breaks encapsulation. I suggest you do this.
public Box(double length){
super(length, length);
}
This way you are calling a constructor in the super class you have defined and you let it set the fields it is responsible for.
Related
I have following java code segment,
class Box{
int length;
int width;
int height;
Box(int length, int width, int height){
this.length=length;
this.width=width;
this.height=height;
}
Box(){
this.length=1;
this.width=1;
this.height=1;
}
}
class Demo{
public static void main(String args[]){
Box b1=new Box(); //calling default
System.out.println("Length : "+b1.length);
System.out.println("Width : "+b1.width);
System.out.println("Height : "+b1.height);
b1.Box(12,5,3);
System.out.println();
}
}
but when I compile the java file following code highlight and not the compile
b1.Box(12,5,3);
what is the problem and how can fix this?
Box(12,5,3) is a parameterized constructor and not a method of Box class, hence there no need to call it using a reference. Just call it directly without using the object.
Therefore, instead of declaring b1 with the default constructor you can use the parameterized one as follows:
Box b1 = new Box(12, 5, 3);
Or you can create a setter function in the Box class if you need to set the values later as
public void setDimensions(int length, int width, int height) {
this.length = length;
this.width = width;
this.height = height;
}
then b1.Box(12,5,3); becomes b1.setDimensions(12,5,3);
There are two tasks to solve:
First a class rectangle should be inherited from the class GeoObjects.
Second a class square should be inherited from the class rectangle.
The abstract class GeoObjects was given.
abstract class GeoObjects{
public abstract double Perimeter();
public abstract double Surface();
public static void main (String [] argv){
double width = 4.0, height = 5.0, side= 3.0;
GeoObject rectangle = new Rectangle (width, height);
GeoObject square= new Square(side);
System.out.println ("Perimeter = " + rectangle.Perimeter());
System.out.println ("Surface= " + rectangle.Surface());
System.out.println ("Perimeter= " + square.Perimeter());
System.out.println ("Surface= " + square.Surface());
}
}
class Rectangle extends GeoObjects{
double width, height, side;
Rectangle (double width, double height){
this.width = width;
this.height= height;
}
public double Perimeter (){
return 2*(width+ height);
}
public double Surface(){
return width* height;
}
}
class Square extends Rectangle {
double side;
Square (double side){
this.side= side;
}
public double Perimeter (){
return 4*side;
}
public double Surface(){
return side*side;
}
}
I get the compiler information that the Square constructor has a different amount of variables than the one from Rectangle.
How can i solve this without hurting the requirement that Square has to be inherited from rectangle and not GeoObjects?
The compiler error message is informing you that you're attempting to call the superclass constructor in Rectangle with a different number of parameters than what the constructor has. You are not explicitly calling a superclass constructor in Square, so the compiler has inserted a call to the default superclass constructor in Rectangle -- effectively super(); as the first line in Square().
But there is no constructor in Rectangle with no parameters; there is only one with 2 parameters. Call it appropriately, by passing side to super() twice.
You'll also notice that the Perimeter and Surface methods no longer need to be overridden, because they will now use the proper values from the superclass.
Also, normal Java method naming conventions would have you name those methods starting with a lowercase character: perimeter and surface.
To fix your Square class you will need to use super(side, side) to call the constructor from Rectangle. You will no longer need the side class variable inside of Square, it can be simplified to just this:
Square:
class Square extends Rectangle {
Square (double side){
super(side,side);
}
}
Rectangle:
class Rectangle extends GeoObjects{
double width, height;
Rectangle (double width, double height){
this.width = width;
this.height= height;
}
public double Perimeter (){
return 2*(width+ height);
}
public double Surface(){
return width* height;
}
}
If you want to overload the methods in Rectangle or place methods that implement specific functions to square, you need to use the width and height variables instead of side.
import java.util.Scanner;
public class Rectangle {
private double length, width;
Scanner scan = new Scanner(System.in);
public Rectangle(double length, double width){
this.length = length;
this.width = width;
}
public void setLength(double l){
length = l;
}
public double getLength(){
return length;
}
public void setWidth(double w){
w = width;
}
public double getWidth(){
return width;
}
public double getArea(){
return length*width;
}
public double getPerimeter(){
return ((2*length) + (2*width));`enter code here`
}
}
Why don't I need a set method for Area and Perimeter? Also does it matter if you make the code this.length = length as compared to length = l?
Think about it this way: how would you implement a setter for the perimeter or the area property?
There is no good way of doing it that would make reasonable sense to users of your class: let's say the width is 10 and the height is 7, and a call arrives to set the perimeter to 24. Would you adjust the height, the width, or both in order to satisfy the new value of the perimeter? There is no usable solution to this problem.
Area and Perimeter are so-called calculated properties, i.e. properties computed from values of settable properties. That is why neither area nor perimeter can have a setter.
I realized I had this issue when i was typing down simple code to find the perimeter and rectangle to demonstrate method overriding in java. can i return the area (length * breadth) in the same subclass method?
package override;
class perimeter{
int length,breadth;
perimeter(){length = breadth = 0;}//default constructor
perimeter(int length, int breadth){
this.length = length;
this.breadth = breadth;
}
int show(int length, int breadth){
return 2*(length + breadth);
}
}
class area extends perimeter{
area(int length, int breadth){
super(length,breadth);
}
int show(int length, int breadth){
return super.show(length, breadth);
// how can i return this too? : return length * breadth;
}
}
public class overrideshapes {
public static void main(String args[]){
area shape1 = new area(5,10);
System.out.println(""+ shape1.show(shape1.length,shape1.breadth));
}
}
I don't know what are you trying to do, but I'm going to explain a few more what I'm thinking about your question.
I think you want to calculate perimeter and areas for different polygons, so the best way is to create an Interface, something like this.
public Interface Calculate {
public int calculateArea(int length, int width);
}
Then you should to implement your interface, in many clase according your polygons, for example:
public class Square implements Calculate {
#Override
public int calculateArea(int length, int width){
return length*width; //Because this is the way you calculate square areas
}
}
So you have to implement for your polygons your interface method "calculateArea" using #Override annotation, so each polygon knows how to calculate its area.
Hope it helps to you.
I am fairly new to Java and I am learning about Inheritance. I am trying to create a subclass called BetterRectangle under the superclass Rec1.
Rec 1 gets the x and y coordinates (location) and also gets the width and height (size) of the rectangle. BetterRectangle calculates the perimeter and area of the rectangle.
I get errors in the main method. It cannot find any of the symbols (i.e. cannot find rec1.getHeight(20) symbol).
public class Rec1 {
private double x;
private double y;
private double width;
private double height;
public void setLocation(double xCord, double yCord) {
x = xCord;
y = yCord;
}
public void setSize(double h, double w) {
height = h;
width = w;
}
public double getHeight(double h) {
return height;
}
public double getWidth(double w) {
return width;
}
}
public class BetterRectangle extends Rectangle {
public BetterRectangle(int x, int y, int width, int height) {
super(x, y, width, height);
super.setLocation(x, y);
super.setSize(width, height);
}
public double calcPerimeter() {
return super.getHeight() * 2 + super.getWidth() * 2;
}
public double calcArea() {
return super.getHeight() * super.getWidth();
}
}
Look closely:
Rectangle Rec1 = new Rectangle();
Rec1.getHeight(20);
The type of Rec1 is Rectangle. But the Rectangle class doesn't have a getHeight method. Maybe you wanted this:
Rec1 rec1 = new Rectangle();
rec1.getHeight(20);
Notice that I renamed the variable to rec1, and changed its type.
class BetterRectangle is extending Rectangle in your sample code. So here BetterRectangle would be child and Rectangle would be parent. Now BetterRectangle uses super keyword to access getHeight,getWidth etc...functions. since these functions are not present in Rectangle class it is giving compilation Error.
Instead of extending Rectangle class extend Rec1 class in BetterRectangle and then run the main Class Rectangle. This should work.
Look carefully here
Rectangle BetterRectangle = new Rectangle();
Rectangle Rec1 = new Rectangle();
You are declaring variables of type Rectangle instead of your own classes. I can't see your import statements but you are likely creating types of java.awt.rectangle. You're mixing up variable types and variable names. When creating instances of your own classes it should look like this:
Rec1 mySimpleRec = new Rec1();
BetterRectangle myBetterRec = new BetterRectangle()
When declaring things put them in this order "Type variableName = new Type()" or "Class variableName = new Class()".