Java实验报告

班级 计算机科学与技术一班 学号 20188390 姓名 宋志豪

实验

  1. 写一个名为Rectangle的类表示矩形。其属性包括宽width、高height和颜色color,width和height都是double型的,而color则是String类型的。要求该类具有:

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

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

    (3) 提供计算面积的getArea()方法和计算周长的getLength()方法。

实验源码

package 矩形;

import java.util.Scanner;

public class Rectangle {
private double width;
private double high;
private String color;
public Rectangle(String color) {
this.color = color;
}
public Rectangle(double width, double high) {
this.width = width;
this.high = high;
}
public Rectangle(double width, double high, String color) {
this.width = width;
this.high = high;
this.color = color;
}
public double getWidth() {
return width;
}
public void setWidth(double width) {
this.width = width;
}
public double getHigh() {
return high;
}
public void setHigh(double high) {
this.high = high;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public double getArea(){
return getWidth()*getHigh();
}
public double getLength(){
return 2*(getWidth()+getHigh());
}
public static void main(String [] args){
Scanner input=new Scanner(System.in);
double width=input.nextDouble();
double high=input.nextDouble();
String color=input.next();
Rectangle rectangle=new Rectangle(width,high,color);
System.out.print("面积为: "+rectangle.getArea()+", "+"周长为: "+rectangle.getLength()+", 颜色为: "+rectangle.getColor());
}
}

实验结果

实验

