1answer.
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
Murrr4er [49]
3 years ago
13

Write a program in JAVA to perform the following operator based task: Ask the user to choose the following option first: If User

Enter 1 - Addition If User Enter 2 - Subtraction If User Enter 3 - Division If User Enter 4 - Multiplication If User Enter 5 - Average Ask the user to enter the 2 numbers in a variable for first and second(first and second are variable names) for the first 4 options mentioned above and print the result. Ask the user to enter two more numbers as first and second 2 for calculating the average as soon as the user chooses an option 5. In the end, if the answer of any operation is Negative print a statement saying "Oops option X(1/2/3/4/5/) is returning the negative number" NOTE: At a time users can perform one action at a time.

Computers and Technology
1 answer:
kirill [66]3 years ago
5 0

Answer:

Here is the JAVA program:    

import java.util.Scanner;

public class Operations{      

  public static void main(String[] args) {      

    Scanner input= new Scanner(System.in);

      float first,second;

      System.out.println("Enter first number:");

       first = input.nextFloat();

       System.out.println("Enter second number:");

       second = input.nextFloat();

       System.out.println("Enter 1 for Addition, 2 for Subtraction 3 for Multiplication, 4 for Division and 5 for Average:");

       int choice;

       choice = input.nextInt();

       switch (choice){

       case 1: //calls add method to perform addition

           System.out.println("Addition result = " + add(first,second));

           break;

       case 2: //calls sub method to perform subtraction

           System.out.println("Subtraction result = " + sub(first,second));

           break;      

       case 3: //calls mul method to perform multiplication

           System.out.println("Multiplication result = " +mul(first,second));

           break;

       case 4: //calls div method to perform division

           System.out.println("Division result = " +div(first,second));

           break;

       case 5: //calls average method to compute average

           System.out.println("Average = " +average(first,second));

           break;

       default: //if user enters anything other than the provided options

               System.out.println("Invalid Option"); }}

   public static float add(float first, float second)    {

       float result = first + second;

       if(result<0)

       {System.out.println("Oops option 1 is returning a negative number that is: ");      }        

       return result;    }

   public static float sub(float first, float second)    {

       float result = first - second;

       if(result<0){System.out.println("Oops option 2 is returning negative number that is: ");}        

       return result;    }

   public static float mul(float first, float second)    {

       float result = first*second;

        if(result<0)

        {System.out.println("Oops option 3 is returning negative number that is: ");}        

       return result;    }

   public static float div(float first, float second){

       if(second==0)

       {System.out.println("Division by 0");}

       float result = first/second;

        if(result<0){System.out.println("Oops option 4 is returning negative number that is: ");}      

       return result;    }    

   public static float average (float first,float second)    {

       Scanner inp= new Scanner(System.in);

       float first2,second2;

        System.out.println("Enter two more numbers:");

        System.out.println("Enter first number:");

       first2 = inp.nextInt();

      System.out.println("Enter second number:");

       second2 = inp.nextInt();

       float result;

       float sum = first+second+first2+second2;

       result = sum/4;

        if(result<0)

        {System.out.println("Oops option 5 is returning negative number that is: ");}        

       return result;  }}

Explanation:

The above program first prompts the user to enter two numbers. Here the data type of two number is float in order to accept floating numbers. The switch statement contains cases which execute according to the choice of user. The choice available to user is: 1,2,3,4 and 5

If the user selects 1 then the case 1 executes which calls the method add. The method add takes two numbers as parameters, adds the two numbers and returns the result of the addition. This result is displayed on output screen. However if the result of addition is a negative number then the message: "Oops option 1 is returning the negative number that is:" is displayed on the screen along with the negative answer of that addition.

If the user selects 2 then the case 2 executes which calls the method sub. The method works same as add but instead of addition, sub method takes two numbers as parameters, subtracts them and returns the result of the subtraction.

If the user selects 3 then the case 3 executes which calls the method mul. The method mul takes two numbers as parameters, multiplies them and returns the result of the multiplication.

If the user selects 4 then the case 4 executes which calls the method div. The method div takes two numbers as parameters and checks if the denominator i.e. second number is 0. If its 0 then Division by 0 message is displayed along with result i.e. infinity. If second number is not 0 then method divides the numbers and returns the result of the division.

If the user selects 5 then the case 5 executes which calls the method average. The average method then  asks the user to enter two more numbers first2 and second2 and takes the sum of all four numbers i.e. first, second, first2 and second2. Then the result of sum is divided by 4 to compute average. The method returns the average of these four numbers.

You might be interested in
I just downloaded this song and if any one wants to listen to it then go ahead and do that. It’s called Trap Music 2018 âa bass
xz_007 [3.2K]

