Java homework need help. Please answer my question by understanding what the que
ID: 3852443 • Letter: J
Question
Java homework need help.
Please answer my question by understanding what the question want, Thank you!
Define the class Rectangle that contains double instance variables named width and height, and a static variable named printChar. Include the following:
A no-arg constructor that creates a default rectangle with 1.0 for both width and height.
A constructor that creates a rectangle with the specified width, and height.
Get and set methods for all the instance variables.
A method getArea() that returns the area of the rectangle.
A static method setPrintChar() that modifies the static variable
A toString() method that returns a String that represents an outline drawing of the rectangle using the character given by printChar. For example, if printChar were ‘c’ and one Rectangle had a width of 5 and a height of 4, then printing the toString() method to output would give
ccccc
c c
ccccc
Write a test program that first sets the printChar for the Rectangle class as ‘*’, then creates two Rectangle Objects of reasonable sizes. Print out each rectangle separately to output using the toString() method, followed by the value of that rectangle’s area.
Change the printChar to a reasonable (non-white-space) character of your choosing, modify both instance variables for both Rectangle Objects, and then repeat the output process: print out each rectangle separately to output followed by the value of that rectangle’s area
Explanation / Answer
Rectangle.java
package com.chegg.serial;
public class Rectangle {
private double width;
private double height;
private static char printChar;
/**
*
*/
public Rectangle() {
super();
this.width = 1.0;
this.height = 1.0;
}
/**
* @param width
* @param height
*/
public Rectangle(double width, double height) {
super();
this.width = width;
this.height = height;
}
/**
* @return the width
*/
public double getWidth() {
return width;
}
/**
* @param width the width to set
*/
public void setWidth(double width) {
this.width = width;
}
/**
* @return the height
*/
public double getHeight() {
return height;
}
/**
* @param height the height to set
*/
public void setHeight(double height) {
this.height = height;
}
/**
* @return the printChar
*/
public static char getPrintChar() {
return printChar;
}
/**
* @param printChar the printChar to set
*/
public static void setPrintChar(char printChar) {
Rectangle.printChar = printChar;
}
public double getArea(){
return width * height;
}
/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
char[][] arr = new char[(int) getHeight()][(int) getWidth()];
for (int i = 0; i < getWidth(); i++) {
for (int j = 0; j < getHeight(); j++) {
if(i==0 || j==0 || i==getWidth()-1){
arr[i][j] = printChar;
}else{
arr[i][j] = ' ';
}
}
}
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr.length; j++) {
System.out.print(arr[i][j]);
}
}
return arr.toString();
}
}
TestRectangle.jave
package com.chegg.serial;
import java.util.Scanner;
public class TestRectangle {
public static void main(String[] args) {
System.out.println("Enter height & width");
Scanner s = new Scanner(System.in);
Rectangle r = new Rectangle();
r.setHeight(s.nextDouble());
s= new Scanner(System.in);
r.setWidth(s.nextDouble());
System.out.println("Enter print char");
s= new Scanner(System.in);
String str= s.nextLine();
r.setPrintChar(str.charAt(0));
r.toString();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.