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

Sunday, July 31, 2011

Assignment 8 Exercise 6 & 7

/************************************
*T'Nora Green
*Assignment 8, Exercise 6
*************************************/
System.arraycopy(allSalaries, 2, workerSalaries, 0, 5);


/************************************
*T'Nora Green
*Assignment 
8, Exercise 7
*************************************/
a: Homer Flanders Apu
b: System.arraycopy(temp,0,list,0,list.length);

Tuesday, July 26, 2011

Assignment 7 Exercise 6 & 10

/************************************
*T'Nora Green
*Assignment 7, Exercise 6

*************************************/

public class Test
{
  private int x;
  private static int y;
  public void doIt()
  {
    x = 1;
    y = 2;
  }
  public static void tryIt()
  {
    x = 3;                               
    y = 4;
  }
  public static void main(String[] args)
  {
    doIt();                             
    tryIt();
    Test t = new Test();
    t.doIt();
    Test.doIt();                         
    Test.tryIt();
  }
} // end Test class

Reasons for errors from the debugger:
non-static variable x cannot be referenced from the static tryIt method
non-static method doIt() cannot be referenced from the static main method
 non-static method doIt cannot be referenced  instance method using a dot prefix.


/************************************
*T'Nora Green
*Assignment 7, Exercise 10
*************************************/
Yes, its legal to omit the PennyJar dot prefixes in the PennyJar class. In the PennyJarDriver class PennyJar.getAllPennies() is needed and cannot be omitted.

Saturday, July 23, 2011

Assignment 6 Exercise 4, 12 & 5

/************************************
*T'Nora Green
*Assignment 6, Exercise 4
*************************************/


public void swapHardDrive(Computer otherComputer)

{

  String temp; //temporary storage location

temp = otherComputer.hardDrive;
otherComputer.hardDrive = this.hardDrive;
this.hardDrive = temp;

}//end swapHardDrive.





/************************************
*T'Nora Green
*Assignment 6, Exercise 12
*************************************/


The two classes gave the output of:


10
20
30
60
50





/************************************
*T'Nora Green
*Assignment 6, Exercise 5
*************************************/


 public boolean copyTo(Car5 newCar)
 {
 
   String truth;

  if (newCar.make == this.make && newCar.year == this.year && newCar.color == this.color)
     {
      return false;
      }//end if statement
 
else{

      newCar = this;
      return true;
  

     }//end else statement


     }//end of boolean

Tuesday, July 12, 2011

Assignment 4 Exercise 7

/************************************
*T'Nora Green
*Assignment 4, Exercise 7
*************************************/

import java.util.Scanner;

public class ExtractLine
{
public static void main(String[] args)
{
Scanner stdIn = new Scanner(System.in);
String songs =
"1. Bow Wow - Fresh Azimiz\n" +
"2. Weezer - Beverly Hills\n" +
"3. Dave Matthews Band - Crash Into me\n" +
"4. Sheryl Crow - Leaving Las Vegas\n";

String songNum; //song number that is searched for
int songIndex; //position of where song number is found
int eolIndex; //position of end of line character
String song; //the specified line

System.out.print("Enter song number: ");
songNum = stdIn.nextLine();

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

System.out.println(song);
}//end main
}//end class ExtractLine

Assignment 4 Exercise 6

/************************************
*T'Nora Green
*Assignment 4, Exercise 6
*************************************/
import java.util.Scanner;

public class CountSubstringOccurrences
{
public static void main(String[] args)
{
Scanner stdIn= new Scanner(System.in);
String songs =
" 1. Green Day - American Idiot\n" +
" 2. Jesus Jones - Right Here, Right Now\n" +
" 3. Indigo Girls - Closer to Fine\n" +
" 4. Peter Tosh - Equal Rights\n";

String searchText;  // text that is searched for
int foundIndex=0; // position of where text is found
int count = 0; // number of occurences of search text

System.out.print("Enter search text: ");
searchText= stdIn.nextLine();

while(foundIndex <= songs.length())
{
foundIndex += songs.indexOf(searchText, foundIndex);
count++;
} //end loop

System.out.println("Number of occurences of \"" + searchText + "\": " + count);

} //end main
} //end class CountSubstringOccurrences

Monday, July 11, 2011

Assignment 3 Exercise 5

/************************************
*T'Nora Green
*Assignment 3, Exercise 5
*************************************/
import java.util.Scanner;

public class ProductEvenInts
{
public static void main(String[] args)
{
Scanner stdIn = new Scanner(System.in);
int i, num, product;

System.out.print("Enter a positive even number: ");
num = stdIn.nextInt();

product=1;
i=2;


do
{
product *= i;
i+=2;
}


while (i <= num);


System.out.println("Product = " + product);
}//end main
}//end class ProductEvenInts

Sunday, July 10, 2011

Assignment 3 Excercise 3

/************************************
*T'Nora Green
*Assignment 3, Exercise 3
*************************************/
public class Numbers

{
public static void main (String[] args)
{
int count = 0;
int sum = 0;
int product = 1;
do
{
count++;
sum += count;
product *= count;
if (count == 5)
{
System.out.println("Sum = " + sum);
System.out.println("Product = " + product);
}
}
while (count < 5);

} //end main
} //end class Numbers

// solutions:
//must add a class heading
//change the placement of the opening and closing braces to inclose class and main method appropriately.
// change int product to 1
//add opening an closing braces around the if statement
//add semicolon to the end of the while statement

Wednesday, July 6, 2011

A{KA}cademics First

Please excuse my blog while it is reconstructed and edited for the collegiate arena.

Thanks! : )

Buckle up

Welcome to my blog! From here we will be flying first class, and at the highest altitude to ensure a smooth ride. Buckle up and enjoy the fun, especially the view during landing, from your window seat of course!