Find the Error Assume the following declaration exists enum

Find the Error Assume the following declaration exists: enum coffee {MEDIUM, DARK, DECAF} Find the error(s) in the following switch statement://This code has errors! coffee mycup = DARK; switch (mycup) {case coffee.MEDIUM: system. out.promtIn(\"Mild flavor. \"); break; case coffee.DARK system.out.promtIn(\"Strong flavor.\"); break; case coffee.DECAF system.out.promtIn(\"Won\'t keep you awake.\"); break; default: system.out.promtIn(\"Never heard of it.\");}

Solution

What\'s wrong about it?

Does it not compile?

Does it give an unexpected result?

the line Coffee myCup = DARK needs to become Coffee myCup = Coffee.DARK

as because DARK is part of Coffee so we must use Coffee.DARK.

Second error is in the switch case.

we must have to declare as name itself

switch (myCup)
{

case MEDIUM :

System.out.println(\"Mild flavor.\");
break;

case DARK :
System.out.println(\"Strong flavor.\");
break;

case DECAF :
System.out.println(\"Won\'t keep you awake.\");
break;

default:
System.out.println(\"Never heard of it.\");   
}

myCup is be defined as dateType of Coffee so no need of decalring case options as Coffee.\"Names\"

Here is corrected code:

public class HelloWorld{
enum Coffee { MEDIUM, DARK, DECAF };
public static void main(String []args){
  
Coffee myCup = Coffee.DARK;
switch (myCup)
{

case MEDIUM :

System.out.println(\"Mild flavor.\");
break;

case DARK :
System.out.println(\"Strong flavor.\");
break;

case DECAF :
System.out.println(\"Won\'t keep you awake.\");
break;

default:
System.out.println(\"Never heard of it.\");   
}
}
}

Output:

Strong flavor.

 Find the Error Assume the following declaration exists: enum coffee {MEDIUM, DARK, DECAF} Find the error(s) in the following switch statement://This code has e
 Find the Error Assume the following declaration exists: enum coffee {MEDIUM, DARK, DECAF} Find the error(s) in the following switch statement://This code has e

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site