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.

Slides:



Advertisements
Similar presentations
Looking inside classes Fields, Constructors & Methods Week 3.
Advertisements

The Line Class Suppose you are involved in the development of a large mathematical application, and this application needs an object to represent a Line.
Copyright 2008 by Pearson Education Building Java Programs Chapter 8 Lecture 8-3: Encapsulation, toString reading: self-checks: #13-18,
CS0007: Introduction to Computer Programming Introduction to Classes and Objects.
Written by: Dr. JJ Shepherd
1 Classes Overview l Classes as Types l Declaring Instance Variables l Implementing Methods l Constructors l Accessor and Mutator Methods.
Classes and Objects April 6, Object Oriented Programming Creating functions helps to make code that we can reuse. When programs get large it becomes.
CS 106 Introduction to Computer Science I 03 / 21 / 2008 Instructor: Michael Eckmann.
COMP 110 Introduction to Programming Mr. Joshua Stough October 8, 2007.
1 Fall 2007ACS-1903 Chapter 6: Classes Classes and Objects Instance Fields and Methods Constructors Overloading of Methods and Constructors Scope of Instance.
Copyright 2010 by Pearson Education Building Java Programs Chapter 8 Lecture 8-2: Object Behavior (Methods) and Constructors reading:
UML Basics & Access Modifier
Writing Classes (Chapter 4)
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals; Apply data abstraction.
CSC 212 – Data Structures Lecture 12: Java Review.
Java Quiz Bowl A fun review of the Java you should know from CMPT 201 If you don’t know the answers - this week is for you to study up!
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 Pearson Education, Inc., Upper.
CSC 142 Computer Science II Zhen Jiang West Chester University
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Copyright 2010 by Pearson Education Building Java Programs Chapter 8 Lecture 8-2: Object Behavior (Methods) and Constructors, Encapsulation, this reading:
A First Look at Java Chapter 2 1/29 & 2/2 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010.
Copyright © 2012 Pearson Education, Inc. Chapter 9 Classes and Multiform Projects.
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
10-Nov-15 Java Object Oriented Programming What is it?
CSC1401 Classes - 2. Learning Goals Computing concepts Adding a method To show the pictures in the slide show Creating accessors and modifiers That protect.
CSC 142 Computer Science II Zhen Jiang West Chester University
Chapter 4 Introduction to Classes, Objects, Methods and strings
Java - Classes JPatterson. What is a class? public class _Alpha { public static void main(String [] args) { } You have been using classes all year – you.
More about Java Chapter 2 9/8 & 9/9 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010.
CS0007: Introduction to Computer Programming Classes: Documentation, Method Overloading, Scope, Packages, and “Finding the Classes”
Introduction Chapter 1 8/31 & 9/1 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Chapter 3 Introduction to Classes and Objects Definitions Examples.
Classes Modeling the Object. Objects model the world Classes are programmer defined types that model the parts of a system Class serve as blueprints for.
CS305j Introduction to Computing Classes II 1 Topic 24 Classes Part II "Object-oriented programming as it emerged in Simula 67 allows software structure.
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 3 A First Look at Classes and Objects.
Topic 8Classes, Objects and Methods 1 Topic 8 l Class and Method Definitions l Information Hiding and Encapsulation l Objects and Reference Classes, Objects,
Written by: Dr. JJ Shepherd
Starting Out With Java 5 Control Structures to Objects By Tony Gaddis Copyright © 2005, 2005 Pearson Addison-Wesley. All rights reserved. Chapter 6 Slide.
Topics Instance variables, set and get methods Encapsulation
5.1 Basics of defining and using classes A review of class and object definitions A class is a template or blueprint for an object A class defines.
OOP Basics Classes & Methods (c) IDMS/SQL News
Class Everything in Java is in a class. The class has a constructor that creates the object. If you do not supply a constructor Java will create a default.
Chapter 2 Wrap Up 2/18/16 & 2/22/16. Topics Review for Exam I DecimalFormat More Style Conventions Debugging using eclipse The Java API.
Powerpoint slides from A+ Computer Science Modified by Mr. Smith for his course.
Class Definitions and Writing Methods Chapter 3 3/31/16 & 4/4/16.
Class Definitions and Writing Methods Chapter 3 10/12/15 & 10/13/15 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education.
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Copyright 2010 by Pearson Education Building Java Programs Chapter 8 Lecture 8-2: Object Behavior (Methods) and Constructors, Encapsulation, this reading:
Copyright © 2012 Pearson Education, Inc. Chapter 4 Writing Classes : Review Java Software Solutions Foundations of Program Design Seventh Edition John.
Building Java Programs
Class Definitions and Writing Methods
Chapter 3: Using Methods, Classes, and Objects
User-Defined Classes and ADTs
Chapter 3 Introduction to Classes, Objects Methods and Strings
Chapter 3 Introduction to Classes, Objects Methods and Strings
Ch 4: Writing Classes Java Software Solutions Foundations of Program Design Sixth Edition by Lewis & Loftus Coming up: Classes and Objects.
Building Java Programs
More on Classes and Objects
Implementing Classes Chapter 3.
Defining Classes and Methods
© A+ Computer Science - OOP Pieces © A+ Computer Science -
Building Java Programs
JAVA CLASSES.
Building Java Programs
Dr. R Z Khan Handout-3 Classes
Building Java Programs
Lecture 8-2: Object Behavior (Methods) and Constructors
CSG2H3 Object Oriented Programming
Presentation transcript:

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

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

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

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

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

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

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

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.

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.

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

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.

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

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)); }

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

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

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

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

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?

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

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.

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

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.

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.

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

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.

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);

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

What Data and Methods for the Class?

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

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.

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

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

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

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

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.

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

More Special Methods

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

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

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.

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

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

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

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

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.

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

Notes Quiz 6 soon. Read Chapter 7, p for next week.

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