Code

Document Sample
Code Powered By Docstoc
					Exercise 4.1

import java.io.*;

public class Factorial {

      public static void main(String[] args) {
           int n=0;
           double fact=1;
           System.out.println("Enter a number");
           BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
             try {
                    n = Integer.parseInt(br.readLine());
             } catch (NumberFormatException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
             } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
             }
             if (n>0)
         for (int i=n;i>1;i=i-2)
         {
        fact = fact * i * (i-1);
         }
         System.out.println("The factorial of " + n + " is "+ fact);
      }
Exercise 4.2

import   java.io.BufferedReader;
import   java.io.FileNotFoundException;
import   java.io.FileReader;
import   java.io.IOException;

public class Average {

         public static void main(String[] args) {

               try {
                  BufferedReader br = new BufferedReader(new
FileReader(args[0]));
                  String line;
                  int sum = 0, count = 0, avg = 0;
                  while((line=br.readLine())!=null)
                  {
                    sum = sum + Integer.parseInt(line);//Modified code
                    count++;
                  }
                  if (count!=0)
                  {
                    avg = sum/count;
                  }

                 System.out.println("Number of integers = " + count);
                 System.out.println("Average of integers = " + avg);

               } catch (FileNotFoundException e1)   {
                     // TODO Auto-generated catch   block
                     e1.printStackTrace();
         } catch (NumberFormatException e) {
                     // TODO Auto-generated catch   block
                     e.printStackTrace();
               } catch (IOException e) {
                     // TODO Auto-generated catch   block
                     e.printStackTrace();
               }
         }
}
Exercise 4.3


import java.io.*;

public class Writer {

      public static void main(String[] args) {
            try {
                  BufferedReader br = new BufferedReader(new
FileReader(args[0]));
                  String line;
                  String emails = "";
                  while((line=br.readLine())!=null)
                  {
                      String[] arr = line.toLowerCase().split(":");
                      emails = emails + arr[1].charAt(0) +
arr[0].charAt(0) + arr[2].substring(5,9) + "@se.depaul.edu \r\n";

                  }
                  PrintWriter out = new PrintWriter(
                                        new BufferedWriter(
                                        new FileWriter(args[1])));
                  out.print(emails);
                  out.flush();
                  out.close();
            } catch (FileNotFoundException e1) {
                  // TODO Auto-generated catch block
                  e1.printStackTrace();
      } catch (IOException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
            }
      }
}
Exercise 4.7a

package myprog.single;//Package creation. Remaining code is unchanged
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Average {

      public static void main(String[] args) {
          BufferedReader br;
            try {
                  br = new BufferedReader(new FileReader(args[0]));
                  String line;
                  int sum = 0, count = 0, avg = 0;
                  while((line=br.readLine())!=null)
                  {
                    sum = sum + Integer.parseInt(line);
                    count++;
                  }
                  if (count!=0)
                  {
                    avg = sum/count;
                  }

                System.out.println("Number of integers = " + count);
                System.out.println("Average of integers = " + avg);

            } catch (FileNotFoundException e1)   {
                  // TODO Auto-generated catch   block
                  e1.printStackTrace();
      } catch (NumberFormatException e) {
                  // TODO Auto-generated catch   block
                  e.printStackTrace();
            } catch (IOException e) {
                  // TODO Auto-generated catch   block
                  e.printStackTrace();
            }
      }
}
Exercise 4.8a, 4.8b


import    java.io.BufferedReader;
import    java.io.FileNotFoundException;
import    java.io.FileReader;
import    java.io.IOException;
import    java.io.InputStreamReader;

public class Average {

         public static void main(String[] args) {
             BufferedReader br;
               try {
                     br = new BufferedReader(new FileReader(args[0]));
                     String line;
                     int sum = 0, count = 0, avg = 0;
                     while((line=br.readLine())!=null)
                     {
                         try{
                            sum = sum + Integer.parseInt(line);
                            count++;
                         }
                            catch (NumberFormatException e) {
                         }// Number Format exception is ignored by doing
                     nothing in catch field
                     }
                     if (count!=0)
                     {
                       avg = sum/count;
                     }

                 System.out.println("Number of integers = " + count);
                 System.out.println("Average of integers = " + avg);

            } catch (FileNotFoundException e1) {
                  System.out.println("File not found! Input file name
is incorrect.");
      } catch (IOException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
            }
      }
}

				
DOCUMENT INFO