Download presentation
Presentation is loading. Please wait.
1
CS Week 14 Jim Williams, PhD
2
This Week Final Exam: Conflict Alternatives Emailed
Team Lab: Object Oriented Space Game BP2 Milestone 3: Strategy Lecture: More Classes and Additional Topics
3
BP2 M3 Strategy Test 1: output test. To pass, you'll need: main, promptUser, readInputFile, pigLatin, reverse(ArrayList<String>), manipulate and display working. Test 2 - unit test of reverse(String). To pass, you'll need matchCase from Milestone 2 to be working. Recall P5, actionReverse method, or Lab 10, exercise E has a reverse method. Test 3 - unit test of pigLatin. To pass, you'll need matchCase from Milestone 2 to be working.
4
Visibility Modifiers For members of a class: public private protected
<package> Demo
5
Can methodA call methodB?
//classes in different files in same package class A { public void methodA() { B b = new B(); b.methodB(); } class B { void methodB() { yes no depends error try it and see.
6
Can a method outside the package call methodA()?
//classes in different files in same package class A { public void methodA() { B b = new B(); b.methodB(); } class B { void methodB() { yes no depends error try it and see
7
Memory areas code static heap stack
8
Class (static) variables
class Bug { private static int count = 0; private final int id; Bug() { id = ++count; } public String toString() { return "Bug:" + id;
9
How many Bug instances in list?
2 2 copies of reference to 1 bug none, error, no list 3 ArrayList<Bug> list; list = new ArrayList<Bug>(); list.add( new Bug()); Bug aBug = new Bug(); list.add( aBug); list.add( 0, aBug); import java.util.ArrayList; class Bug { private static int count = 0; private final int id; Bug() { id = ++count; } public String toString() { return "Bug:" + id; public class ClassNameHere { public static void main(String[] args) { ArrayList<Bug> list = new ArrayList<>(); list.add( new Bug()); Bug aBug = new Bug(); list.add( aBug); list.add( 0, aBug); System.out.println( list);
10
class (static) vs instance (non-static)
fields methods
11
Does this print 0, 1, other or error?
public class Person { static int count = 0; private boolean something = false; Person(boolean something) { this.something = something; count++; } System.out.println( Person.count); //in other class in package 1 other error try it and see
12
What will print out? 1 can't access a private field outside the class
1 can't access a private field outside the class error class Employee { private static int employeeCount = 0; private final int id; Employee() { this.id = ++employeeCount; } public static void main(String []args) { Employee anEmployee = new Employee(); System.out.println( anEmployee.id); try it and see
13
What is the value of num? num: 10 Stuff.num: 6 Stuff.num: 10
class Stuff { final static int MAX_VALUE = 10; //allowed BP2 static int num = 6; //NOT allowed in BP2 static void change( int n) { num = n + 1; } public static void main( String [] args) { int num = MAX_VALUE; change( num); System.out.println("num:" + num); System.out.println("Stuff.num:" + Stuff.num); num: 10 Stuff.num: 6 Stuff.num: 10 Stuff.num: 11 error try it.
14
A static field means every instance will have the exact same toppings
toppings is a class variable can be accessed by any method cannot be changed class Pizza { static String toppings; public String getToppings() { if ( toppings == null) toppings = "no toppings"; return toppings; } A, B & C are true D is false since toppings would have to be 'final' to not be changed.
15
setToppings is a class method can change toppings (write access)
is a setter is a mutator class Pizza { private String toppings; public void setToppings( String tops) { if ( tops != null && tops.length() > 0) toppings = tops; } B, C and D are all true. A is not true as setToppings does not have the 'static' keyword in the header before void.
16
getToppings() is a class method class Pizza { can access toppings
cannot access toppings will not compile class Pizza { private String toppings; public static String getToppings() { if ( toppings == null) toppings = "no toppings"; return toppings; } A, C and D are true B is false.
17
More Classes derived classes, overriding member methods, the Object class, polymorphism, ArrayLists of Objects, is-a vs has-a relationships.
18
Class Diagram Shows structure of a designed system at level of classes. Independent of time.
19
Animals in Zoo
20
Object Diagram A snapshot of a system at a point in time.
Instances of classes with specific attribute values.
21
Bike Design a bike class. Instance Fields: numWheels, Color, unique id
Class Field: numBikesCreated, used to assign unique id’s to each bike. Constructor: numWheels and Color, automatically sets the unique identifier. Instance Methods: Number of Wheels and id can be accessed but not changed. Color can be changed. Add a toString() method to return all instance field values in String form. Class Method: returns the number of bikes created. Draw the UML diagram and then write the code. Create a BikeShop class that creates 10 bikes and stores in an array. Print out each bike’s number of wheels, color and id using the toString method.
22
Some Useful Classes Map Properties
23
Recall Fibonacci Sequence
… Each is the sum of the previous 2.
24
Iterative solution public static void main(String[] args) {
int n = 10; int [] fib = new int[n]; fib[0] = 1; fib[1] = 1; for (int i = 2; i < n; i++) { fib[i] = fib[i-1] + fib[i-2]; } System.out.println("fib "+n+"= " + fib[n-1]); {
25
How many times is fib method called?
1 3 9 Error static int fib(int num) { if ( num <= 1) return 1; else return fib( num -1) + fib( num -2); } public static void main(String[] args) { System.out.println("fibonacci 4: " + fib(4)); try it.
26
Next Week: Course Review
Tools, Diagrams, Data Types, Operators, Keywords, Control Flow, Structured/Procedural, Object Oriented, Exception Control Flow, User Input/Output, File Input/Output, Commenting, Style, Unit Testing, Best Practices...
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.