In: Computer Science
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.
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: