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

Managing Web Responses with Enums

Using enums to manage HTTP responses can simplify handling different status codes in web applications. This exercise demonstrates creating enums that include additional information like HTTP status codes.

Bu egzersiz

Input/Output and Streams in Java

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

Egzersiz talimatları

  • Define an int parameter code for the HttpStatus constructor.
  • Return field code when method getCode() is called.
  • Print HttpStatus code for status OK.

Uygulamalı interaktif egzersiz

Bu örnek kodu tamamlayarak bu egzersizi bitirin.

public class HttpStatusExample {
    enum HttpStatus { 
        OK(200), NOT_FOUND(404);

        private int code;
		
        // Constructor for the HttpStatus with status code
        HttpStatus(____ code) { 
            this.code = code;
        }

        public int getCode() {
        	// Return the code for a specific HTTP status
            return ____;
        }
    }

    public static void main(String[] args) {
    	// Call method to print the status code
        System.out.println(HttpStatus.OK.____()); 
    }
}
Kodu Düzenle ve Çalıştır