96.1k views
4 votes
HTTP is the protocol that governs communications between web servers and web clients (i.e. browsers). Part of the protocol includes a status code returned by the server to tell the browser the status of its most recent page request. Some of the codes and their meanings are listed below:

200, OK (fulfilled)

403, forbidden

404, not found

500, server error

Given an int variable status, write a switch statement that prints out the appropriate label from the above list based on status.

I need this answer in java not c++ and i can't seem to find a java answer

1 Answer

7 votes

Answer:

Here is the JAVA program:

class Main { //class name

public static void main(String[] args) { //start of main method

int status = 200; //sets status to 200

switch( status ){ //switch statement

case 200: //if code is 200

System.out.println("OK (fulfilled)"); //displays OK (fulfilled) if case is 200

break;

case 403: //if code is 403

System.out.println("forbidden"); //displays forbidden if case is 403

break;

case 404: //if code is 404

System.out.println("not found"); //displays not found if case is 404

break;

case 500: //if code is 500

System.out.println("server error"); //displays server error if case is 500

break;

default: //if status is set to code other than above codes

System.out.println("Unknown code"); //displays Unknown code

break; } } }

Step-by-step explanation:

If you want to take the status code as input from user then use the following statements:

int status;

Scanner input = new Scanner(System.in);

System.out.print("Enter status code: ");

status= input.nextInt();

After this you can add the switch code chunk in the Answers section. Also import the Scanner class to take status code as input from user:

import java.util.Scanner;

The screenshot of the program along with its output is attached.

HTTP is the protocol that governs communications between web servers and web clients-example-1
User Rein RPavi
by
8.7k points