The following questions refer to the class shown below. public class Rectangle {
ID: 3804247 • Letter: T
Question
The following questions refer to the class shown below. public class Rectangle {private int width; private int height; public Rectangle (int w, int h) {width = w; height = h;} public int getwidth () {return width} public int getHeight () {return height;} public void setwidth (int w) width {} public void setHeight(int h) {height = h;} public int computeArea () {return width * height;}} (a) How many constructors does this class have? (b) How many methods does this class have? (Constructors don't count.) (c) What term do we use to describe methods such as getWidth and getHeight, which return a value stored in an instance variable? (d) What term do we use to describe methods such as setWidth and setHeight, which change the value of an instance variable? (e) Write a declaration that declares a Rectangle variable named rect and initializes it to contain a Rectangle object whose width is 20 and whose height is 10. (f) Write a statement that changes the height of rect to 15. (g) Write a statement that prints the area of rect. (Use the computeArea method in your statement.)Explanation / Answer
a) The class have only one constructor.That is two parameter constructor having width and height.
b) They are totally five methods, two setters, two getters and one normal method.
c) Accessors or Getters are used to represent the methods getWidth() and getHeight() respectively.
d) Mutators or Setters are used to represent the methods setWidth() and setHeight() respectively.
e) Rectangle rect = new Rectangle(20,10);
f) rect.setHeight(15);
g) rect.computeArea();
Example Main method Program:
public static void main(String args[]){
Rectangle rect = new Rectangle(20,10);
rect.setHeight(15);
rect.computeArea();
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.