270 likes | 282 Views
Learn about class definitions, data fields, member methods, access modifiers, and writing a class driver with examples.
E N D
Class Definitions and Writing Methods Chapter 3 10/24/16 and 10/25/16
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.
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.
Objects • Have Data • Have Methods • Things the object can do. • Example – Clock • Data? • Methods?
Get with Partner • Think of an object. • What data and methods would it have?
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.
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.
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
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 ###-##-####
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
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.
Class Methods • Methods – define the class' behaviors. • printUS(), printEU() • Some methods to set the data field values: • setMonth, setDay(), setYear()
publicclass SimpleDate { privateintmonth; privateintday; privateintyear; publicvoid setMonth(intm){ month = m; } publicvoid setDay(intd){ day = d; } publicvoid printDateUS(){ System.out.println(month + "/" + day + "/" + year); }
publicclass SimpleDriver { publicstaticvoid main(String[] args) { SimpleDate today = new SimpleDate(); today.setMonth(10); today.setDay(26); today.printDateUS(); SimpleDate tomorrow = new SimpleDate(); tomorrow.printDateUS(); } }
Method Syntax access-modifier return-type method-name(optional-parameter-list) { statement(s); } • Access modifiers are public and private. • Methods are usually public
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.
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.
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.
Participation • Copy my SimpleDate class and driver • Write the printEU( ) method. • Finish the driver
More on Classes • Commenting classes • this pointer
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
//Class that represents a date publicclass SimpleDate { privateintmonth; privateintday; privateintyear; // Gives the month the value passed to the method. publicvoid setMonth(intm){ month = m; } // Gives the day a value publicvoid setDay(intd){ day = d; } //Prints the date in US format publicvoid printDateUS(){ System.out.println(month + "/" + day + "/" + year); }
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 shadowsthe field name. • Some programmers also use it to make code clearer.
The this pointer • e.g. Dog bella = new Dog(); // Make a dog, no age bella.setYears(5);
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 public void setYears(int years){ this.years = years; } }
Class Definitions and Writing Methods: Chapter 3 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010