Presentation is loading. Please wait.

Presentation is loading. Please wait.

Class Definitions and Writing Methods

Similar presentations


Presentation on theme: "Class Definitions and Writing Methods"— Presentation transcript:

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

2 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

3 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

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

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

6 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

7 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

8 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

9 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

10 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

11 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

12 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

13 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 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

15 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

16 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

17 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

18 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

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

20 Continue Here 20 20 20 20 20 20 20

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

22 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

23 //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

24 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

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

26 //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

27 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


Download ppt "Class Definitions and Writing Methods"

Similar presentations


Ads by Google