Check me...
I have two codes that both work. One code based off of the example in my book, and the example you gave me.
Book example:
import javax.swing.JOptionPane;
public class TestRectangle {
public static void main(String[] args) {
Rectangle rectangle = new Rectangle();
rectangle.setHeight(40);
rectangle.setWidth(4);
rectangle.setArea(0);
rectangle.setColor("Yellow");
JOptionPane.showMessageDialog(null, rectangle.toString());
Rectangle rectangle2 = new Rectangle();
rectangle2.setHeight2(35.9);
rectangle2.setWidth2(3.5);
rectangle2.setArea(0);
rectangle2.setColor("Blue");
JOptionPane.showMessageDialog(null, rectangle2.toString2());
}
}
class Rectangle {
private int height;
private int width;
private double height2;
private double width2;
private double area;
private double perimeter;
private String color = "white";
public Rectangle() {
}
//Int Rectangle
public int getHeight() {
return height;
}
public void setHeight(int newHeight) {
height = newHeight;
}
public int getWidth() {
return width;
}
public void setWidth(int newWidth) {
width = newWidth;
}
//Double Rectangle
public double getHeight2() {
return height2;
}
public void setHeight2(double newHeight2) {
height2 = newHeight2;
}
public double getWidth2() {
return width2;
}
public void setWidth2(double newWidth2) {
width2 = newWidth2;
}
public double getArea(double width, double height) {
return area;
}
public void setArea(double newArea) {
area = newArea;
}
public double getPerimeter() {
return perimeter;
}
public void setPerimeter(double newPerimeter) {
perimeter = newPerimeter;
}
public String getColor() {
return color;
}
public void setColor(String newColor) {
color = newColor;
}
public String toString() {
return "Rectangle # 1\n" + "\n"
+ "Height = " + height + "\n"
+ "Width = " + width + "\n"
+ "Area = " + (width * height) + "\n"
+ "Perimeter = " + (2 * width + 2 * height) + "\n"
+ "Color = " + color;
}
public String toString2() {
return "Rectangle # 2\n" + "\n"
+ "Height " + height2 + "\n"
+ "Width " + width2 + "\n"
+ "Area " + (width2 * height2) + "\n"
+ "Perimeter " + (2 * width2 + 2 * height2) + "\n"
+ "Color " + color;
}
}
Junky example
import javax.swing.JOptionPane;
public class TestRectangle {
public static void main(String[] args) {
Rectangle rec1 = new Rectangle(4, 40);
JOptionPane.showMessageDialog(null, rec1.toString());
Rectangle rec2 = new Rectangle(3.5, 35.9);
JOptionPane.showMessageDialog(null, rec2.toString2());
}
}
class Rectangle {
private int height;
private int width;
private int area;
private int perimeter;
private String color;
private double height2;
private double width2;
private double area2;
private double perimeter2;
private String color2;
public Rectangle(int w, int h) {
width = w;
height = h;
area = (h * w);
perimeter = (2 * h + 2 * w);
color = "Red";
}
public Rectangle(double w2, double h2) {
width2 = w2;
height2 = h2;
area2 = (h2 * w2);
perimeter2 = (2 * h2 + 2 * w2);
color2 = "Yellow";
}
public String toString() {
return "Rectangle # 1\n" + "\n"
+ "Height = " + height + "\n"
+ "Width = " + width + "\n"
+ "Area = " + area + "\n"
+ "Perimeter = " + perimeter + "\n"
+ "Color = " + color;
}
public String toString2() {
return "Rectangle # 2\n" + "\n"
+ "Height " + height2 + "\n"
+ "Width " + width2 + "\n"
+ "Area " + area2 + "\n"
+ "Perimeter " + perimeter2 + "\n"
+ "Color " + color2;
}
}
Please critique both sets of codes if you have time. I would love to go into class today ahead of the game for a change.