How to do well on the AP test.

Download Report

Transcript How to do well on the AP test.

© A+ Computer Science - www.apluscompsci.com
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
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
ArrayList of References / Strings
– get,set,remove,add,size – levels of abstraction
GridWorld or Make a Class
– location, actor, bug, critter, ROCK, grid, super, abstract
Matrix / 2 D Array
– nested loops, GridWorld ( grid )
Make a Class / Interfaces / Abstract
– implement / extend – not seen this ? type in a few years
© 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
-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
-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
-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
-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
-When writing GridWorld question
-use the GridWorld quick reference
-use original method code when overidding
-use Bug and BoxBug code if extending Bug
-use Critter and ChameleonCritter code
if extending Critter
-use Critter and Bug for extends demo
-use Critter for ArrayList and Math.random() demo
-use GW quick reference on the entire AP test
© A+ Computer Science - www.apluscompsci.com
ArrayList of References / Strings
– get,set,remove,add,size – levels of abstraction
GridWorld or Make a Class
– location, actor, bug, critter, ROCK, grid, super, abstract
Matrix / 2 D Array
– nested loops, GridWorld ( grid )
Make a Class / Interfaces / Abstract
– implement / extend – not seen this ? type in a few years
© A+ Computer Science - www.apluscompsci.com
A typical ArrayList question involves
putting something into an ArrayList
and removing something from an
ArrayList.
© A+ Computer Science - www.apluscompsci.com
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.
© A+ Computer Science - www.apluscompsci.com
int[] nums = new int[10];
nums
//Java int array
0
1
2
3
4
5
6
7
8
9
0
0
0
0
0
0
0
0
0
0
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
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
get(spot)
returns the item at spot
size()
returns the # of items in the list
remove()
removes an item from the list
clear()
removes all items from the list
z[spot]=item
return z[spot]
import java.util.ArrayList;
© A+ Computer Science - www.apluscompsci.com
OUTPUT
List ray = new ArrayList();
ray.add("hello");
h
ray.add("whoot");
c
ray.add("contests");
out.println(((String)ray.get(0)).charAt(0));
out.println(((String)ray.get(2)).charAt(0));
ray stores Object references.
© A+ Computer Science - www.apluscompsci.com
List<String> ray;
OUTPUT
ray = new ArrayList<String>();
ray.add("hello");
h
ray.add("whoot");
c
ray.add("contests");
out.println(ray.get(0).charAt(0));
out.println(ray.get(2).charAt(0));
ray stores String references.
© 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
for(int i=list.size()-1; i>=0; i--)
{
if(list.get(i).equals("killIt"))
list.remove(i);
}
© 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
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
© A+ Computer Science - www.apluscompsci.com
String
frequently used methods
Name
Use
equals(s)
checks if this string has same chars as s
compareTo(s)
compares this string and s for >,<, and ==
trim()
removes leading and trailing whitespace
replaceAll(x,y)
returns a new String with all x changed to y
toUpperCase()
returns a new String with uppercase chars
toLowerCase()
returns a new String with lowercase chars
© A+ Computer Science - www.apluscompsci.com
OUTPUT
String sent = "alligators rule";
String find = "gato";
System.out.println(
System.out.println(
System.out.println(
System.out.println(
4
-1
iga
tors rule
sent.indexOf( find ) );
sent.indexOf( "dog" ) );
sent.substring( 3 , 6 ) );
sent.substring( 6 ) );
© A+ Computer Science - www.apluscompsci.com
© A+ Computer Science - www.apluscompsci.com
public String scrambleWord( String word )
{
String ret = "";
for( int i = 0; i < word.length(); i++ )
{
if( i+1 != word.length()
&& word.substring(i,i+1).equals("A")
&& !word.substring(i+1,i+2).equals("A"))
{
ret += word.substring(i+1,i+2) + word.substring(i,i+1);
i++;
//prevents hitting the same “A” again
}
else
{
ret += word.substring(i,i+1);
}
}
return ret;
You must know String!
}
© A+ Computer Science - www.apluscompsci.com
public void scrambleOrRemove( List<String> wordList )
{
for( int i = wordList.size()-1; i >= 0; i--)
{
String cur = wordList.get( i );
String ret = scrambleWord( cur );
if( ret.equals( cur ) )
wordList.remove( i );
else
wordList.set( i , ret );
}
}
You must know ArrayList!
© A+ Computer Science - www.apluscompsci.com
A+ Lab Assignments that cover the same
string concepts as FR question # 1 ::
• 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
M/C Review Question Banks
Live Programming Problems
Tons of great content!
www.facebook.com/APlusComputerScience
© A+ Computer Science - www.apluscompsci.com
One question on the A test free response
will require that you create a class by
extending another class. Critter is the
usual suspect, but sometimes Bug shows
up. If you are really lucky, like Charlie
Brown, you get a Rock.
© A+ Computer Science - www.apluscompsci.com
Location
frequently used methods
Name
Use
Location(row, col)
creates a new row,col Location
getCol()
gets the column value for this location
getRow()
gets the row value for this location
getAdjacentLocation( x)
get a loc adjacent to x
getDirectionToward( x )
get the dir to another actor
import info.gridworld.grid.Location;
© A+ Computer Science - www.apluscompsci.com
Location
frequently used fields
Name
Use
NORTH
indicates going north – value of 0
SOUTH
indicates going south – value of 180
EAST
indicates going east – value of 90
WEST
indicates going west – value of 270
import info.gridworld.grid.Location;
© A+ Computer Science - www.apluscompsci.com
Location locOne = new Location(2,1);
Location locTwo = new Location(1,3);
out.println(locOne.getAdjacentLocation(Location.NORTH));
out.println(locOne.getAdjacentLocation(Location.SOUTH));
out.println(locOne.getAdjacentLocation(Location.EAST));
out.println(locOne.getAdjacentLocation(Location.WEST));
0,0
0,1
0,2
0,3
0,4
1,0
1,1
1,2
1,3
1,4
2,0
2,1
2,2
2,3
2,4
3,0
3,1
3,2
3,3
3,4
© A+ Computer Science - www.apluscompsci.com
OUTPUT
(1, 1)
(3, 1)
(2, 2)
(2, 0)
Location locOne = new Location(2,1);
Location locTwo = new Location(1,3);
out.println(locOne.getDirectionToward(locTwo));
0,0
0,1
0,2
0,3
0,4
1,0
1,1
1,2
1,3
1,4
2,0
2,1
2,2
2,3
2,4
3,0
3,1
3,2
3,3
3,4
© A+ Computer Science - www.apluscompsci.com
OUTPUT
45
Actor
frequently used methods
Name
Use
Actor()
creates new blue north bound actor
act()
reverses the direction for actor
getColor()
gets the actor’s color
getDirection()
gets the actor’s direction
getLocation()
gets the actor’s location
setColor(col)
sets the actor’s color to col
setDirection(dir)
sets the actor’s direction to dir
moveTo(loc)
moves the actor to new location loc
© A+ Computer Science - www.apluscompsci.com
Actor
frequently used methods
Name
Use
putSelfInGrid(grid, loc) put this actor in grid at loc
removeSelfFromGrid()
takes this actor out of the grid
getGrid()
gets the grid which contains this actor
toString()
gets actor data as a String
import info.gridworld.actor.Actor;
© A+ Computer Science - www.apluscompsci.com
ActorWorld world = new ActorWorld();
Actor dude = new Actor();
dude.setColor(Color.GREEN);
dude.setDirection(Location.SOUTH);
Location loc = new Location(2,2);
world.add(loc, dude);
world.show();
What happens if you click
on an empty location?
© A+ Computer Science - www.apluscompsci.com
Grid
abstract methods
Name
Use
get(loc)
returns the ref at location loc
getEmptyAdjacentLocations(loc)
gets the valid empty locs in 8 dirs
getNeighbors(loc)
returns the objs around this
getNumCols()
gets the # of cols for this grid
getNumRows()
gets the # of rows for this grid
getOccupiedAdjacentLocations(loc)
gets the valid locs in 8 dirs that contain objs
getOccupiedLocations()
gets locs that contain live objs
getValidAdjacentLocations(loc)
gets the valid locs in 8 dirs
isValid(loc)
checks to see if loc is valid
put(loc, obj)
put the obj in grid at location loc
remove(loc)
take the obj at location loc out of the grid
import info.gridworld.grid.Grid;
© A+ Computer Science - www.apluscompsci.com
Grid is an interface that details the
behaviors expected of a Grid.
Grid was designed as an interface because
many different structures could be used to
store the grid values.
An interface works perfectly due to the
large number of unknowns.
© A+ Computer Science - www.apluscompsci.com
OUTPUT
Grid<String> grd;
grd = new BoundedGrid<String>( 4, 5 );
null
CAT
grd.put( new Location( 2, 2 ) , "CAT" );
grd.put( new Location( 1, 3 ) , "DOG" );
System.out.println( grd.get( new Location( 0, 0 ) );
System.out.println( grd.get( new Location( 2, 2 ) );
© A+ Computer Science - www.apluscompsci.com
null
null
null
null
null
null
null
null
DOG
null
null
null
CAT
null
null
null
null
null
null
null
Bug
extends Actor
frequently used methods
Name
Use
act()
move if possible or turn
getColor()
gets the bug’s color
getDirection()
gets the bug’s direction
getLocation()
gets the bug’s location
setColor(col)
sets the bug’s color to col
setDirection(dir)
sets the bug’s direction to dir
import info.gridworld.actor.Bug;
© A+ Computer Science - www.apluscompsci.com
Bug
extends Actor
frequently used methods – Bug only
Name
Use
Bug()
make a new red bug going north
Bug(color)
make a new color bug
act()
move if possible or turn
canMove()
check to see if a move is possible
move()
move forward and leave a flower
turn()
turn 45 degrees without moving
import info.gridworld.actor.Bug;
© A+ Computer Science - www.apluscompsci.com
ActorWorld world = new ActorWorld();
Bug dude = new Bug();
world.add(new Location(3,3), dude);
Bug sally = new Bug(Color.GREEN);
sally.setDirection(Location.SOUTHEAST);
world.add(new Location(2,2), sally);
Bug ali = new Bug(Color.ORANGE);
ali.setDirection(Location.NORTHEAST);
world.add(new Location(1,1), ali);
world.show();
© A+ Computer Science - www.apluscompsci.com
The bug act method looks to see if a move is
possible by calling canMove.
canMove looks at the location in front of
this bug to see if it is empty or
if it contains a flower.
canMove returns true or false.
© A+ Computer Science - www.apluscompsci.com
The bug act method calls move if canMove
returns true.
move calls moveTo to move the bug to the
location in front of this bug.
move leaves a flower in the
old location.
© A+ Computer Science - www.apluscompsci.com
The bug act method calls turn if canMove
returns false.
turn changes the direction of the bug by 45
degrees to the right.
© A+ Computer Science - www.apluscompsci.com
Critter
extends Actor
frequently used methods
Name
Use
getColor()
gets the critter's color
getDirection()
gets the critter's direction
getLocation()
gets the critter's location
setColor(col)
sets the critter's color to col
setDirection(dir)
sets the critter's direction to dir
import info.gridworld.actor.Critter;
© A+ Computer Science - www.apluscompsci.com
Critter
extends Actor
frequently used methods – Critter specific
Name
Use
act()
calls the methods listed below
getActors()
gets all actors around this location
processActors(actors)
do something to actors sent in
getMoveLocations()
gets list of possible move locs
selectMoveLocation(locs)
picks loc to move to
makeMove(loc)
moves this critter to loc
import info.gridworld.actor.Critter;
© A+ Computer Science - www.apluscompsci.com
if no grid present – stop
call getActors to get list of actors to proces
processActors received from getActors
call getMoveLocations to get a list of locations
to which the critter might move
call selectMoveLocation to select new location
move to the new loc
© A+ Computer Science - www.apluscompsci.com
The getActors method returns an ArrayList
containing all of the actors around this critter using
the 4 cardinal(N,S,E,W) and 4 intercardinal
directions(NE, NW, SE, SW).
In order to change which actors are returned by
getActors, override the method and provide a
different method of selecting actors.
getActors must not modify any actors.
© A+ Computer Science - www.apluscompsci.com
© A+ Computer Science - www.apluscompsci.com
public class Director extends Rock
{
public Director()
{
setColor( Color.RED );
}
}
public void act()
{
if( getColor().equals( Color.RED) ) {
setColor( Color.GREEN );
}
else
{
setColor( Color.RED );
Grid<Actor> gr = getGrid();
ArrayList<Actor> nbrs = gr.getNeighbors( getLocation() );
for( Actor dude : nbrs )
{
dude.setDirection( dude.getDirection() + 90 );
}
}
}
© A+ Computer Science - www.apluscompsci.com
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
One question on the A test free
response will require you to manipulate
a 2-dimensional array or a GridWorld
grid.
© A+ Computer Science - www.apluscompsci.com
A matrix is an array of arrays.
int[][] mat = new int[3][3];
0
1
2
0
0
0
0
1
0
0
0
2
0
0
0
© A+ Computer Science - www.apluscompsci.com
int[] nums = new int[10];
nums
//Java int array
0
1
2
3
4
5
6
7
8
9
0
0
0
0
0
0
0
0
0
0
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
String[] s = new String[5];
0
s
null
1
null
2
3
4
null
null
null
Arrays are automatically filled with
values when instantiated.
© A+ Computer Science - www.apluscompsci.com
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
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
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]);
© A+ Computer Science - www.apluscompsci.com
OUTPUT
7
4
2
9
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);
© A+ Computer Science - www.apluscompsci.com
OUTPUT
5
0
3
6
A matrix is an array of arrays.
int[][] mat = new int[3][3];
mat[0][1]=2;
Which
array?
Which
spot?
0
1
2
0
0
2
0
1
0
0
0
2
0
0
0
© A+ Computer Science - www.apluscompsci.com
0
1
2
3
4
0
0
0
0
5
0
0
0
mat[2][2]=7;
1
0
0
2
0
0
7
0
0
mat[4][1]=3
3
0
0
0
0
0
4
0
3
0
0
0
0
mat[0][3]=5;
© A+ Computer Science - www.apluscompsci.com
for( int r = 0; r < mat.length; r++)
{
for( int c = 0; c < mat[r].length; c++)
{
mat[r][c] = r*c;
0
0
0
}
}
if mat was 3x3
0
1
2
0
2
4
© A+ Computer Science - www.apluscompsci.com
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();
OUTPUT
11
12
}
© A+ Computer Science - www.apluscompsci.com
21
22
A matrix is an array of arrays.
int[][] mat = new int[3][3];
0
1
2
0
0
0
0
1
0
0
0
2
0
0
0
# of
array
s
© A+ Computer Science - www.apluscompsci.com
size
of
each
array
int[][] mat = {{5,7},{5,3,4,6},{0,8,9}};
out.println(Arrays.toString(mat[0]));
out.println(Arrays.toString(mat[1]));
OUTPUT
[5, 7]
[5, 3, 4, 6]
© A+ Computer Science - www.apluscompsci.com
int[] nums = {1,2,3,4,5,6,7};
for(int r=0; r<nums.length; r++)
{
OUTPUT
out.println(nums[r]);
1
}
2
3
length returns the # of
4
elements/items/spots in the
array!!!
5
6
7
© A+ Computer Science - www.apluscompsci.com
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<mat[1].length; c++)
c++)
{
out.print(mat[1][c]);
out.print(mat[r][c]);
OUTPUT
}
out.println();
57
346
}
5346
089
© A+ Computer Science - www.apluscompsci.com
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();
}
© A+ Computer Science - www.apluscompsci.com
OUTPUT
57
5346
089
© A+ Computer Science - www.apluscompsci.com
public SeatingChart( List<Student> studentList, int rows, int cols)
{
seats = new Student[ rows ] [ cols ];
int i = 0;
for( int c = 0; c < seats[0].length; c++)
{
for( int r = 0; r < seats.length; r++)
{
if( i < studentList.size() )
seats[r][c] = studentList.get( i++ );
}
}
}
This could be optimized, but it
works perfectly and I assume
many students are going to
write something close to this.
© A+ Computer Science - www.apluscompsci.com
public SeatingChart( List<Student> studentList, int rows, int cols)
{
seats = new Student[ rows ] [ cols ];
int i = 0;
boolean stop = false;
for( int c = 0; c < seats[0].length && !stop; c++)
{
for( int r = 0; r < seats.length; r++)
{
if( i < studentList.size() )
seats[r][c] = studentList.get( i++ );
else
//added this in to make it more efficient
{
//not required for AP CS A, but its fun to discuss
stop = !stop;
break;
}
}
Here is the optimized version
}
of ver 1. This not required, but
}
has some fun stuff to discuss.
© A+ Computer Science - www.apluscompsci.com
public SeatingChart( List<Student> studentList, int rows, int cols)
{
seats = new Student[ rows ] [ cols ];
for( int i = 0; i < studentList.size(); i++ )
{
//this algorithmic approach is common on lots
//of matrix programming contest problems
seats[ i % rows ][ i / rows] = studentList.get( i );
}
}
This algorithm is really cool, but not one
that most students would come up with
on the exam. I teach this approach to
my contest teams as there are often
problems that involve storing strings in
matrices at many contests.
© A+ Computer Science - www.apluscompsci.com
public int removeAbsentStudents( int allowedAbsences )
{
int count = 0;
//I stuck with column / row cuz I felt like it
for( int c = 0; c < seats[0].length; c++)
{
for( int r = 0; r < seats.length; r++)
{
//must check for null just like the Horse[] question from 2012
if( seats[r][c] != null &&
seats[r][c].getAbsentCount()>allowedAbsences )
{
seats[r][c] = null;
count ++;
}
}
}
return count;
}
© A+ Computer Science - www.apluscompsci.com
A+ Lab Assignments that cover the same
matrix concepts as FR question # 3 ::
• 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
M/C Review Question Banks
Live Programming Problems
Tons of great content!
www.facebook.com/APlusComputerScience
© A+ Computer Science - www.apluscompsci.com
Well, so much for my stats. The 2014
exam destroyed my system.
Hodgepodge has historically been an
array / string questions. This year it
turned out to be about implementing an
interface. Arrrgh!
© 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
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.
© A+ Computer Science - www.apluscompsci.com
Mammal (abstract class)
Human
Whale
© A+ Computer Science - www.apluscompsci.com
Cow
Any sub class that extends a
super abstract class must
implement all methods defined
as abstract in the super class.
© 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";
}
}
© A+ Computer Science - www.apluscompsci.com
Why define talk as abstract?
public abstract void talk( );
Does each Monster say
the exact same thing?
© A+ Computer Science - www.apluscompsci.com
class Vampire extends Monster
{
public Vampire( String name )
{
super(name);
}
public void talk()
{
out.println("Vampire " + getName() +
" say \"I want to drink your blood!\"");
}
}
© 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
public class PassAPTest extends APlus
{
public abstract class APlus
public PassAPTest(int x)
{
{
public APlus(int x)
//constructor code not shown
super(x);
}
public abstract double goForIt();
public double goForIt()
//other fields/methods not shown
{
}
double run=0.0;
//write some code - run = x*y/z
return run;
}
//other fields/methods not shown
}
© A+ Computer Science - www.apluscompsci.com
public interface Exampleable
{
int writeIt(Object o);
int x = 123;
}
Methods are public abstract!
Variables are public static final!
© A+ Computer Science - www.apluscompsci.com
public interface Exampleable
{
public abstract int writeIt(Object o);
public static final int x = 123;
}
Methods are public abstract!
Variables are public static final!
© 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!!!
© 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.
© 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
public class Trio implements MenuItem
{
private MenuItem one, two, three; //I used MenuItem because that’s how I roll!
public Trio( Sandwich f, Salad s, Drink t) //Boo – constructor should take 3 MenuItems
{
one = f;
two = s;
three = t;
}
public String getName()
{
return one + "/" + two + "/" + three;
}
public double getPrice()
{
return Math.max( one.getPrice() + two.getPrice() ,
Math.max( one.getPrice() + three.getPrice(), two.getPrice() + three.getPrice() ) );
}
}
public String toString()
{
return getName() + " " + getPrice();
}
© A+ Computer Science - www.apluscompsci.com
A+ Lab Assignments that cover the same
class concepts as FR question # 4 ::
• Tic Tac Toe
• Winter Scene
• BlackJack – 21
www.apluscompsci.com
© A+ Computer Science - www.apluscompsci.com
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
ArrayList of References / Strings
– get,set,remove,add,size – levels of abstraction
GridWorld or Make a Class
– location, actor, bug, critter, ROCK, grid, super, abstract
Matrix / 2 D Array
– nested loops, GridWorld ( grid )
Make a Class / Interfaces
– implement an interface – not seen this type in a few years
© 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
-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
-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
-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
-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
-When writing GridWorld question
-use the GridWorld quick reference
-use original method code when overidding
-use Bug and BoxBug code if extending Bug
-use Critter and ChameleonCritter code
if extending Critter
-use Critter and Bug for extends demo
-use Critter for ArrayList and Math.random() demo
-use GW quick reference on the entire AP test
© A+ Computer Science - www.apluscompsci.com
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
M/C Review Question Banks
Live Programming Problems
Tons of great content!
www.facebook.com/APlusComputerScience
© A+ Computer Science - www.apluscompsci.com