• Java第四周总结+实验报告


    实验二 Java简单类与对象

    实验目的

    掌握类的定义,熟悉属性、构造函数、方法的作用,掌握用类作为类型声明变量和方法返回值;
    理解类和对象的区别,掌握构造函数的使用,熟悉通过对象名引用实例的方法和属性;
    理解static修饰付对类、类成员变量及类方法的影响。

    实验内容

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

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

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

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

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

    实验过程

    第①题实验代码:

    package o;
    public class a
    {
        public static void main(String[] args)
        {
        	 
        	 Rectangle re = new Rectangle();
             re.get();
             double a =re.getArea();
             System.out.println(a);
             double b =re.getLength();
             System.out.println(b);
         }
     }
    
     class Rectangle {
         double width;
         double heigh;
         String color;
    
         Rectangle() {
             this.width=3;
             this.heigh=4;
             this.color="Red";
         }
    
         public void get(){
             System.out.println("rec "+this.width);
             System.out.println("higher "+this.heigh);
             System.out.println("color "+this.color);
         }
    
         public void setwidth(double single) {
             this.width=single;
         }
    
         public void setheigh(double single) {
             this.heigh=single;
         }
    
         public void setcolor(String single) {
             this.color=single;
         }
    
         public double getArea() {
             return this.width*this.heigh;
         }
    
         public double getLength(){
             return this.width*1+this.heigh*2;
         }
     }
    
    
    

    运行截图:

    第②题实验代码:

    
    package o;
    public class a
    {
        public static void main(String[] args)
        {
    	
    	 Account re = new Account("boniu","hl",2019,9,20,"123456",2000);
         re.setAll();
         re.changebalance(0.05);
         re.setAll();
         re.changepsd("123456");
         re.setAll();
                           }
                              }
    
         class Account 
         
           {
                     String ID;           
                     String psd;                
                     int year;
                     String name;          
                     double balance;         
                     int month;
                     int day;
                     
             Account()
                     {
                         this.ID="boniu";
                         this.name="null";
                         this.year=0;
                         this.month=0;
                         this.day=0;
                         this.psd="null";
                         this.balance=0;
                                              }
    
                 Account(String ID, String name, int year, int month, int day, String psd, double balance)
                 
                    {
          
                	 this.ID       =   ID;
                     this.name     =   name;
                     this.year     =   year;
                     this.month    =   month;
                     this.day      =   day;
                     this.psd      =   psd;
                     this.balance  =   balance;
                     
                      }
    public void changebalance(double single) 
                     
       {
                     this.balance += single;
                                               }
    
     public void changepsd(String single)
       {
         this.psd = single;
                                               }
    
     public void setAll()
       
     {
         System.out.println(this.ID);
         System.out.println(this.name);
         System.out.println(this.year+"."+this.month+"."+this.day);
         System.out.println("balance:"+this.balance);
                                                }
     
    }
    
    

    运行截图:

    总结

    这周所学及小拓展:

    一、java中的String类

    ①字符加密

    代码
    package o;
    import java.util.Scanner;
    public class a {
           public static void main(String args[]){
            Scanner in=new Scanner(System.in);
            System.out.print("输入字符串:");
            String str=in.next();
            int i;    
               char b[]=new char[50];           
               System.out.print("加密后为:");            
               for(i=0;i<str.length();i++)  
              {
                   b[i]=str.charAt(i);       
                   b[i]+=3;            
                   if(b[i]-'a'>25) 
                       b[i]-=26;               
                   System.out.print(b[i]); 
              } 
       }
    }
    
    运行截图

    将输入的字符串中的每个字符加3,输出,实现字串的加密,大于26的字符,减去26返回。

    ②String equals()方法

    用来判断两个对象是否一样,对其所指向的地址也能判断是否相等,而“==”判等符号只可以判断两个对象的值是否相等。

    代码
    
    public class StringEquals
     {
     
           public static void main(String[] args)
     {
           
              String s1=new String("Hello");
              String s2=new String("Hello");
              System.out.println(s1==s2);
              System.out.println(s1.equals(s2));
              String s3="Hello";
              String s4="Hello";
              System.out.println(s3==s4);
              System.out.println(s3.equals(s4));
           
       }
            }
    
    

    当分别new两个对象时,这两个对象所指向的存储地址不一样,开辟了新的存储单元,而“”只能判断值是否相等,所以第一个会输出错误,而equals()判断两个对象是否完全一样,包括值和地址,第二个输出true。当只String两个相同字符串时,这两个字符串指向的是同一个地址,即用“”和equals()都输出true。

    ③String类的一些使用说明

    CharAt() 获取指定位置字符

    Length() 取字串的长度

    GetChars() 获取制定未知的字串复制到字符串数组中。

    toUpperCase(),toLowerCase)() 大小写的转换

    trim() 去除头尾空格

    Replace() 字串替换

    toCharArray() 字符串转换为字符数组。

    小总结:这周主要是“重”做了一下上次作业的7个题目,以为能一气呵成的写下去,结果边写边出现一片一片的红叉叉。在室友的帮助下我还是完成了三个题目,为什么看别人写的代码,看得懂,自己写的时候却总是错呢?...这周的题目我觉得emmmm好像不是很难,因为我这样的菜鸡竟然都能写出碎片化的代码了,虽然最终还是靠着度娘和别人的代码填满了自己代码的窟窿吧。但我已心满意足,后面的学习过程中慢慢长进吧!

  • 相关阅读:
    关于android sdk 与 Eclipse 的一些错误的解决注意事项
    Java2D范例 ——创建一副地图
    大河奔流的精神 ——俞敏洪
    利用异或进行两个数的交换
    Android中怎么使图片显示
    JDK源码学习笔记——HashSet LinkedHashSet TreeSet
    JDK源码学习笔记——TreeMap及红黑树
    HashMap与Hashtable
    JDK源码学习笔记——LinkedHashMap
    JDK源码学习笔记——HashMap
  • 原文地址:https://www.cnblogs.com/Duya9ian/p/11553450.html
Copyright © 2020-2023  润新知