栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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());
    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/530497.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号