Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

New Free Tools on Dev Shed!

#1
July 4th, 2013, 10:50 AM
 lordrey22
Registered User

Join Date: Jul 2013
Posts: 8
Time spent in forums: 57 m
Reputation Power: 0
Homework - 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");

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

System.out.print("\nEnter first number: ");
System.out.print("Enter second number: ");

System.out.println("");

switch (choice)
{
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
July 4th, 2013, 10:52 AM
 lordrey22
Registered User

Join Date: Jul 2013
Posts: 8
Time spent in forums: 57 m
Reputation Power: 0
pls help me for my activity. its a simple calculator that outputs decimal place required for our instructor
2. subtraction = no decimal place
3. multiplacation = 3 decimal place
4. division = 4 decimal place
5. modulus
Thank u

#3
July 4th, 2013, 05:32 PM
 Cameron0960
Contributing User

Join Date: Feb 2012
Posts: 103
Time spent in forums: 1 Day 28 m 24 sec
Reputation Power: 2
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 08:22 PM.

#4
July 4th, 2013, 08:47 PM
 lordrey22
Registered User

Join Date: Jul 2013
Posts: 8
Time spent in forums: 57 m
Reputation 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
July 4th, 2013, 08:50 PM
 Cameron0960
Contributing User

Join Date: Feb 2012
Posts: 103
Time spent in forums: 1 Day 28 m 24 sec
Reputation Power: 2
Quote:
 Originally Posted by lordrey22 [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.

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
July 4th, 2013, 09:09 PM
 lordrey22
Registered User

Join Date: Jul 2013
Posts: 8
Time spent in forums: 57 m
Reputation Power: 0
Quote:
 Originally Posted by Cameron0960 [QUOTE=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.

 Viewing: Dev Shed Forums > Programming Languages > Java Help > Homework - JAVA Calculator Problem