473,407 Members | 2,676 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

Calculations using different data types

I have an assignment that wants me to write an application that takes 5 numbers from a user. As the user inputs the numbers I have to make them into a double, float, long, int and byte. All I need after this is to output the result of the following calculation and store the result in an int variable.
Calculation to do
Subtract the floatValue from the intValue, then multiply by the doubleValue, then divide by the longValue, then multiply by the byteValue and then divide by 2. I am to store the result in an int variable. I can get the user input but am at a loss as to how to do the calculation and store the result in an int variable. Can someone out in cyberworld help? Thanks to anyone who responds.

CODE
import java.util.*;


public class TestNumbers
{
public static void main(String args[])
{

Scanner input = new Scanner(System.in);

int result;

System.out.println("Enter a number:");
double num1 = input.nextDouble();


System.out.println();
System.out.println("Enter second number:");
float num2 = input.nextFloat();


System.out.println();
System.out.println("Enter third number:");
long num3 = input.nextLong();


System.out.println();
System.out.println("Enter fourth number:");
int num4 = input.nextInt();


System.out.println();
System.out.println("Enter fifth number:");
byte num5 = input.nextByte();


System.out.println();

}

}
Jun 26 '08 #1
12 2421
JosAH
11,448 Expert 8TB
Subtract the floatValue from the intValue, then multiply by the doubleValue, then divide by the longValue, then multiply by the byteValue and then divide by 2. I am to store the result in an int variable. I can get the user input but am at a loss as to how to do the calculation and store the result in an int variable.
That assignment text almost reads like COBOL; what is the problem? Assume
the variables are fv, iv, dv, lv and bv. The formula reads as:

(iv-fv)*dv/lv*bv/2

The result needs to be an int again so you have to cast. In Java that reads:

Expand|Select|Wrap|Line Numbers
  1. int result= (int)((iv-fv)*dv/lv*bv/2);
  2.  
kind regards,

Jos
Jun 26 '08 #2
BigDaddyLH
1,216 Expert 1GB
What you have to do is spelled out in detail, no?

1. Subtract the floatValue from the intValue,
2. then multiply by the doubleValue,
3. then divide by the longValue,
4. then multiply by the byteValue and
5. then divide by 2.
6. I am to store the result in an int variable.

My advice is to try to do this and post it if you still have questions.
Jun 26 '08 #3
JosAH
11,448 Expert 8TB
What you have to do is spelled out in detail, no?
Admit it: you programmed in COBOL too didn't you?

kind regards,

Jos ;-)
Jun 26 '08 #4
Nepomuk
3,112 Expert 2GB
Expand|Select|Wrap|Line Numbers
  1. import java.util.*;
  2.  
  3. public class TestNumbers
  4. {
  5.     public static void main(String args[])
  6.     {
  7.  
  8.         Scanner input = new Scanner(System.in);
  9.  
  10.         int result;
  11.  
  12.         System.out.println("Enter a number:");
  13.         double num1 = input.nextDouble();
  14.  
  15.         System.out.println();
  16.         System.out.println("Enter second number:");
  17.         float num2 = input.nextFloat();
  18.  
  19.         System.out.println();
  20.         System.out.println("Enter third number:");
  21.         long num3 = input.nextLong();
  22.  
  23.         System.out.println();
  24.         System.out.println("Enter fourth number:");
  25.         int num4 = input.nextInt();
  26.  
  27.         System.out.println();
  28.         System.out.println("Enter fifth number:");
  29.         byte num5 = input.nextByte();
  30.  
  31.         System.out.println();
  32.     }
  33. }
Hi jlgraham!
Calculations in Java aren't difficult at all. There are a lot of automatic conversions. Just make sure, you don't loose precision.
The data types you named are sorted as follows: (top being the most precise)
  1. double
  2. float
  3. long
  4. int
  5. byte
For example:
Subtract the floatValue from the intValue
Expand|Select|Wrap|Line Numbers
  1. float res1=num4-num2;
Of course, you can use double from the beginning. You should be able to figure the rest of the calculations out yourself.

To save the result as an integer, use
Expand|Select|Wrap|Line Numbers
  1. int result = (int) res5;
Greetings,
Nepomuk
Jun 26 '08 #5
That assignment text almost reads like COBOL; what is the problem? Assume
the variables are fv, iv, dv, lv and bv. The formula reads as:

(iv-fv)*dv/lv*bv/2

The result needs to be an int again so you have to cast. In Java that reads:

Expand|Select|Wrap|Line Numbers
  1. int result= (int)((iv-fv)*dv/lv*bv/2);
  2.  
kind regards,

Jos
I tried this and it still will not work. I am posting the code and I still get errors. Is it because I am using the Scanner and determining the data types as the user enters them? It will not allow me to cast the result to an integer. Please help
[code]
import java.util.*;


