Java bootcamp Spring 20041 Java Bootcamp 2004 Expectations: We assume that you have studied C, C++, or Java and know about the following: Variables and.

Slides:



Advertisements
Similar presentations
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.
Advertisements

L2:CSC © Dr. Basheer M. Nasef Lecture #2 By Dr. Basheer M. Nasef.
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
CERTIFICATION OBJECTIVES Use Class Members Develop Wrapper Code & Autoboxing Code Determine the Effects of Passing Variables into Methods Recognize when.
Data Types in Java Data is the information that a program has to work with. Data is of different types. The type of a piece of data tells Java what can.
23-May-15 Polymorphism. 2 Signatures In any programming language, a signature is what distinguishes one function or method from another In C, every function.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Inheritance and Polymorphism.
15-Jun-15 Polymorphism. 2 Signatures In any programming language, a signature is what distinguishes one function or method from another In C, every function.
1 Review of classes and subclasses M fast through this material, since by now all have seen it in CS100 or the Java bootcamp First packages Then classes.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
Java Syntax Primitive data types Operators Control statements.
1 Inheritance and Polymorphism. 2 Motivations Suppose you will define classes to model circles, rectangles, and triangles. These classes have many common.
Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
1 CS100J 05 February 2005 Today’s topic: Customizing a class (continued) Quiz 1 is today Quiz 2 is next Tuesday Quote for the day: There is no reason anyone.
CS 1110 Final Exam: Review Session 2 Part 1 : Inheriting classes 1. Inheritance Facts 2. Constructors in Subclasses BREAK : 10 sec. Part 2 : Working with.
1 CS1130 Spring 2011: David Gries & Walker White Transition to Object-Oriented Programming Assumes programming knowledge in a language like Matlab, C,
1 Inheritance and Polymorphism Chapter 9. 2 Polymorphism, Dynamic Binding and Generic Programming public class Test { public static void main(String[]
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
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.
1 Do you have a CS account? Primitive types –“ building blocks ” for more complicated types Java is strongly typed –All variables in a Java program must.
Lecture 2 Object Oriented Programming Basics of Java Language MBY.
1 Biggest issue!!! You can’t do questions on this topic correctly unless you draw variables, draw objects when they are created, and draw frames for method.
1 CS2111 Spring Instructor: David Gries You job: Come to class each week, take part. My job in first few weeks: Get you to under- stand and be facile.
APCS Java AB 2004 Review of CS1 and CS2 Review for AP test #1 Sources: 2003 Workshop notes from Chris Nevison (Colgate University) AP Study Guide to go.
CS/ENGRD 2110 SPRING 2015 Lecture 6: Consequence of type, casting; function equals 1.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
CS/ENGRD 2110 FALL 2014 Lecture 6: Consequence of type, casting; function equals 1.
CS/ENGRD 2110 SPRING 2012 Lecture 2: Objects and classes in Java 1.
Slides prepared by Rose Williams, Binghamton University Chapter 5 Defining Classes II.
Java development environment and Review of Java. Eclipse TM Intergrated Development Environment (IDE) Running Eclipse: Warning: Never check the “Use this.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Some Standard Classes Goals The Object class The String class Wrapper classes The Math class Random Numbers.
Copyright Curt Hill Variables What are they? Why do we need them?
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Chapter 5 Objects and Classes Inheritance. Solution Assignments 3 & 4 Review in class…..
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 9 Inheritance and.
CS100A, Fall 1998 Key Concepts 1 These notes contain short definitions of the basic entities that make up a Java program, along with a description of the.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
1 CS100J 08 September 2005 Today’s topic: Customizing a class (continued) Quote for the day: There is no reason anyone would want a computer in their home.
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
TeachJava! 2003 Corky Cartwright Dung Nguyen Stephen Wong Charlie Reis, James Hsia, Peter Centgraf.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
CS/ENGRD 2110 SPRING 2016 Lecture 2: Objects and classes in Java 1.
CS/ENGRD 2110 FALL 2015 Lecture 6: Consequence of type, casting; function equals 1.
1 Object-Oriented Programming Inheritance. 2 Superclasses and Subclasses Superclasses and Subclasses  Superclasses and subclasses Object of one class.
Basic Object-Oriented concepts. Concept: Classes describe objects Every object belongs to (is an instance of) a class An object may have fields –The class.
1 CS100J 27 September 2005 Casting About 1.Casting between classes 2.Apparent and real classes. 3.Operator instanceof Procrastination Leave nothing for.
CS/ENGRD 2110 FALL 2013 Lecture 3: Fields, getters and setters, constructors, testing 1.
28-Dec-04polymorhism.ppt1 Polymorphism. 28-Dec-04polymorhism.ppt2 signatures in any programming language, a signature is what distinguishes one function.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
CS100Lecture 61 Announcements Homework P1 due on Thursday Homework P2 handed out.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
CS/ENGRD 2110 Fall 2017 Lecture 2: Objects and classes in Java
Chapter 7 User-Defined Methods.
Lecture 10 Review of classes
CS100J 21 February 2005 Casting About
Java Primer 1: Types, Classes and Operators
Multiple variables can be created in one declaration
Sorry these slides weren’t available last evening!
The fattest knight at King Arthur's round table was Sir Cumference
Polymorphism 11-Nov-18.
Overloading and Constructors
Java Classes and Objects 3rd Lecture
CS/ENGRD 2110 Spring 2019 Lecture 2: Objects and classes in Java
Java Programming Language
Chap 2. Identifiers, Keywords, and Types
CS/ENGRD 2110 Spring 2019 Lecture 2: Objects and classes in Java
Presentation transcript:

Java bootcamp Spring Java Bootcamp 2004 Expectations: We assume that you have studied C, C++, or Java and know about the following: Variables and variable declarations Expressions (integer, boolean) Assignment statement If-statement and if-else statement While-loop and for-loop

Java bootcamp Spring We concentrate on the following Java constructs: Primitive types, variables, expressions Casting between types The class as a definition of the format of an object (instance, manilla folder) The new-expression Referencing instance variables and methods Methods (procedures, functions, constructors) Subclasses, inheritance, and overriding

Java bootcamp Spring DrJava We use the IDE (Interactive Development Environment) DrJava in this course. We use it to demo during lecture. Its Interactions pane allows us to evaluate expressions and execute statements (including method calls) without having to have a complete Java application. If you have your own computer, please get on the course website, download DrJava, and practice using it. Use it to learn about Java. We’ll use it in this bootcamp.

Java bootcamp Spring Resources for learning Java See website for reading material ProgramLive, by Gries & Gries. Has a CD, which has minute lectures with synched animation. Used in CS100J this year. The glossary of the CD is a good source of information. Course textbook. Java Precisely. Java in a Nutshell. Java tutorial:

Java bootcamp Spring Primitive types typerange of valuesspace used byte– byte short– bytes int– bytes *** long– bytes float 6 significant digits, 10 – bytes double15 sig. digits, 10 – bytes *** charUnicode character2 bytes *** boolean{false, true}1 bit *** or 1 byte

Java bootcamp Spring Use mainly these types typerange of valuesspace used int– bytes double15 sig. digits, 10 – bytes charUnicode character2 bytes boolean{false, true}1 bit or 1 byte Operations on type int – h h + k h – k h * k h / k h % k h / k yields an int: 7 / 2 is 3!!!! h % k is the remainder when h is divided by k.

Java bootcamp Spring min, max values for primitive types Short.MAX_VALUE smallest short value Short.MIN_VALUE largest short value Integer.MAX_VALUEsmallest int value Integer.MIN_VALUElargest int value Double.MAX_VALUEsmallest POSITIVE double Double.MIN_VALUElargest POSITIVE double etc.

Java bootcamp Spring Type boolean Values: true and false Complement: ! b And (conjunction):b && c value: if b is false, then false; otherwise, whatever c is. Or (disjunction):b || c value: if b is true, then true; otherwise, whatever c is SHORT-CIRCUIT EVALUATION x = 0 || 5 / x = 1 is true x 5 / x = 1 || x = 0 GIVES AN EXCEPTION C, C++ use 1 and 0 for true and false. Java has a special type, with two values: true and false. 0

Java bootcamp Spring Type boolean Don’t write Write if (b == true)if ( b ) if (b == false)if ( !b ) if (x == 0) y= x == 0; y= true; else y= false

Java bootcamp Spring Basic variable declarations Variable: a name with associated integer; a named box, with a value in the box. Basic declaration: ; int x; x boolean b; b Whether a variable has a specific default value when declared depends on where it is declared. Discuss later. 5 true

Java bootcamp Spring Assignment statement syntax: = ; semantics: Evaluate the and store its value in the. read as: becomes x= x + 1; // Add 1 to x // or add 1 to the value of x x= y;// The value of the expression is the value in // y. The value is stored in x. true

Java bootcamp Spring Basic initializing declarations Basic initializing declaration: = ; Equivalent to: ; = ; int x= 5 * 3; x boolean b= true; b 15 true

Java bootcamp Spring Casting narrowest type widest type byte -> short -> int -> long -> float -> double There are no operations in types byte and short. If they appear as operand of an operation, they are promoted (automatically cast) to int or long and the operation is performed in int or long. If one operand of x + y is long, the other is cast to long and a long addition is done. Otherwise, the operation is an int addition.

Java bootcamp Spring Casting narrowest type widest type byte -> short -> int -> long -> float -> double byte b= 5; b= b + 5; // illegal, because exp has type int. b= (byte) (b + 5); (byte) is a prefix operator, called a caste. It casts its operand to type byte. Use any type as a caste, e.g. (int) Widening casts performed automatically. Narrowing casts have to be explicit.

Java bootcamp Spring Type char narrowest type widest type char -> int -> long -> float -> double Values of type char: the characters, ‘b’ ‘5’ ‘&’ ‘\n’ new-line character ‘\\’ backslash char (int) ‘A’ is the integer that represents char ‘A’: 65 (char) 65 is the character that is represented by 65: ‘A’ You don’t need to remember much about type char. Just that it exists, and you can look it up whenever you want. Best reference: ProgramLive.

Java bootcamp Spring Class String Mentioned now because you may hear about it from time to time. An object of class String is a sequence of chars: “abcd123#\n” Note: double quotes for String, single quotes for char 1 + “abc” is “1abc2.1” “abc” is 3 + “abc” is “3abc” If at least one operand of + is a String, then + denotes “catenation”. The other operand is converted to a String, if necessary. s.length() number of characters in String s s.charAt(i) character at position i of s (0 is first) s.substring(h..k)substring s[h..k-1]

Java bootcamp Spring The Class All variables, methods (procs, funcs) are declared in a class. Class def. defines format of objects (instances) of the class. public class C { Declaration of instance variable (field) x; Declaration of instance variable (field) y; Declaration of instance method m1(int); Declaration of instance method m2(); } a0 C x y 5 true m1(int) m2() Object drawn like a manilla folder Tab contains name of object (address in memory) class name

Java bootcamp Spring The Class public class C { private int x; private double y; public void m1(int) { …} public int m2() { … } a0 C x y 5 true m1(int) m2() Object drawn like a manilla folder Tab contains name of object (address in memory) public components can be referenced anywhere; private ones only in the class itself. Generally, but not always, fields are private and methods are public.

Java bootcamp Spring Three kinds of method public void proc(par. decs) { body } public int func(par. decs) { body } public C(par. decs) { body } proc is a procedure. It does not return a value. A call of it is a statement. func is a function. It returns a value of type int. A call of it is an expression. C is a constructor. Can be defined only in class C. Can be called only in restricted places. Purpose: initialize (some) fields of a new object of class C. body: sequence of state- ments and declarations.

Java bootcamp Spring The new-expression public class C1 { private int x= 4; private double y= 2.0; public void m1(int) { …} public C1(int p) { x= p; } new C1(5) The new-expression is evaluated in 3 steps: 1.Create an object of class C1 (that’s what “new C1” says), initializing fields acc. to their declarations. 2.Execute constructor call C1(5). 3. Yield as the value of the new- expression the name of the object.

Java bootcamp Spring The new-expression public class C1 { private int x= 4; private double y= 2.0; public void m1(int) { …} public C1(int p) { x= p; } a0 C1 x y m1(int) C1(int) new C1(5) 1.Create an object of class C1 (that’s what “new C1” says), initializing fields acc. to their declarations

Java bootcamp Spring The new-expression public class C1 { private int x= 4; private double y= 2.0; public void m1(int) { …} public C1(int p) { x= p; } a0 C1 x y m1(int) C1(int) new C1(5) 1.Create an object of class C1 (that’s what “new C1” says), initializing fields acc. to their declarations 2.Execute constructor call C1(5).

Java bootcamp Spring The new-expression public class C1 { private int x= 4; private double y= 2.0; public void m1(int) { …} public C1(int p) { x= p; } a0 C1 x y m1(int) C1(int) new C1(5) 1.Create an object of class C1 (that’s what “new C1” says), initializing fields acc. to their declarations 2.Execute constructor call C1(5). 3.Yield as the value of the new-expression the name of the object. a0

Java bootcamp Spring The new-expression –used in an assignment public class C1 { private int x= 4; private double y= 2.0; public void m1(int) { …} // Constructor: an instance with x field equal to p public C1(int p) { x= p; } a0 C1 x y m1(int) C1(int) C1 c; … c= new C1(5); a0 c

Java bootcamp Spring Our analogy for explaining classes and objects public class C1 { private int x; private double y; public void m1(int) { …} public C1(int p) { … } } a0 C1 x y m1(int) C1(int) a0 C1 x y m1(int) C1(int) file drawer C1 Class is a file drawer: contains the objects (manilla folders) of the class.

Java bootcamp Spring Default constructor public class C1 { private int x= 4; private double y= 2.0; public void m1(int) { …} } a0 C1 x y m1(int) C1() a0 c If you don’t declare a constructor, then the following one is automatically declared for you: public C1() {} c= new C1();

Java bootcamp Spring Static components public class CS { private static int c= 4; private int s= 2.0; public void mc(int) { …} public static void ms(int) { … } } Static variable or class variable. Static method or class method. Static component goes not in object but in file drawer. Only ONE copy of each static component. a0 CS s 4 ms(int) a1 CS s 8 ms(int) File drawer for CS c mc(int) 8

Java bootcamp Spring Java class Math contains only static components public class Math { public static final double PI; // pi public static final double E; // base of natural log public static double sin(double angle) public static int abs(int x) public static double abs(double x) public static int ceil(int x) public static double ceil(double) … } final: no other assignments to it. Can’t be changed Some method names are overloaded. Distinguish between them based on number and types of arguments.

Java bootcamp Spring Getter methods public class C1 { private int x= 4; private double y= 2.0; /** = field x */ public int getX() { return x; } /** = field y */ public double getY() { return y; } Generally, fields are private, so that they cannot be accessed directly. To allow access, provide a public getter method, i.e. a function that returns the value of the field. This technique provides some security and is a good software engineering technique. The example to the right shows conventions for naming and specifying getter methods.

Java bootcamp Spring Setter methods public class C1 { private int x= 4; private double y= 2.0; /** = field x */ public int getX() { return x; } /** = field y */ public double getY() { return y; } /** Set field x to p */ public void setX(int p) { x= p; } A setter method is an instance procedure that saves some value in an object. The example to the right shows conventions for naming and specifying setter methods.

Java bootcamp Spring Method toString public class Point { /* The point is (x, y) */ private int x= 4; private int y= 2; /** = description of this instance */ public String toString() { return “(“ + x + “, “ + y + “)”; } Define instance method toString in almost every class. It yields a description of the object in which it occurs, using a format that makes sense for the class. System.out.println(new Point()); prints (4, 2) In some contexts where a String is expected and an object appears, the objects’ toString method is called.

Java bootcamp Spring Keyword this.When it appears in a method, this refers to the object in which the method occurs. public class Point { /* The point is (x, y) */ private int x= 4; private int y= 2; /** = point p equals point q */ public static boolean equals(Point p, Point q) { return p.x == q.x && p.y == q.y; } /** = this point equals point p */ public boolean equals(Point p) { return equals(p, this); } b1 Point x 5 y 8 equals(Point) b2 Point x 4 y 2 equals(Point)

Java bootcamp Spring Summary of classes in Java Class defines content of file drawer and format of objects: File drawer contains static components and created objects, drawn as manilla folders. The name of an object —its location in memory— is drawn on the tab of the folder. new-expression, used to create objects. Know the 3 steps in evaluating it. Constructor: called in new-expression to initialize fields. Use of private and public. Getter and setter methods. static vs. non-static variables (instance variables or fields). static vs. non-static methods (instance methods). Method toString. Two uses of keyword this.

Java bootcamp Spring Subclasses The subclass definition has this form: public class subclass-name extends superclass-name { declarations of  instance variables  instance methods  class variables  class methods } only difference between a subclass definition and a superclass definition

Java bootcamp Spring Class Animal public class Animal { private String name= “”; /** Constructor: instance with name n */ public Animal(String n) { name= n; } /** = a description of this Animal */ public String toString() { return name; } /** = noise this animal makes */ public String noise() { return null; } } Animal a0 ? name Animal(String) toString() noise()

Java bootcamp Spring Subclass Dog public class Dog extends Animal { private int friendliness; /** Constructor: instance with name n, friendly rank r */ public Dog(String n, int r) { …} /** = desc. of this Dog */ public String toString() {…} /** = noise this animal makes */ public String noise() {…} /** = friendliness of this Dog */ public int getFriendliness() {…} } Animal d1 fido name Animal(String) toString() noise() Dog Dog(String,int) toString() noise() getFriendliness() friendliness 5 subclass inherits all components of superclass

Java bootcamp Spring Subclass Dog: constructor public class Dog extends Animal { private int friendliness; /** Constructor: instance with name n, friendly rank r */ public Dog(String n, int r) { super(n); friendliness= r; } … } Animal d1 fido name Animal(String) toString() noise() Dog Dog(String,int) toString() noise() getFriendliness() friendliness 5 constructor can’t reference field name —it’s private. FIRST statement of constructor can be a call on a superclass constructor. Example shows how to do it.

Java bootcamp Spring Subclass Dog: Overriding public class Dog extends Animal { public Dog(String n, int r) { … } public String toString() { … } … } Animal d1 fido name Animal(String) toString() noise() Dog Dog(String,int) toString() noise() getFriendliness() friendliness 5 d= new Dog(“fido”, 10); d.toString(); declaration of toString in Dog overrides declaration of toString in Animal. When determining which method to call, use the bottom- up rule: start at bottom of folder and work upword

Java bootcamp Spring Calling the inherited method: another use of super public class Dog extends Animal { public String toString() { return super.toString() + “, friendliness “ + friendliness; } … } Animal d1 fido name Animal(String) toString() noise() Dog Dog(String,int) toString() noise() getFriendliness() friendliness 5 Within toString, a call toString() refers to the same method toString! To refer to the toString method of the superclass, prefix the call with “super.”.

Java bootcamp Spring Casting up and down (narrowing and widening) Assume Dog and Cat are subclasses of Animal Animal a; Dog d; Cat c; a= d; a= (Animal) d; d= (Dog) a; d= a; object d is cast up (widened) to Animal Animal fido name Animal(String) toString() noise() Dog Dog(String,int) toString() noise() getFriendliness() friendliness 5 a0 implicit explicit object a is cast down (narrowed) to Dog illegal

Java bootcamp Spring Casting up and down (narrowing and widening) Assume Dog and Cat are subclasses of Animal Animal a; Dog d; Cat c; a= d; … if (d instanceof Dog) { … } else { … } Animal Fido name Animal(String) toString() noise() Dog Dog(String,int) toString() noise() getFriendliness() friendliness 5 a0

Java bootcamp Spring Why cast? Assume Dog and Cat are subclasses of Animal Animal[ ] x= new Animal[100]; x[1]= new Dog(“Fido”, 10); x[2]= new Dog(“Pitty”, 0); x[3]= new Cat(“Tabby”, … ); Animal fido name Animal(String) toString() noise() Dog Dog(String,int) toString() noise() getFriendliness() friendliness 5 a0 Have an array of objects of class Animal, each of which is a Cat or a Dog. Assignment to x[k] of a Dog or Cat object is an up-cast!!

Java bootcamp Spring Apparent and real class-type of a variable? Assume Dog and Cat are subclasses of Animal Animal[ ] x= new Animal[100]; x[1]= new Dog(“Fido”, 10); Animal Fido name Animal(String) toString() noise() Dog Dog(String,int) toString() noise() getFriendliness() friendliness 5 a0 Apparent type of x[1] is Animal. Syntactic property. Apparently, looking at the declaration of x, x[1] contains an Animal. real type of x[1] is Dog. Semantic property. Really, x[1] is a Dog. Real type can change at runtime when x[1]= e; is executed.

Java bootcamp Spring Apparent class-type determines what components of object can be referenced Animal[ ] x= new Animal[100]; x[1]= new Dog(“Fido”, 10); Animal Fido name Animal(String) toString() noise() Dog Dog(String,int) toString() noise() getFriendliness() friendliness 5 a0 Apparent type of x[1] is Animal. Syntactically legal: x[1].name x[1].toString() x[1].noise() Other components are there but cannot be referenced.

Java bootcamp Spring Apparent class-type determines what components of object can be referenced Animal[ ] x= new Animal[100]; x[1]= new Dog(“Fido”, 10); Animal Fido name Animal(String) toString() noise() Dog Dog(String,int) toString() noise() getFriendliness() friendliness 5 a0 real type of x[1] is Dog. x[1].noise() CALLS THIS METHOD That’s what the bottom-up rule says.

Java bootcamp Spring x[5].noise(); x[7].noise(); Animal Fido name Animal(String) toString() noise() Dog Dog(String,int) toString() noise() getFriendliness() friendliness 5 a0 Animal Fido name Animal(String) toString() noise() Cat Cat(String,int) toString() noise() … 5 a1 aloofness x[5] x[7] a1 a0

Java bootcamp Spring Class Object the superest class of them all public class Object { /** = description of this Object */ public String toString() { … } /** Object ob and this object are the same */ public boolean equals(Object ob) { return this = ob; } … } Object has a few methods. For us, the most important ones are toString and equals. They are inherited by all classes. Usually, they are overridden. Object, in package java.lang, automa- tically is the super- class of all classes that do not extend another class.

Java bootcamp Spring Overriding function equals /** An instance is a point in the plane */ public class Object { private int x; private int y; /** = description of this Point */ public String toString() { return “(“ + x + “, “ + y + “)”; } /** Object ob and this object describe the same point */ public boolean equals(Object ob) { return ob != null && ob instanceof Point && x = ((Point) ob).x && y = ((Point)ob).y; } Object, in package java.lang, is the superclass of all classes that do not extend another class.

Java bootcamp Spring Overriding function equals public class Object { private int x; private int y; /** Object ob and this object describe the same point */ public boolean equals(Object ob) { return ob != null && ob instanceof Point && x = ((Point) ob).x && y = ((Point)ob).y; } Java says: equals should be an equivalence relation, i.e. Reflexive: b.equals(b) Symmetric: b.equals(c) = c.equals(b) Transitive: if b.equals(c) and c.equals(d), then bequals(d)

Java bootcamp Spring Four kinds of variable public class C { private int ins; public static int cla; public void p(int par) } if (…) { par= par+1; int loc; … } ins: instance variable or field. Belongs in each folder (object, instance) of class C. Created when folder is created. cla: class variable. Belongs in file drawer class C. Created at start of program. par: parameter. Created when frame for a call on p is created; destroyed when frame is erased. Scope: method body. loc: local variable. Created when frame for a call on p is created; destroyed when frame is erased. Scope: all the statements after its declaration in the block in which it is declared.

Java bootcamp Spring Array: an object that contains a bunch of variables of the same type. int[] b;// Declaration of variable b of type int[] (int array). b= new int[4]; // Create an assign to b an object that is an array of 4 int vars. // Vars are named b[0], b[1], b[2], b[3]. // Number of elements in the array is b.length. b[k]= b[j]+2; // Evaluate b[j]+2, store the value in b[k]. a a0 b

Java bootcamp Spring Array: an object that contains a bunch of variables of the same type. C[] b;// Declaration of variable b of class-type C[] (C array). b= new C[4]; // Create and assign to b an object that is an array of 4 C vars. b[3]= new C(); // Create and assign to b[3] a new folder of class C. a null null null null null a0 b null null null a1 null C a1