Define a class Rectangle with all required instance variables with appropriate data type

huangapple go评论58阅读模式
英文:

Define a class Rectangle with all required instance variable with appropriate data type

问题

class Rectangle {

double length, width;

Rectangle() {
    length = 1;
    width = 1;
}

Rectangle(double length, double width) {
    this.length = length;
    this.width = width;
}

double getArea() {
    return (length * width);
}

double getPerimeter() {
    return (2 * (length + width));
}

}

public class TestRectangle {

public static void main(String[] args) {
    
    Rectangle rect1 = new Rectangle();
    
    Rectangle rect2 = new Rectangle(15.0, 8.0);
    
    System.out.println("Area of first object=" + rect1.getArea());
    
    System.out.println("Perimeter of first object=" + rect1.getPerimeter());
    
    System.out.println("Area of second object=" + rect2.getArea());
    
    System.out.println("Perimeter of second object=" + rect2.getPerimeter());
}

}

英文:
  1. Define a class Rectangle with all required instance variable with appropriate data types. Write all set and get methods, area and perimeter, in addition to use defined constructor(s).
    Also write a constructor that will work as a default constructor.

  2. Develop a Java application that will sue the class Rectangle and create two rectangle objects using constructors. Show that you understand the manipulation of the rectangle objects using set and get method, printing the area, and perimeter of the rectangle etc.

this is what i could do but i need more help

class FindRectangleArea
{
    public static void main(String arg[])
    {
        Rectangle rect = new Rectangle(11, 5);
        
        System.out.println("Length = " + rect.length);
        System.out.println("Breadth = " + rect.breadth);
        System.out.println("Area = " + rect.getArea());
        System.out.println("Perimeter = " + rect.getPerimeter());
    
    }
}

class Rectangle
{
    double length;
    double breadth;

    Rectangle(double length, double breadth)
    {
        this.length = length;
        this.breadth = breadth;
    }

    double getArea()
    {
        return length * breadth;
    }

    double getPerimeter()
    {
        return 2 * (length + breadth);
    }
}

no.2

package testrectangle;

class Rectangle{
    
    double length, width;

    Rectangle()
    
    {
        length = 1;
        
        width = 1;
        
    }
    
    
    Rectangle(double length, double width)
            
            
    {
        this.length = length;
        
        this.width  = width;
        
    }
    
    // define a method
    double getArea()
            
    {
        return (length * width);
        
    }
    
    double getPerimeter()
            
    {
        return (2 * (length + width));
    }
}
public class TestRectangle {

    public static void main(String[] args) {
        
        
        Rectangle rect1 = new Rectangle();
        
        
        Rectangle rect2= new Rectangle(15.0,8.0);
        
        System.out.println("Area of first object="+rect1.getArea());
        
        System.out.println("Perimeter of first object="+rect1.getPerimeter());
        
        System.out.println("Area of second object="+rect2.getArea());
        
        System.out.println("Perimeter of second object="+rect2.getPerimeter());
   }
    
}

答案1

得分: 0

你离目标很近了希望这能帮助你请研究`toString()`的使用方法重写和`String.format`,或者如果你不熟悉可以在`Application`类中使用字符串连接来替代如果你打算提交这个确保你完全理解它

    // 部分 1
    // "定义一个带有所有必需实例变量的Rectangle类,使用适当的数据类型"
    public class Rectangle {
        private int length;
        private int breadth;
    
        // "还要编写一个作为默认构造函数工作的构造函数。"
        public Rectangle(){
        }
    
        // "除了使用自定义构造函数之外"
        public Rectangle(int length, int breadth){
            this.length = length;
            this.breadth = breadth;
        }
    
        // "编写所有的设置和获取方法"
        public int getLength() {
            return length;
        }
    
        public void setLength(int length) {
            this.length = length;
        }
    
        public int getBreadth() {
            return breadth;
        }
    
        public void setBreadth(int breadth) {
            this.breadth = breadth;
        }
    
        // "面积和周长"
        public int getArea(){
            return length * breadth;
        }
    
        public int getPerimeter(){
            return (length + breadth) * 2;
        }
    
        @Override
        public String toString() {
            return String.format("length [%s], breadth [%s], area [%s], perimeter [%s]", length, breadth, getArea(), getPerimeter());
        }
    }

    // 部分 2
    // "开发一个Java应用程序,将使用Rectangle类。"
    public class Application {
        public static void main(String[] args) {
            // "并使用构造函数创建两个矩形对象"
            Rectangle rectangle1 = new Rectangle(1, 1);
            Rectangle rectangle2 = new Rectangle(5, 10);
            System.out.println(rectangle1);
            System.out.println(rectangle2);
            // "展示你理解对矩形对象的操作,使用设置和获取方法,打印矩形的面积和周长等。"
            rectangle1.setLength(20);
            rectangle1.setBreadth(30);
            System.out.println(rectangle1);
        }
    }
英文:

You're really close. Hopefully this helps. Look into the use of toString(), method overriding and String.format, or just replace it with String concatenation in the Application class if you're out of your comfort zone. If this is something you're going to submit, make sure you understand it in full.

// Part 1
// "Define a class Rectangle with all required instance variable with appropriate data types"
public class Rectangle {
private int length;
private int breadth;
// "Also write a constructor that will work as a default constructor."
public Rectangle(){
}
// "in addition to use defined constructor(s)"
public Rectangle(int length, int breadth){
this.length = length;
this.breadth = breadth;
}
// "Write all set and get methods"
public int getLength() {
return length;
}
public void setLength(int length) {
this.length = length;
}
public int getBreadth() {
return breadth;
}
public void setBreadth(int breadth) {
this.breadth = breadth;
}
// "area and perimeter"
public int getArea(){
return length * breadth;
}
public int getPerimeter(){
return (length + breadth) * 2;
}
@Override
public String toString() {
return String.format("length [%s], breadth [%s], area [%s], perimeter [%s]", length, breadth, getArea(), getPerimeter());
}
}
// Part 2
// "Develop a Java application that will sue the class Rectangle."
public class Application {
public static void main(String[] args) {
//" and create two rectangle objects using constructors"
Rectangle rectangle1 = new Rectangle(1, 1);
Rectangle rectangle2 = new Rectangle(5, 10);
System.out.println(rectangle1);
System.out.println(rectangle2);
// "Show that you understand the manipulation of the rectangle objects using set and get method, printing the area, and perimeter of the rectangle etc."
rectangle1.setLength(20);
rectangle1.setBreadth(30);
System.out.println(rectangle1);
}
}

huangapple
  • 本文由 发表于 2020年9月25日 05:38:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/64054778.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定