1 / 27

Class Definitions and Writing Methods

Learn about class definitions, data fields, member methods, access modifiers, and writing a class driver with examples.

johnniep
Download Presentation

Class Definitions and Writing Methods

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  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.

  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.

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

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

  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.

  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.

  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

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

  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

  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.

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

  13. publicclass SimpleDate { privateintmonth; privateintday; privateintyear; publicvoid setMonth(intm){ month = m; } publicvoid setDay(intd){ day = d; } publicvoid printDateUS(){ System.out.println(month + "/" + day + "/" + year); }

  14. publicclass SimpleDriver { publicstaticvoid main(String[] args) { SimpleDate today = new SimpleDate(); today.setMonth(10); today.setDay(26); today.printDateUS(); SimpleDate tomorrow = new SimpleDate(); tomorrow.printDateUS(); } }

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

  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.

  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.

  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.

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

  20. Continue Here

  21. More on Classes • Commenting classes • this pointer

  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

  23. //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); }

  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 shadowsthe field name. • Some programmers also use it to make code clearer.

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

  26. 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; } }

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

More Related