Week 3 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.

Slides:



Advertisements
Similar presentations
Copyright 2008 by Pearson Education Building Java Programs Chapter 8 Lecture 8-3: Encapsulation, toString reading: self-checks: #13-18,
Advertisements

Chapter 2: Using Objects Part 1. To learn about variables To understand the concepts of classes and objects To be able to call methods To learn about.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Chapter Three - Implementing Classes.
Chapter 2 – An Introduction to Objects and Classes Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.
1 Classes Overview l Classes as Types l Declaring Instance Variables l Implementing Methods l Constructors l Accessor and Mutator Methods.
Chapter 2 Using Objects. These slides for CSE 110 Sections are based in part on the textbook-authors ’ slides, which are copyright 2003 by Cay Horstmann.
1 Basic Object Oriented Concepts Overview l What is Object-Orientation about? l What is an Object? l What is a Class? l Constructing Objects from Classes.
Chapter 2 storing numbers and creating objects Pages in Horstmann.
Datalogi A 2: 15/9. Java Slides based on Horstmann chapter 2&3 Objects and classes Import, methods, references Implementing a class.
Chapter 3 Implementing Classes. Chapter Goals To become familiar with the process of implementing classes To be able to implement simple methods To understand.
Chapter Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
Chapter 2  Using Objects 1 Chapter 2 Using Objects.
Copyright 2010 by Pearson Education Building Java Programs Chapter 8 Lecture 8-2: Object Behavior (Methods) and Constructors reading:
UML Basics & Access Modifier
Java: Chapter 1 Computer Systems Computer Programming II.
CompSci 42.1Intro to Java Anatomy of a Class & Terminology Running and Modifying a Program.
Recitation 2 Main Method, API & Packages, Java Basics.
Copyright 2008 by Pearson Education Building Java Programs Chapter 8 Lecture 8-2: Constructors and Encapsulation reading: self-checks: #10-17.
Java Classes Using Java Classes Introduction to UML.
Week 4 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Week 12 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
The string data type String. String (in general) A string is a sequence of characters enclosed between the double quotes "..." Example: Each character.
CHAPTER 2 Using Objects. Basic Programming Terminology  Computer program process values.  Numbers (digits)  Words (Strings)  These values are different.
An Introduction to Java Chapter 11 Object-Oriented Application Development: Part I.
Week 5 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Copyright 2010 by Pearson Education Building Java Programs Chapter 8 Lecture 8-2: Object Behavior (Methods) and Constructors, Encapsulation, this reading:
Chapter 2 Using Objects. Chapter Goals To learn about variables To understand the concepts of classes and objects To be able to call methods To be able.
Week 2 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Week 4 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
10-Nov-15 Java Object Oriented Programming What is it?
Jens Dalsgaard Nielsen Jan Dimon Bendtsen Dept. of Electronic Systems Basic Programming INS-basis GF, PDP and HST.
1 Principles of Computer Science I Prof. Nadeem Abdul Hamid CSC 120 – Fall 2005 Lecture Unit 2 - Using Objects.
Anatomy.1 Anatomy of a Class & Terminology. Anatomy.2 The Plan Go over MoveTest.java from Big Java Basic coding conventions Review with GreeterTest.java.
Week 14 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
CS305j Introduction to Computing Classes 1 Topic 23 Classes – Part I "A 'class' is where we teach an 'object' to behave." -Rich Pattis Based on slides.
2 Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
Week 7 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
IT108 Objects and Classes Part I George Mason University Revised 4/3/2012.
CMSC 202 Advanced Section Classes and Objects: Object Creation and Constructors.
CMSC 150 METHODS AND CLASSES CS 150: Wed 25 Jan 2012.
Chapter 2 Using Objects. Chapter Goals To learn about variables To understand the concepts of classes and objects To be able to call methods To be able.
Powerpoint slides from A+ Computer Science Modified by Mr. Smith for his course.
Week 13 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
CompSci 42.1Intro to Java Anatomy of a Class & Terminology Running and Modifying a Program.
Internet Computing Module II. Syllabus Creating & Using classes in Java – Methods and Classes – Inheritance – Super Class – Method Overriding – Packages.
1 Sections 5.1 – 5.2 Digital Image Processing Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
Powerpoint slides from A+ Computer Science Modified by Mr. Smith for his course.
© 2004 Pearson Addison-Wesley. All rights reserved3-1 Objects Declaration: String title;  title (object variable) of type String( Class )  title is just.
Class Definitions: The Fundamentals Chapter 6 3/30/15 & 4/2/15 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education.
Copyright 2010 by Pearson Education Building Java Programs Chapter 8 Lecture 8-3: Constructors; Encapsulation reading: self-checks: #13-18,
Copyright 2010 by Pearson Education Building Java Programs Chapter 8 Lecture 8-2: Object Behavior (Methods) and Constructors, Encapsulation, this reading:
A Simple Object Oriented Program public class Simple { public static void main (String [] args) { System.out.println(“howdy”); } System.out is an object.
Lecture 3 John Woodward.
Using Objects.
Chapter Three - Implementing Classes
Chapter 2 Using Objects.
Building Java Programs
Implementing Non-Static Features
Introduction to Computer Science and Object-Oriented Programming
Building Java Programs
Building Java Programs
Building Java Programs
Kinds of methods accessor: A method that lets clients examine object state. Examples: distance, distanceFromOrigin often has a non-void return type mutator: A.
Building Java Programs
Dr. R Z Khan Handout-3 Classes
Building Java Programs
Lecture 8-2: Object Behavior (Methods) and Constructors
Building Java Programs
Introduction to Computer Science and Object-Oriented Programming
Presentation transcript:

Week 3 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham

Week 3 Topics Constructing Objects Accessor and Mutator Methods The API Documentation Implementing a Test Program Object References

3.1.1 Constructing Objects We will learn how to construct objects that allow us to go beyond our earlier String object and System.out object creation examples Let’s look at the Java class library Rectangle class A Rectangle object isn’t a rectangular shape, it is an object that contains a set of numbers that describes a rectangle

3.1.1 Constructing Objects Cont. Each rectangle is described by the x and y coordinates of its top-left corner and its width and height Rectangle box = new Rectangle(5,10,20,30) x = 5 y = 10 width = 20 height = 30

3.1.1 Constructing Objects Cont. The new operator makes a Rectangle object It uses parameters in the call to one of the constructors to initialize the data of the object It returns the object’s address in memory In our example, the address (object reference) is stored in the object variable identified by box, of type Rectangle

3.1.2 Accessor and Mutator Methods A method of a class that accesses an object and returns some information about it without changing the object is called an accessor method A method whose purpose is to modify the state of an object (its data) is called a mutator method

3.1.2 Accessor and Mutator Methods Cont. The length method of the String class is an accessor method. It does not modify the state of the string object, it just counts the number of stored characters The Rectangle class has a number of accessor methods, getX, getY, getWidth and getHeight double width = box.getWidth();

3.1.2 Accessor and Mutator Methods Cont. The translate method of the Rectangle class is a mutator method Translate moves a rectangle by a certain distance in the x and y directions box.translate(15, 25); The above method call moves the rectangle by 15 units in the x direction and 25 units in the y direction

3.1.2 Accessor and Mutator Methods Cont. Illustration: Effect of translate method Using the translate method to move a rectangle

3.1.3 The API Documentation The classes and methods of the Java library are listed in the API documentation (application programming interface) The API documentation can be found on the web at The API for each class starts out with a purpose section, then summary tables for the constructors and methods Click on a method link to get detailed info

3.1.4 Implementing a Test Program 1.Provide a new class 2.Supply a main method 3.Inside the main method, construct one or more objects 4.Apply methods to the objects 5.Display the results of the method calls

import java.awt.Rectangle; public class MoveTester { public static void main(String[] args) { Rectangle box = new Rectangle(5, 10, 20, 30); // Move the rectangle box.translate(15, 25); // Print information about the moved rectangle System.out.println("After moving, the top-left corner is:"); System.out.println(box.getX()); System.out.println(box.getY()); } Output: text message then 20 followed by 35

/** * Test the translate method. */ protected void testTranslate() { Rectangle box = new Rectangle(5, 10, 20, 30); box.translate(15, 25); assertEquals(20.0, box.getX(),.0001); assertEquals(35.0, box.getY(),.0001); } Example: JUnit test method. Upon doing Run Tests a green checkmark indicates successful test, a red X indicates that test failed.

3.1.5 Object References In Java a variable whose type is a class does not hold an object, it holds the memory location of an object. Object reference is the technical term to denote the memory location of an object. Rectangle box = new Rectangle(5,10,20,30) The variable box refers to the object that the new operator constructed.

3.1.5 Object References Cont. The new operator returned a reference to the new object, and that reference is stored in the box variable. Again, the box variable does not contain the object, it refers to the object Remember that number variables actually store numbers, not a reference to the number

3.1.5 Object References Cont. A number variable and an object variable behave differently when you make a copy of the variable: // number variable 1: int luckyNbr = 13; // number variable 2 has value 13: int luckyNbr2 = luckyNbr; // number variable 2 now has value 12, but // number variable 1 still has value 13: luckyNbr2 = 12;

3.1.5 Object References Cont. Note object variable behavior: // object variable 1: Rectangle box = new Rectangle(5, 10, 20, 30); // object variable 2 will have same value // as object variable 1, a MEMORY ADDRESS: Rectangle box2 = box; // calling a mutator method on object variable // 2 will change the object referenced by both // variables: box2.translate(15, 25);

3.1.5 Object References Cont. Thus, both these statements will produce identical output since the object variables reference the same object: System.out.println(box); System.out.println(box2); Output: java.awt.Rectangle[x=20,y=35,width=20, height=30]

Reference: Big Java 4th Edition by Cay Horstmann Constructing Objects (section 2.6 in Big Java) Accessor and Mutator Methods (section 2.7 in Big Java) The API Documentation (section 2.8 in Big Java) Implementing a Test Program (section 2.9 in Big Java) Object References (section 2.10 in Big Java)