Sign In | Subscribe

Enter your Sign on user name and password.

Forgot password?
  • Follow us on:
Start learning today, and be successful in your academic & professional career. Start Today!
Loading video...
This is a quick preview of the lesson. For full access, please Log In or Sign up.
For more information, please see full course syllabus of Introduction to Java
  • Discussion

  • Study Guides

  • Download Lecture Slides

  • Table of Contents

  • Related Services

Lecture Comments (13)

0 answers

Post by Fancong Mo on May 6, 2016

Hi, would you mind solve the following question for me?

Prove the following propositions:
Suppose a, b, c ? Z. If a2+b2=c2, then a or b is even.
The product of any five consecutive integers is divisible by 120.

1 answer

Last reply by: Professor Quayle
Sat Mar 21, 2015 10:20 PM

Post by Jimmy Jones on March 21, 2015

I believe te grid world case study is not going to be used anymore? Will there be an alternative in the ap comp science 2015 test?

1 answer

Last reply by: Professor Quayle
Wed Jul 10, 2013 12:39 PM

Post by Darius Smith on July 8, 2013

AP stands for Assoc. Programmer?

3 answers

Last reply by: Professor Quayle
Fri Mar 29, 2013 2:01 AM

Post by Alex Moon on March 25, 2013

Hello, during my practice with free response questions, I had aa few questions myself.
This assignment asks me to create AttractingCritter class, where I have to mudify among other things,
getActors (). Basically, I had to get a list of all actors on the grid and return it.


public ArrayList <Actor> getActors()
ArrayList<Location> occupiedLocs = getGrid().getOccupiedLocations();
ArrayList<Actor> a = new ArrayList <Actor> ;

for ( Location i: occupiedLocs)

return a;


public ArrayList <Actor> getActors()
ArrayList<Location> occupiedLocs = new ArrayList();
ArrayList<Actors> a = new ArrayList <Actor>;
occupiedLocs = getGrid().getOccupiedLocations()

for(i=0 ; i<occupiedLocs.size(); i++)

return actors;


As you can see, the main difference is the fact that I used "a.set(i,get(occupiedLocs.get(i)))",
setting each of the slots for ArrayList a with however many actors there are in the ArrayList occupiedLocs,

while collegeboard used "a.add(getGrid().get(i)), simply adding additional actors into ArrayList a.

Question 1: since ArrayList a has just been created, would it be illegal to use set(index)? Or is there always one
empty slot after the last array, that would let me keep extending the array using iteration of the set method?

---Gernerally speaking----

Question 2: Is it not necessary to write "new ArrayList<Location>()" when instantiating new ArrayLists, as long as
the values of the ArrayList is ddecided upon instantiation?

Question 3:
Apparently, checking just "getloc) == null" does not work. It must be "getGrid().get(loc).

Is getGrid() method necessary whenever I refer to Locations or objects?

When is getGrid() required, and when is it not?

Question 4: What is the function of "this"? If there is no specified object preceding a method that is called,
does it function just as if it had "this." in front of it? (example: this.getLocation())


Sometimes, collegeboard asks me to trim any zeros in the array before nonzero numbers start (regardless of zeros later on,
In 0,0,0,4,9,0,2 only trim first 3 zeros)

The answers display usage of the while loop to count the zeros untill a nonzero number comes in ,
falsifying the samples[I] == 0 statement, allowing the program to move on.

I used a for loop to iterate through the array, and used if statement to increment a variable for every zero and used else
statement to move on to the rest of the method, and ending it with a return statemnt to prevent rechecking for zeros.

for( i =0 ; samples.length; i++)
if (samples[i]== 0)

for (j=0; samples.length-k;j++)


Question 5: Is the while loop the only way to end loops, for the purpose of extracting information collected through
the loop to be used within the method, without ending the entire method(such as with a return statement)? If not, is my
process going to work in the same manner?

For this problem, I very much wanted to create another method just to return the number of starting zeros, but of course,
the question only asks to implement one method.

----------------- General ----------------

6: Does an empty string contain " "? I thought it would contain null. Does null mean that something is empty, such as
the contents of ArrayLists , arrays, and strings? What is the difference between "empty" and "null"
in thse cases? When would something be considered null?

Im glad that I have such a dedicated instructor as a teacher. Thank you for your prompt responses!

3 answers

Last reply by: Professor Quayle
Wed Mar 27, 2013 4:46 PM

Post by Alex Moon on March 19, 2013

Thanks for the informative lectures! I just have a couple questions.

This is in Critter Class

