Chapter 5 Classes and Methods II Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas, E.

Slides:



Advertisements
Similar presentations
Chapter 4 Decision Making Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas, E. Reingold.
Advertisements

Introduction to C Programming
Programming Languages and Paradigms The C Programming Language.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
Chapter 7: User-Defined Functions II
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
Chapter 7: User-Defined Functions II Instructor: Mohammad Mojaddam.
Constructors & An Introduction to Methods. Defining Constructor – Car Example Public class car { String Model; double speed; String colour; { Public Car.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
Road Map Introduction to object oriented programming. Classes
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 6 Functions.
Unit 4II 1 More about classes H Defining classes revisited H Constructors H Defining methods and passing parameters H Visibility modifiers and encapsulation.
COMP 110 Introduction to Programming Mr. Joshua Stough October 8, 2007.
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
Java An introduction. Example 1 public class Example1 { public static void main (String [] args) { System.out.println (“This is the first example”); int.
Chapter 7: User-Defined Methods
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the structure of a C-language program. ❏ To write your first C.
Chapter 2 Classes and Methods I Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas, E.
Comp 248 Introduction to Programming Chapter 4 - Defining Classes Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
Chapter 6 Iteration Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas, E. Reingold.
Chapter 12 Inheritance and Exceptions Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas,
Java Class Syntax CSIS 3701: Advanced Object Oriented Programming.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 6 Functions.
Chapter 6: User-Defined Functions
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
 2005 Pearson Education, Inc. All rights reserved. 1 Methods Called functions or procedures in other languages Modularize programs by separating its tasks.
Agenda Object Oriented Programming Reading: Chapter 14.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 7 Clicker Questions September 22, 2009.
Chapter 15 Text Processing and File Input/Output Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin,
Java™ How to Program, 10/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Assignment An assignment statement changes the value of a variable The assignment operator is the = sign total = 55; Copyright © 2012 Pearson Education,
1 Methods Introduction to Methods Passing Arguments to a Method More About Local Variables Returning a Value from a Method Problem Solving with Methods.
ITI 1120 Lab #11 Contributors: Diana Inkpen, Daniel Amyot, Sylvia Boyd, Amy Felty, Romelia Plesa, Alan Williams.
1 Brief Version of Starting Out with C++, 4th Brief Edition Chapter 6 Functions.
User Defined Methods Methods are used to divide complicated programs into manageable pieces. There are predefined methods (methods that are already provided.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 6 Objects and Classes.
CS305j Introduction to Computing Classes II 1 Topic 24 Classes Part II "Object-oriented programming as it emerged in Simula 67 allows software structure.
Chapter 5 Classes and Methods II Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas, E.
Review Expressions and operators Iteration – while-loop – for-loop.
Chapter 7 Classes and Methods III: Static Methods and Variables Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition)
 Static  Example for Static Field  Example for Static Method  Math class methods  Casting  Scope of Declaration  Method Overloading  Constructor.
Classes - Intermediate
1 Lecture # 2. * Introducing Programming with an Example * Identifiers, Variables, and Constants * Primitive Data Types * Byte, short, int, long, float,
1 Sections 6.4 – 6.5 Methods and Variables Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
CSCI 51 Introduction to Programming Dr. Joshua Stough February 26, 2009.
CCSA 221 Programming in C CHAPTER 3 COMPILING AND RUNNING YOUR FIRST PROGRAM 1 ALHANOUF ALAMR.
ITI 1120 Lab #10 Objects and Classes Slides by: Romelia Plesa, Sylvia Boyd, Alan Williams, Diana InkPen, Daniel Amyot, Gilbert Arbez, Mohamad Eid.
Chapter 7 User-Defined Methods.
Chapter 6: User-Defined Functions I
Chapter 7: User-Defined Functions II
Introduction to Computer Science / Procedural – 67130
Java Programming: Guided Learning with Early Objects
Yanal Alahmad Java Workshop Yanal Alahmad
Road Map Introduction to object oriented programming. Classes
User-Defined Functions
Chapter 2.
Introduction to the C Language
CS/ENGRD 2110 Spring 2017 Lecture 5: Local vars; Inside-out rule; constructors
Chapter 3 Introduction to Classes, Objects Methods and Strings
Defining Classes and Methods
CS/ENGRD 2110 Spring 2016 Lecture 5: Local vars; Inside-out rule; constructors
Classes Lecture 7 from Chapter /1/11.
Object Oriented Programming in java
Classes, Objects and Methods
CS 1054: Lecture 2, Chapter 1 Objects and Classes.
Standard Version of Starting Out with C++, 4th Edition
Corresponds with Chapter 5
Presentation transcript:

Chapter 5 Classes and Methods II Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas, E. Reingold

Chapter Preview In this chapter we will: formally introduce the class construct as it is used in the Java language discuss the use of instance variables to facilitate method communication demonstrate the use of classes to improve program structure

Building Classes with Multiple Methods Computer programs can be thought of using phases –Input –Computation –Output Using separate methods for each phase can improve the maintainability of a class or program

Building Class Definitions public class classname { // Author, data, explanation declarations of instance variables public void methodName1 (parameter) { declarations of local variables executable statements with comments } public void methodName2 (parameter) { declarations of local variables executable statements with comments }

Instance Variables Local variables –variables declared inside methods –not accessible to any other method –cannot be used for communication Instance variables –declared outside the methods, but declared inside the class –all class methods have access to the class instance variables –can be used for communication inside class

Initialization of Instance Variables Instance variable declarations can contain initializers just like local variables Unlike local variables, instance variables will be initialized to default values if no initializers are found –integers and doubles are initialized to 0 –characters are initialized to the null character (ASCII code 0) –booleans are initialized to false –object-type variables are initialized to the reference value null

Hose Class Methods void getData() // Reads and stores the height and // weight data. void compute() // Computes and stores hose size. void display() // Displays the results of the // computation.

UML Diagram for Hose Class

Variable Scope Rules 1.The scope of an instance variable is the entire class body unless another identifier is found with the same name. 2.The scope of a formal argument in a method header is the entire method body. 3.The scope of a local variable in a method is from the point of declaration to the end of the method body. 4.It is not legal to declare a variable within a method using the same name as variable in the enclosing block in that method. You cannot declare two instance variables using the same name.

Scope Example

Bad Variable Declarations

Class Constructors with Arguments A constructor is a special method that is called when an object is allocated. We can write OutputBox out = new OutputBox(“A Title”); Instead of OutputBox out = new OutputBox(); Out.setTitle(“A Title”); Writing constructors for programmer defined classes will be discussed in Chapter 7.

Return Types It is possible to have methods that have return types other than void Example: public class Clock ( int hour; public int getHour (){ return hour; }

return Statement The return statement –allows a method to return a value to the caller –can appear any where in the method body –can be conditionally executed –results in immediate exit from a method when executed Form: return expression ;

Clock Class Methods ExampleExplanation void setup() Initializes the clock void getData() Reads and stores the hour and minute data String toString() Returns string version of time suitable for printing void setHour(int h) Sets hour to h void setMinute(int m) Sets minute to m int getHour() Returns value of hour int getMinute() Returns value of minute boolean priorTo(Clock c) Returns true is receiver < c void display (DrawingBox d, int x, int y, int r) Draws the clock with center at (x,y) and radius r, in the DrawingBox referred to by d

Clock Class Outline

Geometry of Clock Drawing

Theta Calculations For the hour hand theta = 2*Math.PI*minute/60.0; For the minute hand theta = 2*Math.PI*(hour + minute)/60.0/12.0;

Drawing the Clock Hands Assuming (x, y) is the bottom vertex and recalling that computer graphics coordinates are upside down x1 = x + (int) (r*Math.sin(theta)); y1 = y - (int) (r*Math.cos(theta)); d.drawLine(x, y, x1, y1); For the hour hand use r*.8 in place of r

TwoClocks Client public class TwoClocksClient { public static void main (String[] args){ TwoClock twins = new TwoClocks(); twins.drawClocks(); twins.compareClocks(); }

Output from TwoClocks Client

UML Class Diagram for Clock-DrawingBox Composition