Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 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 - Prentice Hall, 2010

2 Announcements Exam II – M 04/13/15 and Th 04/09/15 Assignment 5 due: – M 04/06/15 and Th 04/02/15 Take Quiz 4 by next week: – M 04/06/15 and Th 04/09/15 Assignment 6 – Due: M 04/20/14 & Th 04/23/15

3 Review for Exam 2 Over Chapter 3 and 4 Look at Website.

4 Objects Have Data Have Methods  Things the object can do. Example – Clock  Data?  Methods? Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010

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

6 Objectives For Today: Given a description a class, list its data fields and methods. Write a class once you have decided what its data fields and methods are. Describe the use of the access modifiers public and private. Write a method definition. Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010

7 Objectives Write methods that use parameters. Write constructors For Next Week: Describe the use of accessor methods and mutator methods, and write their definitions. Call a method given its header. Describe the effect of a given call to a method. Write a class definition. Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010

8 Classes Are Types We have used classes such as String and Scanner. The class is a type we can use to instantiate, or make, objects. The classes have data and methods associated with them.  e.g. nextDouble( ) is a method of Scanner.  "Joe Smith" might be data in a String object.

9 Designing a Class To design a class, first decide what data the class will have and what methods it will have. For example: I want to create a Rectangle class. I want a Rectangle to be able to tell me what its area and perimeter are. I need to consider what its data and methods will be.

10 A Car Class(If time?) A Car class is needed to simulate fuel consumption in a car. A car should know how to add gas, drive a given distance, and check the amount of fuel left in the tank. Design this class. – Tell what data fields and methods it needs

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

12 Implement Class Rectangle Make a class without a main(). Put datafields at top of class, outside of any method Methods are like small versions of the main method. Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010

13 public class Rectangle { private double length; private double width; //Print the rectangle's area public void printArea(){ System.out.println("Area = " + length*width); } //Print the rectangle's perimeter public void printPerimeter(){ System.out.println("Perimeter =" + 2.0*(length + width)); }

14 Defining the Class Data fields length and width a)Declared within a class b)private c)Allocated within an object of the class d)Each object has its own length and width data fields. e)Data fields also called instance variables. Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010

15 Defining the Class Methods – define the class' behaviors. The printLength and printWidth methods – Print the length and width – void because they don't return a value. – Empty parens indicate no parameters. Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010

16 Driver to Test Rectangle Need a program to test the class. Instantiate some Rectangles and make sure they work correctly.

17 public class TestRectangle { public static void main(String[] args) { Rectangle slab = new Rectangle(); slab.printArea(); slab.printPerimeter(); }

18 Rectangles with 0.0 Area Need a method to initialize the Rectangles. What do you call a method that is called when an object is created?

19 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

20 Putting Constructors in the Rectangle Class It is nice to have a constructors to initialize a rectangle. A constructor's name is the same as the class name. In this case I will make two:  Default constructor – sets length and width to default values  Parameterized constructor – sets length and width to values passed.

21 Constructor Syntax access-modifier class-name(optional-parameter-list) { statement(s); }  Access modifiers are public and private.

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

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

24 More About Methods Mon 4/6/15 and Thur 4/16/15

25 StoreSale Class A clothing store wants a StoreSale class to help with its sales. It has sales where there is a certain storewide discount, like 20% off everything.

26 StoreSale Class An instance of the class is created by passing the percent discount. For example for April 15 th they are having a tax return sale at 15% off. StoreSale taxDaySale = new StoreSale(15.0);

27 StoreSale Class To find a discounted price on an item the price is send to a findSalePrice() method: Double salePrice = taxDaySale.findSalePrice(60.00);

28 What Data and Methods for the Class?

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

30 Parameters and Arguments In new StoreSale(15.0), 15.0 is an argument.  It is a value that is passed to a Method. The Constructor needs to receive this argument and store it.  It uses a parameter.  A parameter is a dummy variable that waits for a value to be passed into it.

31 Start the Class The class name is StoreSale. Write the parameterized constructor. What will be the type of the parameter?

32 Returning a Value If a method needs to get a value back to where it is called, it returns a value via the return statement. – return x; The findSalePrice method needs to tell us the sale price, so it needs a return statement. (Constructors cannot return a value.)

33 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 write the method

34 Parameters Are Local Parameters act like local variables in a method.

35 Referencing Instance Variables The data fields of the class. They can be referred to by any method. With a call to taxDaySale.findPrice( ), the data fields belonging to the taxDaySale object are used in the methods.

36 Participation See handout, part I. Write it on paper.

37 More Special Methods

38 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”(Skip)  Lets 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

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

40 Questions In the following Dog class identify: 1. The data field. 2.The constructors. 3. Which is the default constructor? 4. The accessor and mutator methods. 5. A local variable. 6. A parameter.

41 //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; }

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

43 Wrap Up Classes Commenting classes toString( ) method Generated default method.

44 Comments in Classes Each class should have a description. Each method within a class should have a comment. Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010

45 toString( ) It is a good idea to have a toString( ) method in every class. It returns a string that shows the datafields in an object. I put a call to toString in the.  Java generates one, if I don't provide it.  Automatically used in println. I'll make a toString( ) method for the StoreSale class.

46 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

47 Notes Quiz 6 soon. Read Chapter 7, p. 181-195 for next week.

48 Class Definitions: The Fundamentals Chapter 6 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010


Download ppt "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."

Similar presentations


Ads by Google