Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

public class Main { public static void main(String[] args) { Rectangle r = new Rectangle( 3, 5 ); System.out.println( r ); // length: 3, width:

image text in transcribedimage text in transcribed

public class Main { public static void main(String[] args) { Rectangle r = new Rectangle( 3, 5 ); System.out.println( r ); // length: 3, width: 5 r.setWidth( 11 ); System.out.println( r ); // length: 3, width: 11 Square sq = new Square( 6 ); System.out.println( "The square's initial area is " + sq.area() + " square units."); // 36 sq.setWidth( 4 ); System.out.println( sq ); // length: 4, width: 4 sq.setLength( 8 ); System.out.println( sq ); // length: 8, width: 8 System.out.println( "The new area is " + sq.area() + " square units."); // 64 } }

public class Rectangle { private int length, width; public Rectangle( int len, int wid ) { length = len; width = wid; } public int area() { return length*width; } public void setWidth( int n ) { width = n; } public void setLength( int n ) { length = n; } @Override public String toString() { return "length: " + length + ", width: " + width; } }

public class Rectangle { private int length, width; public Rectangle( int len, int wid ) { length = len; width = wid; } public int area() { return length*width; } public void setWidth( int n ) { width = n; } public void setLength( int n ) { length = n; } @Override public String toString() { return "length: " + length + ", width: " + width; } }

public class Rectangle { private int length, width; public Rectangle int len, int wid) { length = len; Write the Square class as a subclass of Rectangle. The Square class: Should NOT declare any instance variables. Its constructor has one parameter which is the length of a side. Override the setWidth method so that when the width changes, the length changes to the same value. Override the setLength method so that when the length changes, the width changes to the same value. Do NOT override area. Do NOT override toString width = wid; } public int areal) { return length*width; } public void setWidth(int n) { width = n; } public void setLength(int n) { length = n; } @Override public String toString() { return "length: " + length +", width:" + width; } } Here's an example. Square sq = new Square( 6 ); System.out.println( sq.area()); // 36 sq.setWidth( 4 ); System.out.println( sq); // length: 4, width: 4 sq.setLength( 8 ); System.out.println( sq); // length: 8, width: 8

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

The Database Experts Guide To Database 2

Authors: Bruce L. Larson

1st Edition

0070232679, 978-0070232679

More Books

Students also viewed these Databases questions

Question

2. Develop a program for effectively managing diversity.

Answered: 1 week ago

Question

7. What is coaching? Is there only one type of coaching? Explain.

Answered: 1 week ago