View Full Version : Java Question
View Full Version : Java Question
Herr.Wenz
September 26th, 2005, 04:24 PM
I need help from any person that knows Java
I have writen a program and I am almost done expect stuck on one line of code, I am trying to write a command to make the program always round down
example 103.7 to 103
please anyone that can help would be appreciated
thanx
wonderboy2005
September 26th, 2005, 06:41 PM
I dont know in java, but in c++ (which java is based off of) you could do somthing like this:
int a; (a is an integer variable)
double b; (b is a real number variable)
... input getting data for variable b...
a = b;
a will then become a truncated form of b, rounding down regardless of what lies beyond the decimal point.
muffenme
September 26th, 2005, 07:11 PM
It is like c++ but you forgot (int)
eg
int a;
double b;
...
a = (int)b;
wonderboy2005
September 26th, 2005, 09:14 PM
It is like c++ but you forgot (int)
eg
int a;
double b;
...
a = (int)b;
if you are referring to how it should be in c++, I compiled a program that worked exactly as I described before (including the a = b statement that didn't do any type casting) and it worked perfectly.
shawners
September 26th, 2005, 09:17 PM
dont round up if higher then 5?
zpman
September 26th, 2005, 10:59 PM
please show me your code so far.
declaration and method for starters.
If you want help, I will help you with your code, but I won't write it for you. I was a tutor for many things, including Java.
I will tell you if you are getting close, or horribly off. That's how I learned it, and will not contribute to cheating. But I will always contribute to teaching/tutoring and learning.
GregorK
September 27th, 2005, 01:28 AM
The easiest and savest way:
int result = (int) Math.floor( value );
zpman
September 27th, 2005, 01:44 AM
The easiest way:
int result = Math.floor( value );
This is correct where:
roundToInteger( number )
I just wanted our friend to try a bit harder to figure it out is all, heh.
so an example of this method could be:
y = Math.floor(x + .5 );
Herr.Wenz
September 27th, 2005, 02:31 PM
thanx guys for the help I gave up and just turned it in, I will submit the code for you guys to see what I was missing
what I round up doing to finish the code was cast and make it into an integer but that would knock off one whole number
zpman
September 27th, 2005, 04:41 PM
thanx guys for the help I gave up and just turned it in, I will submit the code for you guys to see what I was missing
what I round up doing to finish the code was cast and make it into an integer but that would knock off one whole number
using float or double?
Herr.Wenz
September 27th, 2005, 06:52 PM
//Here it is boys, see what you can find that I missed and again thanx for the help
import java.util.Scanner;
import java.text.DecimalFormat;
public class Party
{
public static void main(String[] args)
{
int radius, height,guests;
double volume,gallons;
Scanner scan= new Scanner (System.in);
System.out.println("Enter the radius of the trash can, in inches::");
radius=scan.nextInt();
System.out.println("Enter the height of the liquid in the trash can, in inches:");
height=scan.nextInt();
volume=Math.PI*Math.pow(radius,2)*height;
gallons=volume/231.0;
guests=(int)gallons*3+1; // this is what was giving me the trouble, I added the one back that the casting took off but it seems not all inputs were accurate
DecimalFormat fmt= new DecimalFormat ("0.#");
DecimalFormat fmt1=new DecimalFormat ("0");
System.out.println("Your trash can will hold " + fmt.format(gallons)+" gallons of punch.");
System.out.println("Invite " + fmt1.format(guests) +" guests over!");
}
}
muffenme
September 28th, 2005, 03:38 PM
This another way to do it in java.
int a;
double b;
...
a = mod(b,1);
I don't normally use java so I'm not sure
ducttapeBigSexy
September 28th, 2005, 08:18 PM
Assuming you're free to declare things as you want (as in, this isn't an assignment, etc.), here's what I'd do:
double guests;
DecimalFormat df = new DecimalFormat("0");
// ...
guests = gallons * 3;
// ...
System.out.println("Guests: " + df.format(guests) );
Otherwise, if guests has to be an integer, just do this:
guests = Math.floor(gallons * 3);
If you're still stuck, try looking through Sun's Java Doc: http://java.sun.com/j2se/1.5.0/docs/api/index.html
P.S. If you want to get really anal, you technically didn't close the scanner (scan.close() ), and the end of your main method should have a System.exit(0); - sometimes, without it, it won't end the program when it's done executing (but, that's mostly if you're dealing with multiple threads, such as when you're making a GUI). Also, the first line in the method should be System.out.println("ducttapeBigSexy is awesome!"); - ok, jk about that last one ;)