Java学习总结3

一、实验目的
(1) 掌握类的定义,熟悉属性、构造函数、方法的作用,掌握用类作为类型声明变量和方法返回值;
(2)理解类和对象的区别,掌握构造函数的使用,熟悉通过对象名引用实例的方法和属性;
(3)理解static修饰付对类、类成员变量及类方法的影响。
二、实验内容
1.写一个名为Rectangle的类表示矩形。其属性包括宽width、高height和颜色color, width 和height都是double型的,而color 则是String 类型的。要求该类具有:
(1)使用构造函数完成各属性的初始赋值
(2)使用gt.()和set...()的形式完成属性的访问及修改
(3)提供计算面积的getArea()方法和计算周长的getLength()方法

2.银行的账户记录 Account有账户的唯一-性标识 (11 个长度的字符和数字的组合),用户的姓名,开户日期,账户密码(六位的数字,可以用0开头),当前的余额。银行规定新开一一个账户时,银行方面提供-一个标识符、账户初始密码123456,客户提供姓名,开户时客户可以直接存入一笔初始账户金额,不提供时初始余额为0。定义该类,并要求该类提供如下方法:存款、取款、变更密码、可以分别查询账户的标识、姓名、开户日期、当前余额等信息。

package pta.two;

public class Rectangle {
    private double width;
    private double height;
    private String color;

    public Rectangle() {
    }

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

    public double getWidth() {
        return width;
    }

    public void setWidth(double width) {
        this.width = width;
    }

    public double getHeight() {
        return height;
    }

    public void setHeight(double height) {
        this.height = height;
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }
}

定义一个构造方法和Rectangle类

package pta.two;
import java.util.Random;

public class testRectangleS {
    public static void main(String[] args) {
        Random s=new Random();

        double randomwidth=s.nextDouble();
        System.out.println("随机高:"+randomwidth);
            double randomheigth=s.nextDouble();
        System.out.println("随机宽:"+randomheigth);
        System.out.println("面积="+getArea(randomheigth,randomwidth));

        System.out.println("周长="+getLength(randomheigth,randomwidth));

    }

    public static double getArea(double a,double b) {
double area=a*b;
return area;

    }
    public static double getLength(double c,double d){
        double length=2*c+2*d;
        return length;
    }

在这里我用的是Random类,也即是电脑随机给的数值,并没有自己赋值;
运行结果:

posted on 2019-09-20 01:02  Mi_Yun  阅读(60)  评论(0)    收藏  举报