Mr. Meinzen - Advanced Placement Computer Science - A

"Success is the ability to go from one failure to another with no loss of enthusiasm." Winston Churchill

AP CSA Lab Assignment : Program 6 : Divisibility

Write an application class that has the following methods:

  1. A method that reads in 4 different integers from a data file and which THROWS an Exception (i.e. no TRY...CATCH blocks)

  2. A method that finds the Greatest Common Factor of the first two numbers.

  3. A method that finds the Least Common Multiple of the 2nd two numbers.

  4. An appropriate main method that calls and prints out the results of the above 3 methods

 

AP CSA Lab Assignment : Program 7 : Intro to Analysis : Big-Oh and Asymptotic Behavior

 

Write one application class to accomplish the following:

  1. Determine how to store the system time in a variable as well as print the time out in milliseconds or nanoseconds. Make sure any times recorded for running each method are non-zero. If you cannot achieve a non-zero time for any method, write an explanation in your comments as specified in Part V below.

  2. Write a static method called generateRandomArray() that will instantiate a field variable that is an integer array of 10,000 elements--or any power of 10 size such as 100,000 integers--that gives a non-zero time in milliseconds as specified in 1. above). This array shold be filled with random integers. Also write a separate static method called generateSortedArray() that will generate a field that is an array of pre-sorted integers (lowest to highest).

    1. Note: To actually use very large arrays, you may have to increase the memory set aside by the Java Virtual Machine (JVM). This can be done using the JVM argument -Xms256m (or -Xms512m) to set aside 256 MB (or 512MB) memory. The default JVM is often around 128MB which can store an "int" array size less than 32,000,000.
    2. For an Eclipse Project, from the Menu: Run > Run Configurations... > Arguments Tab. In the VM Arguments textbox, type -Xms256m
  3. Write a static method that will sort the array using BubbleSort

  4. Create a file called "data.txt" containing a single integer. This will be your target.

  5. Write a static method called getTarget() to read the integer from the file "data.txt" into an int field called target

  6. Write a static method called binarySearch() that will search the above sorted array for the target using the binary search algorithm. The return value should be a boolean determining if the target was found in the array or not.

  7. Write a main method that will:

    • Determine and print out the time it takes to call generateRandomArray() for the array field. Do the same for generateSortedArray().

    • Determine and print out the time it takes to sort the array.

    • Determine whether the target was in the array and how much time it took to determine search for the target.

As a comment at the bottom of your class, write the answers the following questions.

  1. For the generateRandomArray() method :

    1. What "n" did you choose for the problem and what is O(n)? _______for random _________________for pre-sorted

    2. What was the time the generate...Array() method took to run? t1 = ____________for random; t1 =_________________for pre-sorted

    3. Run the simulation again using using 10 times the number of elements in question 1

    4. What was the time the constructor took to run? t2 = ____________for random; t2 =_________________for pre-sorted

    5. What is t2 / t1 and how does it relate to O(n) from question (i)? ___________________for random _________________for pre-sorted

  2. For the sort method :

    1. What "n" did you choose for the problem and what is O(n)? ___________________________

    2. What was the time the sort took to run? t1 = ____________________________

    3. Run the simulation again using 10 times the number of elements in question 1

    4. What was the time the sort took to run? t2 = ____________________________

    5. What is t2 / t1 and how does it relate to O(n) from question (i)? ____________________

  3. For search method:

    1. What "n" did you choose for the problem and what is O(n)? ___________________________

    2. What was the time the search took to run? t1 = ____________________________

    3. Run the simulation again using using 10 times the number of elements in question 1

    4. What was the time the search took to run? t2 = ____________________________

    5. What is t2 / t1 and how does it relate to O(n) from question (i)? ____________________

  4. Based upon the above answers, did the time correspond with the "Big-Oh" calculations for each of the three methods? constructor? __yes / no______ sorting? _____yes / no_________ searching? ___yes / no__________

  5. Clarify any discrepencies between O(n) theory and what you found in practice:

AP CSA Lab Assignment : Program 8 : Parameters & Poorly Written Code Specifications

 

