Announcements for the Class

Similar documents
Readings for This Lecture

Announcements for This Lecture

Lecture 5. Defining Functions

Lecture 10. Overriding & Casting About

ACORN.COM CS 1110 SPRING 2012: ASSIGNMENT A1

Announcements for This Lecture

CS/ENGRD 2110 FALL Lecture 5: Local vars; Inside-out rule; constructors

Lecture 13. Call Stacks & Debugging

Lecture 8. Reviewing Methods; Wrapper Classes

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

CS100J, Fall 2003 Preparing for Prelim 1: Monday, 29 Sept., 7:30 9:00PM

CS/ENGRD 2110 SPRING Lecture 4: The class hierarchy; static components

Lecture 3. COMP1006/1406 (the Java course) Summer M. Jason Hinek Carleton University

C a; C b; C e; int c;

CSE 113 A. Announcements - Lab

Lecture 7. Strings & Stepwise Refinement

CS/ENGRD 2110 SPRING Lecture 5: Local vars; Inside-out rule; constructors

Advanced MEIC. (Lesson #18)

Notes on Chapter Three

CS 315 Software Design Homework 3 Preconditions, Postconditions, Invariants Due: Sept. 29, 11:30 PM

COMP 250 Winter 2011 Reading: Java background January 5, 2011

Programming II (CS300)

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 3: SEP. 13TH INSTRUCTOR: JIAYIN WANG

} Evaluate the following expressions: 1. int x = 5 / 2 + 2; 2. int x = / 2; 3. int x = 5 / ; 4. double x = 5 / 2.

CS 1027b Foundations of Computer Science II Assignment 1: File Compression Due Date: February 5, 11:55 pm Weight: 10%

PREPARING FOR PRELIM 1

Programming II (CS300)

CS 302 Week 9. Jim Williams

CISC-124. Dog.java looks like this. I have added some explanatory comments in the code, and more explanation after the code listing.

Lecture 02, Fall 2018 Friday September 7

CS/ENGRD 2110 FALL Lecture 3: Fields, getters and setters, constructors, testing

Lecture 4. Strings, Wrappers, & Containers

CS/ENGRD 2110 SPRING Lecture 3: Fields, getters and setters, constructors, testing

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

Lab Exercise 6: Abstract Classes and Interfaces CS 2334

Introduction to Programming System Design CSCI 455x (4 Units)

Lecture 18. Using Classes Effectively

coe318 Lab 1 Introduction to Netbeans and Java

Learning objectives. Reading carefully. Managing your time. CS2110 Fall 2017 Assignment A1. PhD Genealogy. See CMS for the due date

CS112 Lecture: Defining Instantiable Classes

Recitation 3 Class and Objects

CS/ENGRD 2110 SPRING Lecture 4: The class hierarchy; static components

Lecture 4. Defining Functions

Lecture 08: Static Fields and Methods

Week 3 Classes and Objects

CS/ENGRD 2110 FALL Lecture 4: The class hierarchy; static components

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.

Synchronized Methods of Old Versions of Java

Lecture 1. Course Overview Types & Expressions

CS100J Prelim I, 29 Sept. 2003

Interlude. Object Oriented Design

CSE 143: Computer Programming II Summer 2017 HW5: Anagrams (due Thursday, August 3, :30pm)

Two Types of Types. Primitive Types in Java. Using Primitive Variables. Class #07: Java Primitives. Integer types.

CS/ENGRD 2110 FALL Lecture 2: Objects and classes in Java

Lecture 18. Methods and Operations

Anatomy of a Method. HW3 is due Today. September 15, Midterm 1. Quick review of last lecture. Encapsulation. Encapsulation

CSCI 1301: Introduction to Computing and Programming Summer 2018 Lab 07 Classes and Methods

One of these "compartments" is more correctly referred to as an element of the array

CS61B Lecture #7. Announcements:

CS 215 Software Design Homework 3 Due: February 28, 11:30 PM

CS 1331 Exam 1 ANSWER KEY

Lecture 4. Defining Functions

Chapter 3 Classes. Activity The class as a file drawer of methods. Activity Referencing static methods

Assertions, pre/postconditions

CSCI 1301: Introduction to Computing and Programming Spring 2019 Lab 10 Classes and Methods

CmSc 150 Fundamentals of Computing I. Lesson 28: Introduction to Classes and Objects in Java. 1. Classes and Objects

CS/ENGRD 2110 SPRING Lecture 2: Objects and classes in Java

ECE 122. Engineering Problem Solving with Java

