How to do well on the AP CS Free Response Questions

Slides:



Advertisements
Similar presentations
Big Ideas behind Inheritance. Can you think of some possible examples of inheritance hierarchies?
Advertisements

AP Computer Science TOPICS TO DISCUSS.equals() == instanceof operator compareTo Interfaces Abstract Classes.
Written by: Dr. JJ Shepherd
Arrays and ArrayLists Ananda Gunawardena. Introduction Array is a useful and powerful aggregate data structure presence in modern programming languages.
How to do well on the AP CS Free Response Questions
© A+ Computer Science - ArrayList © A+ Computer Science - Lab 16.
One Dimensional Arrays. Declaring references to array objects How would you declare a variable somearray that is an array of ints? int[] somearray;
Multiple Choice Solutions True/False a c b e d   T F.
Writing Methods Using if, loops, and variables to implement algorithms Copyright © 2012 Pearson Education, Inc.
Iteration. Adding CDs to Vic Stack In many of the programs you write, you would like to have a CD on the stack before the program runs. To do this, you.
© A+ Computer Science - Inheritance © A+ Computer Science - Lab 20.
CS125 Exam Review Winter Some Exam Info Tuesday (22nd) at 4:00-6:30pm in the PAC CHECK YOUR SEAT!!! Read Final Exam Information on website –Practice.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
APCS Java AB 2004 Review of CS1 and CS2 Review for AP test #1 Sources: 2003 Workshop notes from Chris Nevison (Colgate University) AP Study Guide to go.
Programming With Java ICS Chapter 8 Polymorphism.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Chapter 6- Arrays. Overview n What are arrays? n Declaring/initializing arrays. n Using arrays. n Arrays details. n Using arrays with classes/ in classes.
GCOC – A.P. Computer Science A. College Board Topics – A.P. Computer Science A Program Design - Read and understand a problem's description, purpose,
© A+ Computer Science - public Triangle() { setSides(0,0,0); } Constructors are similar to methods. Constructors set the properties.
How to do well on the AP CS Free Response Questions
M180: Data Structures & Algorithms in Java Arrays in Java Arab Open University 1.
ArrayList Class An ArrayList is an object that contains a sequence of elements that are ordered by position. An ArrayList is an object that contains a.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
© A+ Computer Science - Which of the following statements assigns the letter S to the third row and first column of a two-dimensional.
Aug 9, CMSC 202 ArrayList. Aug 9, What’s an Array List ArrayList is  a class in the standard Java libraries that can hold any type of object.
© A+ Computer Science - Visit us at Full Curriculum Solutions M/C Review Question Banks.
CIS 270—Application Development II Chapter 8—Classes and Objects: A Deeper Look.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Types and Interfaces COMP.
1 Intro to Computer Science Arrays Instructor: Ms. Catherine Stocker Teaching Assistants: Alex, Katie, Siraaj, Isaiah, Allison, Thibault University of.
CSE 1201 Object Oriented Programming ArrayList 1.
Lecture 8: Advanced OOP Part 2. Overview Review of Subtypes Interfaces Packages Sorting.
Written by: Dr. JJ Shepherd
Powerpoint slides from A+ Computer Science Modified by Mr. Smith for his course.
College Board Topics – A.P. Computer Science A Program Design - Read and understand a problem's description, purpose, and goals; Apply data abstraction.
© A+ Computer Science - Visit my site at Full Curriculum Solutions M/C Review Question Banks Live Programming.
Week 6 - Friday.  What did we talk about last time?  Loop examples.
This In Java, the keyword this allows an object to refer to itself. Or, in other words, this refers to the current object – the object whose method or.
Chapter 9 Introduction to Arrays Fundamentals of Java.
© A+ Computer Science - Visit us at Full Curriculum Solutions M/C Review Question Banks.
© A+ Computer Science - Visit us at Full Curriculum Solutions M/C Review Question Banks.
© A+ Computer Science - Visit us at Full Curriculum Solutions M/C Review Question Banks.
Powerpoint slides from A+ Computer Science Modified by Mr. Smith for his course.
© A+ Computer Science - Visit us at Full Curriculum Solutions M/C Review Question Banks.
Coming up Implementation vs. Interface The Truth about variables Comparing strings HashMaps.
© A+ Computer Science - Visit us at Full Curriculum Solutions M/C Review Question Banks.
1 Topic 5 Polymorphism "“Inheritance is new code that reuses old code. Polymorphism is old code that reuses new code.”
LESSON 8: INTRODUCTION TO ARRAYS. Lesson 8: Introduction To Arrays Objectives: Write programs that handle collections of similar items. Declare array.
© A+ Computer Science - Visit us at Full Curriculum Solutions M/C Review Question Banks.
© A+ Computer Science - Visit us at Full Curriculum Solutions M/C Review Question Banks.
© A+ Computer Science - Visit us at Full Curriculum Solutions M/C Review Question Banks.
© A+ Computer Science - Elevens is a lab about classes and Lists. List is a major concept being tested by the Elevens lab. Elevens.
Lecture 18: Nested Loops and Two-Dimensional Arrays
Objects as a programming concept
OOP Powerpoint slides from A+ Computer Science
Topic Dimensional Arrays
A+ Computer Science AP Review 2018 AP CS A EXAM
Multiple Choice -answer the easiest question 1st
Lesson A4 – Object Behavior
Nested Loop Review and Two-Dimensional Arrays
© A+ Computer Science - Arrays and Lists © A+ Computer Science -
© A+ Computer Science - OOP © A+ Computer Science -
The Matrix A b a d e a a a a a a a A b a d e a a a a a a a
Lecture 13: Two-Dimensional Arrays
© A+ Computer Science - Classes And Objects © A+ Computer Science -
© A+ Computer Science - OOP Pieces © A+ Computer Science -
CIS 199 Final Review.
Objects with ArrayLists as Attributes
A+ Computer Science AP Review 2019 AP CS A EXAM
What is a String? String s = "compsci"; s c o m p s i
First Semester Review.
Presentation transcript:

