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

Thursday, August 4, 2011

Assignment 9 Exercises for chapter 11, 12, 13

/**********************************************
*T'Nora Green
*Assignment 9, Chapter 11,  Exercise 11
***********************************************/

i= 5, factorial = 1


/**********************************************
*T'Nora Green
*Assignment 9, Chapter 11,  Exercise 12
***********************************************/

for(;!entry = q;)
{
System.out.println("Enter 'q' to quit: ");
entry = stdIn.nextLine();
}



/*********************************************
*T'Nora Green
*Assignment 9, Chapter 12,  Exercise 8
**********************************************/

a)     doIt(); calls the subclass's doIt method again, instead of calling the superclass's doIt; method,\ that creates an infinite loop that causes only a blank screen to show.


b) change doIt()to super.doIt()


/***********************************************
*T'Nora Green
*Assignment 9, Chapter 12,  Exercise 12
************************************************/

structural member \beam: inheritance
building \floor: composition
company \fixed assets: composition
employee\ salesperson:  inheritance
forest \tree: composition
bird\robin:  inheritance
class \method:  composition
neurosis\ paranoia:  inheritance



/*********************************************
*T'Nora Green
*Assignment 9, Chapter 13,  Exercise 3
**********************************************/

output:
sparky = bark, bark     lassie = Animal@4fe5e2c3
Press any key to continue . . .


how the output is generated:
The variable sparky was instantiated as a Dog, and Lassie was instantiated as an animal. When the JVM sees sparky inside the println statement, the JVM calls Dog's toString method. When the JVM sees Lassie inside the println statement, the JVM calls Object's toString method.

/*********************************************
*T'Nora Green
*Assignment 9, Chapter 13,  Exercise 8
**********************************************/

Dog lassie = (Dog) animals[0];
Cat fluffy = (Cat) animals[1];