2021-11-16【JAVA】【实验三:类与对象】

2021-11-16【JAVA】【实验三:类与对象】,第1张

2021-11-16【JAVA】【实验三:类与对象】
  1. 编程创建一个矩形类,在其中定义两个变量表示一个矩形的长、宽,再定义一个方法setDemo对这两个个变量进行初始化,然后定义两个个方法求矩形得周长和面积。创建一个对象,求给定尺寸的矩形的体积。
import java.util.Scanner;
public class Rectangle {
    private int length;
    private int width;
    private int height;
    public void setDemo(int length, int width,int height){
        this.length = length;
        this.width = width;
        this.height = height;
    }
    public int area(){
        return length * width;
    }
    public int perimeter(){
        return 2 * ( length + width );
    }
    public int volume(){
        return length * width * height;
    }
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入长:");
        int length = scanner.nextInt();
        System.out.println("请输入宽:");
        int width = scanner.nextInt();
        System.out.println("请输入高:");
        int heigth = scanner.nextInt();
        Rectangle rectangle = new Rectangle();
        rectangle.setDemo(length,width,heigth);
        System.out.println("周长为:"+rectangle.perimeter());
        System.out.println("面积为:"+rectangle.area());
        System.out.println("体积为:"+rectangle.volume());
    }
}


  1. 将上题的方法setDemo改用构造函数实现初始化。
package com.company;
import java.util.Scanner;
public class Rectangle1 {
    private int length;
    private int width;
    private int height;
    Rectangle1(int a,int b,int c){
        length = a;
        width = b;
        height = c;
    }
    public int area(){
        return length * width;
    }
    public int perimeter(){
        return 2 * ( length + width );
    }
    public int volume(){
        return length * width * height;
    }
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入长:");
        int length = scanner.nextInt();
        System.out.println("请输入宽:");
        int width = scanner.nextInt();
        System.out.println("请输入高:");
        int heigth = scanner.nextInt();
        Rectangle1 rectangle = new Rectangle1(length,width,heigth);
        System.out.println("周长为:"+rectangle.perimeter());
        System.out.println("面积为:"+rectangle.area());
        System.out.println("体积为:"+rectangle.volume());
    }
}


  1. 定义一个圆形类,做1、2中得事情。
package com.company;
import java.util.Scanner;
public class Round {
    private double radius;
    final static double PI=3.1415926;
    public void setDemo(double radius){
        this.radius = radius;
    }
    public double area(){
        return PI * radius * radius;
    }
    public double perimeter(){
        return 2 * PI * radius;
    }
    public double volume(){
        return 4*PI*radius*radius*radius/3;
    }
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入半径:");
        double radius = scanner.nextDouble();
        Round round = new Round();
        round.setDemo(radius);
        System.out.println("周长为:"+round.perimeter());
        System.out.println("面积为:"+round.area());
        System.out.println("体积为:"+round.volume());
    }
}

构造函数:

package com.company;
import java.util.Scanner;
public class Round1 {
    private double radius;
    final static double PI=3.1415926;
    public Round1(double a){
        radius = a;
    }
    public double area(){
        return PI * radius * radius;
    }
    public double perimeter(){
        return 2 * PI * radius;
    }
    public double volume(){
        return 4*PI*radius*radius*radius/3;
    }
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入半径:");
        double radius = scanner.nextDouble();
        Round1 round = new Round1(radius);
        System.out.println("周长为:"+round.perimeter());
        System.out.println("面积为:"+round.area());
        System.out.println("体积为:"+round.volume());
    }
}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5522307.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-13
下一篇 2022-12-13

发表评论

登录后才能评论

评论列表(0条)

保存