zoukankan      html  css  js  c++  java
  • 第四周课程总结&试验报告(二)

    实验二 Java简单类与对象

    实验目的
    掌握类的定义,熟悉属性、构造函数、方法的作用,掌握用类作为类型声明变量和方法返回值;
    理解类和对象的区别,掌握构造函数的使用,熟悉通过对象名引用实例的方法和属性;
    理解static修饰付对类、类成员变量及类方法的影响。
    实验内容
    (一)写一个名为Rectangle的类表示矩形。其属性包括宽width、高height和颜色color,width和height都是double型的,而color则是String类型的。要求该类具有:
    (1) 使用构造函数完成各属性的初始赋值
    (2) 使用get…()和set…()的形式完成属性的访问及修改
    (3) 提供计算面积的getArea()方法和计算周长的getLength()方法

    1.实验代码

    public class Rectangle {
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		private String color;
    		private double width,height;
    		public Rectangle (String color, double width,double height){
    		this.setColor(color);
    		this.setWidth(width);
    		this.setHeight(height);
    		}
    		public double getWidth() {
    			return width;
    		}
    		public void setWidth(double x) {
    			width=x;
    		}
    		public double getHeight() {
    			return height;
    		}
    		public void setHeight(double y) {
    			height=y;
    		}
    		public String getColor() {
    			return color;
    		}
    		public void setColor(String color2) {
    			color=color2;
    		}
    		
    		public void double getArea() {
    			System.out.println("面积:"+getWidth()*getHeight());
    		}
    		public void  getLength() {
    			System.out.println("周长:"+2*(getWidth()+getHeight()));
    		}
    		
    		public void getColor1(){                                   
    	         System.out.println("颜色:"+getColor());
    	    }
    		
    	}
    	 class Rectangle {
    		public static void main(String[] args) {
    			public class Rectangle  {
    				 Rectangle rec1= new  Rectangle("red",8.00,2.00);
    				 re.getArea();         
    		         re.getLength();
    		         re.getColor1();
    		     }
    		 }
    					
    		}
    
    }
    

    2.运行截图

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

    package test;
    import java.util.Scanner;
    class Account{
      private String id;
      private String name;
      private int time,password;
      private double money;
      public Account(){
    }
      public Account(String id,String name,int time,int password,double money){
      this.setId(id);
      this.setName(name);
      this.setTime(time);
      this.setPassword(password);
      this.setMoney(money);
    }
     public void setId(String s){
      id=s;
    }
     public void setName(String n){
      name=n;
    }
     public void setTime(int m){
      time=m;
    }
    public void setPassword(int e){
      password=e;
    }
    public void setMoney(double c){
      money=c;
    }
    public String getId(){
      return id;
    }
    public String getName(){
      return name;
    }
    public int getTime(){
      return time;
    }
    public int getPassword(){
      return password;
    }
    public double getMoney(){
      return money;
    }
    public void showup(){
     System.out.println("账户名:"+id);
     System.out.println("姓名:"+name);
     System.out.println("开户时间:"+time);
     System.out.println("账户密码:"+password);
     System.out.println("账户余额:"+money);
    }
    public void qukuan(){
     while(true){
     Scanner sc =new Scanner(System.in);
     System.out.println("请输入密码:");
     int pass = sc.nextInt();
     if(pass==password){
     System.out.println("取款金额:");
     int sum = sc.nextInt();
     if(sum<=money){
     money=money-sum;
    System.out.println("余额:"+money);
    }
        else{
      System.out.println("余额不足,请重新输入金额");
    }
    break;
    }
    else
    {
    System.out.println("密码错误,请再次输入");
    }
    }
    }
    public void cunqian(int balance){
     money=money+balance;
     System.out.println("存入金额:"+balance);
     System.out.println("余额:"+money);
    }
    public static void main(String agrs[]){
    Account acc =new Account("chenxin0527","陈新",19990527,123456,0);
    Scanner sc = 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:退出");
    int g = sc.nextInt();
    switch(g){
    case 1:
        System.out.println("账户基本信息");
         acc.showup();
     
    case 2:
       System.out.println("取款");
       acc.qukuan();
    case 3:
       System.out.println("存款");
        acc.cunqian(0);
       
    case 4:
    case 5:
        System.exit(0);
     break;
    }
    }
    }
    

    2.运行截图

    写的代码和别人差不多,可就是运行不出。

  • 相关阅读:
    laravel 多对多 belonsToMany
    C语言union关键字
    FW:程序在内存的划分(转)
    操作系统:进程/线程同步的方式和机制,进程间通信
    FW:考查嵌入式C开发人员的最好的16道题(转)
    操作系统死锁产生、条件、和解锁
    100层高楼摔2个鸡蛋的问题?
    【转】看完这个你的位运算学得就差不多了
    函数递归的几个例子
    如何查看服务器(linux系统)当前的负载信息(转)
  • 原文地址:https://www.cnblogs.com/zuoshuai/p/11560164.html
Copyright © 2011-2022 走看看