Chapter 4: Writing Classes Presentation slides for Java Software Solutions Foundations of Program Design Second Edition by John Lewis and William Loftus.

Slides:



Advertisements
Similar presentations
© 2011 Pearson Education, publishing as Addison-Wesley Chapter 4: Writing Classes Presentation slides for Java Software Solutions for AP* Computer Science.
Advertisements

Interfaces A Java interface is a collection
1 Classes, Encapsulation, Methods and Constructors (Continued) Class definitions Instance data Encapsulation and Java modifiers Method declaration and.
CSCI 1100/1202 April 3, Testing A program should be executed multiple times with various input in an attempt to find errors Debugging is the process.
Chapter 4: Writing Classes
Chapter 4: Writing Classes
© 2006 Pearson Education Chapter 4: Writing Classes Presentation slides for Java Software Solutions for AP* Computer Science A 2nd Edition by John Lewis,
Chapter 3, More on Classes & Methods Clark Savage Turner, J.D., Ph.D. Copyright 2003 CSTurner, from notes and text.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Chapter Day 7. © 2007 Pearson Addison-Wesley. All rights reserved4-2 Agenda Day 7 Questions from last Class?? Problem set 1 Corrected  Good results 3.
Chapter 4: Writing Classes Presentation slides for Java Software Solutions Foundations of Program Design Second Edition by John Lewis and William Loftus.
COMP 14 Introduction to Programming Miguel A. Otaduy May 25, 2004.
COMP 110 Introduction to Programming Mr. Joshua Stough October 8, 2007.
Foundations of Program Design Writing Classes. 2 Objects b An object has: state - descriptive characteristicsstate - descriptive characteristics behaviors.
Chapter 4: Writing Classes Presentation slides for Java Software Solutions Foundations of Program Design Second Edition by John Lewis and William Loftus.
Classes, Encapsulation, Methods and Constructors
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 8, 2005.
Aalborg Media Lab 26-Jun-15 Software Design Lecture 5 “ Writing Classes”
Chapter 4: Writing Classes Presentation slides for Java Software Solutions Foundations of Program Design Second Edition by John Lewis and William Loftus.
Chapter 4: Writing Classes Presentation slides for Java Software Solutions Foundations of Program Design Third Edition by John Lewis and William Loftus.
Copyright © 2012 Pearson Education, Inc. Chapter 4 Writing Classes Java Software Solutions Foundations of Program Design Seventh Edition John Lewis William.
COMP 14: Writing Classes June 6, 2000 Nick Vallidis.
Methods in Java Selim Aksoy Bilkent University Department of Computer Engineering
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Chapter 4: Writing Classes Presentation slides for Java Software Solutions for AP* Computer Science by John Lewis, William Loftus, and Cara Cocking Java.
1 (c) elsaddik CSI 1102 Introduction to Software Design Prof. Dr.-Ing. Abdulmotaleb El Saddik University of Ottawa (SITE.
© 2011 Pearson Education, publishing as Addison-Wesley Chapter 4: Writing Classes Presentation slides for Java Software Solutions for AP* Computer Science.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley John Lewis, Peter DePasquale, and Joseph Chase Chapter 5: Writing Classes.
Writing Classes (Chapter 4)
© 2004 Pearson Addison-Wesley. All rights reserved November 9, 2007 Method Design & Method Overloading ComS 207: Programming I (in Java) Iowa State University,
© 2006 Pearson Education Chapter 4: Writing Classes Presentation slides for Java Software Solutions for AP* Computer Science A 2nd Edition by John Lewis,
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Java Software Solutions Lewis and Loftus Chapter 4 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Objects and Classes -- Introduction.
Chapter 4 -2 part Writing Classes 5 TH EDITION Lewis & Loftus java Software Solutions Foundations of Program Design © 2007 Pearson Addison-Wesley. All.
Chapter 4 Writing Classes Part 2. © 2004 Pearson Addison-Wesley. All rights reserved4-2 Classes A class can contain data declarations and method declarations.
© 2004 Pearson Addison-Wesley. All rights reserved September 14, 2007 Anatomy of a Method ComS 207: Programming I (in Java) Iowa State University, FALL.
CSE 1301 Lecture 6 Writing Classes Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.
Wednesday –POD –I have updated grades in powerschool. If you have a zero for a lab grade, it probably means you didn’t DropItToMe. Please do so. –Slides.
Lecture 4 b Writing Classes b class declarations b method declarations b constructors b instance variables b encapsulation b method overloading b program.
© 2011 Pearson Education, publishing as Addison-Wesley Chapter 4: Writing Classes Presentation slides for Java Software Solutions for AP* Computer Science.
Chapter 4: Writing Classes. 2 b We've been using predefined classes. Now we will learn to write our own classes to define new objects b Chapter 4 focuses.
Programming in Java (COP 2250) Lecture 10 Chengyong Yang Fall, 2005.
Chapter 4 Writing Classes. © 2004 Pearson Addison-Wesley. All rights reserved4-2 Writing Classes We've been using predefined classes. Now we will learn.
Copyright © 2012 Pearson Education, Inc. Chapter 4 Writing Classes Java Software Solutions Foundations of Program Design Seventh Edition John Lewis William.
© 2011 Pearson Education, publishing as Addison-Wesley Chapter 4: Writing Classes Presentation slides for Java Software Solutions for AP* Computer Science.
Chapter 4 Writing Classes 5 TH EDITION Lewis & Loftus java Software Solutions Foundations of Program Design © 2007 Pearson Addison-Wesley. All rights reserved.
Outline Anatomy of a Class Encapsulation Anatomy of a Method Graphical Objects Graphical User Interfaces Buttons and Text Fields Copyright © 2012 Pearson.
1 Chapter 4: Writing Classes  Chapter 4 focuses on: class definitions encapsulation and Java modifiers method declaration, invocation, and parameter passing.
© 2004 Pearson Addison-Wesley. All rights reserved April 5, 2006 Method Design & Method Overloading ComS 207: Programming I (in Java) Iowa State University,
© 2004 Pearson Addison-Wesley. All rights reserved3-1 Objects Declaration: String title;  title (object variable) of type String( Class )  title is just.
Copyright © 2012 Pearson Education, Inc. Chapter 4 Writing Classes : Review Java Software Solutions Foundations of Program Design Seventh Edition John.
Chapter 5: Enhancing Classes
Chapter 4: Writing Classes
Chapter 4: Writing Classes
Writing Classes Chapter 4.
Chapter 4: Writing Classes
Lecture 14 Writing Classes part 2 Richard Gesick.
Chapter 4: Writing Classes
Methods The real power of an object-oriented programming language takes place when you start to manipulate objects. A method defines an action that allows.
Ch 4: Writing Classes Java Software Solutions Foundations of Program Design Sixth Edition by Lewis & Loftus Coming up: Classes and Objects.
CSCI 1100/1202 April 10,
Group Status Project Status.
Classes, Encapsulation, Methods and Constructors (Continued)
Anatomy of a Method.
Writing Classes.
Chapter 4 Writing Classes.
Corresponds with Chapter 5
Chapter 4: Writing Classes
Presentation transcript:

Chapter 4: Writing Classes Presentation slides for Java Software Solutions Foundations of Program Design Second Edition by John Lewis and William Loftus Java Software Solutions is published by Addison-Wesley Presentation slides are copyright 2000 by John Lewis and William Loftus. All rights reserved. Instructors using the textbook may use and modify these slides for pedagogical purposes.

Data Scope b The scope of data is the area in a program in which that data can be used (referenced) b Data declared at the class level can be used by all methods in that class b Data declared within a method can only be used in that method b Data declared within a method is called local data b Data and methods are primary components of a class, they work together to bring the concept alive as a unit

Local and Class scope public class X{ public static int a; // a has class scope, can be seen from // anywhere inside the class // anywhere inside the class…. public static void m() { a=5; // no problem int b = 0; // b is declared inside the method, local scope ….. } // here variable b is destroyed, no one will remember him public static void m2() { a=3; // ok b = 4; // who is b? compiler will issue an error }

Writing Methods b A method declaration specifies the code that will be executed when the method is invoked (or called) b When a method is invoked, the flow of control jumps to the method and executes its code b When complete, the flow returns to the place where the method was called and continues b The invocation may or may not return a value, depending on how the method was defined

myMethod(); myMethodcompute Method Control Flow b The called method could be within the same class, in which case only the method name is needed b Since methods are closely related, they are on first name basis

doIt helpMe helpMe(); Y obj = new Y(); obj.doIt(); main Method Control Flow b The called method could be part of another class or object b t, which only invokes the interface methods Class X Class Y

7 Visibility Modifiers b In Java, we accomplish encapsulation through the appropriate use of visibility modifiers b A modifier is a Java reserved word that specifies particular characteristics of a method or data value  We've used the modifier final to define a constant  Java has three visibility modifiers: public, private, and protected  We will discuss the protected modifier later

8 Visibility Modifiers b Members of a class that are declared with public visibility can be accessed from anywhere b Members of a class that are declared with private visibility can only be accessed from inside the class b Members declared without a visibility modifier have default visibility and can be accessed by any class in the same package b Java modifiers are discussed in detail in Appendix F b.b.b.b.

Method Declarations Revisited b A method declaration begins with a method header char calc (int num1, int num2, String message) methodname returntype parameter list The parameter list specifies the type and name of each parameter The name of a parameter in the method declaration is called a formal argument

Method Declarations b The method header is followed by the method body char calc (int num1, int num2, String message) { int sum = num1 + num2; char result = message.charAt (sum); return result; } The return expression must be consistent with the return type sum and result are local data They are created each time the method is called, and are destroyed when it finishes executing

11 The return Statement b The return type of a method indicates the type of value that the method sends back to the calling location  A method that does not return a value has a void return type b The return statement specifies the value that will be returned b Its expression must conform to the return type

Parameters b Each time a method is called, the actual arguments in the invocation are copied into the formal arguments char calc (int num1, int num2, String message) { int sum = num1 + num2; char result = message.charAt (sum); return result; } ch = obj.calc (25, count, "Hello");

Parameters … b We can view the parameters as local variables given initial values when the method gets called. Therefore they have local scope, valid only inside the method char calc () { int num1 = 25; int num2 = count; String message = “Hello”; int sum = num1 + num2; char result = message.charAt (sum); return result; } ch = obj.calc (25, count, "Hello");

14 Overloading Methods b Method overloading is the process of using the same method name for multiple methods b The signature of each overloaded method must be unique b The signature includes the number, type, and order of the parameters b The compiler must be able to determine which version of the method is being invoked by analyzing the parameters b The return type of the method is not part of the signature

Overloading Methods float tryMe (int x) { return x +.375; } Version 1 float tryMe (int x, float y) { return x*y; } Version 2 result = tryMe (25, 4.32)Invocation

16 Overloaded Methods  The println method is overloaded: println (String s) println (String s) println (int i) println (int i) println (double d) println (double d) etc. etc.  The following lines invoke different versions of the println method: System.out.println ("The total is:"); System.out.println ("The total is:"); System.out.println (total); System.out.println (total);

The StringTokenizer Class  The next example makes use of the StringTokenizer class, which is defined in the java.util package  A StringTokenizer object separates a string into smaller substrings (tokens) b By default, the tokenizer separates the string at white space  The StringTokenizer constructor takes the original string to be separated as a parameter  Each call to the nextToken method returns the next token in the string

Method Decomposition b A method should be relatively small, so that it can be readily understood as a single entity b A potentially large method should be decomposed into several smaller methods as needed for clarity b Therefore, a service method of an object may call one or more support methods to accomplish its goal b See PigLatin.java (page 207) PigLatin.java b See PigLatinTranslator.java (page 208) PigLatinTranslator.java

Pig Latin String st = "A method should be relatively small, so that it can be readily understood as a single entity"; String result = PigLatinTranslator.translate (st); System.out.println(result);output: ayay ethodmay ouldshay ebay elativelyray all,smay osay atthay ityay ancay ebay e adilyray understoodyay asyay ayay inglesay entityyay

PigLatinTranslator import java.util.StringTokenizer; public class PigLatinTranslator { // // // Translates a sentence of words into Pig Latin. // Translates a sentence of words into Pig Latin. // // public static String translate (String sentence) public static String translate (String sentence) { String result = ""; String result = ""; sentence = sentence.toLowerCase(); sentence = sentence.toLowerCase(); StringTokenizer tokenizer = new StringTokenizer (sentence); StringTokenizer tokenizer = new StringTokenizer (sentence); while (tokenizer.hasMoreTokens()) while (tokenizer.hasMoreTokens()) { result += translateWord (tokenizer.nextToken()); result += translateWord (tokenizer.nextToken()); result += " "; result += " "; } return result; return result; }

PigLatinTranslator // // // Translates one word into Pig Latin. If the word begins with a // Translates one word into Pig Latin. If the word begins with a // vowel, the suffix "yay" is appended to the word. Otherwise, // vowel, the suffix "yay" is appended to the word. Otherwise, // the first letter or two are moved to the end of the word, // the first letter or two are moved to the end of the word, // and "ay" is appended. // and "ay" is appended. // // private static String translateWord (String word) private static String translateWord (String word) { String result = ""; String result = ""; if (beginsWithVowel(word)) if (beginsWithVowel(word)) result = word + "yay"; result = word + "yay"; else else if (beginsWithBlend(word)) if (beginsWithBlend(word)) result = word.substring(2) + word.substring(0,2) + "ay"; result = word.substring(2) + word.substring(0,2) + "ay"; else else result = word.substring(1) + word.charAt(0) + "ay"; result = word.substring(1) + word.charAt(0) + "ay"; return result; return result; }

PigLatinTranslator // // // Determines if the specified word begins with a vowel. // Determines if the specified word begins with a vowel. // // private static boolean beginsWithVowel (String word) private static boolean beginsWithVowel (String word) { String vowels = "aeiou"; String vowels = "aeiou"; char letter = word.charAt(0); char letter = word.charAt(0); return (vowels.indexOf(letter) != -1); return (vowels.indexOf(letter) != -1); }