suggested reading: Java Ch. 6

Slides:



Advertisements
Similar presentations
IMPLEMENTING CLASSES Chapter 3. Black Box  Something that magically does its thing!  You know what it does but not how.  You really don’t care how.
Advertisements

Introduction to Object-Oriented Programming CS 21a: Introduction to Computing I First Semester,
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Chapter Three - Implementing Classes.
Copyright 2008 by Pearson Education Building Java Programs Chapter 8 Lecture 8-1: Classes and Objects reading: self-checks: Ch. 8 #1-9 exercises:
The Java Programming Language  Simple – but abstract  Safe  Platform-independent ("write once, run anywhere")  Has a Rich growing library  Designed.
Chapter 9: Classes with Instance Variables or Classes=Methods+Variables Asserting Java © Rick Mercer.
Chapter 3 Implementing Classes. Chapter Goals To become familiar with the process of implementing classes To be able to implement simple methods To understand.
Programming Languages and Paradigms Object-Oriented Programming.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
CMP-MX21: Lecture 6 Objects & Methods 1 Steve Hordley.
Writing Classes (Chapter 4)
Based on slides at buildingjavaprograms.com Objects and Classes, take 2 Managing Complexity with Programmer-Defined Types  Classes are programmer-defined.
Introduction to Object-Oriented Programming
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 2.
CSE 143 Lecture 2 More ArrayList ; classes and objects reading: 10.1; slides created by Marty Stepp and Hélène Martin
Classes: Member Functions and Implementation November 22, 2002 CSE103 - Penn State University Prepared by Doug Hogan.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Lecture 101 CS110 Lecture 10 Thursday, February Announcements –hw4 due tonight –Exam next Tuesday (sample posted) Agenda –questions –what’s on.
CS 106X Classes and Objects
Chapter 4 Introduction to Classes, Objects, Methods and strings
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
ACM/JETT Workshop - August 4-5, : Defining Classes in Java.
Bank Account Example public class BankAccount { private double balance; public static int totalAccounts = 0; public BankAccount() { balance = 0; totalAccounts++;
Object Oriented Programming. OOP  The fundamental idea behind object-oriented programming is:  The real world consists of objects. Computer programs.
Object Oriented Programming and Data Abstraction Rowan University Earl Huff.
1 Classes, Encapsulation, Methods and Constructors Class definitions Scope of Data –Instance data –Local data The this Reference Encapsulation and Java.
This document is copyright (C) Stanford Computer Science and Marty Stepp, licensed under Creative Commons Attribution 2.5 License. All rights reserved.
Topics Instance variables, set and get methods Encapsulation
©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 3: An Introduction to Classes 1 Chapter 3 An Introduction to Classes.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Chapter Three - Implementing Classes.
Chapter 3 Implementing Classes
COMPUTER SCIENCE & TECHNOLOGY DEGREE PROGRAMME FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UVA WELLASSA ‏ Properties of Object Oriented Programming.
Introduction to Constructors Lecture # 4. Copyright © 2011 Pearson Education, Inc. 3-2 Arguments Passed By Value In Java, all arguments to a method are.
Programming Abstractions
GC211 Data structure Lecture 3 Sara Alhajjam.
Lecture 3 John Woodward.
Building Java Programs
Adapted from slides by Marty Stepp and Stuart Reges
Java OOP Overview Classes and Objects, Members and Class Definition, Access Modifier, Encapsulation Java OOP Overview SoftUni Team Technical Trainers.
Classes and OOP.
CSE 143 Lecture 3 More ArrayList; object-oriented programming
Implementing Classes Yonglei Tao.
CS 106A, Lecture 28 Final Exam Review 2
CS 106A, Lecture 22 More Classes
Chapter Three - Implementing Classes
Classes and Objects, State and Behavior
Topic 27 classes and objects, state and behavior
More on Classes and Objects
Chapter 8 Lecture 8-1: Classes and Objects
CS2011 Introduction to Programming I Objects and Classes
Class Everything if Java is in a class. The class has a constructor that creates the object. public class ClassName private Field data (instance variables)
References and objects
JAVA CLASSES.
Building Java Programs
Building Java Programs
AN INTRODUCTION TO OBJECTS AND CLASSES
Introduction to Object-Oriented Programming
Building Java Programs
Building Java Programs
Building Java Programs
CSE 143 Lecture 3 More ArrayList; object-oriented programming
Building Java Programs
CS 1054: Lecture 2, Chapter 1 Objects and Classes.
Building Java Programs
Building Java Programs
Building Java Programs
Previous Lecture: Today’s Lecture: Reading (JV):
Introduction to Computer Science and Object-Oriented Programming
Presentation transcript:

suggested reading: Java Ch. 6 CS 106A, Lecture 21 Classes suggested reading: Java Ch. 6

Plan for today Recap: HashMaps + What’s Trending Classes Recap

Recap: HashMaps

Plan for today Recap: HashMaps + What’s Trending Classes Recap

Large Java Programs There are some large programs written in Java! How? Custom variable types

Defining New Variable Types Inbox Database Email Sender Login Manager Email User Inbox

A class defines a new variable type. What Is A Class? A class defines a new variable type.

Why Is This Useful? A student registration system needs to store info about students, but Java has no Student variable type. A music synthesizer app might want to store information about different types of instruments, but Java has no Instrument variable type. An email program might have many emails that need to be stored, but Java has no Email variable type. Classes let you define new types of variables, which lets you decompose your program code across different files. Let you decompose your program across files

Classes Are Like Blueprints iPod blueprint (class) state: current song volume battery life behavior: power on/off change station/song change volume choose random song constructs iPod (variable) #1 state: song = "1,000,000 Miles" volume = 17 battery life = 2.5 hrs behavior: power on/off change station/song change volume choose random song iPod (variable) #2 state: song = "Letting You" volume = 9 battery life = 3.41 hrs iPod (variable) #3 state: song = "Discipline" volume = 24 battery life = 1.8 hrs

A class defines a new variable type. What Is A Class? A class defines a new variable type.

Creating A New Class Let’s define a new variable type called BankAccount that represents information about a single person’s bank account. A BankAccount: - contains the name of account holder - contains the balance - can deposit money - can withdraw money

What if… What if we could write a program like this: BankAccount nickAccount = new BankAccount(); nickAccount.setName(“Nick”); nickAccount.deposit(50); BankAccount rishiAccount = new BankAccount(); rishiAccount.setName(“Rishi”); rishiAccount.deposit(50); boolean success = rishiAccount.withdraw(10); if (success) { println(“Rishi withdrew $10.”); }

Creating A New Class What information is inside this new variable type? These are its private instance variables.

Example: BankAccount Each BankAccount object has its own copy // In file BankAccount.java public class BankAccount { // Step 1: the data inside a BankAccount private String name; private double balance; } Not really useful if we can’t do anything with this though! Each BankAccount object has its own copy of all instance variables.

Creating A New Class What information is inside this new variable type? These are its instance variables. What can this new variable type do? These are its public methods.

What if… What if we could write a program like this: BankAccount nickAccount = new BankAccount(); nickAccount.setName(“Nick”); nickAccount.deposit(50); println(nickAccount); BankAccount rishiAccount = new BankAccount(); rishiAccount.setName(“Rishi”); rishiAccount.deposit(50); boolean success = rishiAccount.withdraw(10); if (success) { println(“Rishi withdrew $10.”); }

Example: BankAccount public class BankAccount { // Step 1: the data inside a BankAccount private String name; private double balance; // Step 2: the things a BankAccount can do public void deposit(double amount) { balance += amount; } public boolean withdraw(double amount) { if (balance >= amount) { balance -= amount; return true; return false; Methods are public so other files can access them

Defining Methods In Classes ba1 Methods defined in classes can be called on an instance of that class. When one of these methods executes, it can reference that object’s copy of instance variables. ba1.deposit(0.20); ba2.deposit(1000.00); This means calling one of these methods on different objects has different effects. name = "Marty" balance = 1.45 deposit(amount) { balance += amount; } ba2 name = "Mehran" balance = 901000.00 deposit(amount) { balance += amount; }

Getters and Setters Instance variables in a class should always be private. This is so only the object itself can modify them, and no-one else. To allow the client to reference them, we define public methods in the class that set an instance variable’s value and get (return) an instance variable’s value. These are commonly known as getters and setters. account.setName(“Nick”); String accountName = account.getName(); Getters and setters prevent instance variables from being tampered with.

Example: BankAccount public class BankAccount { private String name; private double balance; ... public void setName(String newName) { if (newName.length() > 0) { name = newName; } public String getName() { return name;

Creating A New Class What information is inside this new variable type? These are its instance variables. What can this new variable type do? These are its public methods. How do you create a variable of this type? This is the constructor.

Constructors GRect rect = new GRect(); GRect rect2 = new GRect(50, 50); This is calling a special method! The GRect constructor.

Constructors BankAccount ba1 = new BankAccount(); BankAccount ba2 = new BankAccount(“Nick”, 50); The constructor is executed when a new object is created.

Example: BankAccount public class BankAccount { // Step 1: the data inside a BankAccount private String name; private double balance; // Step 2: the things a BankAccount can do (omitted) // Step 3: how to create a BankAccount public BankAccount(String accountName, double startBalance) { name = accountName; balance = startBalance; } public BankAccount(String accountName) { balance = 0; Methods are public so other files can access them

Using Constructors When you call a constructor (with new): ba1 BankAccount ba1 = new BankAccount("Marty", 1.25); BankAccount ba2 = new BankAccount("Mehran", 900000.00); When you call a constructor (with new): Java creates a new object of that class. The constructor runs, on that new object. The newly created object is returned to your program. name = "Marty" balance = 1.25 BankAccount(nm, bal) { name = nm; balance = bal; } ba2 name = "Mehran" balance = 900000.00 BankAccount(nm, bal) { name = nm; balance = bal; }

Constructors constructor: Initializes the state of new objects as they are created. public ClassName(parameters) { statements; } The constructor runs when the client says new ClassName(...); no return type is specified; it "returns" the new object being created If a class has no constructor, Java gives it a default constructor with no parameters that sets all fields to default values like 0 or null.

Plan for today Recap: HashMaps + What’s Trending Classes Recap

A class defines a new variable type. What Is A Class? A class defines a new variable type.

Creating A New Class What information is inside this new variable type? These are its instance variables. What can this new variable type do? These are its public methods. How do you create a variable of this type? This is the constructor.

Recap Recap: HashMaps + What’s Trending Classes Recap Next time: more classes