Copywrite 2003 Walter Savitch These slides are for the exclusive use of students in CSE 11 at UCSD, Winter quarter 2003. They may not be copied or used.

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

Intro to CS – Honors I Class Types and Object References GEORGIOS PORTOKALIDIS
Lecture 9: More on objects, classes, strings discuss hw3 assign hw4 default values for variables scope of variables and shadowing null reference and NullPointerException.
Object Reference. Variables: Class Type vs. Primitive Type What does a variable hold? –primitive type value of the variable –class type memory address.
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.
1 Chapter 4 l Class and Method Definitions l Information Hiding and Encapsulation l Objects and Reference l Parameter passing Classes, Objects, and Methods.
Chapter 41 Defining Classes and Methods Chapter 4.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Classes, Objects, and Methods
Slides prepared by Rose Williams, Binghamton University Chapter 13 Interfaces and Inner Classes.
CSE 11 February 6, © 2003 Walter Savitch These slides are for the exclusive use of students in CSE 11 at UCSD, Winter quarter They may not.
Copywrite 2003 Walter Savitch These slides are for the exclusive use of students in CSE 11 at UCSD, Winter quarter They may not be copied or used.
Copywrite 2003 Walter Savitch These slides are for the exclusive use of students in CSE 11 at UCSD, Winter quarter They may not be copied or used.
Slides prepared by Rose Williams, Binghamton University Chapter 13 Interfaces and Inner Classes.
© 2003 Walter Savitch These slides are for the exclusive use of students in CSE 11 at UCSD, Winter quarter They may not be copied or used for any.
Chapter 4Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 4 l Class and Method Definitions l Information Hiding and Encapsulation.
Information Hiding and Encapsulation
CSE 11 HW 2, 3 and 4 Posted in public and on web Deadlines HW 2 turnin: Wed Jan 22 interview: Saturday January 25.class files in public directory for HW.
Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
© 2003 Walter Savitch These slides are for the exclusive use of students in CSE 11 at UCSD, Winter quarter They may not be copied or used for any.
Copywrite 2003 Walter Savitch These slides are for the exclusive use of students in CSE 11 at UCSD, Winter quarter They may not be copied or used.
Understanding class definitions Looking inside classes.
CSE 11 February 11, © 2003 Walter Savitch These slides are for the exclusive use of students in CSE 11 at UCSD, Winter quarter They may not.
Copywrite 2003 Walter Savitch These slides are for the exclusive use of students in CSE 11 at UCSD, Winter quarter They may not be copied or used.
Comp 248 Introduction to Programming Chapter 4 - Defining Classes Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
Classes, Objects, and Methods
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.
11 Chapter 5 METHODS CONT’D. 22 MORE ON PASSING ARGUMENTS TO A METHOD Passing an Object Reference as an Argument to a Method Objects are passed by reference.
Lecture # 8 Constructors Overloading. Topics We will discuss the following main topics: – Static Class Members – Overloaded Methods – Overloaded Constructors.
Classes CS 21a: Introduction to Computing I First Semester,
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Chapter 4Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 4 l Class and Method Definitions l Information Hiding and Encapsulation.
Comp 249 Programming Methodology Chapter 13 Interfaces & Inner Classes Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
Slides prepared by Rose Williams, Binghamton University Chapter 5 Defining Classes II.
Vladimir Misic: Java1 Basic Java Syntax The java language will be described by working through its features: –Variable types and expressions.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Programs and Classes A program is made up from classes Classes may be grouped into packages A class has two parts static parts exist independently Non-static.
Types in programming languages1 What are types, and why do we need them?
Comp 248 Introduction to Programming Chapter 4 & 5 Defining Classes Part B Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Chapter 3 Introduction to Classes and Objects Definitions Examples.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 Pearson Education, Inc., Upper.
Interfaces and Inner Classes
CMSC 202 Advanced Section Classes and Objects: Object Creation and Constructors.
1 Week 8 l Methods l Parameter passing Methods. 2 Using Methods l Methods are actions that an object can perform. l To use a method you invoke or call.
JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River,
JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River,
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 Pearson Education, Inc., Upper.
Chapter 4Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapters 4 and 5: Excerpts l Class and Method Definitions l Information.
Defining Classes I Part B. Information hiding & encapsulation separate how to use the class from the implementation details separate how to use the class.
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.
Winter 2006CISC121 - Prof. McLeod1 Last Time Reviewed class structure: –attributes –methods –(inner classes) Looked at the effects of the modifiers: –public.
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.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
Objects and Classes. F OO Programming Concepts F Creating Objects and Object Reference Variables –Differences between primitive data type and object type.
Defining Classes and Methods
More Sophisticated Behavior
Classes, Objects, and Methods
CMSC 202 Java Primer 2.
Defining Classes and Methods
Programs and Classes A program is made up from classes
Fundamental OOP Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
Defining Classes and Methods
Defining Classes and Methods
In this class, we will cover:
Classes, Objects and Methods
CMPE212 – Reminders Assignment 2 due next Friday.
Presentation transcript:

