開始使用免費開始

POJO、包裝器與 java.math

每個偉大的任務都從穩健的發射開始!在這個練習中,你要啟動一個已為你匯入的 Rocket 類別。

準備起飛了嗎?

本練習屬於課程

Java 的資料型別與例外狀況

檢視課程

練習說明

  • 建立一個 Rocket POJO 的實例。
  • 使用相對應的 setter,將 Rocketthrust 欄位設為 "7770000"BigDecimal
  • 使用 .setManned() setter,將 manned 欄位設為 true

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

public class Launch {

	public static void main(String[] args) {
    	// Create an instance of the Rocket POJO
		Rocket rocket = ____ ____();
		rocket.setName("Saturn");
        // Set the thrust field to as a BigDecimal
		rocket.setThrust(____ ____("7770000"));
        // Set the rocket's manned Boolean wrapper field to true
		rocket.____(____);
		fire(rocket);
	}

	public static void fire(Rocket r) {
		BigDecimal newtons = r.getThrust().divide(new BigDecimal(224));
		System.out.println("We have liftoff of: " + r.getName());
		System.out.print("Thrust is: " + newtons + " newtons of energy");
	}
}
編輯並執行程式碼