CS/ENGRD 2110 FALL Lecture 5: Local vars; Inside-out rule; constructors

CS/ENGRD 2110 SPRING Lecture 2: Objects and classes in Java

Before We Begin. Class Implementation II. Introduction to Computer Science II. Overview (1): Administrative Details (1):

ASSIGNMENT 5 Objects, Files, and a Music Player

CS61B Lecture #12. Today: Various odds and ends in support of abstraction.

1B1b Classes in Java Part I

Recommended Group Brainstorm (NO computers during this time)

CS 31 Discussion 1A, Week 8. Zengwen Yuan (zyuan [at] cs.ucla.edu) Humanities A65, Friday 10:00 11:50 a.m.

Lecture 17: Classes (Chapter 15)

CS/ENGRD 2110 SPRING Lecture 2: Objects and classes in Java

King Abdulaziz University Faculty of Computing and Information Technology Computer Science Department

Objects as a programming concept

Review questions. Review questions, cont d. Class Definition. Methods. Class definition: methods. April 1,

Model-based Software Engineering (02341, Spring 2017) Ekkart Kindler

Announcements. Last modified: Fri Sep 8 00:59: CS61B: Lecture #7 1

Chapter 15: Object Oriented Programming

CS1004: Intro to CS in Java, Spring 2005

CS/ENGRD 2110 SPRING Lecture 4: The class hierarchy; static components

PREPARING FOR THE FINAL EXAM

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

COMP 401 Spring 2013 Midterm 1

Object Oriented Programming. Week 1 Part 1 An introduction to Java, Objects and JUnit

CS180 Recitation. More about Objects and Methods

CS18000: Programming I

CS 116. Lab Assignment # 1 1

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

CS/ENGRD 2110 FALL Lecture 2: Objects and classes in Java

COMP-202: Foundations of Programming. Lecture 9: Classes and Objects Sandeep Manjanna, Summer 2015

Example: Fibonacci Numbers

Review: Object Diagrams for Inheritance. Type Conformance. Inheritance Structures. Car. Vehicle. Truck. Vehicle. conforms to Object

CSCI 136 Data Structures & Advanced Programming. Lecture 3 Fall 2017 Instructors: Bill & Bill

Lecture 4. Defining Functions

Transcription:

Lecture 2 Classes

Announcements for the Class Readings Section 1.4, 1.5 in text Section 3.1 in text Optional: PLive CD that comes with text References in text Assignment Assignment 1 due next week Due Friday, February 8th By 11:59PM to CMS Graded for mastery Keep submitting until correct But must make progress Visit consultant hours! Sunday Thursday 4:30-9:30 9/7/12 Classes 2 Java Apologies to XKCD

Assignment Details This is a very simple assignment Just the basics of OO programming Show you know enough to start a real program Work alone or with one partner Partners group themselves on the CMS Only one person submits the files. Partners must do the work together, sit next to each other, with each taking turns driving (writing the code) Academic Integrity Never look at someone s code or show yours to someone else Never possess someone else s code (except your partner) 1/28/13 Classes 3

Extended Review From Last Time p name of folder @105dc @105dc name of folder name of class variable name p.getname() Has value W. White Function; gives value p.pay(250.0); Sets owes to 0 Procedure; it does something name W. White address New York owes 250.00 getname() function pay(double d) procedure Patient fields (variables) methods 1/28/13 Classes 4

Class versus Object Anatomy of a declaration + assignment statement: int x = 2; Type Variable Value Point3d Class p = new Point3d(); Object 1/28/13 Classes 5

The Value null! You can declare a class variable w/o using new Example: Point3d var3; Value in variable is null! null: Absence of a name var3.getx() gives error! There is no name in var3 Does not know which Point3d to access NullPointerException var1 var2 var3 @4e0a1 @13fc8 null @4e0a1 Point3d x 2.2 y 5.4 z 6.7 @13fc8 Point3d x 3.5 y -2.0 1/28/13 Classes 6 z 0.0

Class Definition Describes the format of a folder (instance, object) of the class. /** * Description of what the class is for */ public class <class-name> { declarations of fields and methods (in any order) The class and every method has a comment of the form /** specification */ This is a comment It does nothing. It is a note to yourself This is a Javadoc comment (Part of Lab next week). 1/28/13 Classes 7

Field: A Variable in each Folder of a Class @4e0a1 Declarations of fields lname ssn boss Worker Invariants: Properties that are always true /** An instance is a worker in a certain organization. */ public class Worker { private String lname; // Last name ( if none; never null) private int ssn; // Social security #: in 0..999999999 private Worker boss; // Immediate boss (null if none) Note the private and public keywords. They are important but we will explain them later. 1/28/13 Classes 8

We Write Programs to Do Things Methods are the key doers Method Definition Method Call Defines what method does Command to do the method public void setname(string n) { Method Body (inside {) lname= n; declaration of parameter n Method Header var.setname( Bob ); argument to assign to n Parameter: variable that is declared within the parentheses of a method header. Argument: a value to assign to the method parameter when it is called 1/28/13 Classes 9

Getter and Setter Methods /** Yields: worker s last name*/ public String getname() { return lname; /** Set worker s last name to n * Cannot be null; can be */ public void setname(string n) { lname= n; /** Yields: last 4 SSN digits, as int * Try writing it yourself. Full code on website @4e0a1 lname ssn boss getname() setname(string n) Worker Getter methods (functions) get or retrieve values from a folder. Setter methods (procedures) set or change fields of a folder 1/28/13 Classes 10

Getter and Setter Methods /** Yields: worker s last name*/ public String getname() { return lname; value of function /** Set worker s last name to n * Cannot be null; can be */ public void setname(string n) { lname= n; /** Yields: last 4 SSN digits, as int * Try writing it yourself. Full code on website procedure; no value @4e0a1 lname Getter methods (functions) get or retrieve values from a folder. Setter methods (procedures) set or change fields of a folder 1/28/13 Classes 11 ssn boss getname() setname(string n) Worker

Why Getters and Setters? Fields have invariants: /** An instance is a worker in a certain organization. */ public class Worker { private String lname; // Last name ( if none; never null) private int ssn; // Social security #: in 0..999999999 private Worker boss; // Immediate boss (null if none) Allowing direct access to a field is bad. w.lname = null; // Violates invariant! Sometimes want a field to be read-only Can use in expressions, but not assignments How do we do this? 1/28/13 Classes 12

Why Getters and Setters? Setters Protect field invariants Example: public void setname(string n) { lname = n; if (n == null) { lname = ; Getters Allow read, not write Example: public int getname() { return lname; w.getname() = null; // Illegal! Invariant preserved 1/28/13 Classes 13

How Methods Work! Example: var1.getx() Gets object (folder) name from the variable Searches class (file drawer) for object (folder) Executes commands inside the method on that object var1 var2 @4e0a1 @13fc8 Memorize This! Write it down several times. @4e0a1 x 2.2 y z 5.4 6.7 Point3d Methods apply to the object (folder), not the variable! Execute var2.setx(8.2); Makes var3.getx() == 8.2 var3 @13fc8 @13fc8 Point3d x 3.5 y -2.0 1/28/13 Classes 14 z 0.0

Initializing the Fields of an Object (Folder) Creating a new Worker is now a multi-step process: Worker w = new Worker(); w.setname( White ); lname is null violates invariant We would like to be able to use something like Worker w = new Worker( White, 1, null); Create a new Worker, sets the last name to White, the SSN to 0000000001, and the boss to null. Need a special kind of method: the constructor 1/28/13 Classes 15

Initializing the Fields of an Object (Folder) Creating a new Worker is now a multi-step process: Worker w = new Worker(); w.setname( White ); Invariants must always be true. Always. Memorize This! Write it down lname several is null times. violates invariant Purpose of the Constructor We would like to be able to use something like Initialize the fields of a newly created object Worker w = new Worker( White, 1, null); Make sure that the invariants are true Create a new Worker, sets the last name to White, the SSN to 0000000001, and the boss to null. Need a special kind of method: the constructor 1/28/13 Classes 16

Example Constructor /** * Constructor: an instance with last * name n (can t be null, can be ), * SSN s (an int in 0..999999999), and * boss b (null if none) */ public Worker(String n, int s, Worker b) { lname = n; ssn = s; boss = b; name of constructor = name of class @4e0a1 Worker lname ssn boss getname() setname(string n) Worker(String n, int s, Worker b) no void or type! 1/28/13 Classes 17

How new Is Evaluated Create a new object (folder) of class Worker Initializes fields to default values e.g. 0 for int, null for String Put the folder in file drawer Execute the constructor call Worker( White, 1, null) Executes commands in body Primarily to initialize fields Gives the name of the object as the final value of this expression new Worker( White, 1, null) @4e0a1 lname ssn boss getname() setname(string n) Worker Worker(String n, int s, Worker b) 1/28/13 Classes 18