Thursday, August 11, 2011

Assignment 10


Exercise 4
a)
import java.util.Scanner;
public class Division2{
   public static void main(String[] args){

      Scanner stdIn = new Scanner (System.in);
      boolean zero = true;
      double n;
      int d = 0;
       
      System.out.print("\nEnter numerator: ");
      n = stdIn.nextDouble();
       
      System.out.print("Enter divisor: ");
      d = stdIn.nextInt();      
       
      if (d ==0){
         do{
            System.out.print("Enter divisor: ");
            d = stdIn.nextInt();
            zero = false;
            }
         while(d == 0);
        }  
       
        System.out.println(n / d);
    }
}

b)
import java.util.Scanner;
import java.util.*;
public class Division2b{
    public static void main(String[] args){
        Scanner stdIn = new Scanner (System.in);

        boolean ok = true;
        int n;
        int d = 0;
        int result;  
       
        System.out.print("Enter numerator: ");
        n = stdIn.nextInt();
        System.out.print("Enter divisor: ");
        d = stdIn.nextInt();  
       
        do{
            try{
                result = n/d;
                System.out.println(result);
                ok = true;
            }
            catch (ArithmeticException mistake){  
                System.out.print("Invalid Entry. Enter divisor: ");
                d = stdIn.nextInt();
                ok = false;
            }
        }
       while(ok == false);
    }
}



Exercise 5)
import java.util.*;
import java.io.*;

public class WordsInFile{
   public static void main(String[] args){

   Scanner stdIn = new Scanner(System.in);
   Scanner fileIn;
   int numWords = 0;

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


      while (fileIn.hasNextLine()){
         String file = fileIn.next();
         numWords++;
         }


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

   catch(FileNotFoundException e){
      System.out.println("Invalid Filename");
      }
   catch(Exception e){
      System.out.println("Error reading from the file.");
      }
   }
}

Sunday, August 7, 2011

Assignment 9

Problem 11
i = 5
factorial = 5

Problem 12
import java.util.Scanner;

public class Test{
    public static void main(String[] args){
        Scanner stdIn = new Scanner(System.in);
        String entry;

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

Problem 8
1. creates an infinite loop
2. change doIt(); to super.doIt();

Problem 12
structural and member beam, I
building and floor,C
company and fixed assets, C
employee and salesperson, I
forest and tree, C
bird and robin, I
class and method, C
neurosis and paranoia, I

Problem 3
sparky = bark, bark  
lassie = Animal@62f72617

Animals extends to the Dog class.
Bark is pulled from the dog class.
Lassie is pulled straight from Animal, which returns the memory location.

Problem 8
Dog lassie = new Dog("lassie");
Cat fluffy = new Cat("fluffy");

Monday, August 1, 2011

Assignment 8

 #6

System.arraycopy(allSalaries, 2, workerSalaries, 0, 5);



 #7

list = temp; should be changed to
System.arraycopy(temp, 0, list, 0, 3);