201771010131孔維瀅《面向對象程序設計(java)》第四周學習總結

 學習目標:java

  1. 掌握類與對象的基礎概念,理解類與對象的關係;
  2. 掌握對象與對象變量的關係;
  3. 掌握預約義類的基本使用方法,熟悉Math類、String類、math類、Scanner類、LocalDate類的經常使用API;
  4. 掌握用戶自定義類的語法規則,包括實例域、靜態域、構造器方法、更改器方法、訪問器方法、靜態方法、main方法、方法參數的定義要求;(重點、難點)
  5. 掌握對象的構造方法、定義方法及使用要求;(重點)
  6. 理解重載概念及用法;
  7. 掌握包的概念及用法;
  8. 瞭解文檔註釋技術;

第一部分:理論知識學習部分數據結構

    1.類:類是構造對象的模板或藍圖。由類構造對象的過程稱爲建立類的實例;函數

    2.對象:重點——①對象的行爲②對象的狀態③對象標識學習

    3.類與對象的關係:類是對象的抽象,而對象是類的具體實例。類是抽象的,不佔用內存,而對象是具體的,佔用存儲空間。類是用於建立對象的藍圖,它是一個定義包括在特定類型的對象中的方法和變量的軟件模板。測試

    4.使用預約義類Math類(數學庫函數),math類(大整數、大浮點數),String類,Scanner類spa

    5.用戶自定義類,實例域、靜態域、構造器方法、更改器方法、訪問器方法、靜態方法、main方法、方法參數。設計

第二部分:實驗四3d

    1、實驗目的與要求orm

      (1) 理解用戶自定義類的定義;對象

      (2) 掌握對象的聲明;

      (3) 學會使用構造函數初始化對象;

      (4) 使用類屬性與方法的使用掌握使用;

      (5) 掌握package和import語句的用途。

    2、實驗內容和步驟 

      實驗1 測試如下程序,掌握文件輸入輸出程序設計技術

import java.io.*;
import java.util.*;
public class FileWriteReadTest {
	public static void main(String[] args) throws IOException{
			//寫入文件演示
		PrintWriter out = new PrintWriter("myfile.txt");
		out.println("姓名 高數 Java 數據結構 平均成績 總成績");
		out.println("張三 20 30 40 0 0");
		out.println("李四 50 60 70 0 0");
		out.close();//輸出完畢,須要close
			//讀入文件演示
		Scanner in = new Scanner(new File("myfile.txt"));//爲myfile.txt這個File建立一個掃描器in
		int number = 1;//行號
		System.out.println(in.nextLine());
		while(in.hasNextLine()){//判斷掃描器是否還有下一行未讀取,該循環把文件的每一行都讀出
			String line = in.nextLine();//讀出myfile.txt的下一行
			System.out.print("第"+(++number)+"行的內容: ");			
			Scanner linescanner = new Scanner(line);//行內容創建掃描器
			linescanner.useDelimiter(" ");//使用空格做爲分隔符
			String name = linescanner.next();
			String math = linescanner.next();
			String java = linescanner.next();
			String ds = linescanner.next();
			String avg = linescanner.next();
			String total = linescanner.next();
			System.out.println("name="+name+"  math="+math+"  java="+java+"  ds="+ds+"  avg"+avg+"  total="+total);
		}
		in.close();//讀入完畢,最後須要對其進行close。
	}	
}

  實驗結果:

      實驗2 導入第4章示例程序並測試。

        測試程序1:

public class EmployeeTest {
           public static void main(String[] args)
           {
              // fill the staff array with three Employee objects
              Employee[] staff = new Employee[3];

              staff[0] = new Employee("Carl Cracker", 75000, 1987, 12, 15);
              staff[1] = new Employee("Harry Hacker", 50000, 1989, 10, 1);
              staff[2] = new Employee("Tony Tester", 40000, 1990, 3, 15);

              // raise everyone's salary by 5%
              for (Employee e : staff)
                 e.raiseSalary(5);

              // print out information about all Employee objects
              for (Employee e : staff)
                 System.out.println("name=" + e.getName() + ",salary=" + e.getSalary() + ",hireDay="
                       + e.getHireDay());
           }
}

  

import java.time.LocalDate;

class Employee
{
   private String name;
   private double salary;
   private LocalDate hireDay;

   public Employee(String n, double s, int year, int month, int day)
   {
      name = n;
      salary = s;
      hireDay = LocalDate.of(year, month, day);
   }

   public String getName()
   {
      return name;
   }

   public double getSalary()
   {
      return salary;
   }

   public LocalDate getHireDay()
   {
      return hireDay;
   }

   public void raiseSalary(double byPercent)
   {
      double raise = salary * byPercent / 100;
      salary += raise;
   }
}

  結果以下:

 

import java.util.Scanner;

public class StudentTest
{
   public static void main(String[] args)
   {
      
      Students[] staff = new Students[3];
      System.out.println("請輸入學生信息:");
	  @SuppressWarnings("resource")
	  Scanner in = new Scanner(System.in); 
      for(int i=0;i<staff.length;i++) {
          staff[i]=new Students(in.next(),in.next(),in.next());
      }
      System.out.println("name"+" "+"sex"+" "+" "+"javascore");

      for (Students e : staff)
         System.out.println(e.getName() +"    "+e.getSex()+"         "+e.getJavaScore()+" ");
   }
}

class Students
{
   private String name;
   private String sex;
   private String javascore;

   public Students(String n, String s, String j)
   {
      name = n;
      sex = s;
      javascore =j;
   }

   public String getName()
   {
      return name;
   }

   public String getSex()
   {
      return sex;
   }

   public String getJavaScore()
   {
      return javascore;
   }

}

  結果以下:

        測試程序2:

          測試程序3:

      測試程序4:

       測試程序5:

      實驗3

import java.util.*;

public class RectangleCircle {

	@SuppressWarnings("resource")
	public static void main(String[] args) {
	        Scanner price=new Scanner (System.in);
	        System.out.println("輸入矩形的長、寬:");
	        double x=price.nextInt();
	        double y=price.nextInt();
	        System.out.println("矩形周長="+(x+y)*2);
	        System.out.println("矩形面積="+(x*y));
	        
	        System.out.println("輸入圓的半徑:");
	        double r=price.nextInt();
	        double π = 3.14;
			System.out.println("圓周長="+(2*π*r));
	        System.out.println("圓面積="+(r*r*π));
	        
	        System.out.println("周長和="+((x+y)*2+2*π*r));
	        System.out.println("面積和="+((x*y)+r*r*π));
	
	}

}

  結果以下:

總結:

經過本週的學習,我學習到了面向對象程序設計(OOP)的概論,以前老是聽老師說Java是一門面向對象的語言,我也終於知道了什麼是類,什麼是對象,瞭解了對象的三個特徵:對象的行爲,對象的狀態,對象標識。掌握瞭如何使用預約義類的基本,和構造並初始化對象。課上我學習到了Java類庫中的LocalDate類,讓我對類有了更深的認識。可是我對用戶自定義類的內容瞭解的還不夠,對於第四章我還須要更多的學習。

相關文章
相關標籤/搜索