今天要學 Catching and throwing Exception

import java.util.InputMismatchException;
import java.util.Scanner;
public class CatchingAndThrowingExceptions {
public static void main(String[] args) {
int result = divide();
System.out.println("x / y = " + result);
}
private static int divide() {
int x = getInt();
int y = getInt();
System.out.println("x is " + x + ", y is " + y);
return x / y;
}
private static int getInt() {
Scanner s = new Scanner(System.in);
System.out.print("Please enter an integer: ")…

今天要學 Stack Trace and Call Stack

import java.util.Scanner;public class StackTraceAndCallStack {
public static void main(String[] args) {
int result = divide();
System.out.println("x / y = " + result);
}
private static int divide() {
int x = getInt();
int y = getInt();
System.out.println("x is " + x …

今天要學習的是 Exceptions

public class Main {    public static void main(String[] args) {
int x = 99;
int y = 0;
System.out.println(divide(x, y));
}
private static int divide(int x, int y) {
return x / y;
}
}

輸出結果:

Exception in thread “main” java.lang.ArithmeticException: / by zero

at Main.divide(Main.java:20)

at Main.main(Main.ja …


My Github

MainContact.java

package contactChallenge;import contactChallenge.datamodel.ContactData;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
import java.util.Objects;public class MainContact extends Application { public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
Parent root = FXMLLoader.load(Objects.requireNonNull(getClass().getResource("mainwindow.fxml")));
primaryStage.setTitle("Contacts");
primaryStage.setScene(new Scene(root, 820, 420));
primaryStage.show();
}
@Override
public void init() throws Exception {
ContactData.getInstance().loadContacts();
}
@Override
public void stop() throws Exception {
ContactData.getInstance().saveContacts();
}
}

Controller.java

package contactChallenge;import contactChallenge.datamodel.Contact;
import contactChallenge.datamodel.ContactData;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.scene.control.*;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.control.cell.TextFieldTableCell;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent…

小挑戰:

Modify the program so that adding items to the shopping basket doesn’t

actually reduce the stock count but, instead, reserves the requested

number of items.

You will need to add a “reserved” field to the StockItem class to store the

number of items reserved.

Items can continue to be added to the basket, but it should not be possible to

reserve more than the available stock of any item. An item’s available stock

is the stock count less the reserved amount.

The stock count for each item is reduced when a basket is checked out, at which

point all reserved…


Basket.java 改

package Stock;import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
public class Basket {
private final String name;
private final Map<StockItem, Integer> list;
public Basket(String name) {
this.name = name;
this.list = new HashMap<>();
}
public int addToBasket(StockItem item, int quantity) {
if ((item != null) && (quantity > 0)) {
int inBasket = list.getOrDefault(item, 0);
list.put(item, inBasket + quantity);
return inBasket;
}
return 0;
}
public Map<StockItem, Integer> Items() {
return Collections.unmodifiableMap(list);
}
@Override
public String toString() {
String s = "\nShopping basket " +…

Main.java 改

package Stock;public class Main {
private static StockList stockList = new StockList();
public static void main(String[] args) {
StockItem temp = new StockItem("bread", 0.86, 100);
stockList.addStock(temp);
temp = new StockItem("cake", 1.10, 7);
stockList.addStock(temp);
temp = new StockItem("car", 12.50, 2);
stockList.addStock(temp);
temp = new StockItem("chair", 62.0, 10);
stockList.addStock(temp);
temp = new StockItem("cup", 0.50, 200);
stockList.addStock(temp);
temp = new StockItem("door", 72.95, 4);
stockList.addStock(temp);
temp = new StockItem("juice", 2.50, 36);
stockList.addStock(temp);
temp = new StockItem("phone", 96.99, 35);
stockList.addStock(temp);
temp = new StockItem("towel", 2.40, 80);
stockList.addStock(temp);
temp = new StockItem("vase", 8.76, 40);
stockList.addStock(temp);

今天要學的是 Sorted Collections

StockItem.java

package Stock;public class StockItem implements Comparable<StockItem> {
private final String name;
private double price;
private int quantityStock = 0;
public StockItem(String name, double price) {
this.name = name;
this.price = price;
this.quantityStock = 0; // or here ( but you wouldn't normally do both)
}
public StockItem(String name, double price, int quantityStock) {
this.name = name;
this.price = price;
this.quantityStock = quantityStock;
}
public String getName() {
return name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
if (price > 0.0) {
this.price = price;
}
}

小練習:

Modify the previous HeavenlyBody example so that the HeavenlyBody

class also has a “bodyType” field. This field will store the

type of HeavenlyBody (such as STAR, PLANET, MOON, etc).

You can include as many types as you want, but must support at

least PLANET and MOON.

For each of the types that you support, subclass the HeavenlyBody class

so that your program can create objects of the appropriate type.

Although astronomers may shudder at this, our solar systems will

allow two bodies to have the same name as long as they are not the

same type of body: so you…


SetMain.java

import java.util.HashSet;
import java.util.Set;
public class SetMain {
public static void main(String[] args) {
Set<Integer> squares = new HashSet<>();
Set<Integer> cubes = new HashSet<>();
for (int i = 1; i <= 100; i++) {
squares.add(i * i);
cubes.add(i * i * i);
}
System.out.println("Squares size is: " + squares.size());
System.out.println("cubes size is: " + cubes.size());
Set<Integer> union = new HashSet<>(squares);
union.addAll(cubes);
System.out.println("Union size is: " + union.size());
}
}

輸出結果:

Squares size is: 100

cubes size is: 100

Union size is: 196

1 個 set 為 1~100的次方

另 1 個 set 為 1~100的立方

196 為 兩個 set 裡出現的所有數字,總和有 196…

張小雄

記錄成為軟體工程師的過程

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store