The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 13, 2005.

Slides:



Advertisements
Similar presentations
Classes and Objects. What is Design? The parts of the software including – what information each part holds – what things each part can do – how the various.
Advertisements

Objects, Variables & Methods Java encapsulates data and action modules that access the data in one container, called an object. Object members that.
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
OBJECT-ORIENTED PROGRAMMING. What is an “object”? Abstract entity that contains data and actions Attributes (characteristics) and methods (functions)
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 8, 2005.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 20, 2005.
Unit 08 & 091 Nested Classes Introduction Inner Classes Local Classes Anonymous Classes Exercises.
Road Map Introduction to object oriented programming. Classes
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 5, 2005.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 6, 2005.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 14, 2005.
COMP 10 Introduction to Programming Mr. Joshua Stough October 29, 2007.
COMP 14 Introduction to Programming Miguel A. Otaduy May 25, 2004.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 28, 2005.
COMP 110 Introduction to Programming Mr. Joshua Stough October 8, 2007.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP Introduction to Programming Adrian Ilie July 13, 2005.
COMP 14 Introduction to Programming Mr. Joshua Stough February 28, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 29, 2005.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 8, 2005.
What is a class? a class definition is a blueprint to build objects its like you use the blueprint for a house to build many houses in the same way you.
COMP 110 Introduction to Programming Mr. Joshua Stough October 24, 2007.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 18, 2005.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 1, 2005.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
COMP 14 Introduction to Programming Miguel A. Otaduy May 28, 2004.
COMP 14 Introduction to Programming Miguel A. Otaduy June 3, 2004.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 30, 2005.
Saravanan.G.
6-1 Object-Oriented Design Today we focuses on: –the this reference (Chapter 7) –the static modifier (Chapter 7) –method overloading (Chapter 7)
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
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.
Chapter 8. About the Midterm Exam.. Exam on March 12 Monday (Tentatively) Review on March 7 Wednesday Cover from Chapter 6 Grades will be out before spring.
© A+ Computer Science - public Triangle() { setSides(0,0,0); } Constructors are similar to methods. Constructors set the properties.
Chapter 7 Object-Oriented Design Concepts
Object Based Programming Chapter 8. 2 In This Chapter We will learn about classes Garbage Collection Data Abstraction and encapsulation.
CSC 142 Computer Science II Zhen Jiang West Chester University
Introduction to Java Chapter 7 - Classes & Object-oriented Programming1 Chapter 7 Classes and Object-Oriented Programming.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
User Defined Methods Methods are used to divide complicated programs into manageable pieces. There are predefined methods (methods that are already provided.
Building java programs, chapter 3 Parameters, Methods 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,
Powerpoint slides from A+ Computer Science Modified by Mr. Smith for his course.
Attribute - CIS 1068 Program Design and Abstraction Zhen Jiang CIS Dept. Temple University SERC 347, Main Campus 12/24/2016.
1 Introduction to Object Oriented Programming Chapter 10.
Powerpoint slides from A+ Computer Science Modified by Mr. Smith for his course.
Object-Oriented Design Chapter 7 1. Objectives You will be able to Use the this reference in a Java program. Use the static modifier for member variables.
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.
Today Encapsulation. Build a fully encapsulated Halloween class, going from Halloween1 to Halloween6 (eventually!): –The final version will have overloaded.
C# Programming: From Problem Analysis to Program Design1 Creating Your Own Classes C# Programming: From Problem Analysis to Program Design 4th Edition.
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
COMP 14 Introduction to Programming Mr. Joshua Stough March 23, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218.
Chapter 5: Enhancing Classes
Creating Your Own Classes
Chapter 7 User-Defined Methods.
OOP Powerpoint slides from A+ Computer Science
Examples of Classes & Objects
CS 302 Week 11 Jim Williams, PhD.
More Object Oriented Programming
CSC240 Computer Science III
Object Based Programming
Java Programming Language
Classes & Objects: Examples
Classes, Encapsulation, Methods and Constructors (Continued)
Interfaces.
Chap 1 Chap 2 Chap 3 Chap 5 Surprise Me
© A+ Computer Science - OOP Pieces © A+ Computer Science -
Object Oriented Programming Review
Object-Oriented Programming
Presentation transcript:

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 13, 2005

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 2 Today Defining classes, using objects public, private, static Assignment operator and objects jGRASP projects, multiple files, Java archives

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 3 Multiple Classes So far, we have added methods to the main class Classes extend the concept of data types: data + operations Define new classes and instantiate objects

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 4 Multiple Classes Concept: Basketball team ♦ players (objects of class Player) ♦ coach (object of class Coach) ♦ games ♦ … Define each class in one file, and instantiate objects in other classes or in the main method

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 5 Basketball.java Create main class: Basketball Define Player class ♦ player name ♦ year ♦ + operations

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 6 Multiple files Good style: 1 class per file Store all files in 1 directory Create jGRASP project (.gpj)

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 7 BasketballProject.gpj Example of project creation

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 8 Multiple Objects A class has 1 single definition We can instantiate multiple objects of 1 class Each object keeps its own copy of the data members Objects are accessed using reference variables To perform operations on objects: referenceVariable.method (parameter1, parameter2, …)

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 9 Basketball.java Instantiate multiple Player objects Call methods of the Player objects from Basketball.main() Demonstrate flow through different methods with the debugger

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 10 Visibility Modifiers private and public Public ♦ For members that should be accessible outside the class (i.e. from other classes or the main method) ♦ Usually employed with methods Private ♦ For members that should not be accessed outside the class ♦ Usually employed with data members ♦ Also for methods that are only used internally Typically, data members are declared as private, so we need methods to set and get data values

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 11 Player.java Make data members private Make methods public Demonstrate accessibility from Basketball.main()

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 12 Assignment Operator Assignment expressions that involve reference variables do not copy object data!! In order to copy object data, we need additional methods Player player1 = new Player(“Felton”); Player player2 = new Player(); player2 = player1; //player2 points to the same obj. public void Player.copy(Player otherPlayer)

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 13 Player.java Write Player.copy()

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 14 Visibility Modifiers static static data members ♦ Data related to a class that does not need to be defined for each object public class Player { static final int FRESHMAN = 1; static final int SOPHOMORE = 2; private int year; … }

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 15 Visibility Modifiers static static methods ♦ Operations that do not use object-dependent data ♦ They can only use static data members!! (besides local variables) public static int factorial(int x) { … } static BufferedReader keyboard = … public static void main(…) { String str=keyboard.readLine(); … }

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 16 Visibility Modifiers static To access static data members and methods from outside the class: To access non-static data members and methods from outside the class: In general, from inside the class, no need to write class name or reference variable. double res = Math.pow(2.0, 3.0); //ClassName.method(); player1.printInfo(); //referenceVariable.method();

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 17 Player.java Add static data members and methods

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 18 The toString Method Special method in Java classes Produces a String object based on the current object, suitable for printing Mapped to the ' + ' operator (concatenation) Also called when the object is a parameter in a print() or println() method There is a default toString method, but it's better if we write our own

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 19 Player.java Write Player.toString()

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 20 To do Homework 5 (due Wednesday night) ♦ Don’t wait till Tuesday!! ♦ Homework 6 is going to be rather long Read ch. 9