Java programs are built from classes. There is nothing else, but classes.

Slides:



Advertisements
Similar presentations
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Advertisements

DONT PANIC!! Lots of new notions coming in these slides Dont worry if not all of it makes perfect sense Well meet most of this stuff again in detail later.
STRING AN EXAMPLE OF REFERENCE DATA TYPE. 2 Primitive Data Types  The eight Java primitive data types are:  byte  short  int  long  float  double.
Copyright © Texas Education Agency, Computer Programming Class Basics.
Static Methods Static methods are those methods that are not called on objects. In other words, they don’t have an implicit parameter. Random number generation.
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.
The Fundamental Rule for Testing Methods Every method should be tested in a program in which every other method in the testing program has already been.
CIT 590 Intro to Programming Java lecture 3. Hashmaps The equivalent of python dictionaries. With both ArrayLists and Hashmaps, the syntax only allows.
Lecture 2: Object Oriented Programming I
Introduction to Object-Oriented Programming CS 21a: Introduction to Computing I First Semester,
Enhancing classes Visibility modifiers and encapsulation revisited
1 Classes and Objects Overview l Classes and Objects l Constructors l Implicit Constructors l Overloading methods this keyword l public, private and protected.
COMP More About Classes Yi Hong May 22, 2015.
Arrays and Objects OO basics. Topics Basic array syntax/use OO Vocabulary review Simple OO example Instance and Static methods Static vs. instance vs.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
Basic Object- Oriented Concepts Presented By: George Pefanis 21-Sep-15.
NSIT,Jetalpur CORE JAVA CONCEPTS SURABHI MISHRA (LCE)NSIT.
CSC 212 Object-Oriented Programming and Java Part 1.
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.
CSE 1302 Lecture 7 Object Oriented Programming Review Richard Gesick.
Parameters. Overview A Reminder Why Parameters are Needed How Parameters Work Value Parameters Reference Parameters Out Parameters.
Lecture # 8 Constructors Overloading. Topics We will discuss the following main topics: – Static Class Members – Overloaded Methods – Overloaded Constructors.
Week 2 - Wednesday.  What did we talk about last time?  Data representation  Binary numbers  Types  int  boolean  double  char  String.
More arrays Primitive vs. reference parameters. Arrays as parameters to functions.
JAVA Classes Review. Definitions Class – a description of the attributes and behavior of a set of computational objects Constructor – a method that is.
Variables, Primitives, and Objects A Visual Learner’s Guide.
Introduction to Java Lecture Notes 3. Variables l A variable is a name for a location in memory used to hold a value. In Java data declaration is identical.
Introduction to Java Java Translation Program Structure
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 7: Methods & User Input.
RECITATION 4. Classes public class Student { } Data Members public class Student { private String name; public String id; }
Java - Classes JPatterson. What is a class? public class _Alpha { public static void main(String [] args) { } You have been using classes all year – you.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
Objects and Classes Mostafa Abdallah
COP3502 Programming Fundamentals for CIS Majors 1 Instructor: Parisa Rashidi.
Using Objects. 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L7: Objects Slide 2 Java.
ACM/JETT Workshop - August 4-5, : Defining Classes in Java.
Classes and Objects CS177 Rec 10. Announcements Project 4 is posted ◦ Milestone due on Nov. 12. ◦ Final submission due on Nov. 19. Exam 2 on Nov. 4 ◦
CLASSES AND OBJECTS Object-Oriented Basics. Vocabulary Review – C++ Class Object Instance this new Constructor Default constructor Access (private/public/protected)
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Getting Started with Java: Object declaration and creation Primitive.
Bank Account Example public class BankAccount { private double balance; public static int totalAccounts = 0; public BankAccount() { balance = 0; totalAccounts++;
CIT 590 Intro to Programming Lecture 13. Some Eclipse shortcuts CTRL + SHIFT + F – format file (proper indentation etc). Please do this before you submit.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Week 10 - Wednesday.  What did we talk about last time?  Method example  Roulette simulation  Types in Java.
Classes - Intermediate
Topics Instance variables, set and get methods Encapsulation
CPSC 233 Tutorial 5 February 9 th /10 th, Java Classes Each Java class contains a set of instance variables and methods Instance Variables: Type.
OOP Basics Classes & Methods (c) IDMS/SQL News
Structured Programming Dr. Atif Alhejali Lecture 4 Modifiers Parameters passing 1Structured Programming.
Review – Primitive Data What is primitive data? What are 3 examples of primitive data types? How is a piece of primitive data different from an object?
Basic Class Structure. Class vs. Object class - a template for building an object –defines the instance data that the object will hold –defines instance.
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
Java 5 Class Anatomy. User Defined Classes To this point we’ve been using classes that have been defined in the Java standard class library. Creating.
Lecture 3 John Woodward.
Yanal Alahmad Java Workshop Yanal Alahmad
Implementing Classes Yonglei Tao.
More Object Oriented Programming
Lesson 2: Building Blocks of Programming
Class Structure 16-Nov-18.
Lecture 11 C Parameters Richard Gesick.
Defining Classes and Methods
The this Reference The this reference allows an object to refer to itself That is, the this reference, used inside a method, refers to the object through.
Class Structure 7-Dec-18.
Class Structure 2-Jan-19.
JAVA Constructors.
Core Concepts.
Class Structure 25-Feb-19.
Object Oriented Programming Review
References Revisted (Ch 5)
Presentation transcript:

Java programs are built from classes.

There is nothing else, but classes.

A class contains members:

a) data (variables)

A class contains members: a) data (variables) b) procedures (methods)

