Java类与对象实验

一、实验目的

1、掌握类的声明和构造方法,掌握了对象的创建方法与创建机制;

2、掌握了成员变量、类变量和局部变量区别,理解了成员方法与类方法区别。

3、掌握函数重载的方法;

4、理解4种访问修饰符的区别,提高了Java语言的编程能力。

二、实验要求

1、 根据实验步骤中提出的要求,编写相应的Java程序;

2、 编译、运行自己所编写的Java程序;

3、 根据编译与运行过程中所获得的错误信息修改程序直至获得正确的结果;

4、 记录实验中遇到的各类问题以及解决办法。

三、实验步骤及结果

1.【矩形类Rectangle】

1)题目要求:设计一个名为Rectangle的类表示矩形。这个类包括:

两个名为width和height的double型private成员变量,它们分别表示矩形的宽和高。width和height的默认值都为1。

1)创建矩形的无参构造方法。

2)创建width和height为指定值的矩形的构造方法。

3)一个名为getArea()的public方法返回这个矩形的面积,返回值为double型。

4)一个名为getPerimeter()的public方法返回矩形周长,返回值为double型。

编写一个测试程序,创建两个Rectangle对象:一个矩形的宽为4高为40,另一个矩形的宽为3.5高为35.9。依次显示每个矩形的宽、高、面积和周长。

注意:public前缀+号,private前缀-号,protected前缀是#号,私有类型是~号

2)编写源程序文件,代码如下。

class Rectangle{
    private double width;
    private double height;

    public Rectangle(){}

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

    public double getArea(){
        return width*height;
    }

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

    public double getWidth(){
        return  width;
    }

    public double getHeight() {
        return height;
    }
}

public class Test {
    public static void main(String[] args) {
        Rectangle a = new Rectangle(4.0,40.0);
        Rectangle b = new Rectangle(3.5,35.9);
        System.out.printf("a的宽:%.2f,a的高:%.2f,a的面积:%.2f,a的周长:%.2f\n",a.getWidth(),a.getHeight(),a.getArea(),a.getPerimeter());
        System.out.printf("b的宽:%.2f,b的高:%.2f,b的面积:%.2f,b的周长:%.2f\n",b.getWidth(),b.getHeight(),b.getArea(),b.getPerimeter());
    }
}

3)编译运行程序,其结果如图所示。

 

2. 【风扇类Fan】

1)题目要求:设计一个名为Fan的类表示一个风扇。这个类包括:

(1)三个名为SLOW、MEDIUM和FAST而值为1、2、3的常量表示风扇的速度。

(2)一个名为speed的int类型私有数据域表示风扇的速度(默认值为SLOW)。

(3)一个名为on的boolean类型私有数据域表示风扇是否打开(默认值为false)。

(4)一个名为radius的double类型私有数据域表示风扇的半径(默认值为5)。

(5)一个名为color的String类型数据域表示风扇的颜色(默认值为blue)。

(6)给出四个数据域的访问器(getter)和修改器(setter)。

(7)一个创建默认风扇的无参构造方法。

(8)一个名为toString()的方法返回描述风扇的字符串。如果风扇是打开的,那么该方法在一个组合的字符串中返回风扇的速度、颜色和半径。如果风扇没有打开,该方法返回一个由“fan is off”和风扇颜色、半径组成的字符串。

实现这个类。编写一个测试程序,创建两个Fan对象。将第一个对象设置为最大速度、半径为10、颜色为yellow、状态为打开。将第二个对象设置为中等速度、半径为5、颜色为blue、状态为关闭。通过调用它们的toString方法显示这些对象。

2)编写源程序文件,代码如下。

public class Fan {
    final int SLOW=1,MEDIUM=2,FAST=3;
    private int speed;
    private boolean on;
    private double radius;
    public String color;

    public Fan(){
        speed=SLOW;
        on=false;
        radius=5;
        color="blue";
    }
    public void setSpeed(int speed){
        this.speed=speed;
    }
    public void setOn(boolean on){
        this.on=on;
    }
    public void setRadius(double radius){
        this.radius=radius;
    }
    public void setColor(String color){
        this.color=color;
    }
    public int getSpeed(){
        return speed;
    }
    public boolean getOn(){
        return on;
    }
    public double getRadius(){
        return radius;
    }
    public String getColor(){
        return color;
    }
    public String toString(){
        if(on){
            return "\tSpeed: " + speed + "\n\tColor: " + color + "\n\tRadius: " + radius;
        }
        else {
            return "\tfan is off\n\tColor: " + color + "\n\tRadius: " + radius;
        }
    }
}

public class Test {
    public static void main(String[] args) {
        Fan fan1 = new Fan();
        fan1.setSpeed(3);
        fan1.setRadius(10);
        fan1.setColor("yellow");
        fan1.setOn(true);
        System.out.println("fan1:\n"+fan1.toString());

        Fan fan2 = new Fan();
        fan2.setSpeed(2);
        fan2.setRadius(5);
        fan2.setColor("blue");
        fan2.setOn(false);
        System.out.println("fan2:\n"+fan2.toString());
    }
}

3)编译运行程序,其结果如图所示。

 

3、【位置类】

1)题目要求:设计一个名为Location的类,定位二维数组中的最大值及其位置。这个类包括公共的数据域row、column和maxValue,二维数组中的最大值及其下标用double型的maxValue以及int型的row和column存储。

编写下面的方法,返回一个二维数组中最大值的位置,返回值是一个Location的实例。

public static Location locateLargetst(double[][] a)

编写一个测试程序,提示用户输入一个二维数组,然后显示这个数组中的最大元素及下标。运行实例如下:

输入二维数组的行数和列数: 3 4

输入数组:

23.5 35 2 10

4.5 3 45 3.5

35 44 5.5 9.6

最大元素及其下标是: 45 在(1,2)

  

2)编写源程序文件,代码如下。

public class Location {
    public int row;
    public int column;
    public double maxValue;

    public static Location locateLargetst(double[][] a){
        Location loc = new Location();
        loc.maxValue=a[0][0];
        for(int i=0;i<a.length;i++){
            for(int j=0;j<a[i].length;j++){
                if(loc.maxValue<a[i][j]){
                    loc.maxValue=a[i][j];
                    loc.row=i;
                    loc.column=j;
                }
            }
        }
        return loc;
    }
}


import java.util.Scanner;

public class Test {
    public static void main(String[] args) {
       Scanner sc = new Scanner(System.in);
        System.out.println("请输入数组的行、列数");
        int row = sc.nextInt();
        int column = sc.nextInt();
        System.out.println("请输入数组元素");
        double a[][]=new double[row][column];
        for(int i=0;i<row;i++)
            for(int j=0;j<column;j++)
                a[i][j]= sc.nextDouble();
                Location myloc = Location.locateLargetst(a);
                System.out.print("最大值为: "+myloc.maxValue+" 位置: ("+myloc.row+","+myloc.column+")");
    }
}

3)编译运行程序,其结果如图所示。

 

 

  • 2
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值