225k views
1 vote
write a program that first asks the user to enter three real numbers using GUI input. Print the sum if all three numbers are positive, 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.

User Sijia Din
by
6.9k points

1 Answer

4 votes

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

} }

Step-by-step 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.

User CobaltBabyBear
by
6.4k points