Members could be static or not static.

End of story.

Let’s see some examples now.

class One { } Draw this:

class One { } One Draw this:Don’t forget the blueprint (even though it’s empty).

class Two { int u; double q; } Draw this:

class Two { int u; double q; } Two Draw this:Notice that the blueprint is no longer empty:

class Two { int u; double q; } Two u q Draw this:Notice that the blueprint is no longer empty:

class Two { int u; double q; } Two u q Draw this:Notice that the blueprint is no longer empty: u and q are called instance variables

class Student { } Draw this:

class Student { } Student Draw this: Easy. Student or One – same difference.

class Student { int age; } Now draw this:

class Student { int age; } Now draw this: Student age

class Z { int m; double n; } Draw this:

class Z { int m; double n; } Draw this: Z m n

class M { int m; static double n; } Draw this:

class M { int m; static double n; } Draw this: M m n

class M { int m; static double n; } Draw this: M m n

class M { int m; double n; } Draw this: M m n

class M { int m; static double n; } Draw this: M m n

class Q { int v; double z; public static void main(String[ ] args) { } Draw this:

class Q { int v; double z; public static void main(String[ ] args) { } Draw this: For this the picture is bigger:

class Q { int v; double z; public static void main(String[ ] args) { } Draw this: For this the picture is bigger: Q v z main args

class Q { int v; double z; public static void main(String[ ] args) { } Draw this: For this the picture is bigger: Q v z main args

class Q { int v; double z; public static void main(String[ ] args) { } Draw this: For this the picture is bigger: Q v z main args

class E { int v; double z; public static void main(String[ ] args) { int q; q = 3; } Draw this:

class E { int v; double z; public static void main(String[ ] args) { int q; q = 3; } Draw this: Same as above, only main is no longer empty:

class E { int v; double z; public static void main(String[ ] args) { int q; q = 3; } Draw this: Same as above, only main is no longer empty: E v z

class E { int v; double z; public static void main(String[ ] args) { int q; q = 3; } Draw this: Same as above, only main is no longer empty: E v z main args

class E { int v; double z; public static void main(String[ ] args) { int q; q = 3; } Draw this: Same as above, only main is no longer empty: E v z main args int q;

class E { int v; double z; public static void main(String[ ] args) { int q; q = 3; } Draw this: Same as above, only main is no longer empty: E v z main args int q; q is a local variable.

class E { int v; double z; public static void main(String[ ] args) { int q; q = 3; } Draw this: Same as above, only main is no longer empty: E v z main args int q; q is a local variable. It is local to main.

class E { int v; double z; public static void main(String[ ] args) { int q; q = 3; } Draw this: Same as above, only main is no longer empty: E v z main args int q; q = 3;

class E { int v; double z; public static void main(String[ ] args) { int q; q = 3; } Draw this: Same as above, only main is no longer empty: E v z main args int q; q = 3;

class E { int v; double z; public static void main(String[ ] args) { int q; q = 3; } Draw this: Same as above, only main is no longer empty: E v z main args int q; q = 3; 3

class Student { int age; double gpa; public static void main(String[ ] args) { int q = 21; String n = “John”; } Draw this:

class Student { int age; double gpa; public static void main(String[ ] args) { int q = 21; String n = “John”; } Draw this: Student age gpa main args

class Student { int age; double gpa; public static void main(String[ ] args) { int q = 21; String n = “John”; } Draw this: Student age gpa main args q

class Student { int age; double gpa; public static void main(String[ ] args) { int q = 21; String n = “John”; } Draw this: Student age gpa main args q 21

class Student { int age; double gpa; public static void main(String[ ] args) { int q = 21; String n = “John”; } Draw this: Student age gpa main args q 21 n

class Student { int age; double gpa; public static void main(String[ ] args) { int q = 21; String n = “John”; } Draw this: Student age gpa main args q 21 n

class Student { int age; double gpa; public static void main(String[ ] args) { int q = 21; String n = “John”; } Draw this: Student age gpa main args q 21 n java.lang.String “John”

class Student { int age; double gpa; public static void main(String[ ] args) { int q = 21; String n = “John”; } Draw this: Student age gpa main args q 21 n java.lang.String “John” String is a reference type.

Types in Java:

a) primitive

Types in Java: a) primitive (int, double, boolean, char)

Types in Java: a) primitive (int, double, boolean, char) b) reference types

Types in Java: a) primitive (int, double, boolean, char) b) reference types (classes)

Types in Java: a) primitive (int, double, boolean, char) int x;

Types in Java: a) primitive (int, double, boolean, char) int x; int y;

Types in Java: a) primitive (int, double, boolean, char) int x; int y; x = 5;

Types in Java: a) primitive (int, double, boolean, char) int x; int y; x = 5;

Types in Java: a) primitive (int, double, boolean, char) int x; int y; x = 5; 5

Types in Java: a) primitive (int, double, boolean, char) int x; int y; x = 5; 5 y = x;

Types in Java: a) primitive (int, double, boolean, char) int x; int y; x = 5; 5 y = x;

Types in Java: a) primitive (int, double, boolean, char) int x; int y; x = 5; 5 y = x; 5

Types in Java: a) primitive (int, double, boolean, char) int x; int y; x = 5; 5 y = x; 5 x = x + 2;

Types in Java: a) primitive (int, double, boolean, char) int x; int y; x = 5; 5 y = x; 5 x = x + 2;

Types in Java: a) primitive (int, double, boolean, char) int x; int y; x = 5; 5 y = x; 5 x = 5 + 2;

Types in Java: a) primitive (int, double, boolean, char) int x; int y; x = 5; 5 y = x; 5 x = 5 + 2;

Types in Java: a) primitive (int, double, boolean, char) int x; int y; x = 5; 5 y = x; 5 x = 5 + 2;

Types in Java: a) primitive (int, double, boolean, char) int x; int y; x = 5; 7 y = x; 5 x = x + 2;

Types in Java: a) primitive (int, double, boolean, char) int x; int y; x = 5; 7 y = x; 5 x = x + 2; But this doesn’t change y.

Types in Java: a) primitive (int, double, boolean, char) int x; int y; x = 5; 7 y = x; 5 x = x + 2; But this doesn’t change y. And that’s because y is a copy of x.

Types in Java: a) primitive (int, double, boolean, char) int x; int y; x = 5; 7 y = x; 5 x = x + 2; But this doesn’t change y. And that’s because y is a copy of x. Things don’t work the same with reference types.

Types in Java: a) primitive (int, double, boolean, char) int x; int y; x = 5; 7 y = x; 5 x = x + 2; But this doesn’t change y. And that’s because y is a copy of x. But the above is true of primitive types: int (long, short, byte) double (float) char boolean

Types in Java: a) primitive (int, double, boolean, char) b) reference types

Types in Java: a) primitive (int, double, boolean, char) b) reference types Penguin x;

Types in Java: a) primitive (int, double, boolean, char) b) reference types Penguin x; x = new Penguin();

Types in Java: a) primitive (int, double, boolean, char) b) reference types Penguin x; x = new Penguin();

Types in Java: a) primitive (int, double, boolean, char) b) reference types Penguin x; x = new Penguin();

Types in Java: a) primitive (int, double, boolean, char) b) reference types Penguin x; x = new Penguin(); Penguin y;

Types in Java: a) primitive (int, double, boolean, char) b) reference types Penguin x; x = new Penguin(); Penguin y; y = x;

Types in Java: a) primitive (int, double, boolean, char) b) reference types Penguin x; x = new Penguin(); Penguin y; y = x;

Types in Java: a) primitive (int, double, boolean, char) b) reference types Penguin x; x = new Penguin(); Penguin y; y = x;

Types in Java: a) primitive (int, double, boolean, char) b) reference types Penguin x; x = new Penguin(); Penguin y; y = x; x.turnLeft();

Types in Java: a) primitive (int, double, boolean, char) b) reference types Penguin x; x = new Penguin(); Penguin y; y = x; x.turnLeft();

Types in Java: a) primitive (int, double, boolean, char) b) reference types Penguin x; x = new Penguin(); Penguin y; y = x; x.turnLeft();

Types in Java: a) primitive (int, double, boolean, char) b) reference types Penguin x; x = new Penguin(); Penguin y; y = x; x.turnLeft(); But that also changes the value of y.

Types in Java: a) primitive (int, double, boolean, char) b) reference types Penguin x; x = new Penguin(); Penguin y; y = x; x.turnLeft(); But that also changes the value of y. The Penguin object is only one, and shared.

Types in Java: a) primitive (int, double, boolean, char) b) reference types

Types in Java: a) primitive (int, double, boolean, char) b) reference types (classes)

Types in Java: a) primitive (int, double, boolean, char) b) reference types (classes) A value of primitive type fits in a location of that type.

Types in Java: a) primitive (int, double, boolean, char) b) reference types (classes) A value of primitive type fits in a location of that type. A value of reference type (an object) doesn’t.

Types in Java: a) primitive (int, double, boolean, char) b) reference types (classes) A value of primitive type fits in a location of that type. A value of reference type (an object) doesn’t. For objects, a reference to them is kept in the variable.

Let’s continue with the examples.