processActors(ArrayList <Actor> actors)
{for(Actor a: actors)
{ if (!(a instance of Rock) &&! (a instance of Critter))}

1. What in the world is "a instance of Rock"? I have never seen this expression, and it seems strange to be in a form without periods. Is it a boolean method asking " Is object "a" a Rock?"? Is it differnet from ==?

2. It is advised that one use "removeSelfFromGrid()" instead of "put()" or "remove()" methods. I understand that the "put()" or "remove()" does not tell the object itself what is going on, but then, what is the purpose of "put" and "remove" if you cannot use them in this case?How are they used?

In my Barron's Review book, it mentions this case

public class ClassOne

public class ClassTwo extends ClassOne

ClassOne c2 = new ClassTwo()

3. What is the effect of casting c2 to ClassOne and using the ClassTwo constructor? In short, what is purpose/effect of casting to one class and using constructor of its subclass?


4. What is the difference between List<E> and ArrayList<E>?

For Free Response

The free response section makes students define various classes and methods.
5. Is it all right to initiate most variables for methods like "number", "n", "temp" to private variables? (Even if you don't need to)

6. What would be a convenient way to tell if a variable I need to instantiate in the FRQ would be instance variables or should be public, or nothing at all in terms of access level?

Some questions require the implementation of a constructor of a subclass.It turns out that the subclass constructor acts differently from the superclass constructors some times and act same in other times.

7. Do I get penalized if I don't mention "super()" and just rewrite the superclass constructor as part of the subclass constructor, if it did not specifically ask to use super()?

likewise, do I get penalized if I don't use super.processActors and instead rewrite the code for the superclass processActors in the subclass?

8. What is the effect of having a "return" statement at the end of a program when it does not need one? Is it illegal to use "return" in void methods?

Thank you so much. Test date is closing in, and I am in much need of your wisdom!

The GridWorld Study

  • Five or six multiple choice questions and one free response question based on GridWorld
  • Allows AP Exam to test knowledge of advanced concepts without requiring you to write an excessive amount of code
  • At the AP Exam you will receive a Quick Reference Guide to the GridWorld code and APIs
  • Testable APIs (know how to use): Grid interface; Location, Actor, Rock, and Flower classes
  • Testable Code (know how the code works and be able to extend or modify it): Bug, BoxBug, Critter, and ChameleonCritter classes

The GridWorld Study

Lecture Slides are screen-captured images of important points in the lecture. Students can download and print out these lecture slide images to do practice problems as well as take notes while watching the lecture.

  • Intro 0:00
  • Roadmap 0:19
  • Overview of GridWorld 0:51
    • Definition of GridWorld
    • Important part of AP Exam
    • Need to Understand It
  • Overview of GridWorld: Testable APIs 8:00
    • API Defined
    • Testable APIs
  • Testable Code 9:22
    • 4 Classes
  • Location Class 10:21
  • Location Class: 8 Public Constants That Help with Direction 11:36
    • Row Numbering
    • Column Numbering
  • Location Class: 7 Public Constants That Represent Turns 13:31
  • Location Class: Methods Useful in Navigating Through the Grid 15:25
    • getAdjacentLocation
    • getDirectionToward
  • Location Class: 3 Other Useful Methods 16:58
    • Equals Method
    • CompareTo Method
    • ToString Method
  • Grid Interface 18:41
    • Represents
  • Grid Interface: Useful Methods 20:34
    • getNumRows Method
    • getNumCols Method
    • isValid Method
  • Grid Interface: Useful Methods 22:16
    • Put Method
    • Remove Method
    • Get Method
  • Grid Interface: Useful Methods 23:44
    • GetOccupiedLocations Method
    • GetValidAdjacentLocations Method
    • GetEmptyAdjacentLocations Method
    • GetOccupiedAdjacentLocations Method
  • Grid Interface: Useful Methods 25:37
    • GetNeighbors Method
  • Actor Class 26:27
    • Constuctor: Actor
    • GetColor Method
    • SetColor Method
    • GetDirectionToward Method
    • SetDirection Method
    • GetGrid Method
    • GetLocation Method
    • PutSelfInGrid and RemoveSelfFromGrid Method
    • MoveTo Method
    • Act Method
    • ToString Method
  • Rock Class 31:46
    • 3 Methods
  • Flower Class 32:35
    • 3 Methods
  • Bug Class 33:53
    • Includes These Methods
    • How the Move Method Works
    • CanMove Method
  • BoxBug Class 40:49
    • Behavior
    • 2 Private Variables
    • Constructor for BoxBug
  • BoxBug Class Methods 42:16
  • Critter Class 43:41
    • How It Works
    • Next Step
    • 3rd Step
    • Last Step in Act Method
  • ChameleonCritter Class 49:00
    • Redefines Some Characteristics of Critter Class
    • Another Difference
  • Summary 52:25