Re: Files & Methods
- From: "Jeff Higgins" <oohiggins@xxxxxxxxx>
- Date: Sun, 14 Oct 2007 00:12:05 -0400
Jeff Higgins wrote:
cb17890@xxxxxxxxx wrote:
Hi, I'm a new JAVA Programmer and need some help with my homework. My
homework is a multi-part assignment.
My assignment is to modify the code to find and print the sum of the
same series from 1/2 through 1/n, where n is read in from a data
file. The file series1.dat which contains a single positive integer,
can be found in the common area of my personal K: drive. My code
that I have written is not correct. A lot of error messages occur.
import EDU.oswego.cs.dl.util.concurrent.misc.Fraction;
//
<http://gee.cs.oswego.edu/dl/classes/EDU/oswego/cs/dl/util/concurrent/misc/Fraction.java>
public class SumTest
{
public static void main(String[] args)
{
int arg = 6;
Fraction sum = new Fraction(1, 2);
for(int i = 3; i < arg + 1; i++)
{
Fraction current = new Fraction(1, i);
System.out.print(sum.toString() + " + "
+ current.toString()) ;
sum = sum.plus(current);
System.out.println(" = " + sum);
}
}
}
Oops!
Try this for large values of arg.
<http://jscience.org/>
import org.jscience.mathematics.number.LargeInteger;
import org.jscience.mathematics.number.Rational;
public class SumTest
{
public static void main(String[] args)
{
int arg = 1025;
Rational sum = Rational.valueOf(
LargeInteger.ONE, LargeInteger.valueOf(2L));
long start = System.currentTimeMillis();
for(int i = 3; i < arg; i++)
{
sum = sum.plus(Rational.valueOf(
LargeInteger.ONE, LargeInteger.valueOf(i)));
}
long end = System.currentTimeMillis();
System.out.println(sum.toString());
System.out.println(
"Sum as double = " + sum.doubleValue());
System.out.println("Elapsed time: " + (end - start));
}
}
.
- References:
- Files & Methods
- From: cb17890
- Re: Files & Methods
- From: Jeff Higgins
- Files & Methods
- Prev by Date: Re: Files & Methods
- Next by Date: Re: rotating text problem
- Previous by thread: Re: Files & Methods
- Next by thread: Re: Files & Methods
- Index(es):
Relevant Pages
|
|