Class Definitions and Writing Methods

Slides:



Advertisements
Similar presentations
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Four Defining Your Own Classes.
Advertisements

CS0007: Introduction to Computer Programming Introduction to Classes and Objects.
***** SWTJC STEM ***** Chapter 4-1 cg 42 Object Oriented Program Terms Up until now we have focused on application programs written in procedural oriented.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Chapter Three - Implementing Classes.
H OW O BJECTS B EHAVE. O VERVIEW Methods use object state Arguments and return types in methods Java passes by value Getters and Setters Encapsulation.
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
1 Classes Overview l Classes as Types l Declaring Instance Variables l Implementing Methods l Constructors l Accessor and Mutator Methods.
CS 106 Introduction to Computer Science I 03 / 23 / 2007 Instructor: Michael Eckmann.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
Chapter Four Defining Your Own Classes continued.
Copyright 2010 by Pearson Education Building Java Programs Chapter 8 Lecture 8-2: Object Behavior (Methods) and Constructors reading:
Writing Classes (Chapter 4)
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals; Apply data abstraction.
Writing Classes You have already used classes –String, Random, Scanner, Math, Graphics, etc –To use a class: import the class or the package containing.
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.
Object-Oriented Programming (OOP). Implementing an OOD in Java Each class is stored in a separate file. All files must be stored in the same package.
Chapter 7 Objects and Classes 1 Fall 2012 CS2302: Programming Principles.
A First Look at Java Chapter 2 1/29 & 2/2 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010.
Chapter 8: User-Defined Classes and ADTs
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
CSC1401 Classes - 2. Learning Goals Computing concepts Adding a method To show the pictures in the slide show Creating accessors and modifiers That protect.
RECITATION 4. Classes public class Student { } Data Members public class Student { private String name; public String id; }
Java - Classes JPatterson. What is a class? public class _Alpha { public static void main(String [] args) { } You have been using classes all year – you.
Identifiers Identifiers in Java are composed of a series of letters and digits where the first character must be a letter. –Identifiers should help to.
More about Java Chapter 2 9/8 & 9/9 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010.
CS0007: Introduction to Computer Programming Classes: Documentation, Method Overloading, Scope, Packages, and “Finding the Classes”
Introduction Chapter 1 8/31 & 9/1 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010.
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
CreatingClasses-SlideShow-part31 Creating Classes part 3 Barb Ericson Georgia Institute of Technology Dec 2009.
1 Object-Oriented Software Engineering CS Java OO Fundamentals Contents Classes and Objects Making new objects Method declarations Encapsulating.
1 Class 1 Lecture Topic Concepts, Definitions and Examples.
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.
Java: Variables and Methods By Joshua Li Created for the allAboutJavaClasses wikispace.
Chapter 2 Wrap Up 2/18/16 & 2/22/16. Topics Review for Exam I DecimalFormat More Style Conventions Debugging using eclipse The Java API.
Creating Java Applications (Software Development Life Cycle) 1. specify the problem requirements - clarify 2. analyze the problem - Input? Processes? Output.
Class Definitions and Writing Methods Chapter 3 3/31/16 & 4/4/16.
Class Definitions and Writing Methods Chapter 3 10/12/15 & 10/13/15 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education.
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.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Andrew(amwallis) Classes!
CSE 8A Lecture 17 Reading for next class: None (interm exam 4)
Objects as a programming concept
Multiple if-else boolean Data
Lecture 4 D&D Chapter 5 Methods including scope and overloading Date.
CompSci 230 Software Construction
Classes and Objects 2nd Lecture
HKCT Java OOP Unit 02 Object Oriented Programming in Java Unit 02 Methods, Classes, and Objects 1.
User-Defined Classes and ADTs
Repetition Chapter 6 12/06/16 & 12/07/16 1 1
Chapter Three - Implementing Classes
Defining Your Own Classes Part 1
Classes and Objects Encapsulation
Defining Your Own Classes
Building Java Programs
Encapsulation and Constructors
CHAPTER 6 GENERAL-PURPOSE METHODS
Building Java Programs
Defining methods and more arrays
Multiple if-else boolean Data
Implementing Classes Chapter 3.
Java Classes and Objects
CMSC 202 Classes and Objects.
Building Java Programs
CMSC202 Computer Science II for Majors Lecture 07 – Classes and Objects (Continued) Dr. Katherine Gibson Based on slides by Chris Marron at UMBC.
Dr. R Z Khan Handout-3 Classes
Building Java Programs
CSG2H3 Object Oriented Programming
Presentation transcript:

Class Definitions and Writing Methods Chapter 3 10/24/16 and 10/25/16 1 1 1 1 1 1 1 1

Objectives Given a description an object, list its data fields and methods. Write a class once you have decided what its data fields and member methods are. Describe the use of the access modifiers public and private. Write a member method definition. 2 2 2 2 2 2 2 2

Objectives Call a method given its header. Describe the effect of a given call to a method. After writing a class definition, write a driver to test the class. 3 3 3 3 3 3 3 3

Objects Have Data Have Methods Things the object can do. Example – Clock Data? Methods? 4 4 4 4 4 4 4 4

Get with Partner Think of an object. What data and methods would it have? 5 5 5 5 5 5

Classes A class defines a new type that can group data and methods to form an object. Sample classes with methods and data associated with them. e.g. nextDouble( ) is a method of Scanner. A DecimalFormat has a format String, like “00.00” as its data. 6 6 6 6 6 6 6

Designing a Class To design a class, first decide what data the class will have and what methods it will have. Once the class is defined we can make objects of that type. For example: A SimpleDate class. 7 7 7 7 7 7 7 7

A SimpleDate Class A SimpleDate class is needed store a date. A SimpleDate should know how to print its date in either U.S. or European style. Design this class. Tell what data fields and member methods it needs 8 8 8 8 8 8 8 8

Design a Class With your partner design a SSN class to represent a Social Security number. You can decide how to store the number. It needs to be able to print the number as ######### Or ###-##-#### 9 9 9 9

Implement Class SimpleDate Make a class without a main(). Put data fields at top of class, outside of any method Fields are private Write the member methods. Leave off “static” The methods can access the data fields Have public access modifier Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010 10 10 10 10 10 10 10 10

Data Fields Fields: month, day, year Declared within a class private access modifier Outside classes can't access them. Allocated within an object of the class Each object, or instance, has its own month, day and year data fields. Data fields also called instance variables. Do not initialize them. 11 11 11 11 11 11 11 11

Class Methods Methods – define the class' behaviors. printUS(), printEU() Some methods to set the data field values: setMonth, setDay(), setYear() 12 12 12 12 12 12 12 12

public class SimpleDate { private int month; private int day; private int year; public void setMonth(int m){ month = m; } public void setDay(int d){ day = d; public void printDateUS(){ System.out.println(month + "/" + day + "/" + year); 13 13

14 14 public class SimpleDriver { public static void main(String[] args) { SimpleDate today = new SimpleDate(); today.setMonth(10); today.setDay(26); today.printDateUS(); SimpleDate tomorrow = new SimpleDate(); tomorrow.printDateUS(); } 14 14

Method Syntax Access modifiers are public and private. access-modifier return-type method-name(optional-parameter-list) { statement(s); } Access modifiers are public and private. Methods are usually public 15 15 15 15 15 15 15 15

Referencing Instance Variables The data fields of the class. They can be referred to by any method. If a SimpleDate, sd, is named in a call to a method, sd.printUS(), the fields belonging to sd are affected. Let’s write the printUS() method. 16 16 16 16 16 16 16

Driver to Test SimpleDate Need a program to test the class. Instantiate some SimpleDates and make sure all of the methods I wrote work correctly. I can do incremental testing. 17 17 17 17 17 17 17

Setters Now I need some setters to set the mo, day, year. SetMonth( ), setDay( ), setYear( ) They have parameters to store the values passed to them. We will use the values to initialize the data fields Setters are also called mutators. 18 18 18 18

Participation Copy my SimpleDate class and driver Write the printEU( ) method. Finish the driver 19 19 19 19

Continue Here 20 20 20 20 20 20 20

More on Classes Commenting classes this pointer 21 21 21 21 21 21 21

Comments in Classes Each class should have a description. Each method within a class should have a comment. Add comments to the SimpleDate class. Next Slide Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010 22 22 22 22 22 22 22 22

//Class that represents a date public class SimpleDate { private int month; private int day; private int year; // Gives the month the value passed to the method. public void setMonth(int m){ month = m; } // Gives the day a value public void setDay(int d){ day = d; //Prints the date in US format public void printDateUS(){ System.out.println(month + "/" + day + "/" + year); 23 23

The this pointer Allows an object to reference itself. It can be used when a local variable has the same name as a field has. The local name shadows the field name. Some programmers also use it to make code clearer. 24 24 24

The this pointer e.g. Dog bella = new Dog(); // Make a dog, no age bella.setYears(5); 25 25 25

//Name conflict with “years” //Parameter name shadows field public class Dog{ private int years; //Name conflict with “years” //Parameter name shadows field public void setYears(int years){ years = years; } //Use “this” to fix name conflict this.years = years; 26 26 26

Class Definitions and Writing Methods: Chapter 3 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010 27 27 27 27 27 27 27 27