CS 200 - Week 14 Jim Williams, PhD
This Week 1. Final Exam: Conflict Alternatives Emailed 2. Team Lab: Object Oriented Space Game 3. BP2 Milestone 3: Strategy 4. Lecture: More Classes and Additional Topics
BP2 M3 Strategy Piazza @1972 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.
Visibility Modifiers For members of a class: public private protected <package> Demo
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
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
Memory areas code static heap stack
Class (static) variables class Bug { private static int count = 0; private final int id; Bug() { id = ++count; public String tostring() { return "Bug:" + id;
How many Bug instances in list? ArrayList<Bug> list; list = new ArrayList<Bug>(); list.add( new Bug()); Bug abug = new Bug(); list.add( abug); list.add( 0, abug); 2 2 copies of reference to 1 bug none, error, no list 3
class (static) vs instance (non-static) fields methods
Does this print 0, 1, other or error? public class Person { 0 static int count = 0; private boolean something = false; 1 Person(boolean something) { this.something = something; other count++; error System.out.println( Person.count); //in other class in package
What will print out? 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); 0 1 can't access a private field outside the class error
What is the value of num? 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 num: 10 Stuff.num: 10 num: 10 Stuff.num: 11 error
A static field means class Pizza { static String toppings; public String gettoppings() { if ( toppings == null) toppings = "no toppings"; return toppings; every instance will have the exact same toppings toppings is a class variable can be accessed by any method cannot be changed
settoppings class Pizza { private String toppings; public void settoppings( String tops) { if ( tops!= null && tops.length() > 0) toppings = tops; is a class method can change toppings (write access) is a setter is a mutator
gettoppings() class Pizza { private String toppings; public static String gettoppings() { if ( toppings == null) toppings = "no toppings"; return toppings; is a class method can access toppings cannot access toppings will not compile
More Classes derived classes, overriding member methods, the Object class, polymorphism, ArrayLists of Objects, is-a vs has-a relationships.
Class Diagram Shows structure of a designed system at level of classes. Independent of time.
Animals in Zoo
Object Diagram A snapshot of a system at a point in time. Instances of classes with specific attribute values.
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.
Some Useful Classes Map Properties
Recall Fibonacci Sequence 1 1 2 3 5 8 13 21 Each is the sum of the previous 2. https://en.wikipedia.org/wiki/fibonacci_number
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]);
How many times is fib method called? 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)); 1 3 9 Error
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...