POJO、包裝器與 java.math
每個偉大的任務都從穩健的發射開始!在這個練習中,你要啟動一個已為你匯入的 Rocket 類別。
準備起飛了嗎?
本練習屬於課程
Java 的資料型別與例外狀況
練習說明
- 建立一個
RocketPOJO 的實例。 - 使用相對應的 setter,將
Rocket的thrust欄位設為"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");
}
}