湖南第一师范学院信息科学与工程系实验报告
课程名称:Java程序设计成绩评定:
实验项目名称:Java简单类与对象指导教师:
学生姓名:学号:专业班级:
实验项目类型:基础实验地点:实验时间:
实验目的:
1. 掌握类的定义,熟悉属性、构造函数、方法的作用,掌握用类作为类型声
明变量和方法返回值;
2. 理解类和对象的区别,掌握构造函数的使用,熟悉通过对象名引用实例的方法和属性;
3. 理解方法中的形参和实参传递在传递基本类型和引用类型时的差异;
4. 理解static修饰付对类、类成员变量及类方法的影响。
实验内容:
1.下面的程序运行后输出是什么?
class TestReference{
public void change(int num){
num = num + 1;
}
public static void main(String[] args){
int x=2;
TestReference tr = new TestReference();
System.out.print(x);
tr.change(x);
System.out.print(x);
}
}
输出:22
2. 写一个名为Rectangle的类表示矩形。其属性包括宽width、高height和颜
色color,width和height都是double型的,而color则是String类型的。要求该类具有:
(1)使用构造函数完成各属性的初始赋值
(2)使用get…()和set…()的形式完成属性的访问及修改
(3)提供计算面积的getArea()方法
public class Rectangle{
private String color;
private double width;
private double height;
public Rectangle(String color,double width,double height)
{
super();
this.color=color;
this.width=width;
this.height=height;
}
public String getcolor()
{
return this.color;
}
public void setwidth(double width)
{
this.width=width;
}
public double getArea()
{
return this.width*this.height;
}
public static void main(String[] args)
{
Rectangle a=new Rectangle();
a.color=("blue");
a.width=(10.0);
a.height=(4.0);
System.out.println("矩形面积为:"+a.getArea());
System.out.printf("矩形的长为:%d,矩形的宽为:%d,矩形的颜色为:%s",a.height,a.width,a.color);
}
}
调试出错
D:\Java\Rectangle.java:35: 找不到符号
符号:构造函数Rectangle()
位置:类Rectangle
Rectangle a=new Rectangle();
3.一副牌Deck有52张扑克Card组成(不含王牌),每张牌有自己的牌型suit (用char类型)和点数rank(用String类型),补充完整下面的类的定义。public class Card {
public Card( ) {
this.suit = suit;
this.rank = rank;
}
public String toString() {
return suit+rank;
}
public static void main(String[] args){
Card c=new Card('红',"10");
System.out.println(c);
}
}
完整代码:
public class Card {
private char suit;
private String rank;
public Card(char suit, String rank) {
this.suit = suit;
this.rank = rank;
}
public String toString() {
return suit+rank;
}
public static void main(String[] args){
Card c=new Card('红',"10");
System.out.println(c);
}
}
输出:
4.写出程序运行结果,如有错误,指出原因并改正class StaticDemo {
static int x;
int y;
static{
x=10;
}
public static int getX() {
return x;
}
public static void setX(int newX) {
x = newX;
}
public int getY() {
return y;
}
public void setY(int newY) {
y = newY;
}
public static void main(String[] args) {
System.out.println("静态变量x="+StaticDemo.getX());
System.out.println("实例变量y="+StaticDemo.getY());
StaticDemo a= new StaticDemo();
StaticDemo b= new StaticDemo();
a.setX(1);
a.setY(2);
b.setX(3);
b.setY(4);
System.out.println("静态变量a.x="+a.getX());
System.out.println("实例变量a.y="+a.getY());
System.out.println("静态变量b.x="+b.getX());
System.out.println("实例变量b.y="+b.getY());
}
}
错误:无法从静态上下文中引用非静态方法getY()
改正:将System.out.println("实例变量y="+StaticDemo.getY());删除掉
输出:
5.银行的账户记录Account有账户的唯一性标识(11个长度的字符和数字的组合),用户的姓名,开户日期,账户密码(六位的数字,可以用0开头),当前的余额。银行规定新开一个账户时,银行方面提供一个标识符、账户初始密码123456,客户提供姓名,开户时客户可以直接存入一笔初始账户金额,不提供时初始余额为0。定义该类,并要求该类提供如下方法:存款、取款、变更密码、可以分别查询账户的标识、姓名、开户日期、当前余额等信息。
import java.util.Date;
import java.util.Scanner;
public class Bank {
private String id;
private String name;
private Date createTime;
private String mima;
private int balance;
public amount(String id,String name, int balance){
}
super();
this.id=id;
https://www.wendangku.net/doc/4112900968.html,=name;
this.balance=balance;
this.createTime=new Date();
this.mima="123456";
}
public void deposit(int amount){
this.balance+=amount;
}
public void withdraw(int amount){
this.balance-=amount;
}
public Date getCreateTime()
{
return createTime;
}
public void setCreateTime( Date createTime){
this.createTime=createTime;
}
public void changemima(){
Scanner sc=new Scanner(System.in);
System.out.println("请输入新的密码:");
String mima=sc.nextString();
this.mima=new mima;
}
public static void main(String[] args){
Bank a=new Bank("1234567890","luxi",1000);
a.deposit(100);
a.withdraw(150);
a.changemima();
a.createTime();
System.out.println("账户账号:"+a.getId());
System.out.printf("账户姓名: "+a.getName());
System.out.println("账户日期: "+a.getCreateTime());
System.out.println("账户余额: "+a.getAmount());
}
}
实验体会:
做实验写程序的时候,出现的错误很多,许多问题自己解决不了。如方法声明无效和非法类型、需要 <标识符>、需要为 class、interface 或 enum等,不知道问题出在哪里。类变量和对象变量的定义和使用弄不清楚。