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
laila [671]
3 years ago
7

write a program that first asks the user to enter three real numbers using GUI input. Print the sum if all three numbers are pos

itive, print the product of the two positive numbers if only one is negative - use a nested if. Then, ask the user to enter two real numbers from the console. If both numbers are negative, print the quotient.
Computers and Technology
1 answer:
den301095 [7]3 years ago
4 0

Answer:

Here is the complete program:

import javax.swing.JOptionPane;  //to use GUI in JAVA application

import java.util.Scanner;  //to take input from user at console

public class Main {

public static void main(String[] args) {  //start of main function body

 String num1, num2,num3;  //declare three variables to store strings of numbers for input dialog

 double number1, number2, number3, sum,product; //declare three double type variables to hold the real numbers  

 num1 = JOptionPane.showInputDialog("num1");  //shows a dialog box prompting user to enter value of first number i.e. num1

 number1 = Double.parseDouble(num1);  //scans and reads the value of first input number i.e. number1    

 num2 = JOptionPane.showInputDialog("num2");  //shows a dialog box prompting user to enter value of second number i.e. num2

 number2 = Double.parseDouble(num2);   //reads number2  

               num3 = JOptionPane.showInputDialog("num3");  //shows a dialog box prompting user to enter value of third number i.e. num3

 number3 = Double.parseDouble(num3);   //reads number3  

   if(number1>0&& number2>0 && number3>0){  //checks if all three numbers are positive

   sum = number1 + number2+ number3;  //displays the sum by adding all three positive numbers

   JOptionPane.showMessageDialog(null, "the sum is : " + sum , "Results", JOptionPane.PLAIN_MESSAGE );      }   // displays the result of sum in a message dialog box

 

    if(number1<0 && number2>0 && number3>0)      {  //checks if number2 and number3 are positive

   product = number2*number3;  //computes product

   JOptionPane.showMessageDialog(null, "the product is : " + product , "Results", JOptionPane.PLAIN_MESSAGE );      }  //displays the result

   else if (number2<0&&number1>0&&number3>0){  //checks if number1 and number3 are positive

    product = number1*number3;  //computes product

JOptionPane.showMessageDialog(null, "the product is : " + product , "Results", JOptionPane.PLAIN_MESSAGE );      }  //displays the result

   else if (number3<0 && number1>0 && number2>0)     {  //checks if number1 and number2 are positive

     product = number1*number2;  //computes product of 2 positive numbers

     JOptionPane.showMessageDialog(null, "the product is : " + product , "Results", JOptionPane.PLAIN_MESSAGE );      }  //displays the result

   else     {  /.if all the numbers are positive.

/*However this part is optional. You can just use else part for above else if statement and exclude this else part. In this way the product of all three positive numbers is not computed and product is only computed when only two numbers of the three are positive */

     product = number1*number2*number3;  //computes product of all three positive numbers

     JOptionPane.showMessageDialog(null, "the product is : " + product , "Results", JOptionPane.PLAIN_MESSAGE );      }   //displays the result

Scanner scan = new Scanner(System.in);  //creates a Scanner class object

double value1 , value2, quotient;  //declares variables to hold the values of two real numbers and the result of division of the two real numbers is stored in quotient variable

System.out.println("Enter the first real number: ");  //prompts user to enter the value of first real number from the console

value1 = scan.nextDouble();  //reads the first number from user

System.out.println("Enter the second real number : ");  //prompts user to enter the value of second real number from the console

value2 = scan.nextDouble();  //reads the second number from user

if(value1<0&&value2<0)  {  //checks if both the numbers are negative

quotient = value1/value2;  //compute the quotient

JOptionPane.showMessageDialog(null, "the quotient is : " + quotient , "Results", JOptionPane.PLAIN_MESSAGE );    }  //displays result

 else  //if both numbers are not negative

JOptionPane.showMessageDialog(null, "Both numbers are not negative" );   //displays this message in a dialog box

  }  }

Explanation:

In the above given code if you want to display the outputs in console then alter the following statements as follows:

   if(number1>0&& number2>0 && number3>0){

   sum = number1 + number2+ number3;

  System.out.println("the sum is :  " + sum); }      

 

    if(number1<0 && number2>0 && number3>0)      {

    product = number2*number3;

      System.out.println("the product is :  " + product); }          

   else if (number2<0&&number1>0&&number3>0){

          product = number1*number3;

System.out.println("the product is :  " + product);      }

   else if (number3<0 && number1>0 && number2>0)     {

     product = number1*number2;

     System.out.println("the product is :  " + product);     }

   else     {

     product = number1*number2*number3;

      System.out.println("the product is :  " + product);      }

Scanner scan = new Scanner(System.in);

double value1 , value2, quotient;

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

value1 = scan.nextDouble();

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

value2 = scan.nextDouble();

if(value1<0&&value2<0)  {

quotient = value1/value2;

 System.out.println("the quotient is :  " + quotient);   }

 else  

System.out.println("Both numbers are not negative");

Notice that all the JOptionPane.showMessageDialog statement are changed with System.out.println which prints the results on the console.

You might be interested in
1:A presentation program which is developed by Microsoft
LekaFEV [45]
Option 3: PowerPoint.
4 0
2 years ago
Read 2 more answers
The ram called a volatile device select one:<br><br> a. false<br><br> b. true
Paraphin [41]
B. true

RAM (Random Access Memory) is volatile because if power is lost, so is your memory.
6 0
3 years ago
You are provided with the following program poem.cpp. All is good, and the memory is allocated dynamically, but it crashes your
Maslowich

Answer:

See explaination

Explanation:

#include <iostream>

using namespace std;

string * createAPoemDynamically()

{

string *p = new string;

*p = "Roses are red, violets are blue";

return p;

}

int main() {

while(true) {

string *p;

p = createAPoemDynamically();

if (!p)

{

cout << "Memory allocation failed\n";

}

cout<<*p<<"\n";

// assume that the poem p is not needed at this point

//delete allocated memory

delete p;

}

}

8 0
2 years ago
WILL MARK BRAINLIEST AND 20 POINTS
Fantom [35]
With templates, you can Create a new document based on previous formatting, create a custom template for future use, and apply different templates to different pages of a document.
4 0
3 years ago
What is the serializable interface and what makes an object serializable?
Varvara68 [4.7K]

Answer: Serializable interface is the interface that acts as the maker persisting without any specific method and members in Java. The maker has the objective of identification of class for serialization method. This helps in the conversion of the object value in the byte form.

Object is serialized by converting the state into byte version and thus can be returned to object copy . The java.io.Serializable interface is the requirement for the conversion of any object of particular class or superclass. This technique is followed for making the data flow on the network efficient and easy.

5 0
3 years ago
Other questions:
  • Words have both denotative and connotative meaning. The connotative meaning is Question 2 options:
    15·1 answer
  • What are the core scripting and coding technolies used to build ai platforms?
    14·1 answer
  • All animations on the world wide web are flash animations
    11·2 answers
  • Four differences between fourth and fifth generation of computers​
    12·1 answer
  • What does the following code segment do?
    13·1 answer
  • A small company with 100 computers has hired you to install a local area network. All of the users perform functions like email,
    9·1 answer
  • Which of the following symbols is a part of all spreadsheet functions?
    7·2 answers
  • Advantages and disadvantages of java
    10·1 answer
  • 10 computer and operating systems errors​
    5·2 answers
  • T/F static development is the process of constructing the programs and code modules that serve as the building blocks of the inf
    14·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!