### Thread: JAVA Calculator Problem

1. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Jul 2013
Posts
8
Rep Power
0

#### JAVA Calculator Problem

import java.io.*;
import java.util.*;

public class exercise1
{

public static void main(String args[])
{
System.out.println("Selection from the choices below:\n");
System.out.println(" 2. Subtraction");
System.out.println(" 3. Multiplication");
System.out.println(" 4. Division");
System.out.println(" 5. Modulo\n");

System.out.print(" Your choice? ");

Scanner kbReader = new Scanner(System.in);
int choice = kbReader.nextInt();

if((choice<=5) && (choice>0))
{

System.out.print("\nEnter first number: ");
double num1 = kbReader.nextDouble();
System.out.print("Enter second number: ");
double num2 = kbReader.nextDouble();

System.out.println("");

switch (choice)
{
case 1: //addition 1 decimal
System.out.println(num1 + " plus " + num2 + " = " + (num1 + num2) );
break;

case 2: //subtraction no decimal
System.out.println(num1 + " minus " + num2 + " = " + (num1 - num2) );
break;

case 3: //multiplication 3 decimal
System.out.println(num1 + " times " + num2 + " = " + (num1 * num2) );
break;

case 4: //division 4 decimal
System.out.println(num1 + " divided by " + num2 + " = " + (num1 / num2) );
break;

case 5: //modulo
System.out.println(num1 + " modulo " + num2 + " = " + (num1 % num2) );
//break;
}

}
else
{
System.out.println("Please enter a 1, 2, 3, 4 or 5.");
}
}
}
2. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Jul 2013
Posts
8
Rep Power
0
pls help me for my activity. its a simple calculator that outputs decimal place required for our instructor
1. addition = 1 decimal
2. subtraction = no decimal place
3. multiplacation = 3 decimal place
4. division = 4 decimal place
5. modulus
Thank u
3. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Feb 2012
Posts
103
Rep Power
7
What problem are you experiencing? Do you have a specific question about the code?

If your question is about how to round values appropriately, then you can use Math.round
http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/Math.html
Last edited by Cameron0960; July 4th, 2013 at 07:22 PM.
4. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Jul 2013
Posts
8
Rep Power
0
[QUOTE=Cameron0960]What problem are you experiencing? Do you have a specific question about the code?

If your question is about how to round values appropriately, then you can use Math.round

the problem is the output doesn't answer the required decimal place.
5. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Feb 2012
Posts
103
Rep Power
7
[QUOTE=lordrey22]
Originally Posted by Cameron0960
What problem are you experiencing? Do you have a specific question about the code?

If your question is about how to round values appropriately, then you can use Math.round

the problem is the output doesn't answer the required decimal place.
I gotcha. You could use the DecimalFormatter class.

double d = 4.44444444
DecimalFormat df = new DecimalFormat("#.##");
System.out.print(df.format(d));
will return 4.44

Course I don't know what you've been taught in class. So that might not be what the professor wants you to use. They may want you to use modulo or division to get the correct result. If this is a first year programming course, then you are likely supposed to use modulo or division.
6. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Jul 2013
Posts
8
Rep Power
0
[QUOTE=Cameron0960]
Originally Posted by lordrey22

I gotcha. You could use the DecimalFormatter class.

double d = 4.44444444
DecimalFormat df = new DecimalFormat("#.##");
System.out.print(df.format(d));
will return 4.44

Course I don't know what you've been taught in class. So that might not be what the professor wants you to use. They may want you to use modulo or division to get the correct result. If this is a first year programming course, then you are likely supposed to use modulo or division.
okay thanks a lot. i let it try.