public class TestNumbers
{
public static void main(String args[])
{

Scanner input = new Scanner(System.in);

int result;

System.out.println("Enter a number:");
double num1 = input.nextDouble();

System.out.println();
System.out.println("Enter second number:");
float num2 = input.nextFloat();

System.out.println();
System.out.println("Enter third number:");
long num3 = input.nextLong();

System.out.println();
System.out.println("Enter fourth number:");
int num4 = input.nextInt();

System.out.println();
System.out.println("Enter fifth number:");
byte num5 = input.nextByte();

int result= (int)((num2 - num4) * numl / num3 * num5 / 2);

System.out.println("Result is: " + result);

}

}
[code]
Jun 26 '08 #6
Expand|Select|Wrap|Line Numbers
  1. import java.util.*;
  2.  
  3. public class TestNumbers
  4. {
  5.     public static void main(String args[])
  6.     {
  7.  
  8.         Scanner input = new Scanner(System.in);
  9.  
  10.         int result;
  11.  
  12.         System.out.println("Enter a number:");
  13.         double num1 = input.nextDouble();
  14.  
  15.         System.out.println();
  16.         System.out.println("Enter second number:");
  17.         float num2 = input.nextFloat();
  18.  
  19.         System.out.println();
  20.         System.out.println("Enter third number:");
  21.         long num3 = input.nextLong();
  22.  
  23.         System.out.println();
  24.         System.out.println("Enter fourth number:");
  25.         int num4 = input.nextInt();
  26.  
  27.         System.out.println();
  28.         System.out.println("Enter fifth number:");
  29.         byte num5 = input.nextByte();
  30.  
  31.         System.out.println();
  32.     }
  33. }
Hi jlgraham!
Calculations in Java aren't difficult at all. There are a lot of automatic conversions. Just make sure, you don't loose precision.
The data types you named are sorted as follows: (top being the most precise)
  1. double
  2. float
  3. long
  4. int
  5. byte
For example:
Expand|Select|Wrap|Line Numbers
  1. float res1=num4-num2;
Of course, you can use double from the beginning. You should be able to figure the rest of the calculations out yourself.

To save the result as an integer, use
Expand|Select|Wrap|Line Numbers
  1. int result = (int) res5;
Greetings,
Nepomuk
Thank you . It worked doing one calculation at a time.
Jun 26 '08 #7
Admit it: you programmed in COBOL too didn't you?

kind regards,

Jos ;-)
I am 63 and I have never seen a programming code until I signed up for Java 1. I find it very difficult and sure have a lot of respect for you youngsters and how much you know and can do.
Jun 26 '08 #8
JosAH
11,448 Expert 8TB
I am 63 and I have never seen a programming code until I signed up for Java 1. I find it very difficult and sure have a lot of respect for you youngsters and how much you know and can do.
Well, I'm 51 which is a whoooole lot younger than you are of course but I'm
not exactly a youngster anymore ;-)

I appreciate it that you're taking up Java programming, I started with this
mysery (not Java) when I was 18 and computers were made of wood; my compliments.

kind regards,

Jos
Jun 26 '08 #9
RedSon
5,000 Expert 4TB
Well, I'm 51 which is a whoooole lot younger than you are of course but I'm
not exactly a youngster anymore ;-)

I appreciate it that you're taking up Java programming, I started with this
mysery (not Java) when I was 18 and computers were made of wood; my compliments.

kind regards,

Jos
Wood!?1 Didn't you bash little holes into stone tablets and then give them to an elephant to compile together?
Jun 26 '08 #10
BigDaddyLH
1,216 Expert 1GB
Wood!?1 Didn't you bash little holes into stone tablets and then give them to an elephant to compile together?
Elephant Compiler? Luxury!
Jun 26 '08 #11
RedSon
5,000 Expert 4TB
Elephant Compiler? Luxury!
I suppose you had a dozen monkeys with abacuses?
Jun 26 '08 #12
JosAH
11,448 Expert 8TB
I suppose you had a dozen monkeys with abacuses?
Abacuses? Poofter, we used the knuckles of our diseased sibblings to do our
calculations on the bottom of our lake where we lived; if our dad wouldn't beat
us; as he did every night; we had it rough ...

kind regards,

Jos ;-)
Jun 26 '08 #13

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Jennifer | last post by:
I'm running SQL query to caluclate projected food costs. The calculation is this: (ReportedFoodSales / PlanFoodSales) * FullPlanFoodSales Seems simple enough to me. Using the following...
11
by: lduperval | last post by:
Hi, I`m trying to do date calculations in three types of time zones: local, GMT and specified. The issue I am facing is that I need to be able to specify a date in the proper time zone, and I`m...
1
by: christian | last post by:
Hi all, I'm creating a TimeSheet Database and I need to calculate two different set of times in one field (workedhours).Does anybody knows a formula that will allow me to add(Hours and...
3
by: brian kaufmann | last post by:
Hi, I had sent this earlier, and would appreciate any suggestions on this. I need to make calculations for unemployment rate for three different data sources (A,B,C) for many countries and age...
0
by: Lokkju | last post by:
I am pretty much lost here - I am trying to create a managed c++ wrapper for this dll, so that I can use it from c#/vb.net, however, it does not conform to any standard style of coding I have seen....
6
by: Scott M. Lyon | last post by:
As I mentioned in my other post, I'm attempting to, using COM Interop so I can update existing VB6 code to (for several specific functions) return a Hashtable from a .NET library. I've had...
11
by: Martin Joergensen | last post by:
Hi, I've encountered a really, *really*, REALLY strange error :-) I have a for-loop and after 8 runs I get strange results...... I mean: A really strange result.... I'm calculating...
10
by: Robert | last post by:
How do you get an accurate count of the number of records returned from a query when using linked tables. I have an access 2003 database as a front end to another access 2003 database that...
1
by: Grubsy4u | last post by:
Grubsy4u Newbie 7 Posts October 5th, 2007 11:31 AM #1 Report calculations --------------------------------------------------------------------------------
4
by: Pedro Graca | last post by:
Is there a way (preprocessor magic, perhaps ???) to use a type with twice as many bits as `unsigned` (or `int`) with no changes to the source code? ==== #define SINGLE_TYPE unsigned #define...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.