Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Step 1: Edit Rectangle.java Download the Rectangle.java file, and open it in jGrasp (or a text editor of your choice). You will need to define

Step 1: Edit Rectangle.java

Download the Rectangle.java file, and open it in jGrasp (or a text editor of your choice). You will need to define a constructor for this class, along with a series of methods. The comments in Rectangle.java provide further details. Note that there is no main method, so you cannot run this program directly.

File:

public class Rectangle {

// DO NOT MODIFY THE INSTANCE VARIABLES

// begin instance variables

private int width;

private int height;

// end instance variables

// You need to do the following:

//

// 1.) Define a constructor which takes two ints

// representing the width and height, respectively.

// The constructor should set its instance variables

// equal to these values.

//

// 2.) Define a "getter" named getWidth, which returns

// the width of the rectangle.

//

// 3.) Define a "getter" named getHeight, which returns

// the height of the rectangle.

//

// 4.) Define a "setter" named setWidth, which takes

// the new width of the rectangle and sets the

// rectangle's width to that value.

//

// 5.) Define a "setter" named setHeight, which

// takes the new height of the rectangle and sets

// the rectangle's height to that value

//

// 6.) Define a toString method, which returns

// a String representation of the rectangle.

// As an example, if the width of the rectangle is

// 3 and the height of the rectangle is 4, this should

// return the String:

// "Rectangle(3, 4)"

//

// 7.) Define a method named getPerimeter, which

// returns the perimeter of the rectangle.

//

// TODO - write your code below this comment.

}

Step 2: Download, Open, Compile, and Run the Tests In RectangleTest.java

Download the RectangleTest.java file, being sure to put it in the same folder/directory as your Rectangle.java file from step 1. RectangleTest.java contains a bunch of tests for the methods you wrote in the previous step. Use the instructions here to open RectangleTest.java as a tests file, then compile and run the tests. All the tests have been provided, and assuming you implemented your methods correctly in the previous step, they should all pass.

You do not need to save the compiled file. However, you should still complete this step, as this step makes sure that yor completed the previous step correctly.

File:

import static org.junit.Assert.assertEquals;

import org.junit.Test;

public class RectangleTest {

@Test

public void testGetWidth() {

Rectangle r = new Rectangle(2, 3);

assertEquals(2, r.getWidth());

}

@Test

public void testSetWidth() {

Rectangle r = new Rectangle(2, 3);

r.setWidth(5);

assertEquals(5, r.getWidth());

}

@Test

public void testGetHeight() {

Rectangle r = new Rectangle(2, 3);

assertEquals(3, r.getHeight());

}

@Test

public void testSetHeight() {

Rectangle r = new Rectangle(2, 3);

r.setHeight(5);

assertEquals(5, r.getHeight());

}

@Test

public void testGetPerimeter() {

Rectangle r = new Rectangle(2, 3);

assertEquals(10, r.getPerimeter());

}

@Test

public void testToString() {

Rectangle r = new Rectangle(2, 3);

assertEquals("Rectangle(2, 3)",

r.toString());

}

}

Step 3: Edit RectangleMain.java

Download the RectangleMain.java file, and open it in jGrasp (or a text editor of your choice). You will have to complete the addSomethingToWidthHeight method in this file. The comments in RectangleMain.javaprovide further details. Example output from this program follows:

BEFORE:

R1: Rectangle(3, 4)

R2: Rectangle(5, 6)

AFTER:

R1: Rectangle(8, 9)

R2: Rectangle(9, 10)

File:

public class RectangleMain {

public static void addSomethingToWidthHeight(int something, Rectangle r) {

// TODO - write your code below this comment.

// You need to add `something` to both the width and the height

// of the given rectangle r. As a hint, you'll need to use

// the "getters" and "setters" on r.

}

// DO NOT MODIFY main!

public static void main(String[] args) {

Rectangle r1 = new Rectangle(3, 4);

Rectangle r2 = new Rectangle(5, 6);

System.out.println("BEFORE:");

System.out.println("R1: " + r1);

System.out.println("R2: " + r2);

addSomethingToWidthHeight(5, r1);

addSomethingToWidthHeight(4, r2);

System.out.println(" AFTER:");

System.out.println("R1: " + r1);

System.out.println("R2: " + r2);

}

}

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_2

Step: 3

blur-text-image_3

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

Pro Database Migration To Azure Data Modernization For The Enterprise

Authors: Kevin Kline, Denis McDowell, Dustin Dorsey, Matt Gordon

1st Edition

1484282299, 978-1484282298

More Books

Students also viewed these Databases questions

Question

How many Tables Will Base HCMSs typically have? Why?

Answered: 1 week ago

Question

What is the process of normalization?

Answered: 1 week ago