Question

Consider the following declaration for a class that will be used to represent rectangles. public class...

Consider the following declaration for a class that will be used to represent rectangles.

public class Rectangle
{
  private double height;
  private double width;

  public Rectangle()
  {
    height = 2.0;
    width = 1.0;
  }

  public Rectangle(double w, double h)
  {
    height = h;
    width = w;
  }

  public double getHeight()
  {  return height;  }

  public double getWidth()
  {  return width;  }

  public void setHeight(double h)
  {  height = h;  }

  public void setWidth(double w)
  {  width = w;  }

  //Other methods not shown
}

A Square class which extends the Rectangle class is to be written. Which of the following constructors will cause an error upon compilation when added to this class?

(A) public Square()
{
  super(1.0, 1.0);
}
(B) public Square(double w)
{
  height = w;
  width = w;
}
(C) public Square()
{
  setHeight(1.0);
  setWidth(1.0);
}
(D) public Square(double w)
{
  super(w, w);
}
(E) public Square(double w)
{
  super();
  setHeight(w);
  setWidth(w);
}
0 0
Add a comment Improve this question Transcribed image text
Answer #1
 public Square(double w)
{
  height = w;
  width = w;
}

this constructor will cause a compilation error because we have directly assigned height = w and width = w whereas height and width are not defined in the class.

Correct way to set height and width is

public Square(double w)
{
  super();
  setHeight(w);
  setWidth(w);
}

where we are calling methods of Rectangle class to set the height and width.

if you like the answer please provide a thumbs up.

Add a comment
Know the answer?
Add Answer to:
Consider the following declaration for a class that will be used to represent rectangles. public class...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • m The interface Measurable is defined as the following: /** An interface for methods that return...

    m The interface Measurable is defined as the following: /** An interface for methods that return the perimeter and area of an object. */ public interface Measurable { public double getPerimeter(); public double getArea(); } The class Rectangle implements the interface. The incomplete program is written as follows: 1 public class Rectangle 2 { private double width; private double height; public Rectangle(double w, double h) { width = w; height h; } { 3 4 5 6 7 8 9...

  • package rectangle; public class Rectangle {    private int height;    private int width;    public...

    package rectangle; public class Rectangle {    private int height;    private int width;    public Rectangle(int aHeight, int aWidth) {    super();    height = aHeight;    width = aWidth;    }    public int getHeight() {    return height;    }    public int getWidth() {    return width;    }    public void setHeight(int aHeight) {    height = aHeight;    }    public void setWidth(int aWidth) {    width = aWidth;    }    public int...

  • I Need UML Design for the following Java code: Colorable interface: public interface Colorable { //a...

    I Need UML Design for the following Java code: Colorable interface: public interface Colorable { //a void method named howToColor(). void howToColor(); } GeometricObject class: public class GeometricObject { } Sqaure class: public class Square extends GeometricObject implements Colorable{ //side variable of Square double side;    //Implementing howToColor() @Override public void howToColor() { System.out.println("Color all four sides."); }    //setter and getter methods for Square public void setSide(double side) { this.side = side; }    public double getSide() { return...

  • I have to create a java graphics program which will draw 10 rectangles and 10 ellipses...

    I have to create a java graphics program which will draw 10 rectangles and 10 ellipses on the screen. These shapes are to be stored in an arrayList. I have to do this using 6 different class files. I am not sure whether I successfully created an ellipse in the Oval Class & also need help completing the print Class. I need someone to check whether my Oval Class is correct (it successfully creates an ellipse with x, y, width,...

  • Create a Java Project in Eclipse with the following: Include the Rectangle class supplied below. Override...

    Create a Java Project in Eclipse with the following: Include the Rectangle class supplied below. Override the toString method for Rectangle. Override the equals method for Rectangle. Implement the comparable Interface for Rectangle (Compare by area) Rectangle class: public class Rectangle {       private double length;    private double width;       public Rectangle(double l, double w)    {        length = l;        width = w;    }       public double getLength()    {   ...

  • Programming Language: Java Write a class named ColoredRectangle that extends the Rectangle class (given below). The...

    Programming Language: Java Write a class named ColoredRectangle that extends the Rectangle class (given below). The ColoredRectangle class will have an additional private String field named Color. The ColoredRectangle class should have four constructors: a no-arg constructor; a three-arg constructor; a two-arg constructor that accepts a Rectangle object and a color; and a copy constructor. The ColoredRectangle class should have an Equals and toString methods. The ColoredRectangle class should have mutators and accessors for all three fields. public class Rectangle...

  • Hello I have a question. I would like to check if the code follows this requirement....

    Hello I have a question. I would like to check if the code follows this requirement. Rectangle.h - A complete Rectangle Class including both declaration and definition appRectangle,cpp separate in two different files the class definition and implementation Code: rectangle.h // Rectangle class declaration. class Rectangle { private: double width; double length; public: void setWidth(double); void setLength(double); double getWidth() const; double getLength() const; double getArea() const; }; //************************************************** // setWidth assigns a value to the width member. * //************************************************** void...

  • Create a BoxFigure class that inherits RectangleFigure class BoxFigure Class should contain:- Height instance field- Default...

    Create a BoxFigure class that inherits RectangleFigure class BoxFigure Class should contain:- Height instance field- Default constructor -- that calls the default super class constructor and sets the default height to 0- Overloaded constructor with three parameters (length, width and height) – that calls the two parameterized super class constructor passing in length and width passed and sets the height to passed height.- setDimension method with three parameters (length, width, height) – call the super class setDimension and pass in...

  • In Java Create a testing class that does the following to the given codes below: To...

    In Java Create a testing class that does the following to the given codes below: To demonstrate polymorphism do the following: Create an arraylist to hold 4 base class objects Populate the arraylist with one object of each data type Code a loop that will process each element of the arraylist Call the first ‘common functionality’ method Call the second ‘common functionality’ method Call the third ‘common functionality’ method Verify that each of these method calls produces unique results Call...

  • ((( Using Only The mentioned Concepts Solve the following question in DETAILS “ Take your time but don’t disappoint me plleeaasseee”: 1- Control structure (if/if else/while/for/switch/do while/ brea...

    ((( Using Only The mentioned Concepts Solve the following question in DETAILS “ Take your time but don’t disappoint me plleeaasseee”: 1- Control structure (if/if else/while/for/switch/do while/ break continue) 2- Functions 3- Arrays 4- pointers and strings 5- classes 6- file processing ))) if it was a program i need to see the code printed on computer and the output too please . 10. Write a C++code that include two classes: Rectangle and Triangle as shown below: #include <iostream> using...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT