Coding-Style Conventions Helper Methods Encapsulation (with Instance Variables and Local Variables) Algorithmic Design with OOP Top-down Design Stubs Merging.

Slides:



Advertisements
Similar presentations
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 2 Getting Started with Java.
Advertisements

Chapter 4 Loops Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved
Variables in C Amir Haider Lecturer.
Character Arrays (Single-Dimensional Arrays) A char data type is needed to hold a single character. To store a string we have to use a single-dimensional.
Chapter 16 Graphical User Interfaces
Final and Abstract Classes
Basic Java Constructs and Data Types – Nuts and Bolts
1 Inheritance Classes and Subclasses Or Extending a Class.
Copyright © 2002 Pearson Education, Inc. Slide 1.
Copyright © 2003 Pearson Education, Inc. Slide 1.
Chapter 6 Structures and Classes. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-2 Learning Objectives Structures Structure types Structures.
Chapter 11 Introduction to Programming in C
Writing Pseudocode And Making a Flow Chart A Number Guessing Game
Tutorial 9 – Creating On-Screen Forms Using Advanced Table Techniques
Lecture 10 Flow of Control: Loops (Part 2) COMP1681 / SE15 Introduction to Programming.
1 Linked Lists III Template Chapter 3. 2 Objectives You will be able to: Write a generic list class as a C++ template. Use the template in a test program.
1 Designing Hash Tables Sections 5.3, 5.4, Designing a hash table 1.Hash function: establishing a key with an indexed location in a hash table.
Chapter 7 Object-Oriented Programming – Additional Details Object Creation - a Detailed Analysis Assigning a Reference Testing Objects For Equality Passing.
ITP © Ron Poet Lecture 4 1 Program Development. ITP © Ron Poet Lecture 4 2 Preparation Cannot just start programming, must prepare first. Decide what.
1 CSC 221: Computer Programming I Fall 2006 interacting objects modular design: dot races constants, static fields cascading if-else, logical operators.
Cs205: engineering software university of virginia fall 2006 Specifying Procedures David Evans
1.A computer game is an example of A.system software; B.a compiler; C.application software; D.hardware; E.none of the above. 2.JVM stands for: A.Java Virtual.
1 User-Defined Classes The String class is a pre-defined class that is provided by the Java Designer. Sometimes programmers would like to create their.
1 Review Quisioner Kendala: Kurang paham materi. Praktikum Pengaruh teman.
Lecture 6: Software Design (Part I)
Lilian Blot VARIABLE SCOPE EXCEPTIONS FINAL WORD Final Lecture Spring 2014 TPOP 1.
Purpose : To convert this string to a new character array. Return Type : char[ ] Parameters : none Declaration : public char[ ] toCharArray() Returns.
Chapter 5 Loops Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.
Chapter 11 Component-Level Design
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Building Java Programs Chapter 14
Chapter 8 Improving the User Interface
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 3 Loops.
Chapter 2: Using Objects Part 1. To learn about variables To understand the concepts of classes and objects To be able to call methods To learn about.
 2005 Pearson Education, Inc. All rights reserved Introduction.
