Ms Gold's

Computer Science Classes at Marshfield High School

AP Java Programming Assignments for

last updated August 28, 2010 5:09 PM

Link to Lesson Plans

AP Java

 

Resources

Java Concepts, Cay Horstmann
Be Prepared for the AP Computer Science Exam in Java, Maria Litvin
AP Java - Maria Litvin
www.eimacs.com
GridWorld - AP Collegeboard

  • Unit 1 - Introduction to Java and Object Oriented Programming
  • Lesson 1
    • Name, web-site, and email address on the board
    • Review last year's Asteroid Game
      • Brain Storm the class structure of a Asteroid Game
      • Compare the class design to how it would be designed in Game Maker

    Lesson 2

    • Lecture
      • Go through Class Power Point Link to Power Point Procedures, Schedules, and Assessments
      • Hand out Books
      • Basic Review of Java Code Structure

        The file name must be the same name as the class name. For example, in the following case the class name is Sum10, so the corresponding filename is Sum10.java. All class names by convention begin with a capital letter.
        All code in java is put in classes, and for the most part each class is in it's own file.

        /**
        * Computes the sum of the first ten positive integers,
        * 1 + 2 + ... + 10. (comments)
        */
        public class Sum10 // class definition public is it's scope
        {

        public static void main(String[] args)// every java application must have one main method
        {

        System.out.println(1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10);// every java statement ends in with a semi-colon

        }//brackets are delimiters in Java


        } //Note indent is very important for reading

        System.out (Object Name) . println (method name) ("Hello World") (parameters); // break down of the statement to print out Hello World to the console with a newline.

      • Finish up Asteroid discussion
      • How to set up eclipse -Steps to Running Eclipse and Writing Hello World
      • Load and Run a stripped down Asteroid Game - thanks to Beginning Java Game Programming Second Edition, Jonathan S. Harbour, University of Advancing Technology (Chapter 3)
        • Open Eclipse and create a project called Asteroids
        • download the following 5 files into your workspace project directory->src
        • In Eclipse right click to refresh your project
        • From the Run Menu click on Run Configurations then pick the Parameters tab. Set Width = 800 and Height= 600
        • Now you should be able to run your Asteroids projest as a Java Applet.
        • If not as for your neighbors help or my help.
    • Computer
      • Your assignment is to make the ship travel continuously around the rectangle.

         

  • Lesson 3
    • Objective Review Introduction to Java - JVM, Primitives and Casting
    • Write Journal
      • Create a pathname for journal H:/APJava/journal/firstInitialLastnameJournal.doc
      • put date and include what you accomplished:
        • Ran a program in Eclipse on the H drive in a directory called H:/APJava/workspace
        • Find the College Board site
        • Find and do one exercise in JavaBat
        • Go through the links on the Web site.
    • Objective - How the Java Run Time Environment Works and memorize the 8 Java Primitives
    • Lecture
      • JVM- A program written in Java receives services from the Java Runtime Environment (JRE) software by issuing commands to, and receiving the expected results from, the Java software. By providing these services to the program, the Java software is acting as a "virtual machine", taking the place of the operating system or hardware for which the program would ordinarily be tailored. from the Wikipedia explanation of the Java Virtual Machine - read more of this
      • 8 Primitive data types in java
        byte 0
        short 0
        int 0
        long 0L
        float 0.0f
        double 0.0d
        boolean false
        char '\u000'
      • Casting is converting one data type to another. There is two kinds of casting one between primitives numeric types and Objects. Casting between primitives numeric types is used to convert larger values, such as double values, to smaller values such as doubles to integers. For Objects All classes are child classes of java.lang.Object, so you can freely cast any class to Object class, or any class within the same family.
      • Link to Chapter One Power Point Slides
    • Objective - If you know the java primitive and conversion rules you are guaranteed some correct answers on the test
    • Computer
Lesson 6 -
Lesson 7
Continue to finish Chapter 3, 4 and 8 problems. This includes reading the Chapters at home
Read Chapter 1 and 2 in Be Prepared for the AP Computer Science Exam in Java and do the exercisesIf any of you finish before I come back continue with Emac 6 through 11 (give recursion a try!)Know Table 1 Primitive Type Chapter 4 Horstmann, Java Book, Chapter 2 Number Types
Java has separate types for integers and floating-point numbers . Integers are whole numbers; floating-point numbers can have fractional parts. For example, 13 is an integer and 1.3 is a floating-point number.

The double type denotes floating-point numbers that can have fractional parts.

The name “floating-point” describes the representation of the number in the computer as a sequence of the significant digits and an indication of the position of the decimal point. For example, the numbers 13000, 1.3, 0.00013 all have the same decimal digits: 13. When a floating-point number is multiplied or divided by 10, only the position of the decimal point changes; it “floats”. This representation is related to the “scientific” notation 1.3 × 10-4. (Actually, the computer represents numbers in base 2, not base 10, but the principle is the same.)

If you need to process numbers with a fractional part, you should use the type called double, which stands for “double precision floating-point number”. Think of a number in double format as any number that can appear in the display panel of a calculator, such as 1.3 or -0.333333333.

Do not use commas when you write numbers in Java. For example, 13,000 must be written as 13000. To write numbers in exponential notation in Java, use the notation En instead of “×10n”. For example, 1.3 × 10-4 is written as 1.3E-4.

You may wonder why Java has separate integer and floating-point number types. Pocket calculators don't need a separate integer type; they use floating-point numbers for all calculations. However, integers have several advantages over floatingpoint numbers. They take less storage space, are processed faster, and don't cause rounding errors. You will want to use the int type for quantities that can never have fractional parts, such as the length of a string. Use the double type for quantities that can have fractional parts, such as a grade point average.

Horstmann Java Book, Chapter 4 Number Types 4.1
(for more information read advanced topic 4.1, 4,2 and Random fact 4.1)
Numerical Computational Overflows

A numeric computation overflows if the result falls outside the range for the number type.

Each of the integer types has a different range—Advanced Topic 4.2 explains why the range limits are related to powers of two. Generally, you will use the int type for integer quantities. However, occasionally, calculations involving integers can overflow. This happens if the result of a computation exceeds the range for the number type. For example:

int n = 1000000;
System.out.println(n * n); // Prints-727379968

The product n * n is 1012, which is larger than the largest integer (about 2 · 109). The result is truncated to fit into an int, yielding a value that is completely wrong. Unfortunately, there is no warning when an integer overflow occurs.

If you run into this problem, the simplest remedy is to use the long type. Advanced Topic 4.1 shows you how to use the arbitary-precision BigInteger type in the unlikely event that even the long type overflows.

Overflow is not usually a problem for double-precision floating-point numbers. The double type has a range of about ±10308 and about 15 significant digits. However, you want to avoid the float type—it has less than 7 significant digits. (Some programmers use float to save on memory if they need to store a huge set of numbers that do not require much precision.)

Rounding errors occur when an exact conversion between numbers is not possible.

Rounding errors are a more serious issue with floating-point values. Rounding errors can occur when you convert between binary and decimal numbers, or between integers and floating-point numbers. When a value cannot be converted exactly, it is rounded to the nearest match. Consider this example:

double f = 4.35;
System.out.println(100 * f); // Prints 434.99999999999994

This problem is caused because computers represent numbers in the binary number system. In the binary number system, there is no exact representation of the fraction 1/10, just as there is no exact representation of the fraction 1/3 = 0.33333 in the decimal number system. (See Advanced Topic 4.2 for more information.)

For this reason, the double type is not appropriate for financial calculations. In this book, we will continue to use double values for bank balances and other financial quantities so that we keep our programs as simple as possible. However, professional programs need to use the BigDecimal type for this purpose —see Advanced Topic 4.1.

In Java, it is legal to assign an integer value to a floating-point variable:

int dollars = 100;
double balance = dollars; // OK

But the opposite assignment is an error: You cannot assign a floating-point expression to an integer variable.

double balance = 13.75;
int dollars = balance; //Error

To overcome this problem, you can convert the floating-point value to an integer with a cast:

int dollars = (int) balance;


You use a cast (typeName) to convert a value to a different type.

The cast (int) converts the floating-point value balance to an integer by discarding the fractional part. For example, if balance is 13.75, then dollars is set to 13.

The cast tells the compiler that you agree to information loss, in this case, to the loss of the fractional part. You can also cast to other types, such as (float) or (byte).



Use the Math.round method to round a floating-point number to the nearest integer.

If you want to round a floating-point number to the nearest whole number, use the Math.round method. This method returns a long integer, because large floating-point numbers cannot be stored in an int.

long rounded = Math.round(balance);

If balance is 13.75, then rounded is set to 14.

Add Scanner Class to 4.11 or 8.3 Horstmann Java Book, 4.7

  • Chapter 5 (Conditionals) and Chapter 6 (Iterations) - Horstmann
  • Exercises R5.9, R5.10 and R5.14
  • Programming Exercises P5.2
  • Emac - Labs and Tests 6 (Program Control), 7 (While Loops), and 8 (For Loops)
  • Exercises P6.3, P6.4 and P6.5
  • Chapter 7 ArrayLists
  • 2/1 Continue coding Arrays and Arraylist in JavaBat or where you find most helpful. I can give you short problems to solve if you like.
  • JavaBat
  • Home work read Chapter 13 on Recursion Test on Tuesday 9th.
  • Link to Tutorials
  • Inheritance
  • Recursive Array Programming
  • ArrayList methods and Facts
    • ArrayLists <String> myArrayList = new ArrrayList <String>();
      • No Primitives
        • add(E o) - appends to the end of this list
        • add(int index, E element) - inserts the specified element at the specified position in the list)
        • clear() - removes all elements from the list
        • contains(object elem) - Returns true if this list contains a specfied element
        • get(int index) - Returns the element at the specified position in the list
        • indexOf(Object elem) - Searches the first occurence of the given argument, testing for equality using the equals method
        • Iterator <E> iterator() - Returns an iterator over the elements in this list in proper sequence.
        • isEmpty() - Tests if this list has no elements
        • public interface ListIterator extends Iterator -An iterator for lists that allows the progrmmer to traverse the list in either direction, modify the list during iteration, and obtain the iterator;s current position in the list. A ListIteratior has not current element; its cursor position alsways lies between the element that would be returned by a call to previouus() and that would be returned by a call to next(). In a list of length n, there are n+1 values, from 0 to n, inclusive.
        • remove(int index) - Removes the element at the specified position in this list.
        • remove(Object o) - Removes a single instance ofthe specified element from the list, if it is present (optional operation)
        • set(index, E element) - Replaces the number of elements in this list
        • size() - Returns the number of elements in the list.
  • Examples of wrapper Classes
    • Integer
    • Double
    • Autoboxing and Auto-Unboxing - automaticall wraps and unwraps primitives. (You don't need to cast ints)
    • Vocabulary to know
      • Array
      • ArrayIndexOutofBoundsException
      • Autoboxing and Auto-unboxing
      • Collection
      • Dynamic Array
      • Element
      • Generics
      • Index
      • Linear search
      • Interface List <E> - In Java list is a collection that remembers the order of its elements. (Cay Horstmann- Java for Everyone)
      • ListIterator
      • TraversiJng
      • Type parameter
      • Wrapper class
  •  

    Sorting and Searching

    (Searching and Sorting, Comparable Interface, and Generics)

    **Code for Selection, Bubble, and Insertion **

    Read AP Scoring Information

    AP Scoring in Detail

    What AP scores represent

    Each AP Exam score is a weighted combination of the student's score on the multiple-choice section and the free-response section. The final score is reported on a 5-point scale:

    5 = extremely well qualified to receive college credit and/or placement
    4 = well qualified to receive college credit and/or placement
    3 = qualified to receive college credit and/or placement
    2 = possibly qualified to receive college credit and/or placement
    1 = no recommendation for receiving college credit and/or placement

    Periodically, the AP Program conducts college comparability studies for each AP subject by administering a portion of the AP Exam to college students enrolled in equivalent courses. The Program then compares the performance of these students on the sample AP Exam with their actual course scores. Results indicate that:

    * AP Exam scores of 5 are equivalent to grades of A in the corresponding college course.
    * AP Exam scores of 4 are equivalent to grades of A-, B+, and B in college.
    * AP Exam scores of 3 are equivalent to grades of B-, C+, and C in college.

     

    *******************************************************************************************************************************

    How to Download GridWorld.

    First unzip the GridWorld.zip file it into your Eclipse workspace. Don't make a directory the zip will make it for you. This directory will be called GridWorldCode. In Eclipse create a java project called GridWorldCode. Then run firstProject - BugRunner as a Java Application.

    Important Links

    Java Language Coding Guidelines

    Sun Java API's 1.5.0

    JavaBat- Java Practice Problems

    AP Java online Course eimacs.com

    Quick Link to DeMorgans

    Bank Account Example and Bank Account Tester

    OO Vocabulary List

    Java cscie160 -For Distributed Programming

    College Board Information

    Sun the Really Big Index

    Sun OO Tutorials

    Sun Java Package Tutorial

    How to Write Doc Comments for the Javadoc Tool

    GridWorld Quick Reference

    Link to AP Scoring Criteria

    ***Sorting Code - Side by Side - Selection - Bubble - Insertion *

     

    3D Mandelbrot (fractal) images

    Marble Madness - Understanding Binary Math

    How to make a Marble Machine

    Lessons

    Tutorials

    Object Oriented Vocabulary List

    Mathematical Definition of reciprocal is a number that you multiply by so that the result equals 1. The easiest way to find it is to just flip the fraction over.
    Here's an example: What is the reciprocal of 3?
    3 is the same as 3/1, so we flip and the reciprocal is 1/3
    Reciprocal of 4/5 is 5/4
    Change to a fraction --> 5/2, then flip --> 2/5