public void swap() {t = x; x = y; y = t;}

 

Write a single application program that demonstrates the following five public swap methods.

  • The main method should print out the name of the method as well as the values for the variables both before and after the swap.

  • Note1: only the main() method should print. In other words, the other methods should NOT have System.out.println() statements,

  • Note2: Some descriptions may need clarification from your teacher...check with your teacher if you are unsure. Part of this assignment is to communicate with the boss!

  1. A method to swap the values of two static field variables.

  2. A method to swap values of two local parameters..

  3. A method with two local variables that calls another private method to swap the values of the two local variables.

  4. A method with the following header that swaps two elements of a primitive array. public void swapArray(int a[], int index1, int index2)

  5. A method with the following header that swaps two values of two elements of a List. (Hint: ArrayList is a List) public void swapList (List a, int index1, int index2)

Rules for 2018-19

  1. Teams may submit via email questions (aka "Submission") only once every-other-day.

  2. Each Submission must include the date of the Submission and can contain a maximum of 5 questions.

  3. All Submissions must be contained in one file (aka "Document").

  4. The Document must include the team name as well as the team roster. The first name on the roster will be the person who will communicate with the boss.

  5. The boss will save the Document with responses to questions on the student G: drive as
    "G:\\EHS Courses\AP Computer Science\2-classAssignments\Team-Name"

AP CSA Lab Assignment : Program 9 : Insertion & Selection Sorts

Write an application that has the following methods:

  1. A static method that generates a:

    • List of 100 random integers (use Integer class).

    • an array of 100 random integers (use Integer class)

  2. A method that sorts the List using the Insertion Sort with a header of:

    public static void insertionSort(List a) or insertionSort(List <Integer> a)
  3. A method that sorts the array using the Selection Sort with a header of:

    public static void selectionSort(Comparable a[]) or selectionSort(Comparable<Integer> a[])
  4. A method that prints the List and array with the following header and details

    • public static void print(List a, Comparable b[], String msg) throws Exception
    • output to a file
    • prints appropriate message line and 10 numbers per line.
    • See example below.
  5. A main method that calls and prints out the original List and array as well as the List and array after the calls to the sorting methods.

 

Example output:

 

The original List is:

-1, 5, 17, 22, 1002, 3, -4, 7, 10, 0,

4, 44, 16, 33, ... .

 

The original array is:

6, 7, 2, -7, 177, 56, 98, 12, 323, 12 ... .

 

The sorted List is:

-4, -1, 0, 3, 4, 5, 7, 10, 16, 17,

22, 33, 44, 1002 ... .

 

The sorted array is: ... .

AP CSA Lab Assignment : Program 10 : Merge (and/or Quick) Sort

Write an application class called Sorts that has one of the above two sorting method signatures:

  • public static void quickSort(int[])
  • public static void mergeSort(List<Comparable>)

You will be expected to deliver your program on disk to your teacher to be tested with his data / JUnit test.

  • for either program, include a private recursive method and a separate, non-recursive partition (or merge) method

  • include as test cases, an odd-sized array and an even-sized array as well as a random, preSorted, and reverse preSorted arrays [note: the odd and even-sized arrays can be combined with the 3 random and sorted arrays]

 

Example Input:

-1 5 17 22 1002 3 -4 7 10 0 4 44

 

Example output:

The original array is:

-1, 5, 17, 22, 1002, 3, -4, 7, 10, 0, 4, 44.

The sorted List is:

-4, -1, 0, 3, 4, 5, 7, 10, 17, 22,

44, 1002.

AP CSA Lab Assignment : Program 11 : Intro to Exemplar Lab(s)

  • Download and install/unzip the 3 Exemplar Labs [Magpie, PictureLab, Elevens]
  • Demonstrate the operation of each lab using Eclipse
  • As demonstrated in class, write the UML Class Card (using 3x5 or 4x6 notecards) for each java file in the Lab(s) as specified below.
  • Answer the previous year's Free Response question on the Exemplar Lab(s) (handout from Mr. M)

 