© A+ Computer Science - www.apluscompsci.com How to do well on the AP CS Free Response 2015 Questions © A+ Computer Science - www.apluscompsci.com

Visit us at www.apluscompsci.com Full Curriculum Solutions Provided by A+ Computer Science Visit us at www.apluscompsci.com Full Curriculum Solutions M/C Review Question Banks Live Programming Problems Tons of great content! www.facebook.com/APlusComputerScience © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com Provided by A+ Computer Science Visit us at www.apluscompsci.com Full Curriculum Solutions Slides and Examples Labs and Worksheets Tests and Quizzes Solutions provided for all www.facebook.com/APlusComputerScience © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com Free Response Questions © A+ Computer Science - www.apluscompsci.com

AP A Free Response Topics © A+ Computer Science - www.apluscompsci.com Array / Array or Arrays – for and for each loops, nested loops, array of arrays concepts Make a Class - Basic – create a basic class with instance variables, constructors, and methods ArrayList of References / Strings – get,set,remove,add,size – levels of abstraction Make a Class - Interface / Abstract – implement / extend – looks like this topic is back © A+ Computer Science - www.apluscompsci.com

Free Response Strategies © A+ Computer Science - www.apluscompsci.com -Read all 4 questions before writing anything -answer the easiest question 1st -most times question 1 is the easiest -see if part B calls part A and so on -many times part C consists of A and B calls -write something on every question -write legibly / use PENCIL!!!!!!!!!! -keep track of your time © A+ Computer Science - www.apluscompsci.com

Free Response Strategies © A+ Computer Science - www.apluscompsci.com -When writing methods -use parameter types and names as provided -do not redefine the parameters listed -do not redefine the methods provided -return from all return methods -return correct data type from return methods © A+ Computer Science - www.apluscompsci.com

Free Response Strategies © A+ Computer Science - www.apluscompsci.com -When writing a class or methods for a class -know which methods you have -know which instance variables you have -check for public/private on methods/variables -return from all return methods -return correct data type from return methods © A+ Computer Science - www.apluscompsci.com

Free Response Strategies © A+ Computer Science - www.apluscompsci.com -When extending a class -know which methods the parent contains -have the original class where you can see it -make sure you have super calls -check for public/private on methods/variables -make super calls in sub class methods as needed © A+ Computer Science - www.apluscompsci.com

Free Response Strategies © A+ Computer Science - www.apluscompsci.com -When extending abstract / implementing interface -know which methods the parent contains -have the original class where you can see it -make sure you have super calls -check for public/private on methods/variables -make super calls in sub class methods as needed -implement all abstract methods in sub class © A+ Computer Science - www.apluscompsci.com

AP A Free Response Topics © A+ Computer Science - www.apluscompsci.com Array / Array or Arrays – for and for each loops, nested loops, array of arrays concepts Make a Class - Basic – create a basic class with instance variables, constructors, and methods ArrayList of References / Strings – get,set,remove,add,size – levels of abstraction Make a Class - Interface / Abstract – implement / extend – looks like this topic is back © A+ Computer Science - www.apluscompsci.com

Arrays and Array of Arrays One question on the A test free response will require you to manipulate an array and / or an array of arrays. © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com What is a matrix? A matrix is an array of arrays. int[][] mat = new int[3][3]; 0 1 2 0 1 2 Each spot in an matrix stores the location/address of an array. mat[0] stores the location / address of a one-dimensional array. © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com AP A - Arrays int[] nums = new int[10]; //Java int array 0 1 2 3 4 5 6 7 8 9 nums An array is a group of items all of the same type which are accessed through a single identifier. © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com AP A - Arrays String[] s = new String[5]; 0 1 2 3 4 s null Arrays are automatically filled with values when instantiated. © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com Matrix Variables int size = 40; int[][] mat = {{5,7,9,2}, {5,3,4,6}, {7,0,8,9}}; int[][] intMat = new int[size][size]; //intMat is filled with zeros - 0s © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com Matrix Variables String[][] words = new String[4][4]; //words is filled with null double[][] dMat = new double[3][3]; //dMat is filled with 0.0 © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com Printing Spots int[][] mat = {{5,7,9,2,1,9}, {5,3,4}, {3,7,0,8,9}}; out.println(mat[2][1]); out.println(mat[1][2]); out.println(mat[0][3]); out.println(mat[2][4]); OUTPUT 7 4 2 9 When printing out a spot in a matrix, the row and column must be provided. out.println(mat[2][1]); This line prints spot 1 of the array referred to by mat[2]. mat[2] stores the location/address of an array. © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com Printing Spots int[][] mat = {{5,7,9,2,1,9}, {5,3,4}, {3,7,0,8,9}}; out.println(mat[7/4][0]); out.println(mat[1*2][2]); out.println(mat.length); out.println(mat[0].length); OUTPUT 5 0 3 6 When printing out a spot in a matrix, the row and column must be provided. out.println(mat[2][1]); This line prints spot 1 of the array referred to by mat[2]. mat[2] stores the location/address of an array. © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com What is a matrix? A matrix is an array of arrays. int[][] mat = new int[3][3]; mat[0][1]=2; 0 1 2 2 0 1 2 Which array? Each spot in an matrix stores the location/address of an array. mat[0] stores the location / address of a one-dimensional array. mat[0][1]=2; This line sets mat[0] spot 1 to 2. Which spot? © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com Assigning Matrix Values 0 1 2 3 4 0 1 2 3 4 mat[2][2]=7; mat[0][3]=5; mat[4][1]=3 5 7 3 mat[2] stores the location / address of a one-dimensional array. mat[2][2]=7; This line sets mat[2] spot 2 to 7. © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com Assigning Matrix Values for( int r = 0; r < mat.length; r++) { for( int c = 0; c < mat[r].length; c++) mat[r][c] = r*c; } 1 2 4 if mat was 3x3 © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com Nested Loop Review int outer=1; //start //stop //increment for(outer=1; outer<=2; outer++) { } //start //stop //increment for(int inner=1; inner<=2; inner++) out.println(outer + " " + inner); out.println(); Nested loops are very important when accessing all spots in a matrix. The outer loop is used to access each array. The inner loop is used to move from column to column across each array. OUTPUT 1 1 1 2 OUTPUT 1 1 1 2 2 1 2 2 © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com What is a matrix? A matrix is an array of arrays. int[][] mat = new int[3][3]; 0 1 2 # of arrays size of each array 0 1 2 © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com Printing an Array int[][] mat = {{5,7},{5,3,4,6},{0,8,9}}; out.println(Arrays.toString(mat[0])); out.println(Arrays.toString(mat[1])); Arrays.toString() can be used to print out each array in a matrix. OUTPUT [5, 7] [5, 3, 4, 6] © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com Printing an Array int[] nums = {1,2,3,4,5,6,7}; for(int r=0; r<nums.length; r++) { out.println(nums[r]); } OUTPUT1 2 3 4 5 6 7 To print all spots in an array, some type of loop must be used that contains a variable that increases or decreases so that each spot in the array may be visited. length returns the # of elements/items/spots in the array!!! © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com Printing a Matrix Printing a row int[][] mat = {{5,7},{5,3,4,6},{0,8,9}}; for(int r=0; r<mat.length; r++) { } for(int c=0; c<mat[r].length; c++) { out.print(mat[r][c]); } out.println(); for(int c=0; c<mat[1].length; c++) { out.print(mat[1][c]); } out.println(); When printing a matrix, the inner loop is used to move across the columns. The outer loop is used to move from row to row. Each row is an array. OUTPUT 5 3 4 6 OUTPUT 5 7 5 3 4 6 0 8 9 © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com Printing a Matrix int[][] mat = {{5,7},{5,3,4,6},{0,8,9}}; for( int[] row : mat ) { for( int num : row ) System.out.print( num + " "); } System.out.println(); The for each loop works quite well as tool to print a matrix. OUTPUT 5 7 5 3 4 6 0 8 9 © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com 2015 AP A Free Response © A+ Computer Science - www.apluscompsci.com

