Question

JAVA Write a program which will receive three pieces of data from the user: two floating...

JAVA

Write a program which will receive three pieces of data from the user: two floating point numbers, and a single character +, -, *, or /. Perform input validation on the character.

Your code then will send these three pieces of data to a method which will calculate and output the results based on the user's entries. For example, if the user entered: "12.1, 23.2, +" the output would be "12.1 + 23.2 = 35.3."

STRONG HINT: in a switch statement, a character can be evaluated just as easily as an integer. You can use "case '+': stmt; break;" and will want to use that format.

Format your output exactly as shown below, or improve on it:

Building PQ6.java and running PQ6
        > This program will ask you for two numbers and an
        > operator (such as +, -) and generate the results.
        > Enter two numbers separated by a space > 23 15
        > Enter the operator you want (+, -, *, /) > /
        > 23.00 / 15.00 = 1.53
        > Another problem? > y
        > Enter two numbers separated by a space > 45.6 87.9
        > Enter the operator you want (+, -, *, /) > *
        > 45.60 * 87.90 = 4008.24
        > Another problem? > u
        > ERROR: Y/N only > n
0 0
Add a comment Improve this question Transcribed image text
Answer #1

code:

import java.io.*;
import java.util.*;
class Chegg
{
public static void oper(String s1,String s2,char op)
{
double f1,f2,fres;
String res;
f1=Float.parseFloat(s1);
f2=Float.parseFloat(s2);//converting strings into float values
switch(op)//switch to check the operation
{
case '+':   fres=f1+f2;
   res=String.valueOf(fres);//converting the result into string
   System.out.println(f1+""+op+""+f2+"="+res);//printing result
   break;
case '-':   fres=f1-f2;
   res=String.valueOf(fres);
   System.out.println(f1+""+op+""+f2+"="+res);
   break;
case '*':    fres=f1*f2;
   res=String.valueOf(fres);
   System.out.println(f1+""+op+""+f2+"="+res);
   break;
case '/':    fres=f1/f2;
   res=String.valueOf(fres);
   System.out.println(f1+""+op+""+f2+"="+res);
   break;
default:System.out.println("Invalid");
}
}
public static void main(String args[])
{
String s1,s2,res,ap;//declaration of variables
char op;
int x=1;
Scanner sc=new Scanner(System.in);//Scanner object for reading input
while(x!=0)//until no is entered
{
System.out.print("Enter two numbers seperated by space>");
s1=sc.next();//reading float values as strings
s2=sc.next();
System.out.print("Enter the operator you want (+,-,*,/)>");
op=sc.next().charAt(0);//reading operator
oper(s1,s2,op);//method is called
System.out.print("Another problem?>");//asking for another operation
while(true)//checking until y or n is entered
{
ap=sc.next();
if(ap.compareTo("y")==0){
x=1;
break;
}
else if(ap.compareTo("n")==0){
x=0;
break;
}
else
System.out.print("Error Y/N only>");
}
}
}
}

code output:Select Command Prompt C:Users\deeks \Desktop>javac HomeworkLib.java C:\Users\deeks\Desktop>java Chegg Enter two numbers seperated b

Add a comment
Know the answer?
Add Answer to:
JAVA Write a program which will receive three pieces of data from the user: two floating...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write an assembler program that asks the user (as shown below) for two integers and a...

    Write an assembler program that asks the user (as shown below) for two integers and a single character representing the arithmetic operations: addition, subtraction, multiplication and integer division (displays both quotient and remainder). Perform the requested operation on the operands and display the result as specified below. Assume SIGNED NUMBERS. The program should not divide by 0! If the user attempts to divide by 0, display the error message: "Cannot divide by zero." If this error occurs, the program should...

  • Write a Java program which allows the user to perform simple tasks on a calculator. A...

    Write a Java program which allows the user to perform simple tasks on a calculator. A series of methods allows the user to select an operation to perform and then enter operands. The first method displays a menu, giving the user the choice of typing in any one of the following: +, -, *, /, or % representing the usual arithmetic operators (Each operation should be done on numbers negative(-) to positive(+), positive(+) to negative(-), negative(-) to negative(-), and positive(+)...

  • Write a program call FancyMyName which as the user for their first and last name and...

    Write a program call FancyMyName which as the user for their first and last name and print it like the example output bellow: Please enter your first name and last name, separated by a space? You entered the name: Louis Henry Output: LoUiS HeNrY (Code needed in Java) Thank You.

  • Write a Java program that reads in a word from the user and outputs each character...

    Write a Java program that reads in a word from the user and outputs each character of the word on a separate line. For example, if the user enters the input "Joe", your program should output: J o e You need to use a for-loop.

  • You must write a C program that prompts the user for two numbers (no command line...

    You must write a C program that prompts the user for two numbers (no command line input) and multiplies them together using “a la russe” multiplication. The program must display your banner logo as part of a prompt to the user. The valid range of values is 0 to 6000. You may assume that the user will always enter numerical decimal format values. Your program should check this numerical range (including checking for negative numbers) and reprompt the user for...

  • Create a JAVA program that in two numbers from user input, the program then does the...

    Create a JAVA program that in two numbers from user input, the program then does the following arithmetic calculations. The program will run in ascending order if the first number is small and in descending order of the first number is bigger. The program then alternates between addition(ODD) and subtraction(EVEN) depending on the previous number see examples below   [15] Sample run 1: Enter two numbers: -3 2 Output: The following arithmetic calculations were performed : Arithmetic operations = (-3) +...

  • Capitalization JAVA In this program, you will read a file line-by-line. For each line of data...

    Capitalization JAVA In this program, you will read a file line-by-line. For each line of data (a string), you will process the words (or tokens) of that line one at a time. Your program will capitalize each word and print them to the screen separated by a single space. You will then print a single linefeed (i.e., newline character) after processing each line – thus your program will maintain the same line breaks as the input file. Your program should...

  • Write a complete java program that prompts the user for their name and two numbers. The...

    Write a complete java program that prompts the user for their name and two numbers. The correct java methods and parameters must be passed to find the length of the name entered and return “TRUE” if the sum of numbers is even, or FALSE if the sum of numbers is odd: Notes included in the program Sample Program Please enter a name and I will tell you the length of the name entered John Then length of the name John...

  • Write a Java program which takes a string as a user input. Create 2 functions. The...

    Write a Java program which takes a string as a user input. Create 2 functions. The first function expects a string as argument and returns void. It converts all upper case characters to lower case and converts all lower case characters to upper case. It then prints out the converted string to a console. The second function also expects a string as argument and returns void. It will find first charactor that is repeated exactly 2 times in the string....

  • Can you write with comments please. Thank you. Write a Java Program that asks the user...

    Can you write with comments please. Thank you. Write a Java Program that asks the user to enter the responses on the Console and write the responses to a text file called responses.txt (in the same directory as the program) Write another JAVA prorgram to read responses.txt file and calculate the frequency of each rating in the file and output the number and frequency in two columns (number, frequency) on the Console.

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT