第四周课程总结及实验报告
实验二 Java简单类与对象 (1) 使用构造函数完成各属性的初始赋值 (2) 使用get…()和set…()的形式完成属性的访问及修改 (3) 提供计算面积的getArea()方法和计算周长的getLength()方法 实验代码package text2; public class Rectangle { private double width; private double height; private String color; 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; } public double getArea() { return width*height; } public double getLength() { return (width+height)*2; } } package text2; public class Text2 { public static void main(String[] args) { // TODO Auto-generated method stub Rectangle per=null; per=new Rectangle(5,10,"red"); System.out.println("width:"+per.getWidth()); System.out.println("height:"+per.getHeight()); System.out.println("color:"+per.getColor()); System.out.println("面积:"+per.getArea()); System.out.println("周长:"+per.getLength()); } } 运行结果2.银行的账户记录Account有账户的唯一性标识(11个长度的字符和数字的组合),用户的姓名,开户日期,账户密码(六位的数字,可以用0开头),当前的余额。银行规定新开一个账户时,银行方面提供一个标识符、账户初始密码123456,客户提供姓名,开户时客户可以直接存入一笔初始账户金额,不提供时初始余额为0。定义该类,并要求该类提供如下方法:存款、取款、变更密码、可以分别查询账户的标识、姓名、开户日期、当前余额等信息。 实验代码package text3; import java.util.Scanner; import java.util.Date; public class Account { private String id; private String name; private Date date; private int password; private int money; public Account(String id,String name,int money) { super(); this.id = id; this.name = name; this.money = money; this.date = new Date(); this.password = 123456; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public void jin(int num) { this.money = this.money+num; } public void chu(int num) { this.money = this.money-num; } public Date getDate() { return date; } public void setDate(Date date) { this.date = date; } public int getMoney() { return money; } public void setMoney(int money) { this.money = money; } public int getPassword() { return password; } public void setPassword(int password) { this.password = password; } public void mima() { Scanner sc=new Scanner(System.in); int password=sc.nextInt(); this.password = password; sc.close(); } } package text3; public class Text3 { public static void main(String[] args) { // TODO Auto-generated method stub Account per=null; per=new Account("happyday123","Alice",0); per.jin(2000); per.chu(500); per.mima(); System.out.println("标识符:"+per.getId()); System.out.println("姓名:"+per.getName()); System.out.println("日期:"+per.getDate()); System.out.println("余额:"+per.getMoney()); System.out.println("密码:"+per.getPassword()); } } 运行结果实验过程在这次实验的过程中加深了对于构造方法的理解,和对于getter和setter的了解,对于第一个题目,做起来还好,但是第二个题目,知道要干嘛但不知道怎么下手,这是还是对于类不是特别明白的原因,还有构造方法,但是还是学到了不少,例如date,对于日期的一个写法,还有就是改变密码的值,但也不是完全理解了。继续加油吧 总结:本周主要是学习了String类一、学习了两种为String赋值的方法; String str="Alice"; 2) String str=new String("Alice"); 二、String里的“==”是进行地址之间的比较,而不能直接比较内容,如果要比较内容需要需要使用到方法。public boolean equals(String str); 还有学习了对象数组类 对象数组名称[]=new 类[数组长度] 数据类型 数组名称[]=null; 数组名称=new 数据类型[长度]; 包的基本概念,用法包的定义:package 包名称.子包名称; javac -d . 名称.java 类的导入主要用 import. import 包名称.子包名称.类名称;或 import 包名称.子包名称.*; //自动加载所需类 还了解Java的常用系统包。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |