抬头仰望星空,是否能发现自己的渺小。

伪斜杠青年

人们总是混淆了欲望和理想

JAVA 实验4.2

1

public class Rectangle {
    private double length;
    private double width;

    Rectangle() {
    }

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

    void setLength(double length) {
        this.length = length;
    }

    double getLength() {
        return length;
    }

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

    double getWidth() {
        return width;
    }

    double perimiter() {
        return (width + length) * 2;
    }

    double area() {
        return width * length;
    }
}

主程序:

public class RectDemo {

    public static void main(String[] args) {
        Rectangle rt = new Rectangle(2, 1);
        /*
         * rt.setLength=2; rt.setWidth=1
         * System.out.println(rt.getLength()*rt.getWidth()); rt.setLength(2);
         * rt.setWidth(1);
         */        System.out.println(rt.perimiter());
        System.out.println(rt.area());
    }
}

2

package box;

public class Box {
    private double length, width, height;

    Box() {
    }

    Box(double length, double width, double height) {
        this.length = length;
        this.width = width;
        this.height = height;
    }

    void setLength(double length) {
        this.length = length;
    }

    double getLength() {
        return length;
    }

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

    double getWidth() {
        return width;
    }

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

    double getHeight() {
        return height;
    }

    double volume() {
        return length * width * height;
    }
}

主程序:

package box;

public class BoxDemp {

    public static void main(String[] args) {
        Box mybox = new Box(10, 20, 15);
        /*
         * mybox.setLength=10; mybox.setWidth=20; mybox.setHeight=15;
         * mybox.setLength(10); mybox.setWidth(20); mybox.setHeight(15);
         * System.out.println(mybox.getLength()*mybox.getWidth()*mybox.getHeight());
         */        System.out.println(mybox.volume());
    }
}

题目3

package point;

public class PointDemo {
    int x;
    int y;

    PointDemo(int x, int y) {
        this.x = x;
        this.y = y;
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        System.out.println("问题1:");
        PointDemo start = new PointDemo(10, 10);
        PointDemo end = new PointDemo(20, 30);

        System.out.println("start.x=" + start.x + ",start.y=" + start.y);
        System.out.println("end.x=" + end.x + ",end.y=" + end.y);
        System.out.println("问题2:");
        PointDemo stray = end;
        System.out.println("stray.x=" + stray.x + ",stray.y=" + stray.y);
        System.out.println("end.x=" + end.x + ",end.y=" + end.y);

        System.out.println("问题3:");
        stray.x = 40;
        stray.y = 40;
        System.out.println("end.x=" + end.x + ",end.y=" + end.y);
        System.out.println("stray.x=" + stray.x + ",stray.y=" + stray.y);
        System.out.println("问题4:");
        start.x = 50;
        start.y = 50;
        System.out.println("end.x=" + end.x + ",end.y=" + end.y);
        System.out.println("start.x=" + start.x + ",start.y=" + start.y);
    }
}

题目4:

package bankAccount;

import java.util.Date;
import java.util.Scanner;

public class Account {
    private int id;
    private String name;
    private double balance;
    private long num;
    private long idcard;
    private Date dateCreated;

    public Account() {
        super();
    }

    public Account(int id, String name, long num, double balance, long idcard) {
        super();
        this.id = id;
        this.name = name;
        this.num = num;
        this.balance = balance;
        this.idcard = idcard;
        dateCreated = new Date();
    }

    public int getid() {
        return id;
    }

    public void setid(int id) {
        this.id = id;
    }

    public String getname() {
        return name;
    }

    public void setname(String name) {
        this.name = name;
    }

    public long getnum() {
        return num;
    }

    public void setnum(long num) {
        this.num = num;
    }

    public double getbalance() {
        return balance;
    }

    public void setbalance(double balance) {
        this.balance = balance;
    }

    public long getidcard() {
        return idcard;
    }

    public void getidcard(long idcard) {
        this.idcard = idcard;
    }

    public Date getdateCreated() {
        return dateCreated;
    }

    public void deposit(double amount) {
        balance = balance + amount;
    }

    public void withdraw(double amount) {
        balance = balance - amount;
    }

    public static void main(String[] args) {
        // 初始化帐户
        Account myAccount = new Account(14436242, "Til", 4798888, 100, 201603031);
        // 循环选项
        while (1 == 1) {
            System.out.println("请选择:1。存款 2。取款 3.显示信息");
            Scanner sc = new Scanner(System.in);
            int s = sc.nextInt();

            if (s == 1) {
                System.out.println("请输入存款数");
                Scanner dep = new Scanner(System.in);
                int deposit = dep.nextInt();
                myAccount.deposit(deposit);
                System.out.println("余额为:" + myAccount.getbalance());
            } else if (s == 2) {
                System.out.println("请输入取款数");
                Scanner wit = new Scanner(System.in);
                int withdraw = wit.nextInt();
                myAccount.withdraw(withdraw);
                System.out.println("余额为:" + myAccount.getbalance());
            } else if (s == 3) {
                System.out.println("显示信息");
                System.out.println("卡号为:" + myAccount.getid() + " 名字是:" + myAccount.getname() + " 电话号码:"
                        + myAccount.getnum() + " 余额为:" + myAccount.getbalance() + " 身份证为:" + myAccount.getidcard());
            } else
                System.out.println("输入错误,重新输入!");
        }
    }
}

题目5

package com.tools;

import java.util.Scanner;

public class input {

    input() {
    }

    Scanner read = new Scanner(System.in);

    int readInt() {
        System.out.println("请输入一个整数:");
        return read.nextInt();
    }

    double readDouble() {
        System.out.println("请输入一个浮点数:");
        return read.nextDouble();
    }

    String readString() {
        System.out.println("请输入一个字符串:");
        return read.next();
    }
}

主程序:

package com.tools;

import com.tools.input;

public class main {

    public static void main(String[] args) {
        input se = new input();
        int b = se.readInt();
        double a = se.readDouble();
        String c = se.readString();
        System.out.printf("a=" + a + ",b=" + b + ",c=" + c + ";");
    }
}

本站由以下主机服务商提供服务支持:

0条评论

发表评论