Object Oriented Programming Practice 1

Object Oriented Programming Practice 1

Rectangle

Problem

Create a Java class named Rectangle (in a file called Rectangle.java) to represent the concept of a rectangle. The class contains:

  • Two double data fields named width and height that specify the width and height of the rectangle. The default values are 1 for both the width and height.
  • A no-arg constructor that creates a default rectangle.
  • A constructor that creates a rectangle with the specified width and height.
  • A method named getArea() that returns the area of the rectangle.
  • A method named getPerimeter() that returns the perimeter.
  • Write a test program with the main method (in a file called TestRectangle.java) that creates two Rectangle objects, one with a width and height of 1 and another with a width of 4 and a height of 5.5. Display, to standard output, the width, height, area and perimeter of each rectangle. Take a screenshot of the output and save this file. Upload it as part of the lab submission.

Class Rectangle

package Rectangle;

public class Rectangle{
    double width = 1;
    double height = 1;
    
    Rectangle()
    {

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

    double getArea()
    {
        return width * height;
    }

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

TestRectangle

package Rectangle;
public class TestRectangle {

    public static void output(Rectangle r, String times)
    {
        System.out.println("This is the " + times + " rectangle.");
        System.out.println("Width: " + r.width);
        System.out.println("Height: " + r.height);
        System.out.println("Area: " + r.getArea());
        System.out.println("Perimeter: " + r.getPerimeter());
    }
    public static void main(String []args)
    {
        Rectangle r1 = new Rectangle(1,1);
        Rectangle r2 = new Rectangle(4,5.5);
        output(r1,"first");
        output(r2,"second");
    }
}

MyPoint

Problem

Create a class in Java called MyPoint to represent a point in 2-dimensional space with x- and y-coordinates. The class should contain:

  • The data fields x and y to represent the coordinates of the point;
  • A no-arg constructor that creates a point (0,0);
  • A constructor that creates a point with specified coordinates;
  • Two accessor methods that return the values (as doubles) of the coordinates x and y;
  • A method named distance that returns the distance from this point to another point of the MyPoint type; Note the distance between two point $\left( x_1,y_1 \right)$ and $\left( x_2,y_2 \right)$ is: $ \sqrt{(x_2-x_1)^2+(y_2-y_1)^2}$
  • A method named distance that returns the distance from this point to another point specified by its x and y coordinates.
  • Create a class called TestMyPoint.java with the main() method. Inside the main method create a number of MyPoint objects and demonstrate the use of the constructors and both distance methods.

Class MyPoint

package MyPoint;

public class MyPoint {
    private double x;
    private double y;

    MyPoint()
    {
        x = 0;
        y = 0;
    }

    MyPoint(double x, double y)
    {
        this.x = x;
        this.y = y;
    }

    public double getX()
    {
        return this.x;
    }

    public double getY()
    {
        return this.y;
    }

    public double distance(MyPoint point)
    {
        double ans = 0;
        double newX = point.getX() - this.x;
        double newY = point.getY() - this.y;
        newX *= newX;
        newY *= newY;
        ans = newX + newY;
        return Math.sqrt(ans);
    }
}

Class TestMyPoint

package Rectangle;
public class TestRectangle {

    public static void output(Rectangle r, String times)
    {
        System.out.println("This is the " + times + " rectangle.");
        System.out.println("Width: " + r.width);
        System.out.println("Height: " + r.height);
        System.out.println("Area: " + r.getArea());
        System.out.println("Perimeter: " + r.getPerimeter());
    }
    public static void main(String []args)
    {
        Rectangle r1 = new Rectangle(1,1);
        Rectangle r2 = new Rectangle(4,5.5);
        output(r1,"first");
        output(r2,"second");
    }
}

暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