Presentation is loading. Please wait.

Presentation is loading. Please wait.

Class Definitions and Writing Methods Chapter 3 3/31/16 & 4/4/16.

Similar presentations


Presentation on theme: "Class Definitions and Writing Methods Chapter 3 3/31/16 & 4/4/16."— Presentation transcript:

1 Class Definitions and Writing Methods Chapter 3 3/31/16 & 4/4/16

2 Objectives Given a description a class, list its data fields and member 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 JFrame has a length and width 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. It should know how to print the 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 datafields at top of class, outside of any method Write the member methods. Have public access modifier Leave off “static” They can access the data fields Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010

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

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

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); } public void printDateEU(){ System.out.println(day + "/" + month + "/" + year); }

14 public class SimpleDriver { public static void main(String[] args) { SimpleDate today = new SimpleDate(); today.setMonth(4); today.setDay(4); today.printDateUS(); today.printDateEU(); 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.

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

20 Continue Here 4/14/2016 & 4/18/16

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 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); } //Prints the date in EU format public void printDateEU(){ System.out.println(day + "/" + month + "/" + 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 dominates. 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” public void setYears(int years){ years = years; } //Use “this” to fix name conflict public void setYears(int years){ this.years = years; }

27 Chapter 9 4/14/2016 & 4/18/2016

28 Data and Methods for the Class?

29 Local Variable Variable that is declared inside { }, in a block of statements. It can only be used in that block of statements. findSalePrice() could have a local variable. Let’s start the method

30 More Special Methods

31 Data fields are hidden to protect them.  The data is private Accessor methods, the “getters”.  Accessor lets outside methods access, or get, a data field's value. Name usually starts with "get". Returns the private data. Mutator methods, the “setters”, l et outside methods change a value. Name usually starts with "set". Changes the private data. Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010

32 More Special Methods I'll add a getter to the StoreSale class. Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010

33 Questions In the following Dog class identify: 1. The data field. 2.The accessor and mutator methods. 3. A local variable. 4. A parameter. 5.What is the output of the driver?

34 //Dog class public class Dog { private int years; public Dog(){ years = 0; } public Dog(int y){ years = y; } public int getYears(){ return years; } public void setYears(int y){ years = y; } public int dogYears(){ int dy; dy = years *7; return dy; }

35 //Dog Driver -- What is the output? public class DogDriver{ public static void main(String args[]){ Dog bella = new Dog(); // Make a dog, no age bella.setYears(5); System.out.println("Bella: "); System.out.println(bella.getYears()+ " years."); System.out.println(bella.dogYears()+ " dog years"); }

36 Static Methods Method that can be called without creating an object. Can not use data fields. Can get data passed through parameters. Example – convertToMiles method to convert kilometers to miles.

37 Participation Make getters for the datafields in the SimpleDate class that we created last week. If you weren't here, make both getters and setters for the data fields.

38 Notes

39 Constructors-Later Later In Chapter 9

40 Participation Sphere class Design class called Sphere that stores its radius. A Sphere will be able to print its area and volume. What are the data fields and methods of a Sphere? Work with a partner. Write a paper copy.

41 SimpleDate with default value A method to initialize a SimpleDate is usually provided. What do you call a method that is called when an object is created?

42 Special Methods Constructor methods – Have the same name as the class. – Instantiate new Object – Initialize Object's fields  Intialize means to give it a starting value. – A default constructor accepts no arguments. May initialize the field(s) to a default value. Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010

43 Putting Constructors in the SimpleDate Class A constructor's name is the same as the class name. In this case I will make two:  Default constructor – sets month, day and year to default values  Parameterized constructor – sets month, day and year to values passed.

44 Constructor Syntax access-modifier class-name(optional-parameter-list) { statement(s); }  Access modifiers must be public on a Constructor  Constructor do not have a return type.

45 Default Constructor Hints In absence of programmer defined constructor  Java will provide default constructor Good practice to specify default values for data fields Don’t duplicate tasks  Methods should call each other Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010

46 Participation 2 Login and begin a Sphere class. Code the methods to find area, find volume and set the radius. Write a default constructor and a parameterized constructor for it.

47 Participation 2 A Driver The class must be tested. Need to write a program to create a Sphere or two. The constructors can be tested by using the new operator. Test all of the methods.

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


Download ppt "Class Definitions and Writing Methods Chapter 3 3/31/16 & 4/4/16."

Similar presentations


Ads by Google