Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / October 2007

Tip: Looking for answers? Try searching our database.

Files & Methods

Thread view: 
cb17890@yahoo.com - 14 Oct 2007 01:05 GMT
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.

The code to modify:

import java.util.Scanner;
import java.io.*;

public class Summations
{
     public static void main(String[] args) throws IOException
    {
          int value;
          int sumPart = 0;

          // The first part of the text.
          System.out.print("The summation of the series");

        // Loop from 2 to 65536, double value each loop.

          for (value = 2; value <= 65536; value = value * 2)
         {
              sumPart = sumPart + value;

              if (value > 2)
              {
                    System.out.print(" + ");
              }
              System.out.print(" 1/" + value);
         }
     System.out.print(" = 1/" + sumPart + " ");

    // Calculate the results.
    double calced = 1 / (double) sumPart;
    System.out.println("(" + calced + ")");
    }
}

The code that I have written to complete the assignment is:

import java.util.Scanner;
import java.io.*;

public class Summations
{
   public static void main (String[] args) throws IOException
   {
         int n;
         int value;
         int sumPart = 0;
         int number;
         String filename;

        // Create a Scanner object for keyboard input.
        Scanner keyboard = new Scanner(System.in);

       // Get the filename.
       System.out.print("Enter the filename: ");
       filename = keyboard.nextLine();

        // Open the file.
       FileReader freader = new FileReader(filename);
       BufferedReader inputFile = new BufferedReader(freader);

       // Reading data from a file.
       number = inputFile.readInt();

      // Loop from 2 to n, doulbe value each loop.

        for (value = 2; value <= n; value = value * 2)
       {
               sumPart = sumPart + value;

               if (value > 2)
               {
                       System.out.print(" + ");
               }
              System.out.print(" 1/" + value);
       }

      // Print results.
      outputFile.print("The summation of the series");

     System.out.println(" = 1/" + sumPart + " ");

     // Calculate the results
     double calced = 1 / (double) sumPart;
     System.out.println("(" + calced +")");

    // Close the file.
     inputFile.close();
    }
}
Jeff Higgins - 14 Oct 2007 02:14 GMT
> Hi, I'm a new JAVA Programmer and need some help with my homework.  My
> homework is a multi-part assignment.
[quoted text clipped - 4 lines]
> 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/Fracti
on.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);
   }
 }
}
Jeff Higgins - 14 Oct 2007 05:12 GMT
>> Hi, I'm a new JAVA Programmer and need some help with my homework.  My
>> homework is a multi-part assignment.
[quoted text clipped - 24 lines]
>  }
> }

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));
 }
}
Roedy Green - 14 Oct 2007 13:05 GMT
> A lot of error messages occur.

see http://mindprod.com/jgloss/errormessages.html
to learn what the message mean and what sorts of things cause them.
Signature

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com

Andrew Thompson - 14 Oct 2007 14:01 GMT
>Hi, I'm a new JAVA Programmer ...

I note a couple of people have already provided some
assistance, I'll give another couple of tips.

A good group for folks new to Java is comp.lang.java.help.
Once you become more proficient with Java and the
newsgroups, it is well worth tuning into
comp.lang.java.programmer.  This group is comp.lang.java.gui,
which is especially intended for discussions about
the development of Graphical User Interfaces - the buttons,
menus and such the user clicks on.

Since your enquiry had little or nothing to do with GUIs
(that I could see), I suggest that maybe c.l.j.help would
have been the best group for your message.

Another thing is that the word Java is not an acronym or
abbreviation, so it should not be all 'UPPER' case, but
it is a proper name/noun, so 'Java' is correct.

Signature

Andrew Thompson
http://www.athompson.info/andrew/

bcr666 - 15 Oct 2007 21:39 GMT
On Oct 13, 7:05 pm, cb17...@yahoo.com wrote:
> Hi, I'm a new JAVA Programmer and need some help with my homework.  My
> homework is a multi-part assignment.
[quoted text clipped - 97 lines]
>
> }

I don't know what a Scanner object is, it isn't in my JDK, so I did a
work around using a BufferedReader and InputStreamReader.
Notice also that I changed the calculation of "sumPart" and added a
"finalValue" in.  The calculations used in the code that you were to
modify, while workable would give you a wrong answer to the math
problem involved.  For example 1/2 + 1/4 = 3/4 not 1/6, or 1/2 + 1/4 +
1/8 = 7/8 not 1/14.  So I used "sumPart" to keep track of the
numerator, and "finalValue" to keep track of the denominator.  You
need to find a common denominator (in this case the new denominator is
always 2x the preceeding denominator) and multiply top and bottom of
both fractions by the common denominator, then do your add.  So now
1/2 + 1/4 + 1/8 = 3/4 + 1/8 = 7/8.

import java.io.*;

public class Summations
{
 public static void main(String[] args) throws IOException {
   int value = 0;
   int sumPart = 0;
   int number = 0;
   int finalValue = 0;
   String filename = "";

   // Get the filename.
   System.out.print("Enter the filename: ");
   BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
   filename = br.readLine();

   // Open the file.
   FileReader freader = new FileReader(filename);
   BufferedReader inputFile = new BufferedReader(freader);

   // Reading data from a file.
   try {
     number = Integer.parseInt(inputFile.readLine());
   }
   catch (IOException ex) {
     System.out.println("There was a problem reading the file.");
     return;
   }
   catch (NumberFormatException ex) {
     System.out.println("Could not convert file contents to a
number.");
     return;
   }
   finally {
     // Close the file.
     inputFile.close();
   }

   // Loop from 2 to n, doulbe value each loop.

   // Print results.
   System.out.print("The summation of the series ");

   for (value = 2; value <= number; value *= 2) {
     sumPart = sumPart * 2 + 1;

     if (value > 2) {
       System.out.print(" + ");
     }
     System.out.print(" 1/" + value);
     finalValue = value;
   }

   System.out.println(" = " + sumPart + "/" + finalValue + " ");

   // Calculate the results
   double calced = (double)(sumPart) / (double) finalValue;
   System.out.println("(" + calced + ")");

 }
}
Lew - 16 Oct 2007 00:07 GMT
cb17...@yahoo.com wrote:
>> import java.util.Scanner;

> I don't know what a Scanner object is, it isn't in my JDK,

Are you quite sure?  What version of the JDK are you using?

java.util.Scanner has been part of the API since Java 5, something over three
years now.
<http://java.sun.com/javase/6/docs/api/java/util/Scanner.html>

If you're using 1.4 or earlier, be aware that Java 1.4 is already in its
End-of-Life phase, and earlier versions completed theirs some time ago.

Signature

Lew

bcr666 - 16 Oct 2007 21:38 GMT
> cb17...@yahoo.com wrote:
> >> import java.util.Scanner;
[quoted text clipped - 11 lines]
> --
> Lew

I am using JDK 1.4.2_13. My company started converting all of our
mainframe programs to Java. We tried to upgrade to 1.5 but found we
would have to recompile all of our programs with the new JDK.  We are
under a deadline to get the project done this year. After that, I hope
to talk them into upgrading the JDK.
Lew - 16 Oct 2007 21:53 GMT
> I am using JDK 1.4.2_13. My company started converting all of our
> mainframe programs to Java. We tried to upgrade to 1.5 but found we
> would have to recompile all of our programs with the new JDK.  We are
> under a deadline to get the project done this year. After that, I hope
> to talk them into upgrading the JDK.

All your programs?

What was the incompatibility?

Signature

Lew



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.