第四周课程总结&试验报告
实验二 Java简单类与对象
- 实验目的
- 掌握类的定义,熟悉属性、构造函数、方法的作用,掌握用类作为类型声明变量和方法返回值;
- 理解类和对象的区别,掌握构造函数的使用,熟悉通过对象名引用实例的方法和属性;
- 理解static修饰付对类、类成员变量及类方法的影响。
- 实验内容:
- 写一个名为Rectangle的类表示矩形。其属性包括宽width、高height和颜色color,width和height都是double型的,而color则是String类型的。要求该类具有:
(1) 使用构造函数完成各属性的初始赋值
(2) 使用get…()和set…()的形式完成属性的访问及修改
(3) 提供计算面积的getArea()方法和计算周长的getLength()方法
public class Rectangle
{
private double width;
private double 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 double getHeight()
{
return height;
}
public String getColor()
{
return color;
} public void setWidth(double width)
{
this.width = width;
}
public void setHeight(double height)
{
this.height = height;
}
public void setColor(String color)
{
this.color = color;
} public double Area()
{
return width*height;
}
public double Perimeter()
{
return 2*(width+height);
}
}运行结果:
- 银行的账户记录Account有账户的唯一性标识(11个长度的字符和数字的组合),用户的姓名,开户日期,账户密码(六位的数字,可以用0开头),当前的余额。银行规定新开一个账户时,银行方面提供一个标识符、账户初始密码123456,客户提供姓名,开户时客户可以直接存入一笔初始账户金额,不提供时初始余额为0。定义该类,并要求该类提供如下方法:存款、取款、变更密码、可以分别查询账户的标识、姓名、开户日期、当前余额等信息。
实验内容:
package yinghang;
import java.util.Scanner; public class Account
{
public String id;
public int password;
public String name;
public int money;
public String time="2012/08/21 星期天"; public Account(String id, int password, String name, int money)
{
this.id = id;
this.password = password;
this.name = name;
this.money = money;
this.time = time;
} public void show()
{
System.out.println("账户标识:" + id);
System.out.println("姓名: " + name);
System.out.println("开户日期:"+time);
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 adds = sc.nextInt();
if(adds <= money)
{
money= money-adds;
System.out.println("本次取款为: " + adds);
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 void key()
{
System.out.println("设置新密码:"); Scanner sxc=new Scanner(System.in);
int password1=sxc.nextInt();
System.out.println("请再次输入:");
int password2=sxc.nextInt();
if(password1!=password2)
{
System.out.println("您两次输入密码不同!");
while(true)
{
System.out.println("请再次输入:");
int password3=sxc.nextInt();
if(password1==password3)
{
System.out.println("操作成功");
break;
}
}
}
else
System.out.println("操作成功"); }
}
package yinghang;
import java.util.Scanner; public class SSS
{ public static void main(String[] args)
{
Account x = new Account("sss393645216",123456,"鹏",666666666); Scanner sx = 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、退出系统*******"); while(true)
{
int s = sx.nextInt();
int i=0;
switch(s)
{
case 1:
System.out.println("***银行账户信息***");
x.show();
break;
case 2:
System.out.println("***取款操作******");
x.takeMoney();
break;
case 3:
System.out.println("***存款操作******");
x.saveMoney(1000);
break;
case 4:
System.out.println("***修改密码******");
x.key();
break;
case 5:
System.out.println("感谢您的使用!");
System.exit(0);
i=1; break;
default:i=1;break;
}
if(i==1)
break;
} }
}- 运行结果:

实验总结:对于java的掌握还不够熟练,做题还缺乏足够的耐心和思考,还要多钻研
第四周课程总结&试验报告的更多相关文章
- 第四周课程总结&试验报告2
试验报告2 写一个名为Rectangle的类表示矩形.其属性包括宽width.高height和颜色color,width和height都是double型的,而color则是String类型的.要求该类 ...
- 第四周课程总结&试验报告(二)
实验二 Java简单类与对象 实验目的 掌握类的定义,熟悉属性.构造函数.方法的作用,掌握用类作为类型声明变量和方法返回值: 理解类和对象的区别,掌握构造函数的使用,熟悉通过对象名引用实例的方法和属性 ...
- 第五周课程总结&试验报告(三)
第五周课程总结&试验报告(三) 实验三 String类的应用 实验目的 掌握类String类的使用: 学会使用JDK帮助文档: 实验内容 ###1.已知字符串:"this is a ...
- 第四周课程总结&实验报告(二)
Java实验报告(二) 实验二 Java简单类与对象 一. 实验目的 (1) 掌握类的定义,熟悉属性.构造函数.方法的作用,掌握用类作为类型声明变量和方法返回值: (2) 理解类和对象的区别,掌握构造 ...
- 第四周课程总结&实验报告二
第四周课程总结 第四周课程总结 本周重点为学习String;首先String用以创建字符串,且通过有一次课堂练习加强理解到:String 类是不可改变的,一旦创建了 String 对象,那它的值就无法 ...
- 第四周课程总结与第二次实验报告(Java简单类与对象)
1.写一个名为Rectangle的类表示矩形.其属性包括宽width.高height和颜色color,width和height都是double型的,而color则是String类型的.要求该类具有: ...
- 实验报告2&&第四周课程总结
实验报告: 写一个名为Rectangle的类表示矩形.其属性包括宽width.高height和颜色color,width和height都是double型的,而color则是String类型的.要求该类 ...
- 第五周课程总结&试验报告三
第五周课程总结 一.第五周课程总结 1.this关键字 this可用于任何实例方法内指向当前对象,也可指向对其调用当前方法的对象,或者在需要当前类型对象引用时使用.当一个类的属性(成员变量)名与访问该 ...
- 第十四周课程总结&记事本功能的简单实现。
(1)课程总结: 这周简单学习了下JDBC的内容: JDBC API 允许用户访问任何形式的表格数据,尤其是存储在关系数据库中的数据. 执行流程: (1)连接数据源,如:数据库. (2)为数据库传递查 ...
随机推荐
- PAT Basic 1027 打印沙漏 (20 分)
本题要求你写个程序把给定的符号打印成沙漏的形状.例如给定17个“*”,要求按下列格式打印 ***** *** * *** ***** 所谓“沙漏形状”,是指每行输出奇数个符号:各行符号中心对齐:相邻两 ...
- linux运维、架构之路-PHP编译常见报错及解决方法
1. configure: error: xslt-config not found. Please reinstall the libxslt >= 1.1.0 distribution 复制 ...
- 面试题常考&必考之--js中的call()和apply()
apply: 接受两个参数,第一个参数是要绑定给this的值,第二个参数是一个参数数组.当第一个参数为null.undefined的时候,默认指向window. call: 第一个参数是要绑定给thi ...
- codeforces 682C
鸽了两天,还是我太蒟了,mayan游戏调不出来,难题又不会,只有刷水DFS才能勉强维持一下生计这样子,我还是要提高姿势水平啊! 题目描述: 给定一棵树,每条边有边权,每个点有点权,如果某个点到其子节点 ...
- 详解HASH(字符串哈希)
HASH意为(散列),是OI的常用算法. 我们常用哈希的原因是,hash可以快速(一般来说是O(段长))的求出一个子段的hash值,然后就可以快速的判断两个串是否相同. 今天先讲string类的has ...
- 【HDOJ6701】Make Rounddog Happy(启发式合并)
题意:给定一个长为n的序列和k,定义子串[L,R](L<=R)合法当: 1.max(a[L]..a[R])-(R-L+1)<=k 2.[L,R]中没有重复的数字 问合法子串的个数 n,k, ...
- python curl_get-pip.py Installing with get-pip.py
w curl https://bootstrap.pypa.io/get-pip.py > curl_get-pip.pypython curl_get-pip.py https://pip.p ...
- PHP模拟请求和操作响应
模拟请求 fsockopen <?php // 建立连接 $link = fsockopen('localhost', '80'); define('CRLF', "\r\n" ...
- All strings must be XML compatible: Unicode or ASCII, no NULL bytes or control characters
ValueError: All strings must be XML compatible: Unicode or ASCII, no NULL bytes or control character ...
- python字典小知识
字典的小知识dic = {"name": "tom", "age": 23, "price": 110}# 01:提取键 ...