使用类的概念编写Java程序来计算矩形的面积和周长

王林
王林 转载
2023-09-03 11:37:06 688浏览

使用类的概念编写Java程序来计算矩形的面积和周长

Java语言是当今世界上最常用的面向对象编程语言之一。 的概念是面向对象语言中最重要的特性之一。一个就像一个对象的蓝图。例如,当我们想要建造一座房子时,我们首先创建一份房子的蓝图,换句话说,我们创建一个显示我们将如何建造房子的计划。根据这个计划,我们可以建造许多房子。同样地,使用类,我们可以创建许多对象。类是创建许多对象的蓝图,其中对象是真实世界的实体,如汽车、自行车、笔等。一个类具有所有对象的特征,而对象具有这些特征的值。在本文中,我们将使用类的概念编写一个Java程序,以找到矩形的周长和面积。

一个类包括以下内容−

  • 数据成员 - 数据成员表示对象集合的特征/属性

  • 方法 - 方法表示对象执行的操作。

举个例子,如果我们把一个人看作是一个类,那么像姓名、年龄、地址这样的属性就是数据成员,而像坐、站、吃、走这样的动作就是类的方法。

创建类的语法

class ClassName
{
   //data members
   //methods
}

类名始终以大写字母开头。例如,Person(人),House(房屋),Bank(银行)等。

示例

class Person{
   //data members
   String name;
   int age;
   String city;
   //methods
   void read(){
      System.out.println(“Reading”);
   }
}

创建对象的语法

ClassName objectname = new ClassName();

示例

Person person_one =new Person();

矩形的周长

矩形的周长是由矩形的四边围成的总面积,即由矩形的长度和宽度所覆盖的面积。

公式

Perimeter of the rectangle 
= area covered by the sides of the rectangle
= 2(l+w)
where,  l : length of rectangle
        w : width of rectangle

矩形的面积

矩形的面积是在二维平面上由矩形所占据的总空间。

公式

Area of the rectangle 
= area covered by the rectangle
=  l*w
where , l : length of rectangle
             w : width of rectangle

算法

步骤 1 − 创建一个自定义类名为 Rectangle,该类具有“area()”和“perimeter()”方法。这些函数分别给出了矩形的面积和周长作为输出。

步骤 2 − 现在,在主类中使用构造函数创建一个矩形对象。

步骤 3 − 现在调用相应的函数,使用创建的对象找到矩形的面积和周长。

示例

在这个例子中,我们创建了一个自定义的Rectangle类,它具有“area()”和“perimeter()”方法。然后,在主类中使用主类的构造函数创建一个Rectangle类的对象,并在创建的对象上调用相应的方法area()和perimeter()。一旦方法被调用,它们就会被执行并打印输出。

// Java program to calculate  the area and perimeter of a rectangle using class concept
import java.util.*;
// Rectangle Class File
class Rectangle {
    // data members
    int length, width;
    // methods
    //constructor to create Object
    Rectangle(int length, int width) {
        this. length = length;
        this.width = width;
    }
    // prints the area of rectangle
    public void area() {
        int areaOfRectangle;
        areaOfRectangle = this.length * this.width;
        System.out.println("Area of rectangle with the given input is : " + areaOfRectangle);
    }
    // prints the perimeter of rectangle
    public void perimeter() {
        int  perimeterOfRectangle;
        perimeterOfRectangle = 2 * (this.length + this.width);
        System.out.println("Perimeter of rectangle with the given input is : " + perimeterOfRectangle);
    }
}
public class Main {
    public static void main(String args[]) {
        Rectangle rect_obj = new Rectangle(10,5);  // obect creation
        System.out.println("Length = " + rect_obj.length);
        System.out.println("Width = " + rect_obj.width);
        rect_obj.area(); // returns area of rectangle
        rect_obj.perimeter(); //returns perimeter of rectangle
    }
}

输出

Length = 10
Width = 5
Area of rectangle with the given input is : 50
Perimeter of rectangle with the given input is : 30

时间复杂度:O(1) 辅助空间:O(1)

因此,在本文中,我们学习了如何使用类的概念来实现Java代码,以找到矩形的面积和周长。

以上就是使用类的概念编写Java程序来计算矩形的面积和周长的详细内容,更多请关注php中文网其它相关文章!

声明:本文转载于:tutorialspoint,如有侵犯,请联系admin@php.cn删除