CS CAMP 2.0: Outline for OOP and Java
I
-
a. Transition from Functional Programming to OOP
-
i. Modeling data and environments in a General Sense
-
1. The point is to model the problem or environment in a way that is closer to the way humans think than has been done in the past with software design.
-
-
ii. Thinking about a CS CAMPer Database as a Model
-
1. Let’s describe all the attributes about a CS CAMPer
-
2. We could generate a database or even just a simple list of CS CAMPers
-
3. What other things should this database of CS CAMPers contain?
-
-
-
b. Overview of Classes: Attributes (Fields) and Actions (Methods) Ch 4
-
i. More about the CS CAMPer Database (Class Design Recipe) - http://home.adelphi.edu/sbloch/class/171/recipes/class.html
-
1. What are the attributes of such a system?
-
2. What are the actions such a system might take?
-
3. How do we access or change the data for a given CS CAMPer?
-
4. How can we search this database?
-
-
-
c. Java Syntax and Style Whirlwind Tour
/* * Hello World program */ public class HelloWorld { public static void main(String[] args) { System.out.println("Hello, World!"); } } -
i. By convention, Java programs are written entirely in lower case characters with three exceptions.
-
1. Do fahrenheitToCelsius demonstration – Show the method design recipe.
-
a. Create an instance of the class and call the main
-
b. Show the standard main method and call from the class
-
-
2. The first letter of class names is capitalized to distinguish class names from member names.
-
3. The names of constant static final fields are written entirely capital letters. For example, the static final fields in the preceding subsection are all capitalized according to this convention.
-
4. The first letter in each word of a multi-word identifier after the first is capitalized. For example, the built-in Java class Object includes a method called toString() that we will discuss later. The capital S signifies the beginning of a word within the multi-word name toString().
-
Create a java method that computes a person’s body mass index (BMI). BMI is defined as the weight, expressed in kilograms, divided by the square of the height expressed in meters. (One inch is 0.0254 meters and one pound is 0.454 kilograms. The method calculateBmi should take the weight in pounds and height in inches as input arguments and should return the BMI
-
i. http://java.sun.com/j2se/1.4.2/docs/api/index.html
-
f. Simple vs. Compound Data in Java
-
i. Java has 2 types of data
-
1. Simple – primitive: int, double, Boolean (special wrapper classes for primitives)
-
2. Compound – Everything Else!: String, Programmer Defined, etc.
-
a. Methods and fields that belong to an object are separated by a “dot”
-
-
-
-
g. Boolean Expressions and “if-else” statements
-
i. The absolute value example:
public static int abs(int x) { int ax; if (x >= 0) // If x is greater or equal to 0 ax = x; // do this; else // else ax = -x; // do this. return ax; } OR more concisely: public static int abs(int x) { if (x < 0) x = -x; return x; }-
ii. It is a good idea to set apart if-else statements with curly braces
-
iii. The Java boolean Data Type
-
1. boolean isPositive = (x > 0); // can be only true or false.
-
-
iv. Order of Operations and Short-Circuit Evaluation
-
v. Nested if Statements – grading scale is a good example
-
vi. Common Mistakes with “if-else” Statements (page 189)
-
1. Extra Semicolon
if (condition);
-
2. Missing Semicolon
if (conditon1)
statement1
if (conditon2)
statement2;
-
3. Omitted Braces
if (condition)
statement1;
statement2;
-
-
II
-
a. Primitive class wrappers
-
i. All primitives can be convert to Objects designed for that type and vice versa.
-
Double temperature = new Double(98.6);
Integer age = new Integer(18);
Boolean failed = new Boolean(grade < 70);
-
b. Constructors and Overloaded Constructors
-
i. The Fraction Class Example.
-
-
c. Fields and Constants (Static)
-
d. Public vs. Private
-
e. A Class Example following the Class Design Recipe
-
f. The Snackbar Applet
-
g. Strings in Java-
-
i. Immutability
-
ii. equals()
-
iii. compareTo()
-
iv. indexOf()
-
III
-
a. Arrays (simple lists) in Java
-
b. Looping:
-
i. while Lopps
-
ii. for Loops
-
iii. do-while Loops
-
-
c. Searching an Array
-
d. Sorting an Array using Selection Sort
IV
-
a. Some Programming Problems (process an String array with a “for loop”)
-
b. Tunes-R-Us Music Database and Browser
Possible Lab Assignments:
Given:
int a, b, c;
create expressions that calculate the roots of the equation ax^2 + bx + c = 0 (assuming that the two real roots exist) and assign them to two double variables x1 and x2. Use a temporary variable to hold sqrt(b^2 – 4*a*c) in order not to compute it twice.
Create a methodpublic int convertToHumanAge(int dogYears)
that converts a dog’s age to the corresponding human age. Assume that a dog’s first year corresponds to a human age of 13. After that every three years in a dog’s life correspond to sixteen years in human life. The method returns the corresponding human age, rounded to the nearest integer. Write a Java program to test your method.
Create a Boolean method isLeapYear(int year) that returns true if year is a leap year and false otherwise. A leap year is a year that is evenly divisible by 4 and either is not divisible by 100 or is divisible by 400. For example, 2000 and 2004 are leap years, but 2003 and 2100 are not.
Create a Java method totalWages, which calculates the total earnings for a week based on hours worked and the hourly rate. The pay for overtime (hours worked over 40) is 1.5 times the regular rate. For example, totalWages(45, 12.50) should return 593.75.
Priority mail costs $3.50 for one pound or less, $3.95 for over one pound but not more than two pounds, and $1.20 for each additional pound (or fraction) above two pounds. First Class mail costs 37 cents for the first ounce and 21 cents for each additional ounce (or fraction), up to 13 ounces, after which Priority rates apply. For example, Priority Mail for 5 oz Costs $3.50 and for 4.5 lbs - $7.55; First Class mail for 3.5 oz costs $0.97 and for 15 oz - $3.50.
Create a Java method double calculatePostage(double ounces, char mailType) to allow calculation of postage.
Other Materials
- OOP & Java Zip File 1.1mb
- OOP Sorting: TunesBrowser
- Hangman from HtDP