The following UML class diagrams of Mapie Student Activity 5 are to be made:

  • Base class: Magpie5.java

The following UML class diagrams of PictureLab Student Activity are to be made:

  • Base classes: SimplePicture.java; Pixel.java
  • Interfaces & Abstract classes: DigitalPicture.java
  • Testing classes, Implemented Interfaces, GUI, & Realized Abstract classes: PictureTester.java
  • Extended classes: Picture.java

 

The following UML class diagrams of Elevens Student Activity are to be made:

  • Base classes: Card.java; Deck.java
  •  

NOTE: There are a total of 8 UML class diagrams to be hand-written.

Sample program to demonstrate File and Console Reading/Writing using the Scanner class.

 

List of useful classes and methods:

  • Scanner.java - allows use and parsing of various types of input from keyboard (i.e. console), files, networks, and String variables

    • String next() // gets the next String token in the input. Note: each String "token" is typically a single word that is separated from other words whitespace (i.e. spaces, tabs, end of lines, etc.)

    • String nextLine() // returns a full line as a single String including whitespace. Does not break up individual words.

    • int nextInt(), long nextLong(), double nextDouble(), etc. // gets the next primitive value specified in the input.

    • boolean hasNext() // determines if the input has more information to be read in or not.

    • boolean hasNextLine() // determines if the input has another line of input or not...useful for files

    • boolean hasNextInt(), boolean hasNextLong(), boolean hasNextDouble(), etc. // determines if the input has another primitive value available in the scanner.

  • FileReader.java - opens an already-existing file to be read in as a series of characters (as opposed to binary data)

  • BufferedReader.java - converts a series of characters (i.e. stream) into text such as Strings, arrays of characters, and lines.

  • FileWriter.java - allows output to be written to file as a series of characters (as opposed to binary data). Whether or not a file is available or may be created depends upon the underlying platform.

  • BufferedWriter.java - writes text to a character-output stream, buffering characters so as to provide for writing of single characters, arrays, and strings.

  • PrintWriter.java - writes tex and objects (via toString() method) t to a character-output stream using formatted output. Similar to BufferedWriter.

Sample Code for Console Input/Output:

          
/** Example of Java console I/O using Scanner class */

import java.util.Scanner;

class IOexample
{
   public static void main(String argv[])
   {
      String s;
      int    i;
      double d;

      Scanner in = new Scanner (System.in);
	  
      // Read a string
      System.out.print("Enter a string without spaces: ");
      s = in.next();

      // Read an integer
      System.out.print("Enter an int: ");
      i = in.nextInt();

      // Read a double
      System.out.print("Enter a double: ");
      d = in.nextDouble();

      // Print out results
      System.out.println("The string is: " + s);
      System.out.println("The integer is: " + i);
      System.out.println("The double is: " + d);
   }  // end main
}  // end IOexample


Sample Code for File Input/Output:

 

/** Example of Java File I/O using Scanner class */
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.util.*;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.*;          

public class FileTester {           
     public static void main(String[] args) {           
          String fileInputName  = "testReadData.txt";
          String fileOutputName = "testWriteData.txt";
          Scanner sc = null;           
          try {
              sc = new Scanner(new BufferedReader(new FileReader(fileInputName)));
              while (sc.hasNextLine()) {           
 
                  // for reading Strings
                  String line = sc.nextLine();
                  System.out.println("read string = "+line);           
                  
                  // for reading integers
                  int i = sc.nextInt();
                  System.out.println("read integer = "+i);
              }
          }
          catch (FileNotFoundException e) {
              e.printStackTrace();
          }
          finally {
              if (sc != null)
                  sc.close();  // regardless we need to close the scanned file.
          }           
          
          // printing output to a file
          BufferedWriter out = null;
          try {
              out = new BufferedWriter(new FileWriter(fileOutputName));
              out.write(Double.toString(3.141592)+'\n'); //added an end-of-line character
              out.write(Integer.toString(7));            // so that this line is outputted
              out.write("help");                         // on the next line.
              out.close();
          }
          catch(IOException e) {
              e.printStackTrace();
          }
      }
}