zoukankan      html  css  js  c++  java
  • 第二次课程总结&学习总结

    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("是否开户?
     请输入:是 	 或者输入:否 
    ");
    		if(input.next().equals("是")){
    			AccountList.create();        //开户,创造一个新的Account对象用来保存用户各类数据
    		}
    		while(true){                     //多次处理用户命令操作
    			System.out.print("账户操作-->变更密码请输入:1;	存款请输入:2;	取款请输入:3;
    查询操作-->查询账户的标识:4;	查询开户日期:5;	查询当前余额:6;	查询姓名:7;
    开户请输入:8;
    退出请输入:0
    ");   //客户端页面
    		    int number=input.nextInt();
    		    if(number>=0&&number<=8){
    		    	corresponding(number);     //处理数字相应的命令
    		    }else{
    			    System.out.print("错误的输入!自动退出!
    "); //如果并不是零到八的数字,就结束所有操作
    			    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("输入您的存款数:
    ");
    			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("输入您的取款数:
    ");
    			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("是否开户?
     请输入:是 	 或者输入:否 
    ");
    		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;如果需要请输入金额:
    ");
    		Scanner input =new Scanner(System.in);
    		double aom=input.nextDouble();
    		if(aom<=0){
    			System.out.print("您的初始金额为0元!
    ");
    		}else{
    			account.setBalance(aom);
    			System.out.print("您的初始金额为"+account.getBalance()+"元!
    ");
    		}
    	}
    	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()+"
    用户自己ID:"+i.getUserid()+"
    用户标识码:"+i.getIdentifier().toString()+"
    开户日期:"+i.getOpeningDate()+"
    账户余额:"+i.getBalance()+"
    用户密码:"+i.getPassword()+"
    ");
    		}
    	}
    	public static String inputName(){      //返回输入的用户名
    		Scanner input =new Scanner(System.in);
    		System.out.print("请输入您的姓名:
    ");
    		String s=input.nextLine().trim();
    		System.out.print("您的姓名为: "+s+"
    ");
    		return s;
    	}
    	public static String inputPassword(){   //返回输入的密码
    		System.out.print("请输入您的密码:
    ");
    		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值,则再引用对象数组时都要对其进行实例化操作。

    问题

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

  • 相关阅读:
    python环境安装selenium和手动下载安装selenium的方法
    下载及安装selenium IDE
    firefox历史版本下载链接
    python 文件操作,os.path.walk()的回调函数打印文件名
    python 文件操作 练习:取得文件的最后存取时间
    python 文件操作 练习:把一个目录下的所有文件名,打印一下,不要包含后缀名
    python excel操作
    python 线程、多线程
    python pip list 命令列出所有安装包和版本信息
    找一款适合阅读的博客主题
  • 原文地址:https://www.cnblogs.com/songzhihaoT1/p/11556439.html
Copyright © 2011-2022 走看看