Page 2
Agenda - Announcements - Review: OOP and Static vs Non-Static - Worksheet #2 - Review: Arrays - Worksheet #3 and #4 - Worksheet #1
Agenda - Announcements - Review: OOP and Static vs Non-Static - Worksheet #2 - Review: Arrays - Worksheet #3 and #4 - Worksheet #1
Announcements - Project 0: 2048 is due 9/18, next Monday! - Lab this week will partially be about the project - HW2 is out and due 9/22, next Friday - Get started early! - Midterm 1 Conflict Form on Piazza due 9/15, this Friday - MT1 is Wednesday, Sept 27 8-10PM - Discussion survey: tinyurl.com/czdisc3
Review: Object Oriented Programming - Class: define characteristics and public class Human { behaviors of our object, act as a String name; int age; static int population; blueprint/template for our objects public Human(String name, int age) { - Can create instances of the object by this.name = name; using the constructor and the new this.age = age; keyword population += 1; - Human h = new Human(“Bob”, 1) } - “this” refers to the object that you are public void greet() { creating, like “self” in Python! System.out.println(“Hi! I’m ” + this.name) } public static void getPopulation() { System.out.println(population); } }
Review: Object Oriented Programming - Non-static (instance) variables and public class Human { methods: String name; int age; static int population; - Belong to the object itself public Human(String name, int age) { - Each person has their own name, age, this.name = name; and way to greet people this.age = age; - Static (class) variables and population += 1; methods: } - What is one static method that you’ve public void greet() { seen already? System.out.println(“Hi! I’m ” + - Belong to the class, no notion of “this” this.name) - Preceded with “static” } - The “population” variable belongs to all public static void getPopulation() { Humans, not just one Human! System.out.println(population); } }
Review: Object Oriented Programming Examples // In the Human class public class Human { public void testNonStatic(Human you) { String name; int age; static int greet(); population; this.greet(); public Human(String name, int age) { you.greet(); this.name = name; Human.greet(); this.age = age; getPopulation(); population += 1; this.getPopulation(); } you.getPopulation(); public void greet() { Human.getPopulation(); System.out.println(“Hi! I’m ” + } this.name) } public static void getPopulation() { System.out.println(population); } }
Review: Object Oriented Programming Examples // In the Human class public class Human { public void testNonStatic(Human you) { String name; int age; static int greet(); population; this.greet(); public Human(String name, int age) { you.greet(); this.name = name; Human.greet(); // compile-time error! this.age = age; getPopulation(); population += 1; this.getPopulation(); } you.getPopulation(); public void greet() { Human.getPopulation(); System.out.println(“Hi! I’m ” + } this.name) } public static void getPopulation() { System.out.println(population); } }
Review: Object Oriented Programming Examples // In the Human class public class Human { public static void testStatic(Human you) String name; int age; static int { population; greet(); public Human(String name, int age) { this.greet(); this.name = name; you.greet(); this.age = age; Human.greet(); population += 1; getPopulation(); } this.getPopulation(); public void greet() { you.getPopulation(); System.out.println(“Hi! I’m ” + Human.getPopulation(); this.name) } } public static void getPopulation() { System.out.println(population); } }
Review: Object Oriented Programming Examples // In the Human class public class Human { public static void testStatic(Human you) String name; int age; static int { population; greet(); // compile-time error! public Human(String name, int age) { this.greet(); // compile-time error! this.name = name; you.greet(); this.age = age; Human.greet(); // compile-time error! population += 1; getPopulation(); } this.getPopulation(); // compile-time public void greet() { error! System.out.println(“Hi! I’m ” + you.getPopulation(); this.name) Human.getPopulation(); } } public static void getPopulation() { System.out.println(population); } }
Review: Object Oriented Programming Examples public class HumanRunner { public class Human { public static void main(String[] args) { String name; int age; static int Human chris = new Human(“Chris”, 5); population; greet(); public Human(String name, int age) { chris.greet(); this.name = name; chris.getPopulation(); this.age = age; Human.getPopulation(); population += 1; } } } public void greet() { System.out.println(“Hi! I’m ” + this.name) } public static void getPopulation() { System.out.println(population); } }
Review: Object Oriented Programming Examples public class HumanRunner { public class Human { public static void main(String[] args) { String name; int age; static int Human chris = new Human(“Chris”, 5); population; greet(); // compile-time error! public Human(String name, int age) { chris.greet(); this.name = name; chris.getPopulation(); // OK, but bad this.age = age; practice population += 1; Human.getPopulation(); } } public void greet() { } System.out.println(“Hi! I’m ” + this.name) } public static void getPopulation() { System.out.println(population); } }
2 Objects Refresher (a) Determine what would be printed after executing the main method of class Avatar. - Draw something to keep track of all the objects and variables!
2 Objects Refresher (b) Consider swapping Avatar and this in lines 6 and 7. Which swaps, if any would cause errors if we tried to compile and run the code? - Static means it’s for the class, non- static means it’s for the instance!
2 Objects Refresher (c) Will adding the following method to class Avatar cause any errors during compilation or execution? public static String getFluid() { return fluid; } - Think about static vs non-static!
Review: Arrays - Can instantiate in many ways - int[] values = new int[7]; - int[] oneThroughFive = new int[]{1, 2, 3, 4, 5}; - int[] oneThroughFive = {1, 2, 3, 4, 5}; // works but only if you declare and instantiate in same line - Cannot resize bigger/smaller once you create it! - Access elements by indexing, which start at 0 - oneThroughFive[2] = 3 - Constant time accesses wherever you are in the array - This will become more important later on when we compare data structures against each other
3 Min/Max Given an array A, return a 2 element array B where B[0] is the minimum element of A and B[1] is the maximum element of A. import static java.lang.Math.max; // max(a, b) returns max of a, b import static java.lang.Math.min; // min(a, b) returns min of a, b public static int[] minMax(int[] A) { int maxVal = Integer.MIN_VALUE; // smallest int in Java int minVal = Integer.MAX_VALUE; // largest int in Java
4 Reverse Given an array A, reverse its elements in place (i.e. do not create any new arrays; this should be a destructive method). public static void reverse(int[] A) {
Showing the first 17 pages. Download the document to see it all.
No comments yet. Be the first!
Comments (0)
No comments yet. Be the first!