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

Wednesday, July 27, 2011

Assignment 7 # 10

PennyJar.getAllPennies() is needed in PennyJarDriver.java
PennyJar may be omitted in the PennyJar Class

Assignment 7 # 6

public class Test
{

private int x;
private static int y;
public void doIt()
{
x = 1;
y = 2;
}


public void tryIt() //cannot be static
{
x = 3; //x is in a static method - cannot be referenced
y = 4;
}
public static void main(String[] args)
{
doIt(); //object instance must be created
tryIt() //object instance must be created

Test t = new Test();
t.doIt();

Test.tryIt(); //must use t.tryIt();

}
}

Assignment 6

Ch7, #4:
public void swapHardDrive (Computer otherComputer)
{
String temp;
temp = otherComputer.hardDrive;
otherComputer.hardDrive = this.hardDrive;
this.hardDrive = temp;
}



Ch7, #12
10
20
30
60
50



Ch8, #5:
public boolean copyTo(Car5 newCar)
{
if(this.make.isEmpty()||this.year.isEmpty()||this.color.isEmpty())
{
return false;
}
else
{
newCar.make = this.make;
newCar.year = this.year;
newCar.color = this.color;
return true;
}
}

Friday, July 15, 2011

Assignment 4 - #7

songIndex = songs.indexOf(songNum);
eolIndex = songs.indexOf('\n', songIndex);
song = songs.substring(songIndex, eolIndex);