Answer:

ok

Explanation:

4 0
3 years ago
Read 2 more answers
Do you think that feedly will help you create an effective personal online learning environment? Why or why not?
goldenfox [79]

Yes. Feedly will help me create an effective personal online environment. I am able to keep up with all the topics that matter to me most. Feedly is able to help me discover insightful sources from publications and blogs. Feedly is also able to uniquely offer clean, minimalist reading experience optimized for productivity.

5 0
3 years ago
A datagram network allows routers to drop packets whenever they need to. The probability of a router discarding a packetis p. Co
tresset_1 [31]

Answer:

a.) k² - 3k + 3

b.) 1/(1 - k)²

c.) k^{2}  - 3k + 3 * \frac{1}{(1 - k)^{2} }\\\\= \frac{k^{2} - 3k + 3 }{(1-k)^{2} }

Explanation:

a.) A packet can make 1,2 or 3 hops

probability of 1 hop = k  ...(1)

probability of 2 hops = k(1-k)  ...(2)

probability of 3 hops = (1-k)²...(3)

Average number of probabilities = (1 x prob. of 1 hop) + (2 x prob. of 2 hops) + (3 x prob. of 3 hops)

                                                       = (1 × k) + (2 × k × (1 - k)) + (3 × (1-k)²)

                                                       = k + 2k - 2k² + 3(1 + k² - 2k)

∴mean number of hops                = k² - 3k + 3

b.) from (a) above, the mean number of hops when transmitting a packet is k² - 3k + 3

if k = 0 then number of hops is 3

if k = 1 then number of hops is (1 - 3 + 3) = 1

multiple transmissions can be needed if K is between 0 and 1

The probability of successful transmissions through the entire path is (1 - k)²

for one transmission, the probility of success is (1 - k)²

for two transmissions, the probility of success is 2(1 - k)²(1 - (1-k)²)

for three transmissions, the probility of success is 3(1 - k)²(1 - (1-k)²)² and so on

∴ for transmitting a single packet, it makes:

     ∞                             n-1

T = ∑ n(1 - k)²(1 - (1 - k)²)

    n-1

   = 1/(1 - k)²

c.) Mean number of required packet = ( mean number of hops when transmitting a packet × mean number of transmissions by a packet)

from (a) above, mean number of hops when transmitting a packet =  k² - 3k + 3

from (b) above, mean number of transmissions by a packet = 1/(1 - k)²

substituting: mean number of required packet =  k^{2}  - 3k + 3 * \frac{1}{(1 - k)^{2} }\\\\= \frac{k^{2} - 3k + 3 }{(1-k)^{2} }

6 0
3 years ago
Explain how QoS might be implemented
Artist 52 [7]

Answer:

 The quality of service basically provide the sufficient quality in the IP network and it is also known as traffic shaping. As, it assign the priority to every device and the services in the network. It basically recognize the several types of the traffic moving in the network.

The implementation of quality of service is basically done in many network applications by reserve the bandwidth and specific path in the network system.

The various network device control the packet flow so that the network resources can easily accept packets in the network.

 

 

6 0
3 years ago
Which port security violation mode does not generate messages or increment the violations counter?
Zanzabum

The number of violations is increased. When a secure port is in the error-disabled condition, the shutdown and restrict command can be used to restore it.

<h3>What is  offence?</h3>

A deviation from a code of conduct or law is referred to as a violation. When driving a car, going over the posted speed limit is a regular legal violation. Invading someone else's privacy could include reading their journal. failure to uphold a duty or right; breaching the law.

The two types of violations are states and acts. A transgression is a violation of the rules that is less serious than a foul and frequently involves technicalities of the game. A disrespectful or vulgar act: profanation.

Hence, The number of violations is increased. When a secure port is in the error-disabled condition, the shutdown and restrict command can be used to restore it.

To learn more about violation, refer to:

brainly.com/question/1274113

#SPJ4

4 0
2 years ago
Other questions:
  • All formulas that begin with =IF will
    5·1 answer
  • Why do networks need standards?
    12·1 answer
  • How do airbags prevent injury?
    13·1 answer
  • Which best describes a hybrid drive?
    11·1 answer
  • Radar devices are used by law enforcement to be sure that individuals are driving safely. They tell the officer how fast the veh
    12·1 answer
  • 4. What two important events occurred early in the formation of the universe?
    8·1 answer
  • Microsoft Excel is an example of a(n) application.
    11·2 answers
  • Of the people working in concert with security teams to ensure data quality and protection, the head of information management i
    5·1 answer
  • Microsoft word is an example of utility software? <br><br>A.true <br>B.false​
    7·2 answers
  • Write Epic username plz
    6·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!