  1. 银行的账户记录Account有账户的唯一性标识(11个长度的字符和数字的组合),用户的姓名,开户日期,账户密码(六位的数字,可以用0开头),当前的余额。银行规定新开一个账户时,银行方面提供一个标识符、账户初始密码123456,客户提供姓名,开户时客户可以直接存入一笔初始账户金额,不提供时初始余额为0。定义该类,并要求该类提供如下方法:存款、取款、变更密码、可以分别查询账户的标识、姓名、开户日期、当前余额等信息。

实验源码

package 银行;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.Scanner; public class User { //客户端类
static int user=0; //当前客户在AccountList中的编号
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input =new Scanner(System.in);
System.out.print("是否开户?\n 请输入:是 \t 或者输入:否 \n");
if(input.next().equals("是")){
AccountList.create(); //开户,创造一个新的Account对象用来保存用户各类数据
}
while(true){ //多次处理用户命令操作
System.out.print("账户操作-->变更密码请输入:1;\t存款请输入:2;\t取款请输入:3;\n查询操作-->查询账户的标识:4;\t查询开户日期:5;\t查询当前余额:6;\t查询姓名:7;\n开户请输入:8;\n退出请输入:0\n"); //客户端页面
int number=input.nextInt();
if(number>=0&&number<=8){
corresponding(number); //处理数字相应的命令
}else{
System.out.print("错误的输入!自动退出!\n"); //如果并不是零到八的数字,就结束所有操作
break;
}
}
AccountList.printfAll(); //输出所有的Account对象的内容
}
public static void corresponding(int number){ //处理数字相应的命令
if(number==8){
opening(); //开户方法
return;
}
int i=trueOrFalse(number); //返回用户是否在AccountList表中有序号,有则返回正确的序号
if(i==-1)return;
if(number==1){
System.out.println("接下来输入您的新密码--->");
AccountList.List.get(i).setPassword(AccountList.inputPassword()); //设置密码为新输入的密码
System.out.println("密码修改完成!");
}else if(number==2){
System.out.print("输入您的存款数:\n");
int blance=AccountList.inputBlance(); //获取输入的存款值
if(blance>0){
AccountList.List.get(i).setBalance(AccountList.List.get(i).getBalance()+blance); //存款大于零就能存入
System.out.print("存款成功!");
}else{
System.out.print("错误的存款数!");
}
}else if(number==3){
System.out.print("输入您的取款数:\n");
int blance=AccountList.inputBlance(); //获取输入的取款值
if(blance>0&&blance<=AccountList.List.get(i).getBalance()){ //小于存款值并大于零就可以取款
AccountList.List.get(i).setBalance(AccountList.List.get(i).getBalance()-blance); // 改变存款值
System.out.println("取款成功!");
}else{
System.out.print("错误的取款数!");
}
}else if(number==4){
System.out.println("您的账户标识为:"+AccountList.List.get(i).getIdentifier());
}else if(number==5){
System.out.println("您的开户日期为:"+AccountList.List.get(i).getOpeningDate());
}else if(number==6){
System.out.println("您的当前余额为:"+AccountList.List.get(i).getBalance());
}else if(number==7){
System.out.println("您的姓名为:"+AccountList.List.get(i).getName());
}
}
public static void opening(){ //开户
Scanner input =new Scanner(System.in);
System.out.print("是否开户?\n 请输入:是 \t 或者输入:否 \n");
String s=input.next();
if(s.equals("是")){
AccountList.create(); //创建一个新的Account对象用来保存用户各类数据
}else if(s.equals("否")){
System.out.println("谢谢使用!");
}else{
System.out.println("请确认您的输入正确!");
}
}
public static int trueOrFalse(int number) //返回用户是否在AccountList表中有序号,有则返回正确的序号
{
if(number==0){
System.out.print("已安全退出!");
System.exit(1);
}else{
System.out.println("是否改变账户?");
Scanner input =new Scanner(System.in);
String s=input.nextLine().trim();
if(s.equals("是")){
int i=AccountList.search(AccountList.inputName()); //判断是否有输入的用户,有则返回大于等于零的序号
if(i!=-1){
if(AccountList.inputPassword().equals(AccountList.List.get(i).getPassword())){ //判断密码是否正确
user=i; //当前更改后的用户的序号
return i;
}else{
System.out.println("密码错误!请重新启动!");
return -1;
}
}else{
System.out.println("没有此用户!请重试!");
return -1;
}
}else if(s.equals("否")){
return user; //继续使用当前用户的序号
}else{
System.out.println("错误的输入!请新开始!");
}
}
return 0;
}
}
class Account { //保存用户的各类数据,不能直接访问,以免造成糟糕的后果
private static double id=1; //用户共享的内部id
private double userid; //用户本人的id
private String identifier; //用户唯一的标识码
private String name; //用户名
private String openingDate; //用户的开户日期
private String password="123456";//用户初始密码
private double balance=0; //用户初始余数
public Account(String name){ //构造函数,确定不可更改的用户id,唯一标识符,开户日期。
this.name=name;
this.userid=id;
this.setOpeningDate();
this.setUniqueid();
id++;
}
public double getUserid() {
return userid;
}
public String getOpeningDate() {
return openingDate;
}
public String getIdentifier() {
return identifier;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
private void setUniqueid(){ //设置唯一的标识符
if(id>=100){
this.identifier="uniqueid"+(int)id;
}else if(id>=10){
this.identifier="uniqueid0"+(int)id;
}else{
this.identifier="uniqueid00"+(int)id;
}
}
private void setOpeningDate(){ //不可更改的开户日期
Date now=new Date();
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
this.openingDate=sdf.format(now);
}
}
class AccountList{ //保存所有用户各类数据的表
static ArrayList<Account> List=new ArrayList<Account>(); //Account对象类型的数组表
public static void create(){ //创造一个新的Account
Account account=new Account(AccountList.inputName()); //开户需要用户名,银行提供其他的数据
List.add(account);
System.out.print("请问您要存入初始资金吗?如果不需要请输入数字0;如果需要请输入金额:\n");
Scanner input =new Scanner(System.in);
double aom=input.nextDouble();
if(aom<=0){
System.out.print("您的初始金额为0元!\n");
}else{
account.setBalance(aom);
System.out.print("您的初始金额为"+account.getBalance()+"元!\n");
}
}
public static int search(String name){ //在现有的AccountList中寻找是否有这个用户,找到返回其序号,否则返回-1
int i;
for(i=0;i<List.size();i++){
if(List.get(i).getName().equals(name))
return i;
}
return -1;
}
public static void printfAll(){ //输出所有的用户信息
for(Account i:List){
System.out.println("用户名字:"+i.getName()+"\n用户自己ID:"+i.getUserid()+"\n用户标识码:"+i.getIdentifier().toString()+"\n开户日期:"+i.getOpeningDate()+"\n账户余额:"+i.getBalance()+"\n用户密码:"+i.getPassword()+"\n");
}
}
public static String inputName(){ //返回输入的用户名
Scanner input =new Scanner(System.in);
System.out.print("请输入您的姓名:\n");
String s=input.nextLine().trim();
System.out.print("您的姓名为: "+s+"\n");
return s;
}
public static String inputPassword(){ //返回输入的密码
System.out.print("请输入您的密码:\n");
Scanner input =new Scanner(System.in);
while(true){
String password=input.nextLine().trim();
char [] ch=password.toCharArray();
if(ch.length!=6){
System.out.println("不符合规定的密码!请重试!");
}else{
return password;
}
}
}
public static int inputBlance(){ //返回输入的存取款值
Scanner input =new Scanner(System.in);
return input.nextInt();
}
}

实验结果

大概思路:矩形的封装思路没什么好说的,肯定都会的,很简单的东西。

关于银行系统的思路很清晰:1,--User类给用户操作,可以实现各种功能,2—Account类,用于保存用户开户后的各类数据及基本操作。3—AccountList类,各种中间操作实现体。

课程总结

1、String类

实例化String类对象有两种方式

1.直接为string类赋值。

2.调用String类中的构造方法进行实例化。

两种实例化方法的区别

第一种赋值的弊端就是所有的对象共用一个地址,而第二个方法开辟了多个空间

2、等价符号==比较的是对象的地址,

equals()方法比较的则是对象的内容。

3、String类中的字符串内容不可修改。

4、String类中的常用方法



5、对象数组

所谓对象数组,就是包含了一组相关的对象数组,数组一定要先开辟空间,因为其是引用数据类型,所以数组每一个对象都是null值,则再引用对象数组时都要对其进行实例化操作。

问题

自己的实际运用还是有很大的问题,第二题实验的知识上课都有学习,但是在使用的时候就出现了不知道怎么用,用在哪儿,用在那儿会不会有什么错误一概不知,最后还是请教室友李代传才磕磕盼盼的做出来。

第二次课程总结&学习总结的更多相关文章