Copywrite 2003 Walter Savitch These slides are for the exclusive use of students in CSE 11 at UCSD, Winter quarter They may not be copied or used for any other purpose without the written permission of Walter Savitch.

CSE 11 Section B ROOM CHANGE Starting Next Time: Solis 111

CSE Majors Freshman Reception 4-5 pm Faculty Club Today

CSE 11 Warning JOptionPane is covered in this class. But you read about it on your own.

Home Work 2 Posted in public and on web Deadlines turnin: Wed Jan 22 (Do not turn it in before January 19th) interview: Saturday January 25.class files in public directory Do not need to turnin the HW if you take an interview before the turnin deadline

turnin –c cs11w FileName.java Instructions in public. You must be logged onto sunpal as yourself. The file you are turning in must be in the same directory as you. Only has the last file you turned in. If you do an interview before the turnin deadline, then you do not turn in the assignment at all.

Inner Class Class defined within another class. Can be private and then local to the outer class. For now, we use it only to make a hw assignment into a single class for the turnin program. (Can make inner classes either public or private for this.)

Encapsulation ADT (Abstract Data Type) API (Application Programmer Interface) You should be able to change the implementation of a class and not have to change any application program that uses the class.

javadoc Automatically extracts HTML documentation from classes. Only takes public stuff Only takes comments of the form /** This comment must be just before something public. */

Some Style Guidelines Identifiers: Class, aVariable, aMethod, A_CONSTANT Comments: No obvious comments Comment at head of file with name, assignment number, etc. Comment before each class definition describing comment. Comment before each non-obvious method telling what the parameters are and what the method does.

Class Type Variables Contain References A reference is a memory location. A reference is a pointer, but you never say “pointer” in Java. Java has no pointer types, but All class objects are handed by reference. (I.e., its all pointers, but automatic pointers.)

The Species Class public class Species { private String name; private int population; private double growthRate;

public class Species { private String name; private int population; private double growthRate; public void setPopulation(int newPopulation) {//This method not in book, but … if (newPopulation >= 0) population = newPopulation; else { System.out.println("ERROR."); Sytem.exit(0); }

Species s1 = new Species(); s1.setPopulation(13); //s1 now has a population of 13 Species s2; s2 = s1; s2.setPopulation(100); //s1 now has a population of 100

Class Parameter Are call by value, but The value is a reference. Cannot change the reference (pointer) value, but you can change what it references (points to).

Class Parameters A method can change the values of instance variables in a class argument.

== checks for the actual content of the variables (or whatever) and returns true if the contents are equal. Primitive types: No surprises. Class types: returns true if the the two references are the same. Returns false if two classes stored in different locations in memory, even if data all matches

Species klingonSpecies = new Species(); Species earthSpecies = new Species (); klingonSpecies.set("Klingon ox", 10, 15); earthSpecies.set("Klingon ox", 10, 15); if (klingonSpecies == earthSpecies) System.out.println("They are EQUAL."); else System.out.println("They are NOT equal.");

Define an equals method for classes public class Species { private String name; private int population; private double growthRate;

Define an equals method for classes public boolean equals(Species otherObject) { return ((this.name.equalsIgnoreCase(otherObject.name)) && (this.population == otherObject.population) && (this.growthRate == otherObject.growthRate)); }

Quiz Time

public class Species { private String name; private int population; private double growthRate;

public int femalePopulation() { return (population/2 + population%2); }

Static Methods and Variables. Applies to both methods and instance variables. Indicated with modifier static. Means there is only one for all objects. Static methods can be, normally are, invoked with class name, e.g SavitchIn.readLine()

Staic Variables Kind of instance variables. One variables used for all objects. All objects read and write the one variable.

public class Sample { private static int count; public static final double PI = ;

Static Methods Can be invoked using class name instead of a calling object. They have no this anything of the form this.variable or this.method is not allowed Even if the “this.” is only implicit

Static Methods All the methods in SavithcIn Methods in the class Math Methods in Wrapper Classes