class J { int x; public static void main(String[ ] args) { J z = new J(); int x = 5; z.x = 5; } Draw this:

class J { int x; public static void main(String[ ] args) { J z = new J(); int x = 5; z.x = 5; } Draw this: main args x J

class J { int x; public static void main(String[ ] args) { J z = new J(); int x = 5; z.x = 5; } Draw this: main args x J z

class J { int x; public static void main(String[ ] args) { J z = new J(); int x = 5; z.x = 5; } Draw this: main args x J z x 0

class J { int x; public static void main(String[ ] args) { J z = new J(); int x = 5; z.x = 5; } Draw this: main args x J z x 0

class J { int x; public static void main(String[ ] args) { J z = new J(); int x = 5; z.x = 5; } Draw this: main args x J z x x 0

class J { int x; public static void main(String[ ] args) { J z = new J(); int x = 5; z.x = 5; } Draw this: main args x J z x x 5 0

class J { int x; public static void main(String[ ] args) { J z = new J(); int x = 5; z.x = 5; } Draw this: main args x J z x x 5 0

class J { int x; public static void main(String[ ] args) { J z = new J(); int x = 5; z.x = 5; } Draw this: main args x J z x x 5 0

class J { int x; public static void main(String[ ] args) { J z = new J(); int x = 5; z.x = 5; } Draw this: main args x J z x x 5 0

class J { int x; public static void main(String[ ] args) { J z = new J(); int x = 5; z.x = 5; } Draw this: main args x J z x x 5 0

class J { int x; public static void main(String[ ] args) { J z = new J(); int x = 5; z.x = 5; } Draw this: main args x J z x x 5 5

class J { int x; public static void main(String[ ] args) { J z = new J(); int x = 5; z.x = 5; } Draw this: main args x J z x x 5 5

Draw this:

class Student { int age; double gpa; public static void main(String[ ] args) { int q; String n; Student s; s = new Student(); }

Draw this: class Student { int age; double gpa; public static void main(String[ ] args) { int q; String n; Student s; s = new Student(); } What kind of variables are age and gpa?

Draw this: class Student { int age; double gpa; public static void main(String[ ] args) { int q; String n; Student s; s = new Student(); } Yes, they are instance variables.

Draw this: class Student { int age; double gpa; public static void main(String[ ] args) { int q; String n; Student s; s = new Student(); } What kind of variables are q, n, and s?

Draw this: class Student { int age; double gpa; public static void main(String[ ] args) { int q; String n; Student s; s = new Student(); } Exactly, they are local variables.

Draw this: class Student { int age; double gpa; public static void main(String[ ] args) { int q; String n; Student s; s = new Student(); } They’re local to main.

Draw this: class Student { int age; double gpa; public static void main(String[ ] args) { int q; String n; Student s; s = new Student(); } Now let’s draw!

Draw this: class Student { int age; double gpa; public static void main(String[ ] args) { int q; String n; Student s; s = new Student(); } Student age gpa main args q

Draw this: class Student { int age; double gpa; public static void main(String[ ] args) { int q; String n; Student s; s = new Student(); } Student age gpa main args q n

Draw this: class Student { int age; double gpa; public static void main(String[ ] args) { int q; String n; Student s; s = new Student(); } Student age gpa main args q n s

Draw this: class Student { int age; double gpa; public static void main(String[ ] args) { int q; String n; Student s; s = new Student(); } Student age gpa main args q n s age gpa0 0

Draw this: class Student { int age; double gpa; public static void main(String[ ] args) { int q; String n; Student s; s = new Student(); } Student age gpa main args q n s age gpa0 0

Draw this: class Student { int age; double gpa; public static void main(String[ ] args) { int q; String n; Student s; s = new Student(); } Student age gpa main args q n s age gpa0 0 Notice that q and n have no values in them.

Draw this: class Student { int age; double gpa; public static void main(String[ ] args) { int q; String n; Student s; s = new Student(); } Student age gpa main args q n s age gpa0 0 Notice that q and n have no values in them. Unlike instance or class variables, local variables need to be initialized explicitly by the programmer.

Draw this: class Student { int age; double gpa; public static void main(String[ ] args) { int q; String n; Student s; s = new Student(); } Student age gpa main args q n s age gpa0 0 Instance and class variables have default values.

Draw this: class Student { int age; double gpa; public static void main(String[ ] args) { int q; String n; Student s; s = new Student(); } Student age gpa main args q n s age gpa0 0 Instance and class variables have default values. It’s not hard to guess the default values.

Draw this: class Student { int age = 20; double gpa; public static void main(String[ ] args) { Student s = new Student(); Student q = new Student(); q.age = 19; s.age = 21; q.age = 3 + q.age; }

Draw this: class Student { int age = 20; double gpa; public static void main(String[ ] args) { Student s = new Student(); Student q = new Student(); q.age = 19; s.age = 21; q.age = 3 + q.age; } Student age gpa main args

Draw this: class Student { int age = 20; double gpa; public static void main(String[ ] args) { Student s = new Student(); Student q = new Student(); q.age = 19; s.age = 21; q.age = 3 + q.age; } Student age gpa main args s age gpa 0 20

Draw this: class Student { int age = 20; double gpa; public static void main(String[ ] args) { Student s = new Student(); Student q = new Student(); q.age = 19; s.age = 21; q.age = 3 + q.age; } Student age gpa main args s q age gpa 0 20 age gpa 0 20

Draw this: class Student { int age = 20; double gpa; public static void main(String[ ] args) { Student s = new Student(); Student q = new Student(); q.age = 19; s.age = 21; q.age = 3 + q.age; } Student age gpa main args s q age gpa 0 20 age gpa 0 20

Draw this: class Student { int age = 20; double gpa; public static void main(String[ ] args) { Student s = new Student(); Student q = new Student(); q.age = 19; s.age = 21; q.age = 3 + q.age; } Student age gpa main args s q age gpa 0 20 age gpa 0 20

Draw this: class Student { int age = 20; double gpa; public static void main(String[ ] args) { Student s = new Student(); Student q = new Student(); q.age = 19; s.age = 21; q.age = 3 + q.age; } Student age gpa main args s q age gpa 0 20 age gpa 0 20

Draw this: class Student { int age = 20; double gpa; public static void main(String[ ] args) { Student s = new Student(); Student q = new Student(); q.age = 19; s.age = 21; q.age = 3 + q.age; } Student age gpa main args s q age gpa 0 19 age gpa 0 20

Draw this: class Student { int age = 20; double gpa; public static void main(String[ ] args) { Student s = new Student(); Student q = new Student(); q.age = 19; s.age = 21; q.age = 3 + q.age; } Student age gpa main args s q age gpa 0 19 age gpa 0 20

Draw this: class Student { int age = 20; double gpa; public static void main(String[ ] args) { Student s = new Student(); Student q = new Student(); q.age = 19; s.age = 21; q.age = 3 + q.age; } Student age gpa main args s q age gpa 0 19 age gpa 0 21

Draw this: class Student { int age = 20; double gpa; public static void main(String[ ] args) { Student s = new Student(); Student q = new Student(); q.age = 19; s.age = 21; q.age = 3 + q.age; } Student age gpa main args s q age gpa 0 19 age gpa 0 21

Draw this: class Student { int age = 20; double gpa; public static void main(String[ ] args) { Student s = new Student(); Student q = new Student(); q.age = 19; s.age = 21; q.age = 3 + q.age; } Student age gpa main args s q age gpa 0 19 age gpa 0 21

Draw this: class Student { int age = 20; double gpa; public static void main(String[ ] args) { Student s = new Student(); Student q = new Student(); q.age = 19; s.age = 21; q.age = ; } Student age gpa main args s q age gpa 0 19 age gpa 0 21

Draw this: class Student { int age = 20; double gpa; public static void main(String[ ] args) { Student s = new Student(); Student q = new Student(); q.age = 19; s.age = 21; q.age = 22; } Student age gpa main args s q age gpa 0 19 age gpa 0 21

Draw this: class Student { int age = 20; double gpa; public static void main(String[ ] args) { Student s = new Student(); Student q = new Student(); q.age = 19; s.age = 21; q.age = 22; } Student age gpa main args s q age gpa 0 19 age gpa 0 21

Draw this: class Student { int age = 20; double gpa; public static void main(String[ ] args) { Student s = new Student(); Student q = new Student(); q.age = 19; s.age = 21; q.age = 3 + q.age; } Student age gpa main args s q age gpa 0 22 age gpa 0 21

Now write this (in Java): Create two accounts, for Doug and Adrian.

Now write this (in Java): Create two accounts, for Doug and Adrian. Initial balance for both accounts is 10.

Now write this (in Java): Create two accounts, for Doug and Adrian. Initial balance for both accounts is 10. Then Doug’s account doubles, Adrian’s decreases by 3 dollars, and Doug’s account has one more dollar deposited into it.

Let me try it. How about this?

class Account { double balance = 10; public static void main(String[ ] args) { Account doug, adrian; doug = new Account(); adrian = new Account(); doug.balance = doug.balance * 2; adrian.balance -= 3; doug.balance++; } Let me try it. How about this?

class Account { double balance = 10; public static void main(String[ ] args) { Account doug, adrian; doug = new Account(); adrian = new Account(); doug.balance = doug.balance * 2; adrian.balance -= 3; doug.balance++; } Create two accounts, for Doug and Adrian. Initial balance for both accounts is 10. Then Doug’s account doubles, Adrian’s decreases by 3 dollars, and Doug’s account has one more dollar deposited into it.

class Account { double balance = 10; public static void main(String[ ] args) { Account doug, adrian; doug = new Account(); adrian = new Account(); doug.balance = doug.balance * 2; adrian.balance -= 3; doug.balance++; } Create two accounts, for Doug and Adrian. Initial balance for both accounts is 10. Then Doug’s account doubles, Adrian’s decreases by 3 dollars, and Doug’s account has one more dollar deposited into it.

class Account { double balance = 10; public static void main(String[ ] args) { Account doug, adrian; doug = new Account(); adrian = new Account(); doug.balance = doug.balance * 2; adrian.balance -= 3; doug.balance++; } Create two accounts, for Doug and Adrian. Initial balance for both accounts is 10. Then Doug’s account doubles, Adrian’s decreases by 3 dollars, and Doug’s account has one more dollar deposited into it.

class Account { double balance = 10; public static void main(String[ ] args) { Account doug, adrian; doug = new Account(); adrian = new Account(); doug.balance = doug.balance * 2; adrian.balance -= 3; doug.balance++; } Create two accounts, for Doug and Adrian. Initial balance for both accounts is 10. Then Doug’s account doubles, Adrian’s decreases by 3 dollars, and Doug’s account has one more dollar deposited into it.

class Account { double balance = 10; public static void main(String[ ] args) { Account doug, adrian; doug = new Account(); adrian = new Account(); doug.balance = doug.balance * 2; adrian.balance -= 3; doug.balance++; } Create two accounts, for Doug and Adrian. Initial balance for both accounts is 10. Then Doug’s account doubles, Adrian’s decreases by 3 dollars, and Doug’s account has one more dollar deposited into it.

class Account { double balance = 10; public static void main(String[ ] args) { Account doug, adrian; doug = new Account(); adrian = new Account(); doug.balance = doug.balance * 2; adrian.balance -= 3; doug.balance++; } Create two accounts, for Doug and Adrian. Initial balance for both accounts is 10. Then Doug’s account doubles, Adrian’s decreases by 3 dollars, and Doug’s account has one more dollar deposited into it.

class Account { double balance = 10; public static void main(String[ ] args) { Account doug, adrian; doug = new Account(); adrian = new Account(); doug.balance = doug.balance * 2; adrian.balance -= 3; doug.balance++; } Create two accounts, for Doug and Adrian. Initial balance for both accounts is 10. Then Doug’s account doubles, Adrian’s decreases by 3 dollars, and Doug’s account has one more dollar deposited into it.

class Account { double balance = 10; public static void main(String[ ] args) { Account doug, adrian; doug = new Account(); adrian = new Account(); doug.balance = doug.balance * 2; adrian.balance -= 3; doug.balance++; } Account balance

class Account { double balance = 10; public static void main(String[ ] args) { Account doug, adrian; doug = new Account(); adrian = new Account(); doug.balance = doug.balance * 2; adrian.balance -= 3; doug.balance++; } Looks good.

class Account { double balance = 10; public static void main(String[ ] args) { Account doug, adrian; doug = new Account(); adrian = new Account(); doug.balance = doug.balance * 2; adrian.balance -= 3; doug.balance++; } Looks good. Thank you.

Draw this:

class BankAccount { double balance = 25; public static void main(String[ ] args) { BankAccount doug = new BankAccount(); BankAccount adrian = new BankAccount(); doug.balance = doug.balance * 2; adrian.balance -= 3; doug.balance++; }

Draw this: class BankAccount { double balance = 25; public static void main(String[ ] args) { BankAccount doug = new BankAccount(); BankAccount adrian = new BankAccount(); doug.balance = doug.balance * 2; adrian.balance -= 3; doug.balance++; } Too easy, almost the same thing...

Draw this:

class Account { double balance = 20; void deposit(int n) { this.balance += n; } Draw this:

class Account { double balance = 20; void deposit(int n) { this.balance += n; } Draw this: Account this.balance n deposit this.balance += n;

class Account { double balance = 20; void deposit(int n) { this.balance += n; } Draw this: Account this.balance n deposit + this.balance += n;

class Account { double balance = 20; void deposit(int n) { this.balance += n; } Draw this:balance is an instance variable. Account this.balance n deposit + this.balance += n;

class Account { double balance = 20; void deposit(int n) { this.balance += n; } Draw this:balance is an instance variable. It belongs to the object. Account this.balance n deposit + this.balance += n;

class Account { double balance = 20; void deposit(int n) { this.balance += n; } Draw this:deposit is an instance method. It belongs to the object and describes an update on its instance variables (state). Account this.balance n deposit + this.balance += n;

class Account { double balance = 20; void deposit(int n) { this.balance += n; } Draw this:n is not a local variable in deposit. Account this.balance n deposit + this.balance += n;

class Account { double balance = 20; void deposit(int n) { this.balance += n; } Draw this:It is called a parameter of the method deposit. It is a way of naming the method’s inputs. It is a way to refer to the ingredients of the recipe. Account this.balance n deposit + this.balance += n;

class Account { double balance = 20; void deposit(int n) { this.balance += n; } Draw this:The keyword this allows an object to refer to itself. Account this.balance n deposit + this.balance += n;

Draw this: Account this.balance n deposit + this.balance += n; class Account { double balance = 20; void deposit(int n) { this.balance += n; } public static void main(String[ ] args) { Account m = new Account(); Account q = new Account(); m.deposit(10); q.deposit(3); }

Draw this: Account this.balance n deposit + this.balance += n; class Account { double balance = 20; void deposit(int n) { this.balance += n; } public static void main(String[ ] args) { Account m = new Account(); Account q = new Account(); m.deposit(10); q.deposit(3); } main args

Draw this: Account class Account { double balance = 20; void deposit(int n) { this.balance += n; } public static void main(String[ ] args) { Account m = new Account(); Account q = new Account(); m.deposit(10); q.deposit(3); } + this.balance n deposit + this.balance += n; main args m 20

Draw this: Account class Account { double balance = 20; void deposit(int n) { this.balance += n; } public static void main(String[ ] args) { Account m = new Account(); Account q = new Account(); m.deposit(10); q.deposit(3); } + this.balance n deposit + this.balance += n; main args m + q 20

Draw this: Account class Account { double balance = 20; void deposit(int n) { this.balance += n; } public static void main(String[ ] args) { Account m = new Account(); Account q = new Account(); m.deposit(10); q.deposit(3); } + this.balance n deposit + this.balance += n; main args m + q 20

Draw this: Account class Account { double balance = 20; void deposit(int n) { this.balance += n; } public static void main(String[ ] args) { Account m = new Account(); Account q = new Account(); m.deposit(10); q.deposit(3); } + this.balance n deposit + this.balance += n; main args m + q 20

Draw this: Account class Account { double balance = 20; void deposit(int n) { this.balance += n; } public static void main(String[ ] args) { Account m = new Account(); Account q = new Account(); m.deposit(10); q.deposit(3); } + this.balance n deposit + this.balance += n; main args m + q 20

Draw this: Account class Account { double balance = 20; void deposit(int n) { this.balance += n; } public static void main(String[ ] args) { Account m = new Account(); Account q = new Account(); m.deposit(10); q.deposit(3); } + this.balance n deposit + this.balance += n; main args m + q 20

Draw this: Account class Account { double balance = 20; void deposit(int n) { this.balance += n; } public static void main(String[ ] args) { Account m = new Account(); Account q = new Account(); m.deposit(10); q.deposit(3); } + this.balance n deposit + this.balance += n; main args m + q 20 10

Draw this: Account class Account { double balance = 20; void deposit(int n) { this.balance += n; } public static void main(String[ ] args) { Account m = new Account(); Account q = new Account(); m.deposit(10); q.deposit(3); } + this.balance n deposit + this.balance += n; main args m + q 30 20

Draw this: Account class Account { double balance = 20; void deposit(int n) { this.balance += n; } public static void main(String[ ] args) { Account m = new Account(); Account q = new Account(); m.deposit(10); q.deposit(3); } + this.balance n deposit + this.balance += n; main args m + q 30 20

Draw this: Account class Account { double balance = 20; void deposit(int n) { this.balance += n; } public static void main(String[ ] args) { Account m = new Account(); Account q = new Account(); m.deposit(10); q.deposit(3); } + this.balance n deposit + this.balance += n; main args m + q 30 20

Draw this: Account class Account { double balance = 20; void deposit(int n) { this.balance += n; } public static void main(String[ ] args) { Account m = new Account(); Account q = new Account(); m.deposit(10); q.deposit(3); } + this.balance n deposit + this.balance += n; main args m + q

Draw this: Account class Account { double balance = 20; void deposit(int n) { this.balance += n; } public static void main(String[ ] args) { Account m = new Account(); Account q = new Account(); m.deposit(10); q.deposit(3); } + this.balance n deposit + this.balance += n; main args m + q 30 23

Draw this: class Account { double balance; Account(double x) { } public static void main(String[ ] args) { Account m = new Account(10); Account q = new Account(3); m.balance = 10 + m.balance; }

Draw this: class Account { double balance; Account(double x) { } public static void main(String[ ] args) { Account m = new Account(10); Account q = new Account(3); m.balance = 10 + m.balance; } This is a constructor.

Draw this: class Account { double balance; Account(double x) { } public static void main(String[ ] args) { Account m = new Account(10); Account q = new Account(3); m.balance = 10 + m.balance; } It’s like an initialization procedure.

Draw this: class Account { double balance; Account(double x) { } public static void main(String[ ] args) { Account m = new Account(10); Account q = new Account(3); m.balance = 10 + m.balance; } It’s attached to the blueprint

Draw this: class Account { double balance; Account(double x) { } public static void main(String[ ] args) { Account m = new Account(10); Account q = new Account(3); m.balance = 10 + m.balance; } It’s attached to the blueprint (not instances)

Draw this: class Account { double balance; Account(double x) { } public static void main(String[ ] args) { Account m = new Account(10); Account q = new Account(3); m.balance = 10 + m.balance; } It’s attached to the blueprint (not instances) This one is empty.

Draw this: class Account { double balance; Account(double x) { } public static void main(String[ ] args) { Account m = new Account(10); Account q = new Account(3); m.balance = 10 + m.balance; } Account this.balance x Account main args

Draw this: class Account { double balance; Account(double x) { } public static void main(String[ ] args) { Account m = new Account(10); Account q = new Account(3); m.balance = 10 + m.balance; } Account this.balance x Account main args m 10 0 balance

Draw this: class Account { double balance; Account(double x) { } public static void main(String[ ] args) { Account m = new Account(10); Account q = new Account(3); m.balance = 10 + m.balance; } Account this.balance x Account main args m 0 balance

Draw this: class Account { double balance; Account(double x) { } public static void main(String[ ] args) { Account m = new Account(10); Account q = new Account(3); m.balance = 10 + m.balance; } Account this.balance x Account main args m 0 balance q 0

Draw this: class Account { double balance; Account(double x) { } public static void main(String[ ] args) { Account m = new Account(10); Account q = new Account(3); m.balance = 10 + m.balance; } Account this.balance x Account main args m 0 balance q 0

class Account { double balance; Account(double x) { } public static void main(String[ ] args) { Account m = new Account(10); Account q = new Account(3); m.balance = 10 + m.balance; } Draw this: Account this.balance x Account main args m 0 balance q m.balance

class Account { double balance; Account(double x) { } public static void main(String[ ] args) { Account m = new Account(10); Account q = new Account(3); m.balance = 10 + m.balance; } Draw this: Account this.balance x Account main args m 0 balance q

class Account { double balance; Account(double x) { } public static void main(String[ ] args) { Account m = new Account(10); Account q = new Account(3); m.balance = 10 + m.balance; } Draw this: Account this.balance x Account main args m 0 balance q 0 10

class Account { double balance; Account(double x) { } public static void main(String[ ] args) { Account m = new Account(10); Account q = new Account(3); m.balance = 10 + m.balance; } Draw this: Account this.balance x Account main args m 10 balance q 0

class X { int x; } Draw this:

X x How easy. class X { int x; } Draw this:

class A { int x; A (int initialValue) { this.x = initialValue; } /* do this when you first create the object but don’t make it part of the generated instance */ } Draw this:

class A { int x; A (int initialValue) { this.x = initialValue; } /* do this when you first create the object but don’t make it part of the generated instance */ } Draw this:That’s called a constructor. A x initialValue A

class A { int x; A (int initialValue) { this.x = initialValue; } /* do this when you first create the object but don’t make it part of the generated instance */ } Draw this:That’s called a constructor. A x initialValue A

class A { int x; A (int initialValue) { this.x = initialValue; } /* do this when you first create the object but don’t make it part of the generated instance */ } Draw this:Notice the name of the constructor. A x initialValue A

class A { int x; A (int initialValue) { this.x = initialValue; } /* do this when you first create the object but don’t make it part of the generated instance */ } Draw this:Notice the name of the constructor. It’s the name of the class. A x initialValue A

class A { int x; A (int initialValue) { this.x = initialValue; } /* do this when you first create the object but don’t make it part of the generated instance */ } Draw this:Notice the name of the constructor. It’s the name of the class. A constructor looks like a method (procedure). A x initialValue A

class A { int x; A (int initialValue) { this.x = initialValue; } /* do this when you first create the object but don’t make it part of the generated instance */ } Draw this:Notice the name of the constructor. It’s the name of the class. A constructor looks like a method (procedure). But doesn’t have a return type. A x initialValue A

class A { int x; A (int initialValue) { this.x = initialValue; } /* do this when you first create the object but don’t make it part of the generated instance */ } Draw this:Here’s how the constructor works: A x initialValue A

class A { int x; A (int initialValue) { this.x = initialValue; } /* do this when you first create the object but don’t make it part of the generated instance */ } Draw this:Here’s how the constructor works: A x initialValue A this.x = initialValue;

class A { int x; A (int initialValue) { this.x = initialValue; } /* do this when you first create the object but don’t make it part of the generated instance */ } Draw this:When the constructor is invoked, a value is passed (as an argument) to the constructor. A x initialValue A this.x = initialValue;

class A { int x; A (int initialValue) { this.x = initialValue; } /* do this when you first create the object but don’t make it part of the generated instance */ } Draw this:The constructor then starts executing. A x initialValue A this.x = initialValue;

class A { int x; A (int initialValue) { this.x = initialValue; } /* do this when you first create the object but don’t make it part of the generated instance */ } Draw this:The constructor then starts executing. The value is taken from the argument, A x initialValue A this.x = initialValue;

class A { int x; A (int initialValue) { this.x = initialValue; } /* do this when you first create the object but don’t make it part of the generated instance */ } Draw this:The constructor then starts executing. The value is taken from the argument, into the instance variable. A x initialValue A this.x = initialValue;

class A { int x; A (int initialValue) { this.x = initialValue; } /* do this when you first create the object but don’t make it part of the generated instance */ } Draw this:Thus the instance variable will have been initialized with what is in initialValue. A x initialValue A this.x = initialValue; initialValue;

Speaking of this, can I show you an example?

Sure, go ahead.

Here it is: class Speakers { public static void main(String[ ] args) { Speaker a = new Speaker(“Larry”); Speaker b = new Speaker(“Michael”); Speaker c = new Speaker(“Toni”); a.talk(); b.talk(); c.talk(); } class Speaker { String speaker; Speaker(String name) { speaker = name; } void talk() { System.out.println(this.speaker + “ is very happy.”); }

This writer is very happy.Here it is: class Speakers { public static void main(String[ ] args) { Speaker a = new Speaker(“Larry”); Speaker b = new Speaker(“Michael”); Speaker c = new Speaker(“Toni”); a.talk(); b.talk(); c.talk(); } class Speaker { String speaker; Speaker(String name) { speaker = name; } void talk() { System.out.println(this.speaker + “ is very happy.”); }

This writer is very happy.Ha, ha. Draw this: class Student { String name; Student(String w) { this.name = w; } class Tuesday { public static void main(String[ ] args) { Student q = new Student(“Larry”); Student u = new Student(“John”); Student s = new Student(“Adrian”); }

That’s funny.Ha, ha. Draw this: class Student { String name; Student(String w) { this.name = w; } class Tuesday { public static void main(String[ ] args) { Student q = new Student(“Larry”); Student u = new Student(“John”); Student s = new Student(“Adrian”); }

That’s funny.Yes. class Student { String name; Student(String w) { this.name = w; } class Tuesday { public static void main(String[ ] args) { Student q = new Student(“Larry”); Student u = new Student(“John”); Student s = new Student(“Adrian”); } We should always try to refer to instance variables through the instance they belong to.

That’s funny.Yes. We should always try to refer to instance variables through the instance they belong to. This way we would also be able to keep them separate from local variables and parameters (or arguments). class Student { String name; Student(String w) { this.name = w; } class Tuesday { public static void main(String[ ] args) { Student q = new Student(“Larry”); Student u = new Student(“John”); Student s = new Student(“Adrian”); }

Draw this: class Checking { double balance; Checking(double x) { this.balance = x; } void deposit(double y) { this.balance = this.balance + y; }

Draw this: class Tea { double bird; void book(double watch) { this.bird = watch – this.bird; } Tea (double snake) { this.bird = snake * 2; }

Draw this: class Tea { double bird; void book(double watch) { this.bird = watch – this.bird; } Tea (double snake) { this.bird = snake * 2; } instance variable

class Tea { double bird; void book(double watch) { this.bird = watch – this.bird; } Tea (double snake) { this.bird = snake * 2; } Draw this: instance method

class Tea { double bird; void book(double watch) { this.bird = watch – this.bird; } Tea (double snake) { this.bird = snake * 2; } Draw this: formal parameter

class Tea { double bird; void book(double watch) { this.bird = watch – this.bird; } Tea (double snake) { this.bird = snake * 2; } Draw this: formal parameter (like a local variable only initialized when invoked)

class Tea { double bird; void book(double watch) { this.bird = watch – this.bird; } Tea (double snake) { this.bird = snake * 2; } Draw this: constructor

Draw this: class Tea { double bird; void book(double watch) { this.bird = watch – this.bird; } Tea (double snake) { this.bird = snake * 2; }

Draw this: class Hat { public static void main(String[ ] args) { Tea m = new Tea(-2); Tea n = new Tea(42); m.book(92); n.bird = m.bird + n.bird; n.book(25); }

Draw this: class Hat { public static void main(String[ ] args) { Tea m = new Tea(-2); Tea n = new Tea(42); m.book(92); n.bird = m.bird + n.bird; n.book(25); } class Tea { double bird; void book(double watch) { this.bird = watch – this.bird; } Tea (double snake) { this.bird = snake * 2; } Where Tea is the class seen before.

Draw this: class Hat { public static void main(String[ ] args) { Tea m = new Tea(-2); Tea n = new Tea(42); m.book(92); n.bird = m.bird + n.bird; n.book(25); } class Tea { double bird; void book(double watch) { this.bird = watch – this.bird; } Tea (double snake) { this.bird = snake * 2; } Where Tea is the class seen before. (Soliloquy p. 133) We should also use BlueJ to examine this example thoroughly.

Last example: class X { int q; } class Play { public static void main(String[ ] args) { X u = new X(); X p = new X(); u.q = 3; p.q = u.q + p.q; u.q = u.q + p.q; }

Last example: class X { int q; } class Play { public static void main(String[ ] args) { X u = new X(); X p = new X(); u.q = 3; p.q = u.q + p.q; u.q = u.q + p.q; } This should be easy, by now.

So what types do we have in Lab Eight (for example)?

Point

So what types do we have in Lab Eight (for example)? Point Circle

So what types do we have in Lab Eight (for example)? Point Circle Rectangle

So what types do we have in Lab Eight (for example)? Point Circle Rectangle We should also discuss Robot (to understand Penguin).

Thank you.