CS Week 14. Jim Williams, PhD

Similar documents
CS Week 13. Jim Williams, PhD

CS 302 Week 9. Jim Williams

Week 6 CS 302. Jim Williams, PhD

CS 200 More Classes Jim Williams, PhD

CS 200 Arrays, Loops and Methods Jim Williams, PhD

CS 200 Command-Line Arguments & Exceptions Jim Williams, PhD

CS 200 Objects and ArrayList Jim Williams, PhD

CS 302 Week 14. Jim Williams, PhD

News and information! Review: Java Programs! Feedback after Lecture 2! Dead-lines for the first two lab assignment have been posted.!

CS 211: Recursion. Chris Kauffman. Week 13-1

COE318 Lecture Notes Week 10 (Nov 7, 2011)

CS Week 5. Jim Williams, PhD

CS 200 File Input and Output Jim Williams, PhD

CS Week 11. Jim Williams, PhD

Unit 5: More on Classes/Objects Notes

CO Java SE 8: Fundamentals

COMP-202: Foundations of Programming. Lecture 13: Recursion Sandeep Manjanna, Summer 2015

CS 61B: Midterm Exam I

Binghamton University. CS-140 Fall Problem Solving. Creating a class from scratch

CS1083 Week 2: Arrays, ArrayList

AP CS Unit 7: Interfaces Exercises 1. Select the TRUE statement(s).

CS 170 Java Programming 1. Week 12: Creating Your Own Types

PROGRAMMING FUNDAMENTALS

ITI Introduction to Computing II

APCS Semester #1 Final Exam Practice Problems

Practice Questions for Final Exam: Advanced Java Concepts + Additional Questions from Earlier Parts of the Course

Contents. I. Classes, Superclasses, and Subclasses. Topic 04 - Inheritance

Computer Science II (20082) Week 1: Review and Inheritance

CMSC131. Inheritance. Object. When we talked about Object, I mentioned that all Java classes are "built" on top of that.

Exam Duration: 2hrs and 30min Software Design

Introduction to Classes and Objects. David Greenstein Monta Vista High School

ITI Introduction to Computing II

Chapter 15: Object Oriented Programming

Subclass Gist Example: Chess Super Keyword Shadowing Overriding Why? L10 - Polymorphism and Abstract Classes The Four Principles of Object Oriented

Birkbeck (University of London) Software and Programming 1 In-class Test Mar 2018

Unit 5: More on Classes/Objects Exercises

Points off Total off Net Score. CS 314 Final Exam Spring 2016

COE318 Lecture Notes Week 6 (Oct 10, 2011)

CS 2334: Programming Structures and Abstractions: Exam 1 October 3, 2016

CP122 CS I. Iteration

CSE115 Introduction to Computer Science I Coding Exercise #7 Retrospective Fall 2017

CS111: PROGRAMMING LANGUAGE II

About this sample exam:

Administration. Exceptions. Leftovers. Agenda. When Things Go Wrong. Handling Errors. CS 99 Summer 2000 Michael Clarkson Lecture 11

Lecture 2: Java & Javadoc

CS 211: Methods, Memory, Equality

CS 101 Exam 2 Spring Id Name

AP CS Unit 7: Interfaces Exercises Assume all code compiles unless otherwise suggested.

Object Class. EX: LightSwitch Class. Basic Class Concepts: Parts. CS257 Computer Science II Kevin Sahr, PhD. Lecture 5: Writing Object Classes

I. True/False: (2 points each)

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

Fall CS 101: Test 2 Name UVA ID. Grading. Page 1 / 4. Page3 / 20. Page 4 / 13. Page 5 / 10. Page 6 / 26. Page 7 / 17.

Computer Science II (20073) Week 1: Review and Inheritance

Advanced Programming - JAVA Lecture 4 OOP Concepts in JAVA PART II

CS 101 Spring 2007 Midterm 2 Name: ID:

Abstract classes are used to define a class that will be used only to build new classes. No objects will ever be instantiated from an abstract class.

Example: Fibonacci Numbers

Selected Questions from by Nageshwara Rao

CS 206 Introduction to Computer Science II

Informatik II (D-ITET) Tutorial 6

Recursion. General Algorithm for Recursion. When to use and not use Recursion. Recursion Removal. Examples

Object Fundamentals, Part One. Kenneth M. Anderson University of Colorado, Boulder CSCI 4448/5448 Lecture 2 08/27/2009

CS171:Introduction to Computer Science II

This exam is open book. Each question is worth 3 points.

Dealing with Bugs. Kenneth M. Anderson University of Colorado, Boulder CSCI 5828 Lecture 27 04/21/2009

COMP-202: Foundations of Programming. Lecture 26: Review; Wrap-Up Jackie Cheung, Winter 2016

public static boolean isoutside(int min, int max, int value)

Jump Statements. The keyword break and continue are often used in repetition structures to provide additional controls.

Prelim 1. CS 2110, October 1, 2015, 5:30 PM Total Question Name True Short Testing Strings Recursion

Final Exam. Programming Assignment 3. University of British Columbia CPSC 111, Intro to Computation Alan J. Hu. Readings

CS 132 Midterm Exam Spring 2004

CSCI 1103: Object-Oriented Objects

Topic 5 Polymorphism. " Inheritance is new code that reuses old code. Polymorphism is old code that reuses new code.

CS 149. Professor: Alvin Chao

CLASS DESIGN. Objectives MODULE 4

CSCE 145 Exam 2 Review No Answers. This exam totals to 100 points. Follow the instructions. Good luck!

Jump Statements. The keyword break and continue are often used in repetition structures to provide additional controls.

I. True/False: (2 points each)

CMP 326 Midterm Fall 2015

AP CS Unit 4: Classes and Objects Programs

RECURSION (CONTINUED)

Agenda CS121/IS223. Reminder. Object Declaration, Creation, Assignment. What is Going On? Variables in Java

Review sheet for Final Exam (List of objectives for this course)

CS141 Programming Assignment #6

User Defined Classes Part 2. CS 180 Sunil Prabhakar Department of Computer Science Purdue University

Introduction: Abstract Data Types and Java Review

AP COMPUTER SCIENCE A

Classes Classes 2 / 35

Administration. Conditional Statements. Agenda. Syntax. Flow of control. Lab 2 due now on floppy Lab 3 due tomorrow via FTP

COE318 Lecture Notes Week 9 (Week of Oct 29, 2012)

INSTRUCTIONS TO CANDIDATES

AP CS Unit 7: Interfaces. Programs

The class Object. Lecture CS1122 Summer 2008

CS 1331 Exam 1. Fall Failure to properly fill in the information on this page will result in a deduction of up to 5 points from your exam score.

CS 112 Introduction to Programming. (Spring 2012)

Programming II (CS300)

Example: Count of Points

Programming II (CS300)

Introduction to Programming (CS112): Sample

Classes and Objects 3/28/2017. How can multiple methods within a Java class read and write the same variable?

Transcription:

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