Question

In: Computer Science

IN JAVA Write a complete program that asks the user to enter two real numbers from...

IN JAVA

Write a complete program that asks the user to enter two real numbers from the console. If both numbers are positive print the product, if both numbers are negative print the quotient, otherwise print INVALID INPUT. Use a nested if; output should be to a dialog and console; use printf to format the console output (for real numbers specify the width and number of digits after the decimal). The output must be labeled. Follow Java conventions, indent your code, program must be written as demoed in review. No methods except for main, nextLine() method cannot be used.

Solutions

Expert Solution

Program:

import java.util.*;
import javax.swing.*;
import java.text.DecimalFormat;

public class ConsoleDemo
{
//to get two decimal places
private static DecimalFormat df=new DecimalFormat("#.##");
public static void main(String args[])
{
double no1,no2;

//Accept the values of no1 and no2 from user
no1=Double.parseDouble(args[0]);
no2=Double.parseDouble(args[1]);
JFrame f1=new JFrame();
//both the numbers are positive
if(no1>0 && no2>0)
{
System.out.printf("\nThe product: %.2f\n",(no1*no2));
JOptionPane.showMessageDialog(f1,"The product: "+df.format((no1*no2)));
}
else if(no1<0 && no2<0) //both the numbers are negative
{
System.out.printf("\nThe quotient : %.2f\n",(no1/no2));
JOptionPane.showMessageDialog(f1,"The quotient: "+df.format((no1/no2)));
}
else //to check invalid input
{
System.out.println("\nINVALID INPUT");
JOptionPane.showMessageDialog(f1,"INVALID INPUT");
}
System.out.print("\n");
}
}

Output:


Related Solutions

Java program Use Do-while Write a do-wile loop that asks the user to enter two numbers....
Java program Use Do-while Write a do-wile loop that asks the user to enter two numbers. The numbers should be added and the sum displayed. The loop should ask the user whether he or she wishes to perform the operation again. If so, the loop should repeat, otherwise, it should terminate. Use Continue branching statement Write a program that reads an integer and display all the positive ODD numbers from 0 to n (integer entered by the user). Use CONTINUE...
Design a complete program that asks the user to enter a series of 20 numbers. The...
Design a complete program that asks the user to enter a series of 20 numbers. The program should store the numbers in an array and then display each of the following data: I. The lowest number in the array II. The highest number in the array III. The total of the numbers in the array IV. The average of the numbers in the array *PYTHON NOT PSUEDOCODE AND FLOW CHART!!!!*
write java program that prompt the user to enter two numbers .the program display all numbers...
write java program that prompt the user to enter two numbers .the program display all numbers between that are divisible by 7 and 8 ( the program should swap the numbers in case the secone number id lower than first one please enter two integer number : 900 199 Swapping the numbers 224 280 336 392 448 504 560 616 672 728 784 840 896 i need it eclipse
write java program that prompt the user to enter two numbers .the program display all numbers...
write java program that prompt the user to enter two numbers .the program display all numbers between that are divisible by 7 and 8 ( the program should swap the numbers in case the secone number id lower than first one please enter two integer number : 900 199   Swapping the numbers 224 280 336 392 448 504 560 616 672 728 784 840 896 i need it eclipse
In a file called ThreeOperations.java, write a program that: Asks the user to enter two real...
In a file called ThreeOperations.java, write a program that: Asks the user to enter two real numbers (doubles, not integers) called N1 and N2. It is OK if your program crashes when the user does not enter valid double numbers. Computes and displays the following operations between the two: multiplication, division, exponentiation. For the results of these two operations, only two decimal digits should be displayed. For example: if the user enters numbers 11 and 7, your program output should...
Program should be written in Java a) Write a program that asks the user to enter...
Program should be written in Java a) Write a program that asks the user to enter the approximate current population of India. You should have the computer output a prompt and then YOU (as the user should enter the population.)  For testing purposes you may use the value of 1,382,000,000 from August 2020. Assume that the growth rate is 1.1% per year. Predict and print the predicted population for 2021 and 2022. The printout should include the year and the estimated...
Write a Java program that asks the user to enter an integer that is used to...
Write a Java program that asks the user to enter an integer that is used to set a limit that will generate the following four patterns of multiples of five using nested loops •Ascending multiples of five with ascending length triangle •Ascending multiples of five with descending length (inverted) triangle •Descending multiples of five with ascending length triangle •Descending multiples of five with descending length (inverted) triangle Use error checking to keep asking the user for a positive number until...
In java, write a program that asks the user to enter a character. Then pass the...
In java, write a program that asks the user to enter a character. Then pass the character to the following methods. isVowel() – returns true if the character is a vowel isConsonant() – returns true if the character is a consonant changeCase() – if the character is lower case then change it to upper case and if the character is in upper case then change it to lower case. (return type: char) Example output is given below: Enter a character...
Write a program that asks the user to enter an array of random numbers, then sort...
Write a program that asks the user to enter an array of random numbers, then sort the numbers (ascending order), then print the new array, after that asks the user for a new two numbers and add them to the same array and keep the array organization. (c++ ) (using while and do while loops)
Write a C++ program that asks the user to enter in three numbers and displays the...
Write a C++ program that asks the user to enter in three numbers and displays the numbers in ascending order. If the three numbers are all the same the program should tell the user that all the numbers are equal and exits the program. Be sure to think about all the possible cases of three numbers. Be sure to test all possible paths. Sample Runs: NOTE: not all possible runs are shown below. Sample Run 1 Welcome to the order...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT