Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Day13A {
- public static void main(String[] args) {
- Rectangle callRect = new Rectangle(7, 4.32);
- System.out.println("length: 7");
- System.out.println("width: 4.32");
- System.out.printf("Area: " + callRect.getArea());
- Rectangle callRect2 = new Rectangle();
- callRect2.setLen(8);
- callRect2.setWid(6.44);
- System.out.println("length: 8");
- System.out.println("width: 6.44");
- System.out.printf("Area: " + callRect2.getArea());
- Box callBox = new Box(4,7,3);
- System.out.println("length: 4");
- System.out.println("width: 7");
- System.out.println("height: 3");
- System.out.printf("Volume: " + callBox.getBoxVolume());
- System.out.printf("%nSurface Area: " + callBox.getSurfaceArea());
- }
- }
- class Rectangle {
- private double len; //set
- private double wid; //set
- private double area; //get
- // - Encapsulation
- public void setLen(double len) {
- this.len = len;
- }
- public void setWid(double wid) {
- this.wid = wid;
- }
- public double getLen() {
- return len;
- }
- public double getWid() {
- return wid;
- }
- public double getArea() {
- area = computeArea(getLen(), getWid());
- return area;
- }
- // - Constructor
- public Rectangle() {
- }
- public Rectangle(double ln, double wd) {
- this.len = ln;
- this.wid = wd;
- }
- double computeArea(double ln, double wd) {
- double ar = ln * wd;
- return ar;
- }
- }
- class Box extends Rectangle {
- private double hei; //set
- private double boxVolume; //get
- private double surfaceArea; //get
- // - Encapsulation
- public void setHei(double hei) {
- this.hei = hei;
- }
- public double getHei() {
- return hei;
- }
- public double getBoxVolume() {
- boxVolume = computeVolume(getLen(), getWid(), getHei());
- return boxVolume;
- }
- public double getSurfaceArea() {
- surfaceArea = computeSurfaceArea(getLen(), getWid(), getHei());
- return surfaceArea;
- }
- // - Constructor
- public Box() {
- }
- public Box(double ln, double wd, double hi) {
- super(ln, wd);
- this.hei = hi;
- }
- private double computeVolume(double ln, double wd, double hi) {
- double vol = computeArea(ln, wd) * hi;
- return vol;
- }
- private double computeSurfaceArea(double ln, double wd, double hi) {
- double sArea = 2 * (computeArea(ln, wd) + computeArea(ln, hi) + computeArea(hi, wd));
- return sArea;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment