11.07.2015 Views

AP Computer Science Homework Set 10 Classes and Constructor

AP Computer Science Homework Set 10 Classes and Constructor

AP Computer Science Homework Set 10 Classes and Constructor

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

<strong>AP</strong> <strong>Computer</strong> <strong>Science</strong><strong>Homework</strong> <strong>Set</strong> <strong>10</strong><strong>Classes</strong> <strong>and</strong> <strong>Constructor</strong>P<strong>10</strong>A. Below is a driver class <strong>and</strong> sample output of a program that createsinstances of class MyPod:import java.util.ArrayList;public class MyPodDriver{public static void main( String[] args ){MyPod mp1 = new MyPod("White");System.out.println( "Printing serial number using objectref: " + mp1.getSerialNumber() );System.out.println( "Printing serial number using class ref:" + MyPod.getSerialNumber() );System.out.println( "MyPod color is: " + mp1.getColor() );System.out.println( "MyPod partNumber is: " +mp1.getPartNumber() );MyPod mp2 = new MyPod();mp2.setColor("Blue");System.out.println("\n");System.out.println( "Printing serial number using objectref: " + mp2.getSerialNumber() );System.out.println( "Printing serial number using class ref:" + MyPod.getSerialNumber() );System.out.println( "MyPod color is: " + mp2.getColor() );System.out.println( "MyPod partNumber is: " +mp2.getPartNumber() );MyPod mp3 = new MyPod("Red");System.out.println("\n");System.out.println( "Printing serial number using objectref: " + mp3.getSerialNumber() );System.out.println( "Printing serial number using class ref:" + MyPod.getSerialNumber() );System.out.println( "MyPod color is: " + mp3.getColor() );System.out.println( "MyPod partNumber is: " +mp3.getPartNumber() );} // end main} // end class MyPodDriverPage 1<strong>AP</strong> <strong>Computer</strong> <strong>Science</strong> – HW <strong>Set</strong> <strong>10</strong>http://thecubscientist.com


Write the class definition for the MyPod class. The class should include thefollowing:a. a String that holds the color of the MyPod along with setters <strong>and</strong> getters for theString variable.b. A zero-argument <strong>and</strong> one-argument constructor. The one-argument constructorshould take a String parameter to initialize the color of the MyPod.c. a static int serialNumber that is incremented when a new instance of a MyPodobject is instantiated from either a zero-argument or one-argument constructor.d. a final static PARTNUMBER that is initialized to the int 9876 along with agetter for this variable.P<strong>10</strong>B. Fun with Autoboxing! The “Big Five” rules for autoboxing on page 290 aregood to know. Write a short program that demonstrates each of these rules.P<strong>10</strong>C.Use the Calendar method getTimeInMillis() to time how long it takes to create<strong>and</strong> fill a <strong>10</strong>00 element array with r<strong>and</strong>om numbers between 1 <strong>and</strong> <strong>10</strong>0 <strong>and</strong> anArrayList of Integers filled with r<strong>and</strong>om numbers between 1 <strong>and</strong> <strong>10</strong>0. Which do youthink is faster?P<strong>10</strong>E.P<strong>10</strong>F.codingbatcodingbatPage 2<strong>AP</strong> <strong>Computer</strong> <strong>Science</strong> – HW <strong>Set</strong> <strong>10</strong>http://thecubscientist.com


By the end of the lesson students should be able to:a. Explain the difference between regular (non-static) <strong>and</strong> static methods.b. Write the Java code to create a static method.c. Explain the reason for using a static variable <strong>and</strong> give an example of a situation where it canbe used.d. Write the Java code to create <strong>and</strong> initialize a static variable using the keyword “static.”e. Explain the reason for using a “static final” variable <strong>and</strong> given an example of a situationwhere it can be used.f. Write the Java code to create <strong>and</strong> initialize a “final static” variable.g. Explain the reason for using keyword “final” to create a “final method” <strong>and</strong> a “final class.”h. Write the Java code to use the Math class methods Math.r<strong>and</strong>om(), Math.abs(),Math.round(), Math.min(), <strong>and</strong> Math.max().i. Write the Java code to “wrap” primitives using the appropriate Wrapper class: Boolean,Character, Byte, Short, Integer, Long, Float, <strong>and</strong> Double.j. Explain the concept of autoboxing <strong>and</strong> auto-unboxing.k. Write Java code that uses autoboxing with method arguments, return values, booleanexpressions, operations on numbers, <strong>and</strong> assignments.l. Write the Java code to format numbers <strong>and</strong> dates using String.format().Page 3<strong>AP</strong> <strong>Computer</strong> <strong>Science</strong> – HW <strong>Set</strong> <strong>10</strong>http://thecubscientist.com

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!