2015 Question 1 part A public static int arraySum( int[] arr ) { int sum = 0; for( int item : arr ) sum = sum + item; return sum; } 2015 Question 1 part A

2015 Question 1 part B public static int[] rowSums( int[][] arr2D ) { int[] ret = new int[arr2D.length]; for( int i = 0; i < ret.length; i++ ) ret[i] = arraySum(arr2D[i]); return ret; } 2015 Question 1 part B

2015 Question 1 part C – ver 1 { int[] totals = rowSums( arr2D ); public static boolean isDiverse( int[][] arr2D ) { int[] totals = rowSums( arr2D ); for( int i = 0; i < totals.length-1; i++ ) for( int j = i+1; j < totals.length; j++) if( totals[i] == totals[j] ) return false; } return true; 2015 Question 1 part C – ver 1 This version is kinda lame, but I assume most students will do something like this.

2015 Question 1 part C – ver 2 { int[] totals = rowSums( arr2D ); public static boolean isDiverse( int[][] arr2D ) { int[] totals = rowSums( arr2D ); ArrayList<Integer> used; used = new ArrayList<Integer>(); for( int item : totals ) if( used.contains( item) ) return false; else used.add( item ); } return true; 2015 Question 1 part C – ver 2 This algorithm is more interesting and more of what I would expect from my students that participate in contests.

© A+ Computer Science - www.apluscompsci.com Provided by A+ Computer Science A+ Lab Assignments that cover the same matrix concepts as FR question # 1 :: • Magic Square and Spiral Matrix • Forest and Tic-Tac-Toe • Codes and Ciphers www.apluscompsci.com © A+ Computer Science - www.apluscompsci.com

Visit us at www.apluscompsci.com Full Curriculum Solutions Provided by A+ Computer Science Visit us at www.apluscompsci.com Full Curriculum Solutions M/C Review Question Banks Live Programming Problems Tons of great content! www.facebook.com/APlusComputerScience © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com AP A Make a Class One question on the A test free response will require that you create a class sometimes using inheritance and sometimes not. You will need to create instance variables, constructors, and methods. The 2015 question was a class question that involved strings. © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com Class Components © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com classes public class Triangle { private int sideA; private int sideB; private int sideC; Instance variables Instance variables store the state information for an object. © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com constructors public Triangle(int a, int b, int c) { sideA=a; sideB=b; sideC=c; } Initialization Constructor Constructors are similar to methods. Constructors set the properties of an object to an initial state. © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com modifier methods public void setSideA(int a ) { sideA=a; } Modifier methods are methods that change the properties of an object. © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com accessor methods public int getSideA() { return sideA; } Accessor methods are methods that retrieve or grant access to the properties of an object, but do not make any changes. © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com String © A+ Computer Science - www.apluscompsci.com

frequently used methods © A+ Computer Science - www.apluscompsci.com String frequently used methods Name Use substring(x,y) returns a section of the string from x to y not including y substring(x) returns a section of the string from x to length-1 length() returns the # of chars charAt(x) returns the char at spot x indexOf(c) returns the loc of char c in the string, searching from spot 0 to spot length-1 lastIndexOf(c) returns the loc of char c in the string, searching from spot length-1 to spot 0 String is an immutable Object. String cannot be changed. All of the String methods are accessor method. All of the String methods are return methods. © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com Strings OUTPUT 4 -1 iga tors rule String sent = "alligators rule"; String find = "gato"; System.out.println( sent.indexOf( find ) ); System.out.println( sent.indexOf( "dog" ) ); System.out.println( sent.substring( 3 , 6 ) ); System.out.println( sent.substring( 6 ) ); © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com 2015 AP A Free Response © A+ Computer Science - www.apluscompsci.com

2015 Question 2 public class HiddenWord { private String word; public HiddenWord( String w ) word = w; } public String getHint( String guess ) String ret = ""; for( int i = 0; i < word.length(); i++) if( word.substring(i,i+1).equals(guess.substring(i,i+1)) ) ret += word.substring( i, i+1); else if( word.indexOf( guess.substring(i,i+1))!=-1) ret += "+"; else ret += "*"; return ret; 2015 Question 2

© A+ Computer Science - www.apluscompsci.com Provided by A+ Computer Science A+ Lab Assignments that cover the same string concepts as FR question # 2 :: • HexChecker, MorseCode • Rock-Paper-Scissors • Etch-A-Sketch www.apluscompsci.com © A+ Computer Science - www.apluscompsci.com

Visit us at www.apluscompsci.com Full Curriculum Solutions Provided by A+ Computer Science Visit us at www.apluscompsci.com Full Curriculum Solutions M/C Review Question Banks Live Programming Problems Tons of great content! www.facebook.com/APlusComputerScience © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com ArrayList © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com AP A - ArrayList A typical ArrayList question involves putting something into an ArrayList and removing something from an ArrayList. © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com AP A - ArrayList Arraylist is a class that houses an array. An ArrayList can store any type. All ArrayLists store the first reference at spot / index position 0. ArrayList can store a reference to any type of Object. ArrayList was built using an array[] of object references. © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com AP A - ArrayList int[] nums = new int[10]; //Java int array 0 1 2 3 4 5 6 7 8 9 nums An array is a group of items all of the same type which are accessed through a single identifier. © A+ Computer Science - www.apluscompsci.com

frequently used methods © A+ Computer Science - www.apluscompsci.com ArrayList frequently used methods Name Use add(item) adds item to the end of the list add(spot,item) adds item at spot – shifts items up-> set(spot,item) put item at spot z[spot]=item get(spot) returns the item at spot return z[spot] size() returns the # of items in the list remove() removes an item from the list clear() removes all items from the list import java.util.ArrayList; © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com AP A - ArrayList OUTPUT h c List ray = new ArrayList(); ray.add("hello"); ray.add("whoot"); ray.add("contests"); out.println(((String)ray.get(0)).charAt(0)); out.println(((String)ray.get(2)).charAt(0)); In the example above, ray is an ArrayList that stores Object references. In order to call non-Object methods on a spot in ray, casting would be required. ray.add(0,"hello"); out.println(((String)ray.get(0)).charAt(0)); ray stores Object references. © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com AP A - ArrayList List<String> ray; ray = new ArrayList<String>(); ray.add("hello"); ray.add("whoot"); ray.add("contests"); out.println(ray.get(0).charAt(0)); out.println(ray.get(2).charAt(0)); OUTPUT h c In the example above, ray is an ArrayList that stores String references. Casting would not be required to call non-Object methods on ray. ray.add(0,"hello"); ray.add(1,"chicken"); out.println(ray.get(0).charAt(0)); out.println(ray.get(1).charAt(5)); ray stores String references. © A+ Computer Science - www.apluscompsci.com

AP A - ArrayList Removal © A+ Computer Science - www.apluscompsci.com int spot=list.size()-1; while(spot>=0) { if(list.get(spot).equals("killIt")) list.remove(spot); spot--; } © A+ Computer Science - www.apluscompsci.com

AP A - ArrayList Removal © A+ Computer Science - www.apluscompsci.com for(int i=list.size()-1; i>=0; i--) { if(list.get(i).equals("killIt")) list.remove(i); } © A+ Computer Science - www.apluscompsci.com

AP A - ArrayList Removal © A+ Computer Science - www.apluscompsci.com int spot=0; while(spot<list.size()) { if(list.get(spot).equals("killIt")) list.remove(spot); else spot++; } © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com 2015 AP A Free Response © A+ Computer Science - www.apluscompsci.com

2015 Question 3 part A You must know ArrayList! public int getValueAt( int row , int col ) { for( SparseArrayEntry item : entries ) if( item.getRow()==row && item.getCol()==col) return item.getValue(); } return 0; You must know ArrayList!

2015 Question 3 - part B You must know ArrayList! public void removeCol( int col ) { for( int i = entries.size()-1; i>=0; i--) SparseArrayEntry e = entries.get(i); if( e.getCol()==col) entries.remove(i); } else if ( e.getCol()>col) SparseArrayEntry up; up = new SparseArrayEntry( e.getRow(), e.getCol()-1, e.getValue() ); entries.set( i, up); numCols--; You must know ArrayList!

© A+ Computer Science - www.apluscompsci.com Provided by A+ Computer Science A+ Lab Assignments that cover the same list concepts as FR question # 3 :: • MadLib, WordList, Numbers, and Toys • Moving Shapes • BlackJack – 21 www.apluscompsci.com © A+ Computer Science - www.apluscompsci.com

Visit us at www.apluscompsci.com Full Curriculum Solutions Provided by A+ Computer Science Visit us at www.apluscompsci.com Full Curriculum Solutions M/C Review Question Banks Live Programming Problems Tons of great content! www.facebook.com/APlusComputerScience © A+ Computer Science - www.apluscompsci.com

AP A - Abstract/Interface © A+ Computer Science - www.apluscompsci.com A typical Abstract/Interface question requires that a class be written that extends the abstract class or implements the interface and that all abstract method(s) be implemented. © A+ Computer Science - www.apluscompsci.com

AP A - Abstract/Interface © A+ Computer Science - www.apluscompsci.com Abstract classes are used to define a class that will be used only to build new classes. No objects will ever be instantiated from an abstract class. Abstract Classes are used to create hierarchies of classes. Abstract classes are used to setup future classes. An abstract class can not be instantiated. © A+ Computer Science - www.apluscompsci.com

Mammal (abstract class) Human Whale Cow AP A - Abstract/Interface Mammal (abstract class) In this example, Mammal is the abstract class. You would never have just a Mammal. For instance, you would not walk outside and go “Hey, look at that Mammal!” Mammal would be used to create something more specific, like a Dog, Human, or Whale. Human Whale Cow © A+ Computer Science - www.apluscompsci.com

AP A - Abstract/Interface © A+ Computer Science - www.apluscompsci.com Any sub class that extends a super abstract class must implement all methods defined as abstract in the super class. All abstract methods in the Abstract class must be implemented by the sub class extending the abstract class. This process is very similar to implementing an interface. When implementing an interface, all abstract methods in the interface must be implemented in the class. All methods in an interface are Abstract. © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com abstract class Monster{ private String myName; public Monster() { myName =""; } public Monster(String name) { myName =name; public String getName() { return myName; public abstract void talk( ); public String toString() { return myName + "\n"; AP A - Abstract / Interface Monster is an abstract class. Monster x = new Monster(); //illegal Monster y = new Ghost(); //legal Monster cannot be instantiated. Monster contains one abstract method, method talk(); The assumption is that all Monsters will have a name and name related methods. Also, it is assumed that all Monsters will talk. There is no way to know at the Monster level what exactly a particular type of Monster will say. The talk() method is abstract because it makes no sense it implement talk() at the Monster level. It makes more sense to write talk() for each specific Monster as each specific Monster will say something specific. © A+ Computer Science - www.apluscompsci.com

AP A - Abstract/Interface © A+ Computer Science - www.apluscompsci.com Why define talk as abstract? public abstract void talk( ); Does each Monster say the exact same thing? Each Monster will say something specific. A Ghost will say something different than a Werewolf and a Yetti will say something different from a Vampire. Thus, the talk() method is abstract as it will be written in each of the specific Monsters in a way specific to each Monster. © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com Vampire class Vampire extends Monster { public Vampire( String name ) { super(name); } public void talk() out.println("Vampire " + getName() + " say \"I want to drink your blood!\""); Vampire is a Monster, but it is a Monster that talks in its own specific way. A Vampire says “I want to drink your blood!”. Sub Class © A+ Computer Science - www.apluscompsci.com

AP A - Abstract/Interface © A+ Computer Science - www.apluscompsci.com public abstract class APlus { public APlus(int x) //constructor code not shown public abstract double goForIt(); //other fields/methods not shown } Pet Item © A+ Computer Science - www.apluscompsci.com

AP A - Abstract/Interface © A+ Computer Science - www.apluscompsci.com public class PassAPTest extends APlus { public PassAPTest(int x) { super(x); } public double goForIt() { double run=0.0; //write some code - run = x*y/z return run; } //other fields/methods not shown } public abstract class APlus { public APlus(int x) //constructor code not shown public abstract double goForIt(); //other fields/methods not shown } © A+ Computer Science - www.apluscompsci.com

AP A - Abstract/Interface © A+ Computer Science - www.apluscompsci.com public interface Exampleable { int writeIt(Object o); int x = 123; } All methods listed in an interface are public abstract. Abstract methods have no code. Each abstract method listed in an interface must be implemented in the class that implements the interface. All variables listed in an interface are public static final, making them final class variables. Interfaces cannot contain implemented methods, constructors, or instance variables. Methods are public abstract! Variables are public static final! © A+ Computer Science - www.apluscompsci.com

AP A - Abstract/Interface © A+ Computer Science - www.apluscompsci.com public interface Exampleable { public abstract int writeIt(Object o); public static final int x = 123; } All methods listed in an interface are public abstract. Abstract methods have no code. Each abstract method listed in an interface must be implemented in the class that implements the interface. All variables listed in an interface are public static final, making them final class variables. Interfaces cannot contain implemented methods, constructors, or instance variables. Methods are public abstract! Variables are public static final! © A+ Computer Science - www.apluscompsci.com

AP A - Abstract/Interface © A+ Computer Science - www.apluscompsci.com An interface is a list of abstract methods that must be implemented. An interface may not contain any implemented methods. Interfaces cannot have constructors!!! All methods listed in an interface are public abstract. Abstract methods have no code. Each abstract method listed in an interface must be implemented in the class that implements the interface. All variables listed in an interface are public static final, making them final class variables. Interfaces cannot contain implemented methods, constructors, or instance variables. © A+ Computer Science - www.apluscompsci.com

AP A - Abstract/Interface © A+ Computer Science - www.apluscompsci.com Interfaces are typically used when you know what you want an Object to do, but do not know how it will be done. If only the behavior is known, use an interface. Interfaces are used to detail what things an Object should do. Interfaces are used typically when the way an Object will do things is unknown. Comparable is a great example. With Comparable, it is clear that each Object should be compared to another Object of the same type. Comparable is an interface because it is not known what the Objects that implement Comparable will contain. It is known that the Object should be compared to other Obejcts of the same type in a certain way. © A+ Computer Science - www.apluscompsci.com

AP A - Abstract/Interface © A+ Computer Science - www.apluscompsci.com Abstract classes are typically used when you know what you want an Object to do and have a bit of an idea how it will be done. If the behavior is known and some properties are known, use an abstract class. © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com 2015 AP A Free Response © A+ Computer Science - www.apluscompsci.com

2015 Question 4 Part A public interface NumberGroup { boolean contains( int val ); } 2015 Question 4 Part A

2015 Question 4 Part B public class Range implements NumberGroup { private int low; private int high; public Range( int lo, int hi) low = lo; high = hi; } public boolean contains( int val ) return val >= low && val <= high; 2015 Question 4 Part B

public class MultipleGroups implements NumberGroup { private List<NumberGroup> groupList; //constructors , other methods, and such not shown public boolean contains( int val ) for( NumberGroup item : groupList ) if( item.contains( val) ) return true; } return false; 2015 Question 4 Part C

© A+ Computer Science - www.apluscompsci.com Provided by A+ Computer Science A+ Lab Assignments that cover the same class concepts as FR question # 4 :: • Tic Tac Toe • Lots of games www.apluscompsci.com © A+ Computer Science - www.apluscompsci.com

Visit us at www.apluscompsci.com Full Curriculum Solutions Provided by A+ Computer Science Visit us at www.apluscompsci.com Full Curriculum Solutions M/C Review Question Banks Live Programming Problems Tons of great content! www.facebook.com/APlusComputerScience © A+ Computer Science - www.apluscompsci.com

AP A Free Response Topics © A+ Computer Science - www.apluscompsci.com Array / Array or Arrays – for and for each loops, nested loops, array of arrays concepts Make a Class - Basic – create a basic class with instance variables, constructors, and methods ArrayList of References / Strings – get,set,remove,add,size – levels of abstraction Make a Class - Interface / Abstract – implement / extend – looks like this topic is back © A+ Computer Science - www.apluscompsci.com

Free Response Strategies © A+ Computer Science - www.apluscompsci.com -Read all 4 questions before writing anything -answer the easiest question 1st -most times question 1 is the easiest -see if part B calls part A and so on -many times part C consists of A and B calls -write something on every question -write legibly / use PENCIL!!!!!!!!!! -keep track of your time © A+ Computer Science - www.apluscompsci.com

Free Response Strategies © A+ Computer Science - www.apluscompsci.com -When writing methods -use parameter types and names as provided -do not redefine the parameters listed -do not redefine the methods provided -return from all return methods -return correct data type from return methods © A+ Computer Science - www.apluscompsci.com

Free Response Strategies © A+ Computer Science - www.apluscompsci.com -When writing a class or methods for a class -know which methods you have -know which instance variables you have -check for public/private on methods/variables -return from all return methods -return correct data type from return methods © A+ Computer Science - www.apluscompsci.com

Free Response Strategies © A+ Computer Science - www.apluscompsci.com -When extending a class -know which methods the parent contains -have the original class where you can see it -make sure you have super calls -check for public/private on methods/variables -make super calls in sub class methods as needed © A+ Computer Science - www.apluscompsci.com

Free Response Strategies © A+ Computer Science - www.apluscompsci.com -When extending abstract / implementing interface -know which methods the parent contains -have the original class where you can see it -make sure you have super calls -check for public/private on methods/variables -make super calls in sub class methods as needed -implement all abstract methods in sub class © A+ Computer Science - www.apluscompsci.com

AP A Free Response Topics © A+ Computer Science - www.apluscompsci.com Array / Array or Arrays – for and for each loops, nested loops, array of arrays concepts Make a Class - Basic – create a basic class with instance variables, constructors, and methods ArrayList of References / Strings – get,set,remove,add,size – levels of abstraction Make a Class - Interface / Abstract – implement / extend – looks like this topic is back © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com Provided by A+ Computer Science Visit us at www.apluscompsci.com Full Curriculum Solutions Slides and Examples Labs and Worksheets Tests and Quizzes Solutions provided for all www.facebook.com/APlusComputerScience © A+ Computer Science - www.apluscompsci.com

Visit us at www.apluscompsci.com Full Curriculum Solutions Provided by A+ Computer Science Visit us at www.apluscompsci.com Full Curriculum Solutions M/C Review Question Banks Live Programming Problems Tons of great content! www.facebook.com/APlusComputerScience © A+ Computer Science - www.apluscompsci.com