Enhancing classes Visibility modifiers and encapsulation revisited
Object Oriented Programming.  OOP Basic Principles  C++ Classes  September 2004  John Edgar 22.
CS 201 Functions Debzani Deb.
Options for User Input Options for getting information from the user –Write event-driven code Con: requires a significant amount of new code to set-up.
Week 4-5 Java Programming. Loops What is a loop? Loop is code that repeats itself a certain number of times There are two types of loops: For loop Used.
Chapter 7 Designing Classes. Class Design When we are developing a piece of software, we want to design the software We don’t want to just sit down and.
Using Classes Object-Oriented Programming Using C++ Second Edition 5.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved COS240 O-O Languages AUBG,
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
CSC 142 B 1 CSC 142 Java objects: a first view [Reading: chapters 1 & 2]
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
An Introduction to Java Chapter 11 Object-Oriented Application Development: Part I.
Question of the Day  On a game show you’re given the choice of three doors: Behind one door is a car; behind the others, goats. After you pick a door,
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 5: Software Design & Testing; Revision Session.
CPS120: Introduction to Computer Science Lecture 14 Functions.
Java™ How to Program, 10/e © Copyright by Pearson Education, Inc. All Rights Reserved.
More About Objects and Methods Chapter 5. Outline Programming with Methods Static Methods and Static Variables Designing Methods Overloading Constructors.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
CSC Programming I Lecture 6 September 4, 2002.
CS0007: Introduction to Computer Programming Classes: Documentation, Method Overloading, Scope, Packages, and “Finding the Classes”
“Education is a Treasure that follows you everywhere.” – Chines Proverb Methods and Functions.
1 Coding-Style Conventions Helper Methods Encapsulation (with Instance Variables and Local Variables) Algorithmic Design with OOP Top-down Design Stubs.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
Chapter 4Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapters 4 and 5: Excerpts l Class and Method Definitions l Information.
Chapter 5Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 5 l Programming with Methods l Polymorphism l Constructors.
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
CMSC 104, Version 8/061L09VariablesInC.ppt Variables in C Topics Naming Variables Declaring Variables Using Variables The Assignment Statement Reading.
Functions in C++ Top Down Design with Functions. Top-down Design Big picture first broken down into smaller pieces.
Unified Modeling Language (UML)
Classes (Part 1) Lecture 3
More Sophisticated Behavior
Chapter 7 Top-Down Development
Subroutines Idea: useful code can be saved and re-used, with different data values Example: Our function to find the largest element of an array might.
Introduction to Classes and Objects
Presentation transcript:

Coding-Style Conventions Helper Methods Encapsulation (with Instance Variables and Local Variables) Algorithmic Design with OOP Top-down Design Stubs Merging Driver Method into Driven Class Accessing Instance Variables Without Using this Chapter 8 – Software Engineering 1

