Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

THE CODE OF RECTANGLE CLASS IS PROVIDED HERE /* * Rectangle.java * * Computer Science 112, Boston University * * A class that acts as

image text in transcribedimage text in transcribed

THE CODE OF RECTANGLE CLASS IS PROVIDED HERE

/* * Rectangle.java * * Computer Science 112, Boston University * * A class that acts as a blueprint for objects that represent a rectangle. */ public class Rectangle { /* * Fields that capture the state of a Rectangle object. * We make them private to prevent direct access from outside the class. */ private int width; private int height; /* * A constructor that calls the mutator methods to initialize the * fields, so that they can perform the necessary error-checking. */ public Rectangle(int w, int h) { this.setWidth(w); this.setHeight(h); } /* * getWidth - an accessor method for a Rectangle's width. */ public int getWidth() { return this.width; } /* * getHeight - an accessor method for a Rectangle's height. */ public int getHeight() { return this.height; } /* * area - an accessor method for a Rectangle's area. */ public int area() { return this.width * this.height; } /* * perimeter - an accessor method for a Rectangle's perimeter. */ public int perimeter() { return 2*this.width + 2*this.height; } /* * setWidth - a mutator method that changes a Rectangle's width. * * precondition: w must be positive */ public void setWidth(int w) { if (w   Problem 4: Our Rectangle class revisited 12 points total; individual-only Recall the Rectangle class that we defined in lecture. (The relevant version of this class is available here.) 1. Consider a potential instance method named shrink that would take an integer value as a parameter and reduce both of the dimensions of a Rectangle by that value. For example, if a Rectangle's dimensions are 80 x 40, then calling the shrink method with a parameter of 10 would give the Rectangle dimensions of 70 x 30. a. (1 point) What type of instance method would shrink be, an accessor or mutator? b. (2 points) Give an appropriate method header for this method, making sure to take into account the fact that the method is non-static. You do not need to define the body of the method. 2. Now consider a potential instance method named diagonal that would return the length of the rectangle's diagonal as a real number. For example, if a Rectangle's dimensions are 30 x 40, then the diagonal method would return 50.0. a. (1 point) What type of instance method would diagonal be, an accessor or mutator? b. (2 points) Give an appropriate method header for this method, making sure to take into account the fact that the method is non-static. You do not need to define the body of the method. 3. Consider the following client code  i.e., code from another class that uses a Rectangle object: Rectangle rect = new Rectangle(10, 20, 30, 40); System.out.println("width = + rect.width); rect.width = rect.width + rect.height; System.out.println(rect); Because our Rectangle class employs appropriate encapsulation, this code fragment will not compile. a. (2 point) Explain what problems are present in the code fragment that prevent it from compiling. b. (4 points) Rewrite the fragment to eliminate those problems while maintaining the intended functionality of the original version

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Programming The Perl DBI Database Programming With Perl

Authors: Tim Bunce, Alligator Descartes

1st Edition

1565926994, 978-1565926998

More Books

Students also viewed these Databases questions

Question

Define Administration and Management

Answered: 1 week ago

Question

Define organisational structure

Answered: 1 week ago

Question

Define line and staff authority

Answered: 1 week ago

Question

Define the process of communication

Answered: 1 week ago