BaşlayınÜcretsiz Başlayın

Logging APIs

Use Java Logging to track what's happening in a Java bill splitting application.

Bu egzersiz

Data Types and Exceptions in Java

kursunun bir parçasıdır
Kursu Görüntüle

Egzersiz talimatları

  • Add the import statement with the package containing Java's logging capability.
  • Create a Logger object from the Logger class and name it after the class it is in.
  • Log the bill amount for each person as computed in computeEachBill() with log level INFO.
  • Log the exception when the number of persons entered is not positive in the catch block of computeEachBill() with log level SEVERE.

Uygulamalı interaktif egzersiz

Bu örnek kodu tamamlayarak bu egzersizi bitirin.

// Import the package containing Java's logging capability
import java.util.____.*;

public class SplitTheBill {

	// Get a Logger object named for the class
    private static Logger logger = Logger.____("____");

    public static void main(String[] args) {
    	computeEachBill (150, 5);
        computeEachBill(100, 0);
    }

    public static void computeEachBill(int bill, int people){
        try {
            int ea = bill/people;
            // Add a log entry, at INFO level, with the bill for each person
	        logger.____(Level.____, "Bill for each person is: " + ea);
        } catch (ArithmeticException e) {
        	// Add a log entry, at SEVERE level, that the people splitting must be positive
            logger.____(Level.____, "You forgot to provide a positive number of people for splitting: " + bill);
        }
    }
}
Kodu Düzenle ve Çalıştır