Java第四周總結+實驗報告

實驗二 Java簡單類與對象

實驗目的

掌握類的定義,熟悉屬性、構造函數、方法的做用,掌握用類做爲類型聲明變量和方法返回值;
理解類和對象的區別,掌握構造函數的使用,熟悉經過對象名引用實例的方法和屬性;
理解static修飾付對類、類成員變量及類方法的影響。java

實驗內容

一、寫一個名爲Rectangle的類表示矩形。其屬性包括寬width、高height和顏色color,width和height都是double型的,而color則是String類型的。要求該類具備:數組

(1) 使用構造函數完成各屬性的初始賦值函數

(2) 使用get…()和set…()的形式完成屬性的訪問及修改學習

(3) 提供計算面積的getArea()方法和計算周長的getLength()方法this

二、銀行的帳戶記錄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);
                                            }
 
}

運行截圖:

總結

這周所學及小拓展:code

1、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()方法

用來判斷兩個對象是否同樣,對其所指向的地址也能判斷是否相等,而「==」判等符號只能夠判斷兩個對象的值是否相等。blog

代碼
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好像不是很難,由於我這樣的菜雞居然都能寫出碎片化的代碼了,雖然最終仍是靠着度娘和別人的代碼填滿了本身代碼的窟窿吧。但我已心滿意足,後面的學習過程當中慢慢長進吧!

相關文章
相關標籤/搜索