实验报告2
题一
package test2;
public class Rectangle {
private double width,height;
private String color;
public Rectangle(double width,double height,String color){
this.setWidth(width);
this.setHeight(height);
this.setColor(color);
}
public double getWidth(){
return width;
}
public void setWidth(double w){
width = w;
}
public double getHeight(){
return height;
}
public void setHeight(double h){
height = h;
}
public String getColor(){
return color;
}
public void setColor(String c){
color = c;
}
public void getArea(){
double area=0;
area=this.height*this.width;
System.out.println(area);
}
public void getLength(){
double length;
length=width+height+width+height;
System.out.println(length);
}
}
package test2;
public class demo{
public static void main (String args[]){
Rectangle rec=new Rectangle(12.00,14.00,"红色");
rec.getArea();
rec.getLength();
rec.getColor();
System.out.println(rec.toString());
}
}
实验二
package test1;
import java.util.Scanner;
import test1.Account;
public class Account {
public int id;
public int password;
public String name;
public int money;
public Account(int id, int password, String name, int money) {
this.id = id;
this.password = password;
this.name = name;
this.money = money;
}
public void show(){
System.out.println("账户:" + id);
System.out.println("姓名:" + name);
System.out.println("余额:" + money);
}
public void takeMoney(){
while(true){
Scanner sc = new Scanner(System.in);
System.out.println("输入密码");
int pass = sc.nextInt();
if(pass == password){
System.out.println("取款金额:");
int withdrawals = sc.nextInt();
if(withdrawals <= money) {
money= money-withdrawals;
System.out.println("余额为:" + money);
}
else {
System.out.println("当前余额不足" );
}
break;
}
else{
System.out.println("密码错误,请重新输入!");
}
}
}
public void saveMoney(int moneys){
money = money+moneys;
System.out.println("此次存款为:" + moneys);
System.out.println("账户余额为:" + money);
}
public static void main(String[] args) {
Account acc = new Account(10010,123456,"陈果",0);
Scanner sc = new Scanner(System.in);
System.out.println("请输入需要执行的操作");
System.out.println("1银行账户信息");
System.out.println("2取款操作");
System.out.println("3存款操作");
System.out.println("4退出系统");
int s = sc.nextInt();
switch(s) {
case 1:
System.out.println("银行账户");
acc.show();
break;
case 2:
System.out.println("取款");
acc.takeMoney();
break;
case 3:
System.out.println("存款");
acc.saveMoney(1000);
break;
case 4:
System.exit(0);
break;
}
}
}