  1. 【课程汇总】OpenHarmony 成长计划知识赋能第二期课程(附链接)

    OpenHarmony 开源开发者成长计划第二期知识赋能直播课程以入门为主,共设置 8 节课,覆盖了应用开发.设备开发.内核驱动等多个技术领域.带领开发者快速了解如何玩转 OpenHarmony.如何 ...

  2. 《Java程序设计与数据结构教程(第二版)》学习指导

    <Java程序设计与数据结构教程(第二版)>学习指导 欢迎关注"rocedu"微信公众号(手机上长按二维码) 做中教,做中学,实践中共同进步! 原文地址:http:// ...

  3. 第二次作业——C++学习

    课程选择: 以往在自学的过程就比较留意一些自学的网站,所以这次"C++自学"感觉找课程还是比较轻松的. 因为之前网页等学习都是在慕课网(视频学习个人感觉有时挺费时间的,特别是有时以 ...

  4. HTML5课程大纲/学习路线

    HTML5课程大纲/学习路线 这是什么? 这个一个HTML技术路线的课程大纲/学习大纲. 你能用它做什么? 如果你是找工作的人, 利用本大纲, 你可以学习HTML5语言, 做一个HTML前端工程师, ...

  5. Cocos2d-x课程大纲/学习路线

    Cocos2d-x课程大纲/学习路线 这是什么? 这个一个Cocos2d-x技术路线的课程大纲/学习大纲. 你能用它做什么? 如果你是找工作的人, 利用本大纲, 你可以学习Cocos2d-x, 做一个 ...

  6. Yeslab现任明教教主数据中心第二门课程UCS 视频教程下载

    Yeslab现任明教教主数据中心第二门课程UCS 视频教程下载 视频教程目录 Yeslab现任明教教主数据中心第二门课程UCS.1.介绍UCS.rar Yeslab现任明教教主数据中心第二门课程UCS ...

  7. OCM_第二天课程:Section1 —》配置 Oracle 网络环境

    注:本文为原著(其内容来自 腾科教育培训课堂).阅读本文注意事项如下: 1:所有文章的转载请标注本文出处. 2:本文非本人不得用于商业用途.违者将承当相应法律责任. 3:该系列文章目录列表: 一:&l ...

  8. 《C语言程序设计基础1》第二学期第一周学习总结

    **<C语言程序设计基础1>第二学期第一周学习总结 一. 本周学习内容总结 一维数组,了解了一维数组的定义(定义一个数组,需要明确数组变量名,数组元素的类型和数组大小,即数组中元素的数量) ...

  9. 《python基础教程(第二版)》学习笔记 文件和素材(第11章)

    <python基础教程(第二版)>学习笔记 文件和素材(第11章) 打开文件:open(filename[,mode[,buffering]]) mode是读写文件的模式f=open(r' ...

随机推荐

  1. C++中的字符数组、字符指、字符串针(腾讯)

    一.字符数组 1.定义时进行初始化的方式 (1)char c[12]={'I',' ','a','m',' ','h','a','p','p','y'};//最后两个元素自动补‘\0’(不是空格),其 ...

  2. 【NOIP2016提高A组模拟9.15】Math

    题目 分析 因为\((-1)^2=1\), 所以我们只用看\(\sum_{j=1}^md(i·j)\)的值模2的值就可以了. 易证,一个数x,只有当x是完全平方数时,d(x)才为奇数,否则为偶数. 那 ...

  3. Codeforces 962D Merge Equals ( 模拟 )

    题意 : 给出一个序列,然后每次将重复出现的元素进行求和合并(若有多个,则优先取最小的进行合并),若某重复元素有很多,那么取最左边的那两个进行合并且合并后元素位于原来右边元素的位置,例如 3 2 6 ...

  4. Codeforces Round #345 (Div. 2) E. Table Compression 并查集+智商题

    E. Table Compression time limit per test 4 seconds memory limit per test 256 megabytes input standar ...

  5. python中加入中文注释报错处理

    python中加入中文注释,运行报错如下 解决方法: 在py文件的第一行加入   #coding:utf-8  即可

  6. sqli-labs(27)

    0X01 先查询闭合 ?id=' 报错 ?id='' 正确 知道是’的闭合语句 0X02那么开始我们的注入之旅 空格过滤了 尝试一下%0a绕过  #也被过滤了 那么用and '1'='1构造闭合 ?i ...

  7. Java并发编程的艺术笔记(二)——wait/notify机制

    一.概述 一个线程修改了一个对象的值,另一个线程感知到变化从而做出相应的操作.前者是生产者,后者是消费者. 等待/通知机制,是指一个线程A调用了对象O的wait()方法进入等待状态,而另一个线程B调用 ...

  8. Getting CFNetwork SSLHandshake failed (-9806) error

    平常个人打测试包一切OK,今天突然不能联网了 How to handle "CFNetwork SSLHandshake failed" in iOS 参考1 Getting CF ...

  9. modern php笔记---php (性状)

    modern php笔记---php (性状) 一.总结 一句话总结: trait是和继承一个层次的东西 一个类use MyTrait;后,trait中的方法覆盖父类方法,当前类中的方法覆盖trait ...

  10. git && github 相关

    权限问题(error: The requested URL returned error: 403 Forbidden while accessing):1. 将自己机器的ssh public key ...