Question

In: Computer Science

using java. using eclipse. write a program that asks to enter 3 real numbers using GUI....

using java. using eclipse. write a program that asks to enter 3 real numbers using GUI. print the sum if all 3 numbers are positive, print the product of the 2 positive numbers if one is negative, use one nested if statement. all output should be to dialog and the console. ...... So i have most of this program done, i just cant figure out how to get the product of the 2 positve numbers to print to the console and dialog. Here is what I have so far.... i know the if and else if statements are correct, i just dont know how to get the if and else if to print to console and dialog correctly for the product.

import java.util.Scanner;
import javax.swing.JOptionPane;
public class Project3 {
public static float getFloat(){
  String s = JOptionPane.showInputDialog("Enter a real number");
  return Float.parseFloat(s);
}

public static void main(String[] args) {
  float x = getFloat();
  float y = getFloat();
  float z = getFloat();
  // if all three are positive print the sum
  if(x>0 && y>0 && z>0) System.out.printf("Sum:%8.2f\n", (x+y+z));
  //if two of the three are positive print the product
  else if((x>0 && y>0 && z<0)||(x>0 && y<0 && z>0)||(x<0 && y>0 && z>0));
  System.out.printf("Product:%8.2f",((x*y)||(x*z)||(y*z)));
  
  
  JOptionPane.showMessageDialog(null, "Sum:"+(x+y+z)+"\nProduct:"+((x*y)||(x*z)||(y*z)));
  
  
  Scanner scan=new Scanner(System.in);
  System.out.println("\nEnter 2 real numbers");
  // if both are negative print the quotient
  float a = scan.nextFloat();
  float b = scan.nextFloat();
  if(a<0 && b<0) System.out.printf("Quotient:%8.2f", (a/b));
  JOptionPane.showMessageDialog(null, "Quotient:"+(a/b));
  
  
  System.exit(0);
}

}

Solutions

Expert Solution

import java.util.Scanner;
import javax.swing.JOptionPane;
public class Project3 {
public static float getFloat(){
String s = JOptionPane.showInputDialog("Enter a real number");
return Float.parseFloat(s);
}
public static void main(String[] args) {
float x = getFloat();
float y = getFloat();
float z = getFloat();
float sum=x+y+z;
float product=0.0f;
// if all three are positive print the sum
if(x>0 && y>0 && z>0) System.out.printf("Sum:'%8.2f'\n", sum );
//if two of the three are positive print the product
else if((x>0 && y>0 && z<0)||(x>0 && y<0 && z>0)||(x<0 && y>0 && z>0))
{
if(x>0 && y>0 && z<0)
{
product=x*y;
System.out.printf("Product:'%8.2f'", product);
}
else if(x>0 && y<0 && z>0)
{
product=x*z;
System.out.printf("Product:'%8.2f'" ,product);
}
else if(x<0 && y>0 && z>0)
{
product=y*z;
System.out.printf("Product:'%8.2f'", product);
}
}

String Result="Sum:" + sum + "&Product:" +product ;
JOptionPane.showMessageDialog(null, Result);
  
  
Scanner scan=new Scanner(System.in);
System.out.println("\nEnter 2 real numbers");
// if both are negative print the quotient
float a = scan.nextFloat();
float b = scan.nextFloat();
if(a<0 && b<0) System.out.printf("Quotient:%8.2f", (a/b));
JOptionPane.showMessageDialog(null, "Quotient:"+(a/b));
  
  
System.exit(0);
}
}


Related Solutions

Program: Java (using eclipse) Write a program that asks the user to enter today’s sales for...
Program: Java (using eclipse) Write a program that asks the user to enter today’s sales for five stores. The program should then display a bar chart comparing each store’s sales. Create each bar in the bar chart by displaying a row or asterisks. Each asterisk should represent $100 of sales. You must use a loop to print the bar chart!!!! If the user enters a negative value for the sales amount, the program will keep asking the user to enter...
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...
USING THE SWITCH STATEMENT - Write a java program that asks the user to enter the...
USING THE SWITCH STATEMENT - Write a java program that asks the user to enter the number of their favorite month of the year – obviously, that would be 1 – 12. Write a switch statement that takes the number and converts it to the fully spelled out name [ex. 3 would be MARCH] . Be sure to build in error message to catch any invalid data entries such as 0 or 13 etc. Print out the number that was...
Write a JAVA program in eclipse (write comment in every line) that asks the user to...
Write a JAVA program in eclipse (write comment in every line) that asks the user to enter two numbers and an operator (+, -, *, / or %), obtain them from the user and prints their sum, product, difference, quotient or remainder depending on the operator. You need to use OOP techniques (objects, classes, methods….). This program must be place in a loop that runs 3 times.
Write a Java program in Eclipse to calculate the total cost to enter into a waterpark...
Write a Java program in Eclipse to calculate the total cost to enter into a waterpark for a family/group Ticket rates are listed below kids (Age 12 and under)=$ 12.50 Regular( Age between12 -55) =$ 18.25 Seniors (55 and above) = $15.00 Tax =7% Your program should ask the user to enter the total number of members in their group. User needs to enter the ages for all members. Write appropriate error messages for invalid entries. An array should be...
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