/********************************************************* * StudentDriver.java * Dean & Dean * * This class acts as a driver for the Student class. *********************************************************/ public class StudentDriver { public static void main(String[] args) { Student s1; // first student Student s2; // second student s1 = new Student(); s1.setFirst("Adeeb"); s1.setLast("Jarrah"); s2 = new Student("Chun", "HeeJoo"); s2.printFullName(); } // end main } // end class StudentDriver Coding-Style Conventions 2

/********************************************************* * Student.java * Dean & Dean * * This class handles processing of a student's name. *********************************************************/ import java.util.Scanner; public class Student { private String first = ""; // student's first name private String last = ""; // student's last name //******************************************************* public Student() { } // This constructor verifies that each passed-in name starts with // an uppercase letter and contains lowercase letters thereafter. public Student(String first, String last) { setFirst(first); setLast(last); } Coding-Style Conventions 3

//******************************************************* // This method verifies that first starts with an uppercase // letter and contains lowercase letters thereafter. public void setFirst(String first) { // [A-Z][a-z]* is a regular expression. // See API Pattern class. if (first.matches("[A-Z][a-z]*")) { this.first = first; } else { System.out.println(first + " is an invalid name.\n" + "Names must start with an uppercase letter and have" + " lowercase letters thereafter."); } } // end setFirst Coding-Style Conventions 4

// This method verifies that last starts with an uppercase // letter and contains lowercase letters thereafter. public void setLast(String last) { // [A-Z][a-z]* is a regular expression. See API Pattern class. if (last.matches("[A-Z][a-z]*")) { this.last = last; } else { System.out.println(last + " is an invalid name.\n" + "Names must start with an uppercase letter and have" + " lowercase letters thereafter."); } } // end setLast //******************************************************* // Print the student's first and last names. public void printFullName() { System.out.println(this.first + " " + this.last); } // end printFullName } // end class Student Coding-Style Conventions 5

As with all examples discussed so far, methods normally use the public access modifier. public methods are considered to be part of the class's interface. Sometimes, you'll want to create a method that should not be part of the interface; instead it just supports the operation of other methods within its own class. Those methods are called helper methods and they should use the private access modifier. Helper Methods 7

Suppose you're asked to write a program that handles order entries for sports-uniform shirts. For each shirt order, the program should prompt the user for a shirt's primary color and its trim color. For each color selection, the program should perform the same input validation. It should verify that the entered color is one of three values – w, r, or y, for white, red, or yellow. That input validation code is non-trivial. It's in charge of: Prompting the user for a color entry. Checking whether the entry is valid. Repeating the prompt if the entry is invalid. Converting the single-character color entry to a full-word color value. Rather than having the complete input validation code appear every time color selection is needed, you should use a helper method to implement the color-selection task. With a helper method, the color-selection code appears only once in the program, and whenever color selection is needed, the color- selection method is called. Helper Methods 8

import java.util.Scanner; public class Shirt { private String name; // person's name private String primary; // shirt's primary color private String trim; // shirt's trim color //********************************************************* public Shirt() { Scanner stdIn = new Scanner(System.in); System.out.print("Enter person's name: "); this.name = stdIn.nextLine(); this.primary = selectColor("primary"); this.trim = selectColor("trim"); } // end constructor //********************************************************* public void display() { System.out.println(this.name + "'s shirt:\n" + this.primary + " with " + this.trim + " trim"); } // end display //********************************************************* Helper Methods No need for a reference variable dot prefix here. 9

// Helper method prompts for and inputs user's selection private String selectColor(String colorType) { Scanner stdIn = new Scanner(System.in); String color; // chosen color, first a letter, then a word do { System.out.print("Enter shirt's " + colorType + " color (w, r, y): "); color = stdIn.nextLine(); } while (!color.equals("w") && !color.equals("r") && !color.equals("y")); switch (color.charAt(0)) { case 'w': color = "white"; break; case 'r': color = "red"; break; case 'y': color = "yellow"; } // end switch return color; } // end selectColor } // end class Shirt Helper Methods Use the private access modifier for a helper method. 10

In the Shirt constructor from two slides ago, note how we call the selectColor method without a reference variable prefix: this.primary = selectColor("primary"); If you're in a constructor (or an instance method, for that matter), if you call another method that's in the same class, the reference variable dot prefix is unnecessary. Benefits of using helper methods: 1. By moving some of the details from big public methods into helper methods, you enable the public methods to be more streamlined (smaller). And that leads to improved program readability. 2. Using helper methods can reduce code redundancy. For example, in the Shirt program, we called the selectColor method twice, but implemented it only once. Helper Methods 11

Encapsulation A program exhibits encapsulation if its data is hidden; i.e., if its data is difficult to access from the outside world. Why is encapsulation a good thing? Since the outside world isn't able to directly access the encapsulated data, it's more difficult for the outside world to mess things up. How is encapsulation implemented? Break a big problem into separate classes where each class holds a set of encapsulated data (those data items are called instance variables, and they should use the private access modifier). Break a class's tasks into separate methods where each method holds a set of encapsulated data (those data items are called local variables). 12

Encapsulation Declaring data as instance variables within a class is one form of encapsulation, and declaring data as local variables within a method is another form of encapsulation. Which is the stronger (more hidden) form of encapsulation? An instance variable may be shared by all the methods in the class, whereas a local variable is local to one method, so a local variable is more encapsulated. Thus, in the interest of encapsulation, you should use local variables, as opposed to instance variables, whenever possible. Declare a variable locally within a method: If the variable needs to persist (remain alive) only for the duration of that particular method. Declare a variable as an instance variable: If the variable stores a fundamental attribute of the class's objects. If the variable needs to persist for the duration of the class's objects (i.e., it needs to persist longer than the duration of one particular method execution). important 13

Algorithmic Design with OOP Given a problem description. What steps should you use to implement its solution? Decide on the classes/objects that are needed. For each class, decide on its instance variables. For each class, decide on its public methods (= the interface). For each method, implement it using top-down design. Use top-down design for your methods: In writing a public method (a "top" method), if the method is fairly involved and can be broken into subtasks, then have the public method call private helper methods to do the subtask work. Finish writing your public methods before starting to write your private helper methods. 14

Algorithmic Design with OOP Why is top-down design beneficial? The programmer doesn't have to initially worry about the details of each subtask's implementation. That way, the programmer can focus on the big picture first. And focusing on the big picture is crucial because if the big picture is wrong, the whole thing is wrong. Why is top-down design also called stepwise refinement? 15

Algorithmic Design with OOP Example: Write a top-down outline for a method that calculates an estimated cost for a building. Public interface method: pub vd calcBldgCosts() {... calcBathrooms(); calcOtherRooms();... } Private helper method: pri vd calcBathrooms() {... calcFloorSpace();... } Private helper method: pri vd calcOtherRooms() {... calcFloorSpace();... } If two or more methods perform the same subtask, avoid redundant code by having those methods call a shared private helper method that performs the subtask. 16

Algorithmic Design with OOP Algorithmic Design Example: Implement the functionality of a square. The square should be able to: Set/assign its width. Calculate and return its area. Draw itself with asterisks using either an asterisks border or a solid pattern of asterisks. Each time the square is drawn, the user is prompted as to whether he/she wants to draw with a border or with a solid pattern. 17

Stubs After writing your entire program in pencil on paper, enter your program on a PC using stubs for your subtask methods. A stub is a dummy method like this: public void drawSides() { System.out.println("In drawSides"); } Benefit of using stubs: If the program is initially entered with stubs, then the programmer will be able to more easily get an initial successful compilation (and that leads to a "warm fuzzy" feeling). Debugging is easier. After the program is successfully compiled and run with stubs, then the programmer replaces the stubs with actual code one method at a time. Ideally, after each method is replaced, it's thoroughly debugged. That way, if a bug appears, it should be easy to find since you know it's probably in the newly replaced method. 19

Merging Driver Method into Driven Class So far, we've split each of our OOP programs into two separate files - a file that describes an object (e.g., Mouse, Car ) and a driver file (e.g., MouseDriver, CarDriver ). It's legal to merge the two files into one file, but we've refrained from doing that because using two separate files leads to a more understandable program. We'll continue to use separate files for most programs, but for short programs that don't do much except illustrate a concept, we'll merge the main method into the driven class. Why? It's a matter of convenience - there's one less file to create and there's slightly less code to enter. 20

Merging Driver Method into Driven Class public class Time { private int hours, minutes, seconds; //******************************************************* public Time(int h, int m, int s) { this.hours = h; this.minutes = m; this.seconds = s; } //******************************************************* public void printIt() { System.out.printf("%02d:%02d:%02d\n", hours, minutes, seconds); } // end printIt //******************************************************* public static void main(String[] args) { Time time = new Time(3, 59, 0); time.printIt(); } // end main } // end class Time 21

Accessing Instance Variables Without Using this For a while now, we've used this to access instance variables from within a method. Here's a formal explanation for when to use this: Use this within an instance method or a constructor to access the calling object's instance variables. The this reference distinguishes instance variables from other variables (like local variables and parameters) that happen to have the same name. If there's a parameter or a local variable with the same name as an instance variable, then you must use this in front of the name to refer to the instance variable and use the name by itself to refer to the parameter or local variable. However, if there is no name ambiguity (i.e., there are no same-named variables), you may omit the this prefix when accessing an instance variable. 22

Accessing Instance Variables Without Using this public class MouseShortcut { private int age; // age in days private double weight; // weight in grams //************************************************ public MouseShortcut(int age, double weight) { setAge(age); setWeight(weight); } // end constructor //************************************************ public void setAge(int a) { age = a; } // end setAge OK to omit this before instance variable age because it's different from parameter, a. 23

Accessing Instance Variables Without Using this //************************************************ public void setWeight(double weight) { this.weight = weight; } // end setWeight //************************************************* public void print() { System.out.println("age = " + age + ", weight = " + weight); } // end setWeight } // end class MouseShortcut Not OK to omit this before instance variable, weight, because it's same as parameter, weight. OK to omit this before age and weight instance variables. 24