Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a Java Program that contains a function called Magic that takes an integer

ID: 3766879 • Letter: W

Question

Write a Java Program that contains a function called Magic that takes an integer as an argument (parameter) and returns one of 6 different responses based on the value of that integer. Write a main function that

a. asks the user to enter a yes/no question

b. generates a random number between 1 and 6

c. calls the function Magic and provides the generated random number as the given argument

d. prints the response returned from the function Magic as the answer to the user’s question

The program should take questions from the user until the user enters a terminating phrase such as “end”

Explanation / Answer

import java.util.*;
class magic_rand
{

   public static String magic(int n)
   {
       String s="";
       switch(n)
       {
           case 1:
               s="One";
           case 2:
               s="Two";
           case 3:
               s="Three";
           case 4:
               s="Four";
           case 5:
               s="Five";
           case 6:
               s="Six";
       }
       return s;
   }

   public static void main(String args[])
   {
       Scanner scan=new Scanner(System.in);
       Random ran = new Random();
       String ans="yes",s;
       while(ans.equals("yes"))
       {
       int n=ran.nextInt()%6;
       s=magic(n);
       System.out.println(s);
       System.out.println("continue[yes/no]");
       ans=scan.nextLine();
       }
   }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote