加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

第四周课程总结

发布时间:2020-12-15 07:41:42 所属栏目:Java 来源:网络整理
导读:实验二 Java简单类与对象 实验目的 掌握类的定义,熟悉属性、构造函数、方法的作用,掌握用类作为类型声明变量和方法返回值; 理解类和对象的区别,掌握构造函数的使用,熟悉通过对象名引用实例的方法和属性; 理解static修饰付对类、类成员变量及类方法的影

实验二 Java简单类与对象

实验目的
掌握类的定义,熟悉属性、构造函数、方法的作用,掌握用类作为类型声明变量和方法返回值;
理解类和对象的区别,掌握构造函数的使用,熟悉通过对象名引用实例的方法和属性;
理解static修饰付对类、类成员变量及类方法的影响。
实验内容
写一个名为Rectangle的类表示矩形。其属性包括宽width、高height和颜色color,width和height都是double型的,而color则是String类型的。要求该类具有:

(1) 使用构造函数完成各属性的初始赋值。

(2) 使用get…()和set…()的形式完成属性的访问及修改。

(3) 提供计算面积的getArea()方法和计算周长的getLength()方法。
实验代码
···
package 实验2;

public class Rectangle {
public static void main(String args[]) {
Rectangle rec=new Rectangle();
rec.setWidth(3);
rec.setHeight(4);
rec.setColor("红色");
rec.getArea();
rec.getLength();
System.out.println("长:"+rec.getWidth()+"n高:"+rec.getHeight()+"n颜色:"+rec.getColor());
}

double width,height;
String color="red";        
public double getHeight() {
    return height;                
}
public double getWidth() {
    return width;
}
public String getColor() {
    return color;
}
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 void getArea() {
    double area=0;
    area=this.height*this.width;
    System.out.println("面积为"+area);
}
public void getLength() {
    double length=0;
    length=(this.height+this.width)*2;
    System.out.println("周长为"+length);
}

}
···

2.银行的账户记录Account有账户的唯一性标识(11个长度的字符和数字的组合),用户的姓名,开户日期,账户密码(六位的数字,可以用0开头),当前的余额。银行规定新开一个账户时,银行方面提供一个标识符、账户初始密码123456,客户提供姓名,开户时客户可以直接存入一笔初始账户金额,不提供时初始余额为0。定义该类,并要求该类提供如下方法:存款、取款、变更密码、可以分别查询账户的标识、姓名、开户日期、当前余额等信息
··
实验代码
···
package 实验2;
import java.util.Scanner;
public class Banksystem{
private String name,date="2019.9.20";
private int key=123456,money=0;
private String ccount="Barcelona";
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public int getKey() {
return key;
}
public void setCipher(int key) {
this.key = key;
}
public int getMoney() {
return money;
}
public void setMoney(int money) {
this.money = money;
}
public String getCcount() {
return ccount;
}
public void setCcount(String ccount) {
this.ccount = ccount;
}
public void username(){
System.out.print("用户名称:");
Scanner username = new Scanner(System.in);
String name = username.nextLine();
setName(name);
}
public void ChangePWD(){
System.out.print("请输入您要更改的密码:");
Scanner userkey = new Scanner(System.in);
int key = userkey.nextInt();
setCipher(key);
}
public void all(){
System.out.print("用户名:"+name+"n开户日期:"+date+"n账号:"+ccount+"n密码:"+key+"n余额:"+money+"n");
}
public void Depositsandwithdrawals(){
System.out.print("存款:1n"+"取款:0n");
System.out.print("请选择:");
Scanner j = new Scanner(System.in);
int i = j.nextInt();
if(i==0) {
System.out.print("取款数:");
Scanner usermoney = new Scanner(System.in);
int money = usermoney.nextInt();
money=this.money-money;
System.out.print("操作成功!当前余额:"+money);
setMoney(money);
}
else {
System.out.print("存款数:");
Scanner usermoney = new Scanner(System.in);
int money = usermoney.nextInt();
money=this.money+money;
System.out.print("操作成功!当前余额:"+money);
setMoney(money);
}
}
public static void main(String[] args) {
System.out.print(" Welcomen");
System.out.print("进入系统请按:1n");
System.out.print("请输入你要操作的序号:");
Scanner j = new Scanner(System.in);
int J = j.nextInt();
int I=0;
Banksystem user = new Banksystem();
for(int k=1;k>0;) {
if(J==1||I==1) {
System.out.print("开通用户:1n"+"更改密码:2n"+"查询信息:3n"+"存款取款:4n");
System.out.print("请输入你要操作的序号:");
Scanner b = new Scanner(System.in);
int B = b.nextInt();
if(B==1) {
user.username();
}else if(B==2){
user.ChangePWD();
}else if(B==3){
user.all();
}else if(B==4){
user.Depositsandwithdrawals();
}
System.out.print("返回主页:1n"+"退出系统:0n");
System.out.print("请输入你要操作的序号:");
Scanner i = new Scanner(System.in);
I = i.nextInt();
J=0;
}else{
break;
}
}
System.out.print("已安全退出,感谢使用!");
}
}
···
实验结果

学习总结: 这次的实验主要是让我们掌握类的定义,掌握用类作为类型声明变量和方法返回值,理解类和对象的区别,其中,构造方法的名称必须与类名称一致;构造方法的声明处不能有任何返回值类型的声明;不能在构造方法中使用return返回一个值。还有就是熟悉属性、构造函数、方法的作用;掌握构造函数的使用,熟悉通过对象名引用实例的方法和属性;理解static修饰付对类、类成员变量及类方法的影响。 这两道题目对我来言都有点难搞,第一题在同学的帮助下勉强解决了。第二题是真心不懂借鉴了别人的,目前还在理解借鉴作品中。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读