Some odds and ends about Classes and Objects. 6-2 Object-Oriented Programming Object Data (Fields) Methods That Operate on the Data.

Slides:



Advertisements
Similar presentations
Lesson 14 Classes, Continued CS1 Lesson More Classes1.
Advertisements

The Line Class Suppose you are involved in the development of a large mathematical application, and this application needs an object to represent a Line.
EC-241 Object-Oriented Programming
INTERFACES IN JAVA 1.Java Does not support Multiple Inheritance directly. Multiple inheritance can be achieved in java by the use of interfaces. 2.We need.
CS0007: Introduction to Computer Programming Introduction to Classes and Objects.
Chapter 6: A First Look at classes. Object-Oriented Programming  Object-oriented programming is centered on creating objects rather than procedures.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java: Early Objects Third Edition by Tony Gaddis Chapter.
COMP 110 Introduction to Programming Mr. Joshua Stough October 8, 2007.
COMP 14 Introduction to Programming Mr. Joshua Stough February 28, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218.
CS 106 Introduction to Computer Science I 03 / 19 / 2008 Instructor: Michael Eckmann.
1 Fall 2007ACS-1903 Chapter 6: Classes Classes and Objects Instance Fields and Methods Constructors Overloading of Methods and Constructors Scope of Instance.
Chapter 8: User-Defined Classes and ADTs J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
CS 106 Introduction to Computer Science I 03 / 17 / 2008 Instructor: Michael Eckmann.
Chapter 6: A First Look at Classes
UML Basics & Access Modifier
A First Look At Classes Chapter 6. Procedural Programming In procedural- programming all the program functionality is written in a few modules of code.
Starting Out With Java 5 (Control Structures to Objects) Chapter 6 By Tony Gaddis Copyright © 2005 Pearson Addison-Wesley. All rights reserved.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java: From Control Structures through Objects Third Edition.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java: Early Objects Third Edition by Tony Gaddis Chapter.
Starting Out with Java: From Control Structures through Objects.
CS 106 Introduction to Computer Science I 03 / 19 / 2007 Instructor: Michael Eckmann.
5-Aug-2002cse Arrays © 2002 University of Washington1 Arrays CSE 142, Summer 2002 Computer Programming 1
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 13: Introduction to Classes.
Questions? Suggestions?. References References Revisited What happens when we say: int x; double y; char c; ???
Java™ How to Program, 10/e © Copyright by Pearson Education, Inc. All Rights Reserved.
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.
Java - Classes JPatterson. What is a class? public class _Alpha { public static void main(String [] args) { } You have been using classes all year – you.
CS 106 Introduction to Computer Science I 10 / 29 / 2007 Instructor: Michael Eckmann.
CSci 162 Lecture 10 Martin van Bommel. Procedures vs Objects Procedural Programming –Centered on the procedures or actions that take place in a program.
Page 4.1 – Autumn 2010Steffen Vissing Andersen SDJ I1, Autumn 2010 Agenda – Session 4 – 7. September 2009 Java fundamentals, checkpoint from chapter 2.
© 2010 Pearson Addison-Wesley. All rights reserved. 6-1 Chapter Topics Chapter 6 discusses the following main topics: –Classes and Objects –Instance Fields.
Static?. Static Not dynamic class Widget { static int s; int d; // dynamic // or instance // variable }
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 3 A First Look at Classes and Objects.
Topic 8Classes, Objects and Methods 1 Topic 8 l Class and Method Definitions l Information Hiding and Encapsulation l Objects and Reference Classes, Objects,
1 Static Variable and Method Lecture 9 by Dr. Norazah Yusof.
Starting Out With Java 5 Control Structures to Objects By Tony Gaddis Copyright © 2005, 2005 Pearson Addison-Wesley. All rights reserved. Chapter 6 Slide.
CS 106 Introduction to Computer Science I 03 / 22 / 2010 Instructor: Michael Eckmann.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 13: Introduction to Classes.
Class Everything in Java is in a class. The class has a constructor that creates the object. If you do not supply a constructor Java will create a default.
CSCI 51 Introduction to Programming Dr. Joshua Stough February 24, 2009.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley The Unified Modeling Language
CSCI 51 Introduction to Programming Dr. Joshua Stough February 26, 2009.
CS16: UML’s Unified Modeling Language. UML Class Diagram A UML class diagram is a graphical tool that can aid in the design of a class. The diagram has.
Introduction to Constructors Lecture # 4. Copyright © 2011 Pearson Education, Inc. 3-2 Arguments Passed By Value In Java, all arguments to a method are.
Chapter 6 A First Look at Classes. 2 Contents 1. Classes and objects 2. Instance Fields and Methods 3. Constructors 4. Overloading Methods and Constructors.
Chapter 8 Arrays and the ArrayList Class Arrays of Objects.
Copyright © 2011 Pearson Education, Inc. Starting Out with Java: Early Objects Fourth Edition by Tony Gaddis Chapter 3: A First Look at Classes and Objects.
Chapter 6 A First Look at Classes. 2 Contents 1. Classes and objects 2. Instance Fields and Methods 3. Constructors 4. Overloading Methods and Constructors.
A Second Look at Classes and Objects - 1 Static Class Members
Pointer to an Object Can define a pointer to an object:
Objects as a programming concept
Array Review CS 139 – November 28, 2007.
Chapter 3 Classes & Objects
A first Look at Classes.
Lecture 6 D&D Chapter 7 & 8 Intro to Classes and Objects Date.
Introduction to Classes
CS 302 Week 11 Jim Williams, PhD.
CS 302 Week 10 Jim Williams.
Introduction to Classes
An Introduction to Java – Part II
Array Review CS 139 – November 28, 2007.
Chapter 8: User-Defined Classes and ADTs
Today’s topics UML Diagramming review of terms
Class Everything if Java is in a class. The class has a constructor that creates the object. public class ClassName private Field data (instance variables)
Object Oriented Programming Review
Array Review CS 139 – November 28, 2007.
Presentation transcript:

Some odds and ends about Classes and Objects

6-2 Object-Oriented Programming Object Data (Fields) Methods That Operate on the Data

private static int count = 0; public String name; public Name(String name) { this.name = name; count++; } public static int getCount() { return count; } public String getName() { return name; } private static int count = 0; public String name; public Name(String name) { this.name = name; count++; } public static int getCount() { return count; } public String getName() { return name; } 1Class Name sally.name:“Sally” sally.getName() sally.name:“Sally” sally.getName() molly.name: “Molly” molly.getName() molly.name: “Molly” molly.getName() polly.name: “Polly” polly.getName() polly.name: “Polly” polly.getName() 3 3 Name.count sally molly polly new Initial state. Declared three Name objects and instantiated them.

private static int count = 0; public String name; public Name(String name) { this.name = name; count++; } public static int getCount() { return count; } public String getName() { return name; } private static int count = 0; public String name; public Name(String name) { this.name = name; count++; } public static int getCount() { return count; } public String getName() { return name; } 1Class Name sally.name:“Sally” sally.getName() sally.name:“Sally” sally.getName() molly.name: “Molly” molly.getName() molly.name: “Molly” molly.getName() name: “Polly” getName() name: “Polly” getName() 3 3 Name.count sally molly polly new Name gerry; gerry = polly; gerry Declared a Name object, but then assigned the value in one object to the new object.

private static int count = 0; public String name; public Name(String name) { this.name = name; count++; } public static int getCount() { return count; } public String getName() { return name; } private static int count = 0; public String name; public Name(String name) { this.name = name; count++; } public static int getCount() { return count; } public String getName() { return name; } 1Class Name sally.name:“Sally” sally.getName() sally.name:“Sally” sally.getName() molly.name: “Molly” molly.getName() molly.name: “Molly” molly.getName() name: “Gerry” getName() name: “Gerry” getName() 3 3 Name.count sally molly polly new ? gerry.name = “Gerry”; gerry Changed the name of the recently added name.

Objects, pass by reference or value? In Java, ALL arguments are passed by value. The value of the argument is copied into the parameter. Return statements in Java always return a value. What does this really mean though for objects? Let’s look at Arrays.sort and why we deep copy for answers.

Arrays.sort() anArray public static void sort (int [] a) { } public static void sort (int [] a) { }

Why Deep Copy? DMV Exam object solution DMVExam test; test = new DMVExam(); char [] answers; answers = test.getSolution(); answers[0] = ‘X’; DMVExam test; test = new DMVExam(); char [] answers; answers = test.getSolution(); answers[0] = ‘X’; public char[] getSolution() { return solution; } public char[] getSolution() { return solution; } answers

Why Deep Copy? DMV Exam object solution DMVExam test; test = new DMVExam(); char [] answers; answers = test.getSolution(); answers[0] = ‘X’; DMVExam test; test = new DMVExam(); char [] answers; answers = test.getSolution(); answers[0] = ‘X’; public char[] getSolution() { char[] result; result = new char[solution.length]; for (int ii = 0; ii < solution.length; ii++) result[ii] = solution.length; return result; } public char[] getSolution() { char[] result; result = new char[solution.length]; for (int ii = 0; ii < solution.length; ii++) result[ii] = solution.length; return result; } answers result

6-10 UML Diagram – Gaddis 304 Unified Modeling Language (UML) provides a set of standard diagrams for graphically depicting object-oriented systems. Class name goes here Fields are listed here Methods are listed here

6-11 UML Diagram for Rectangle class Rectangle -length: double -width: double +setLength(): double +setWidth(): double getLength() getWidth() getArea()