answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
Ksenya-84 [330]
2 years ago
11

Describe a strategy for avoiding nested conditionals. Give your own example of a nested conditional that can be modified to beco

me a single conditional, and show the equivalent single conditional.
Computers and Technology
1 answer:
Alex787 [66]2 years ago
4 0

Answer:

One of the strategies to avoid nested conditional is to use logical expressions such as the use of AND & operator.

One strategy is to use an  interface class with a method. That method can be created to be used for a common functionality or purpose. This is also called strategy design pattern. You can move the chunk of conditional statement to that method. Then each class can implement that interface class and use that shared method according to their own required task by creating objects of sub classes and call that common method for any such object. This is called polymorphism.

Explanation:

Nested conditionals refers to the use of if or else if statement inside another if or else if statement or you can simply say a condition inside another condition. For example:

if( condition1) {  

//executes when condition1 evaluates to true

  if(condition2) {

//executes when condition1  and condition2 evaluate to true

  }  else if(condition3) {

 //when condition1 is true and condition3 is true

} else {

 //condition1  is true but neither condition2 nor conditions3 are true

}  }

The deeply nested conditionals make the program difficult to understand or read if the nested conditionals are not indented properly. Also the debugging gets difficult when the program has a lot of nested conditionals.

So in order to avoid nested conditionals some strategies are used such as using a switch statement.

Here i will give an example of the strategies i have mentioned in the answer.

Using Logical Expressions:

A strategy to avoid nested conditionals is to use logical expressions with logical operators such as AND operator. The above described example of nested conditionals can be written as:

if(condition1 && condition2){  //this executes only when both condition1 and condition2 are true

} else if(condition1 && condition3) {

this executes only when both condition1 and condition3 are true

} else if(condition1 ){

//condition1  is true but neither condtion2 nor condtion3 are true  }

This can further be modified to one conditional as:

if(!condition3){

// when  condition1 and condition2 are true

}

else

// condition3 is true

Now lets take a simple example of deciding to go to school or not based on some conditions.

if (temperature< 40)

{

   if (busArrived=="yes")

   {

       if (!sick)

       {

           if (homework=="done")

           {

               printf("Go to school.");

           }

       }                    

   }

}

This uses nested conditionals. This can be changed to a single conditional using AND logical operator.

if ((temperature <40) && (busArrived=="yes") &&

(!sick) && (homework=="done"))

{    cout<<"Eligible for promotion."; }

The second strategy is to use an interface. For example you can

abstract class Shape{

//declare a method common to all sub classes

  abstract public int area();

// same method that varies by formula of area for different shapes

}

class Triangle extends Shape{

  public int area() {

     // version of area code for Triangle

return (width * height / 2);

  }

}

class Rectangle extends Shape{

  public int area() {

     // version of area code for Rectangle

    return (width * height)

  }

}

// Now simply create Rectangle or Triangle objects and call area() for any such object and the relevant version will be executed.

You might be interested in
Describe the ways in which a computer-aided design (CAD) drawing makes the details of an image easier to understand.
cupoosta [38]

Answer: CAD is mainly used for detailed engineering of 3D models or 2D drawings of physical components, but it is also used throughout the engineering process from conceptual design and layout of products, through strength and dynamic analysis of assemblies to definition of manufacturing methods of components.

Explanation:

3 0
2 years ago
Read 2 more answers
Jason has decided to use his name on all the images to protect them from misuse. He has also decided to use the logo of his comp
Mila [183]

Answer:

Text watermarking involves adding the photographer's name at the corner of the image.

Explanation:

This was one of the correct answers when I took the test, I got it wrong because I didn't pick all the correct answers. I hope that this was helpful to you! If you know the other correct answer(s) please comment and let me know and i'll edit this answer! :)

6 0
2 years ago
Read 2 more answers
Write an application that allows a user to enter any number of student quiz scores until the user enters 99. If the score entere
maria [59]

Answer:

import java.util.Scanner;

import java.util.*;

import java.util.Collections;

class Main

{

public static void main(String[] args)  

{

     List<Integer> Scores = new ArrayList<Integer>();

     Scanner sc1=new Scanner(System.in);

     int i =0;

     int a=0;

     do

     {

       System.out.println("Enter score");

       a =sc1.nextInt();

       if (a <= 0)

       {

           System.out.println("You entered wrong score");

           continue;

       }

       else if ( a < 10 && a >0)

       {

           System.out.println("You entered wrong score");

           continue;

           

       }

       else

       {

           Scores.add(a);

       }

       i++;

     }while(a!=99);

     int max= Collections.max(Scores);

     int min=Collections.min(Scores);

     int sum=0;

     float averg =0;

     for(i=0; i<=Scores.size()-1;i++)

     {

        sum += Scores.get(i);

     }

     averg= sum/Scores.size();

     System.out.println("Maximum score" +max);

      System.out.println("Minimum score"+min);

     System.out.println("Average:"+averg);

   

}

}

Explanation:

Remember we need to use Arraylist and not array, as we do not know how many scores we are going to have. And we use Collections for this, where we have functions for finding maximum and minimum for arraylist, however, for average, we need to calculate. However, one should know that Arraylist has better options available as compare to arrays.

3 0
1 year ago
If there are 10 routers between the source and destination hosts, how many transport processes will be involved on all devices?
Gnesinka [82]

Answer:

c. 2

Explanation:

As the routers are only involved with the process of finding the best route to reach an IP address (the destination hosts), they don't look at the IP Datagram payload (which includes the transport protocol header), only to IP header.

So, the only transport processes that "talk"each other, are the one on the source host (defining TCP or UDP source port/s, and the other  on the destination host (defining TCP or UDP destination port/s), so two applications (or more) in both machines can exchange data.

5 0
2 years ago
Having which trait will enable you to deal with people in a way that does not offend them?
Pavlova-9 [17]
Have the trait of brain usage might help lol
4 0
2 years ago
Read 2 more answers
Other questions:
  • Dan is a Civil Engineer for a company that builds nuclear power plants throughout the world. Which best describes the places he
    14·2 answers
  • Which option describes wearable technology? A. the incorporation of technology into objects we use regularly B. rugged technolog
    8·1 answer
  • Which telecommunications device is widely used in industries that require closed communication?
    7·2 answers
  • When workers demonstrate patience, are able to manage there emotions, and get along with other employees, which skills are being
    11·2 answers
  • Survey Q, Non-scoring: What role are you playing in your team?
    5·2 answers
  • Which method allows a computer to react accordingly when it requests data from a server and the server takes too long to respond
    5·1 answer
  • Consider a maximal flow problem in which vehicle traffic entering a city is routed among several routes before eventually leavin
    7·1 answer
  • Identify a data type applied in an input element to create slider controls.
    13·1 answer
  • In the file BankAccount.java, build a class called BankAccount that manages checking and savings accounts. The class has three p
    6·1 answer
  • Spark is electrical discharge in air, while air is mix of variety of gases what particles conduct electricity in gas
    12·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!