第四周学习总结
1.了解了static修饰的要求
2.学习了属性的定义、如何构造函数、方法的调用和使用
3.银行账户的题目好难啊,理解了很久才勉强写出了代码,代码显示正确,但是运行不了,所以第二个没运行截图
实验二 Java简单类与对象
实验目的
掌握类的定义,熟悉属性、构造函数、方法的作用,掌握用类作为类型声明变量和方法返回值;
理解类和对象的区别,掌握构造函数的使用,熟悉通过对象名引用实例的方法和属性;
理解static修饰付对类、类成员变量及类方法的影响。
实验报告
1.写一个名为Rectangle的类表示矩形。其属性包括宽width、高height和颜色color,width和height都是double型的,而color则是String类型的。要求该类具有:
(1) 使用构造函数完成各属性的初始赋值
(2) 使用get…()和set…()的形式完成属性的访问及修改
(3) 提供计算面积的getArea()方法和计算周长的getLength()方法
package test2;
class Rectangle {
private double height;
private double width;
private String color;
public Rectangle(){
}
public Rectangle(double width,double height,String color){
this.setColor(color);
this.setHeight(height);
this.setWidth(width);
}
public void setHeight(double height) {
this.height = height;
}
public void setWidth(double width) {
this.width = width;
}
public void setColor(String color) {
this.color = color;
}
public double getHeight() {
return height;
}
public double getWidth() {
return width;
}
public String getColor() {
return color;
}
public double getArea(){
double area;
area=this.height*this.width;
return area;
}
public double getLength(){
double length;
length=width+height+width+height;
return length;
}
};
public class Demo1 {
public static void main(String args[]) {
Rectangle rec=null;
rec=new Rectangle(3.0f,4.0f, "红色");
System.out.println(rec.getArea());
System.out.println(rec.getLength());
System.out.println("长:"+rec.getHeight());
System.out.println("宽:"+rec.getWidth());
}
}
运行截图:
2.银行账号
package test3;
import java.util.Scanner;
class Account{
private String id;
private String name;
private int time,password;
private double money;
public Account(){
}
public Account(String id,String name,int time,int password,double money){
this.setId(id);
this.setName(name);
this.setTime(time);
this.setPassword(password);
this.setMoney(money);
}
public void setId(String s){
id=s;
}
public void setName(String n){
name=n;
}
public void setTime(int m){
time=m;
}
public void setPassword(int e){
password=e;
}
public void setMoney(double c){
money=c;
}
public String getId(){
return id;
}
public String getName(){
return name;
}
public int getTime(){
return time;
}
public int getPassword(){
return password;
}
public double getMoney(){
return money;
}
public void showup(){
System.out.println("账户名:"+id);
System.out.println("姓名:"+name);
System.out.println("开户时间:"+time);
System.out.println("账户密码:"+password);
System.out.println("账户余额:"+money);
}
public void qukuan(){
while(true){
Scanner sc = new Scanner(System.in);
System.out.println("请输入密码:");
int pass = sc.nextInt();
if(pass==password){
System.out.println("取款金额:");
int sum = sc.nextInt();
if(sum<=money){
money=money-sum;
System.out.println("余额:"+money);
}
else{
System.out.println("余额不足,请重新输入金额");
}
break;
}
else
{
System.out.println("密码错误,请再次输入");
}
}
}
public void cunqian(int balance){
money=money+balance;
System.out.println("存入金额:"+balance);
System.out.println("余额:"+money);
}
public static void main(String agrs[]){
Account acc =new Account("chenxin0527","陈新",19990527,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:密码服务");
System.out.println("5:退出");
int g = sc.nextInt();
switch(g){
case 1:
System.out.println("账户基本信息");
acc.showup();
case 2:
System.out.println("取款");
acc.qukuan();
case 3:
System.out.println("存款");
acc.cunqian(0);
case 4:
case 5:
System.exit(0);
break;
}
}
}
原文地址:https://www.cnblogs.com/Vennien/p/11559263.html