Re: Files & Methods




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));
}
}


.



Relevant Pages

  • #VALUE! - urgent
    ... I have a sheet in MS Excel 97. ... When I sum the columns, for some of them I get ... invalid value #ARG!. ... As Stephen Bye informed #ARG!. ...
    (microsoft.public.excel.misc)
  • Re: #VALUE! - urgent
    ... I have a sheet in MS Excel 97. ... When I sum the columns, for some of them I get ... invalid value #ARG!. ... As Stephen Bye informed #ARG!. ...
    (microsoft.public.excel.misc)
  • Re: Files & Methods
    ... homework is a multi-part assignment. ... My assignment is to modify the code to find and print the sum of the ... Fraction sum = new Fraction ...
    (comp.lang.java.gui)