Computer Science, asked by suprajalakshmidevi36, 4 days ago

need answer for this in java.

Attachments:

Answers

Answered by BrainlyProgrammer
12

Answer:

This is a situation-based question. You need to think and go through the question before you start making the program.

Here you go!

Given (Main points) :-

  • Pooja normally answers calls, except in the morning.
  • She answers only her Mom's call.
  • She never answers any calls when she is sleeping.

Note the underlines words above.

Now we have to create a method (also called "function") with name Met that takes 3 Boolean parameters. One for morning, second for Mom's call and third for sleeping.

The program returns true if Pooja answers call otherwise will return false.

So the approach goes like this...

//Mentioned in question only write the method so i assume that class and main methods have been defined.

//Method will be non-returnable function because we have to use System.out.println() for printing true/false. (As mentioned in question)

void Met(isMorning, mcall, isSleeping) {

if(isMorning) {

if(isSleeping)

System.out.println("False");

else if(mcall)

System.out.println("True");

else

System.out.println ("False");

}

else {

if(isSleeping) {

System.out.println("False");

else

System.out.println("True");

}

}

Explanation:-

  1. Before i proceed with explanation, i would like to request the readers to go through the "Main Points" given earlier :D.
  2. The program accepts 3 parameters isMorning, mcall, isSleeping respectively.
  3. The program then proceeds with the condition that if it is Morning, check if Pooja is sleeping or not.
  4. If yes, it will print False otherwise, it checks if it is Mom's call or not.
  5. If it is not Mom's call, it will print False otherwise True.
  6. If it is not Morning then the program checks if Pooja is sleeping or not.
  7. If Pooja is sleeping, the it will print False otherwise, prints True.
  8. No need to check if it is Mom's call or not when it is not morning as "She normally answers calls, except in the morning."
  9. Hurray! You made it!
Attachments:
Answered by anindyaadhikari13
10

Solution:

The given problem is solved in Java.

void Met(boolean isMorning, boolean isMom, boolean isSleeping){

   System.out.println(! ( isSleeping || ( isMorning && ! isMom)));

}

Explanation:

Before solving, lets see the conditions given:

1) Pooja never answers call when she is sleeping.

2) She answers call in the morning if her mother calls else not.

If we use if else to represent this, it will be:

if(sleeping)

   return false;

else

   if(morning and mom_calling)

       return true; - Since its morning but mom calling.

   if(morning and not mom_calling)

       return false; - Since its morning and someone else calling.

   if(not morning)

       return true;  - Since she will accept calls from others.

So, we can conclude that she will not accept calls when she is sleeping or (its morning and mom not calling)

So, the condition becomes:

> condition = sleeping or (morning and not mom calling)

The above condition returns true when she will not call.

So, we can add extra not operator to negate the condition.

> condition = not [ sleeping or (morning and not mom calling)]

Thus, the final condition becomes:

> condition = !(isSleeping || (isMorning && !isMom))

Which is now displayed on the screen.

There are 8 possible test cases:

\boxed{\begin{array}{c|c|c|c}\underline{\rm Morning}:&\underline{\rm Mom\: Calling}:&\underline{\rm Sleeping}:&\underline{\rm Result}:\\ \rm True&\rm True &\rm True&\rm False\\ \rm True&\rm True&\rm False&\rm True\\ \rm True&\rm False&\rm True&\rm False\\ \rm True&\rm False&\rm False&\rm False\\ \rm False&\rm True&\rm True&\rm False\\ \rm False&\rm True&\rm False&\rm True\\ \rm False&\rm False&\rm True&\rm False\\ \rm False&\rm False&\rm False&\rm True\end{array}}

We will check if it works for all cases by writing the program given below:

public class Main{

   public static void main(String s[]){

       boolean test_cases[][] = {

               {true, true, true},

               {true, true, false},

               {true, false, true},

               {true, false, false},

               {false, true, true},

               {false, true, false},

               {false, false, true},

               {false, true, false}

           };

       boolean outcomes[] = {false, true, false, false, false, true, false, true};

       System.out.println("Output:\tExpected:");

       int i=0;

       for(boolean x[] : test_cases)

           System.out.println(Met(x[0], x[1], x[2]) + "\t" + outcomes[i++]);

   }

   static boolean Met(boolean isMorning, boolean isMom, boolean isSleeping){

       return !(isSleeping || (isMorning && !isMom));

   }

}

The output is attached.

Attachments:
Similar questions