Wednesday, August 10, 2011

Assignment 10 Exercises for chapters 14 & 15

/**********************************************
*T'Nora Green
*Assignment 10, Chapter 14,  Exercise 4
***********************************************/


Part A:

import java.util.Scanner;

public class Division2
{
  public static void main(String[] args)
  {
    Scanner stdIn = new Scanner(System.in);
    double n;
    int d;

    System.out.print("Enter numerator: ");
    n = stdIn.nextDouble();
    do
    {
      System.out.print("Enter divisor: ");
      d = stdIn.nextInt();
    } while (d == 0);
    System.out.println(n / d);
  } // end main
} // end Division2 class

Part B:

import java.util.Scanner;

public class Division2
{
  public static void main(String[] args)
  {
    Scanner stdIn;
    double n;
    int d;
    boolean OK = false;

 while(!OK)
    {
      stdIn = new Scanner(System.in);
      try
      {
        System.out.print("Enter numerator: ");
        n = stdIn.nextDouble();
        
do
        {
          System.out.print("Enter divisor: ");
          d = stdIn.nextInt();
 } while (d == 0);
        System.out.println(n / d);
        OK = true;
      } // end try
      catch (Exception e)
      {
        System.out.println("Error!");
      }
    }
  } // end main
} // end Division2 class



/**********************************************
*T'Nora Green
*Assignment 10, Chapter 15,  Exercise 15
***********************************************/
Added Fragment:



System.out.print("Enter full pathname of file: ");
fileIn = new Scanner(new FileReader(stdIn.nextLine()));

while (fileIn.hasNextLine())
{
String file = fileIn.next();
numWords++;
}   //end of while loop

System.out.println("Number of words = " + numWords); 

fileIn.close();}  //end of try

No comments:

Post a Comment