Skip to content

Instantly share code, notes, and snippets.

@Iboatwright
Last active December 20, 2017 20:01
Show Gist options
  • Select an option

  • Save Iboatwright/7235b6bd467ce83d08d339b5f1586f06 to your computer and use it in GitHub Desktop.

Select an option

Save Iboatwright/7235b6bd467ce83d08d339b5f1586f06 to your computer and use it in GitHub Desktop.
COP3003 Exam 3 study notes

COP3003 Exam 3 Study Note

Topics covered

Ordered by date introduced 3. chapter 20: Generic Classes and Methods

  • There is nothing in the study guide directly on generics
  • Likely it will be a part of something else, but not the focus
  1. chapter 16: Collections (Interface Collections, Class Collections)
  • Lists - Ordered by insertion, duplicates allowed
  • Sets - not ordered, only uniques
  • Maps - not ordered, key-value association
  • Collection Algorithms
  • NOTE Collections.sort only works on lists
  • reference HW4 for an example of interface Comparable and compareTo
  • see TimeComparator below
  • Class Arrays
  • PriorityQueue
  1. chapter 17: Lambdas and Streams
  • Single Abstract Method Interfaces and Lambda Expressions
  • Streams
  • Lazy evaluation vs Eager evaluation * lazy are stateless operations. see LazyEvalExample
  1. chapter 23: Concurrency

Types of Questions

  • given incomplete code and output
    • these seem to be based on a major topic of the section
    • Midterm I demonstrate understanding of scope. (class variables, private, use of getters)
    • Midterm II demonstrate understanding of swing layouts, buttons and adding. (borderlayout, gridlayout, buttons, add)
    1. make it do something based on a set of contraints
  • given code: follow variables what's the output and why?
    • these questions don't seem to be related to the section
    • Engine, cars, Ford:hybrid, borken
    • ** My method of tracking changes ** based on the IntelliJ debugger
      • notation I use is var1Name = {Type@lineNumber}{Type.var1 = value, Type.var2 = value}
      • {Type@lineNumber} is a reference to that object in memory
      • I read each line of code, when a variable is instantiated I write down the line number followed by the above syntax: e.g. 12) p1 = {Point@12}{x=10,y=20}
      • when a change is made to that object's instance variables I update on the line of the change only the variable that was changed 16) p1 = {Point@12}{y=30}
      • I also write any output statements on the line they occur 18) (10, 30)
  • from a recent lab explain how something complicated works
    • Lab2: Student, GradStudent, GradTA (implicit calls to super to access private vars)
    • Lab4: Screen Saver Spiral (how Listeners and Handlers work)
    • What are some potentially complicated topic in the last section?
  • Given code, find and correct the errors. Explain.
    • Midterm I abstract classes and constructor calls
    • Midterm II exception handling (try-catch, throws, exception hierarchy)
    • Where in this section did he talk about potential errors?
  • Given skeleton code, main method, and the output fill in the missing code.
    • Midterm I This was a generic straightforward question.
    • Midterm II Rewrite Lab4 (ScreenSaver Spiral) to make wheel movement and click do something new.
      • The BS for this one was having to redrawn the existing points at the new location.

Lab6 (multipart)

Reactor uses threads to combine Hydrogen and Oxygen atoms to form water molecules.

What does Lab6 do?

main();

  1. creates an object that acts like the buffer area; This object has the synchronized methods, thus is the synchronized object.
  2. createscreates a thread from a new object -> for each class;
  3. calls the Thread.start() method on each thread object;
bufferClass();
  1. creates counter variables to track what actions each method can perform
  2. responds to the synchronized methods; each method must have synchronized before the return type; potential exception (throws InterruptedException);
    • each method triggers wait() on the calling thread based on condition of counters;
    •  when the condition is met and a thread returns from waiting, the counter(s) are altered, output is printed, and notifyAll() is called to bring the other threads out of the wait area and to test their conditions. this thread continues to execute the code immediately following the buffer.method call;
  3. each thread from main() is created from a seperate class (not always the case, but is so here);
    • each class implements Runnable;
    • each class has a class variable to reference the synchronized buffer object;
    • each class has a single method Override from Runnable -> run();
    • run() loops a set number of times (class dependant);
    • each iteration wraps a try-catch around actions;
    • first action invokes the buffer.method() passing the current iteration (i);
    • next action sleeps this Thread for a set duration to coordinate frequency this object interacts with the buffer. (3 frequency sets: initial slow, rapid, stablized);
    • on completion of the loop the run terminates and the thread ends.
"It's possible this will be extended to a different more complicated molecule"
"Also could decompose a molecule into parts"

Best Guess Questions

item) topic -> concept

  • Q1) either
    • Add to a JButton an ActionListener which pops a JOptionPane that displays the button’s text.
JButton button = new JButton("My Button");
button.addActionListener(e->JOptionPane.showMessageDialog(null, e.getActionCommand()));
* Sort a list of 2 int arrays each of which represents {minute, second}
int [][] times = new int[][]{{45, 23},{12, 25},{59, 59},{2, 12}};
Arrays.sort(times, (t1,t2)->(t1[0]!=t2[0])?(t1[0]-t2[0]):(t1[1]-t2[1]));
  • Q4 - find and fix errors) He'll probably give a concurrent code example with issues such as
    • missing synchronized label in method declaration
    • using Thread.sleep(x) instead of wait();  * something to do with thread calls by OS are non-deterministic

Code samples

TimeComparator

class TimeComparator implements Comparator<Time>{
     public int compare(Time time1, Time time2){
         int hourCompare=time1.getHour()-time2.getHour();
         (hourCompare!=0) ? (return hourCompare);
     
         int minuteCompare=time1.getMinute()-time2.getMinute();
         (minuteCompare!=0) ? (return minuteCompare);

         int secondCompare=time1.getSecond()-time2.getSecond();
         return secondCompare;
     }
}

LazyEvalExample

while(input.hasNextLine()){

  // This is a lazy evaluation. It doesn't do anything to the stream yet.
  Stream<String> processNext = Arrays.stream(input.nextLine()
        .split("\\W+")
        .filter(t->!t.equals(""))
        .map(String::toLowerCase); 
        
  // This is a terminal operation. Most terminal ops are eager (excluding .iterate())
  mySet.addAll(processNest.collect(Collectors.toList()));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment