Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.gui > #2637 > unrolled thread

Files & Methods

Started by"cb17890" <cb17890@THRWHITE.remove-dii-this>
First post2011-04-27 15:40 +0000
Last post2011-04-27 15:40 +0000
Articles 4 — 4 participants

Back to article view | Back to comp.lang.java.gui


Contents

  Files & Methods "cb17890" <cb17890@THRWHITE.remove-dii-this> - 2011-04-27 15:40 +0000
    Re: Files & Methods "Andrew Thompson" <andrew.thompson@THRWHITE.remove-dii-this> - 2011-04-27 15:40 +0000
    Re: Files & Methods "Roedy Green" <roedy.green@THRWHITE.remove-dii-this> - 2011-04-27 15:40 +0000
    Re: Files & Methods "bcr666" <bcr666@THRWHITE.remove-dii-this> - 2011-04-27 15:40 +0000

#2637 — Files & Methods

From"cb17890" <cb17890@THRWHITE.remove-dii-this>
Date2011-04-27 15:40 +0000
SubjectFiles & Methods
Message-ID<1192320301.965684.190780@v29g2000prd.googlegroups.com>
  To: comp.lang.java.gui
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();
     }
}

---
 * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

[toc] | [next] | [standalone]


#2642

From"Andrew Thompson" <andrew.thompson@THRWHITE.remove-dii-this>
Date2011-04-27 15:40 +0000
Message-ID<79adc8b6e5793@uwe>
In reply to#2637
  To: comp.lang.java.gui
cb17890@yahoo.com wrote:
>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.

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

Message posted via JavaKB.com
http://www.javakb.com/Uwe/Forums.aspx/java-gui/200710/1

---
 * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

[toc] | [prev] | [next] | [standalone]


#2643

From"Roedy Green" <roedy.green@THRWHITE.remove-dii-this>
Date2011-04-27 15:40 +0000
Message-ID<5f14h35i50hvmi3eo6nb4cls3go2skvgi2@4ax.com>
In reply to#2637
  To: comp.lang.java.gui
On Sat, 13 Oct 2007 17:05:01 -0700, cb17890@yahoo.com wrote, quoted or
indirectly quoted someone who said :

> 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.
-- 
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com

---
 * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

[toc] | [prev] | [next] | [standalone]


#2647

From"bcr666" <bcr666@THRWHITE.remove-dii-this>
Date2011-04-27 15:40 +0000
Message-ID<1192480781.063163.239640@y27g2000pre.googlegroups.com>
In reply to#2637
  To: comp.lang.java.gui
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.
>
> 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();
>      }
>
> }

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 + ")");

  }
}

---
 * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.gui


csiph-web