CS 200 - Week 14 Jim Williams, PhD.

Slides:



Advertisements
Similar presentations
Based on Java Software Development, 5th Ed. By Lewis &Loftus
Advertisements

Core Java Lecture 4-5. What We Will Cover Today What Are Methods Scope and Life Time of Variables Command Line Arguments Use of static keyword in Java.
Web Application Development Slides Credit Umair Javed LUMS.
Road Map Introduction to object oriented programming. Classes
1. 2 Introduction to Methods  Type of Variables  Static variables  Static & Instance Methods  The toString  equals methods  Memory Model  Parameter.
COMP 110 Introduction to Programming Mr. Joshua Stough October 8, 2007.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
Vocabulary Key Terms polymorphism - Selecting a method among many methods that have the same name. subclass - A class that inherits variables and methods.
Writing Classes (Chapter 4)
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Defining Classes II. Today’s topics  Static methods  Static variables  Wrapper classes  References  Class parameters  Copy constructor.
Introduction to Java Chapter 7 - Classes & Object-oriented Programming1 Chapter 7 Classes and Object-Oriented Programming.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
1 Class 1 Lecture Topic Concepts, Definitions and Examples.
Announcements Final Exam: TBD. Static Variables and Methods static means “in class” methods and variables static variable: one per class (not one per.
1 C# - Inheritance and Polymorphism. 2 1.Inheritance 2.Implementing Inheritance in C# 3.Constructor calls in Inheritance 4.Protected Access Modifier 5.The.
1 Object-Oriented Programming Inheritance. 2 Superclasses and Subclasses Superclasses and Subclasses  Superclasses and subclasses Object of one class.
OOP in Java : © W. Milner 2005 : Slide 1 Java and OOP Part 3 – Extending classes.
Structured Programming Dr. Atif Alhejali Lecture 4 Modifiers Parameters passing 1Structured Programming.
COMP Inheritance and Polymorphism Yi Hong June 09, 2015.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Topic: Classes and Objects
Values vs. References Lecture 13.
Sixth Lecture ArrayList Abstract Class and Interface
CS Week 10 Jim Williams, PhD.
CS 302 Week 14 Jim Williams, PhD.
CS 200 Arrays, Loops and Methods
CS 302 Week 15 Jim Williams, PhD.
CS 302 Week 11 Jim Williams, PhD.
Interfaces.
CS Week 13 Jim Williams, PhD.
CS 200 Creating Classes Jim Williams, PhD.
CS 302 Week 10 Jim Williams.
CS Week 8 Jim Williams, PhD.
CS Week 6 Jim Williams, PhD.
CS 200 Arrays, Loops and Methods
OBJECT ORIENTED PROGRAMMING II LECTURE 8 GEORGE KOUTSOGIANNAKIS
CS 200 More Classes Jim Williams, PhD.
COMPUTER 2430 Object Oriented Programming and Data Structures I
CS 302 Week 8 Jim Williams, PhD.
CS Week 9 Jim Williams, PhD.
Week 6 CS 302 Jim Williams, PhD.
CS 200 Objects and ArrayList
Outline Writing Classes Copyright © 2012 Pearson Education, Inc.
Group Status Project Status.
CS 302 Week 9 Jim Williams.
CS 200 Loops Jim Williams, PhD.
CS 200 Additional Topics and Review
Programs and Classes A program is made up from classes
CS 200 Additional Topics and Review
CS 200 Objects and ArrayList
Session 2: Introduction to Object Oriented Programming
CIS 199 Final Review.
CS 200 Creating Classes Jim Williams, PhD.
Web Design & Development Lecture 4
CS 200 More Classes Jim Williams, PhD.
OO Programming Concepts
CS 200 Creating Classes Jim Williams, PhD.
Week 7 CS 302 Jim Williams.
CS 200 Objects and ArrayList
Chapter 11 Inheritance and Encapsulation and Polymorphism
CS302 Week 6 Jim Williams.
CS 200 Additional Topics and Review
Chapter 7 Objects and Classes
CS 240 – Advanced Programming Concepts
Presentation transcript:

CS 200 - Week 14 Jim Williams, PhD

This Week Final Exam: Conflict Alternatives Emailed Team Lab: Object Oriented Space Game BP2 Milestone 3: Strategy 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 try it and see.

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

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? 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);

class (static) vs instance (non-static) fields methods

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

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

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.

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.

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.

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.

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? 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.

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...