Static Class Members March 29, 2006 ComS 207: Programming I (in Java)

Slides:



Advertisements
Similar presentations
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Advertisements

CSCI 1100/ , 6.2, 6.4 April 12, 15, 17.
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.
Object Oriented Design and UML
1 Recursion  Recursion is a fundamental programming technique that can provide an elegant solution certain kinds of problems  Chapter 11 of the book.
ECE122 L16: Class Relationships April 3, 2007 ECE 122 Engineering Problem Solving with Java Lecture 16 Class Relationships.
Chapter 10 Recursion. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Explain the underlying concepts of recursion.
ELC 310 Day 24. © 2004 Pearson Addison-Wesley. All rights reserved11-2 Agenda Questions? Problem set 5 Parts A Corrected  Good results Problem set 5.
Static Class Members Wrapper Classes Autoboxing Unboxing.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Chapter 11 Recursion. © 2004 Pearson Addison-Wesley. All rights reserved11-2 Recursion Recursion is a fundamental programming technique that can provide.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
1 Object Oriented Design and UML Software Development Activities Object Oriented Design Unified Modeling Language (UML) Reading for this Lecture: L&L 6.1.
© 2004 Pearson Addison-Wesley. All rights reserved October 27, 2006 Recursion (part 2) ComS 207: Programming I (in Java) Iowa State University, FALL 2006.
6-1 Object-Oriented Design Today we focuses on: –the this reference (Chapter 7) –the static modifier (Chapter 7) –method overloading (Chapter 7)
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Chapter 8 Recursion Modified.
Chapter 4 Recursion. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Explain the underlying concepts of recursion.
11-1 Recursive Thinking A recursive definition is one which uses the word or concept being defined in the definition itself When defining an English word,
Chapter 6 Object-Oriented Design. © 2004 Pearson Addison-Wesley. All rights reserved6-2 Object-Oriented Design Now we can extend our discussion of the.
Lecture 7 b Recursion is a fundamental programming technique that can provide an elegant solution to certain kinds of problems b Today: thinking in a recursive.
Static class members.
CSCI 1100/1202 April 1-3, Program Development The creation of software involves four basic activities: –establishing the requirements –creating.
© 2004 Pearson Addison-Wesley. All rights reserved September 14, 2007 Anatomy of a Method ComS 207: Programming I (in Java) Iowa State University, FALL.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Designing Classes CS239 – Jan 26, Key points from yesterday’s lab  Enumerated types are abstract data types that define a set of values.  They.
1 Recursion Recursion is a powerful programming technique that provides elegant solutions to certain problems. Chapter 11 focuses on explaining the underlying.
1 Object-Oriented Design Now we can extend our discussion of the design of classes and objects Chapter 6 focuses on: software development activities determining.
1 Program Development  The creation of software involves four basic activities: establishing the requirements creating a design implementing the code.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 12 Recursion.
© 2004 Pearson Addison-Wesley. All rights reserved October 31, 2007 Static Class Members ComS 207: Programming I (in Java) Iowa State University, FALL.
Recursion Chapter 17 Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013.
© 2004 Pearson Addison-Wesley. All rights reserved January 23, 2006 Creating Objects & String Class ComS 207: Programming I (in Java) Iowa State University,
Object-Oriented Design Chapter 7 1. Objectives You will be able to Use the this reference in a Java program. Use the static modifier for member variables.
© 2004 Pearson Addison-Wesley. All rights reserved November 2, 2007 Class Relationships ComS 207: Programming I (in Java) Iowa State University, FALL 2007.
Chapter 5: Enhancing Classes
CprE 185: Intro to Problem Solving (using C)
Abdulmotaleb El Saddik University of Ottawa
Creating Objects & String Class
Chapter 4: Writing Classes
Recursive Thinking Chapter 9 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences.
Chapter 8: Recursion Java Software Solutions
Java Software Structures: John Lewis & Joseph Chase
Recursive Thinking Chapter 9 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences.
Recursive Definitions
Chapter 12 Recursion (methods calling themselves)
Inheritance April 7, 2006 ComS 207: Programming I (in Java)
Chapter 5 – Writing Classes
Recursion (part 2) October 26, 2007 ComS 207: Programming I (in Java)
Chapter 3: Program Statements
Chapter 8: Recursion Java Software Solutions
Chapter 4 Writing Classes.
Recursion (part 1) October 24, 2007 ComS 207: Programming I (in Java)
Overriding Methods & Class Hierarchies
Static is one of the modifiers that determine variable and method characteristics. The static modifier associates a variable or method with its class.
Chapter 11 Recursion.
Chapter 8: Recursion Java Software Solutions
11 Recursion Software Solutions Lewis & Loftus java 5TH EDITION
Encapsulation September 13, 2006 ComS 207: Programming I (in Java)
Comparing Data & the ‘switch’ Statement
Outline Software Development Activities
Recursion (part 2) March 22, 2006 ComS 207: Programming I (in Java)
Comparing Data & the ‘switch’ Statement
Java Software Solutions Foundations of Program Design Sixth Edition
Recursion (part 1) October 25, 2006 ComS 207: Programming I (in Java)
Presentation transcript:

Static Class Members March 29, 2006 ComS 207: Programming I (in Java) Iowa State University, SPRING 2006 Instructor: Alexander Stoytchev © 2004 Pearson Addison-Wesley. All rights reserved

Go Over Midterm2 Solutions © 2004 Pearson Addison-Wesley. All rights reserved

HW 8 is out © 2004 Pearson Addison-Wesley. All rights reserved

The Eight Queens Problem © 2004 Pearson Addison-Wesley. All rights reserved [http://home.earthlink.net/~barnold2002/Acgnj/Csig9908.htm]

Quick review of last lecture © 2004 Pearson Addison-Wesley. All rights reserved

Recursive Programming Consider the problem of computing the sum of all the numbers between 1 and any positive integer N This problem can be recursively defined as: © 2004 Pearson Addison-Wesley. All rights reserved

Recursive Programming // This method returns the sum of 1 to num public int sum (int num) { int result; if (num == 1) result = 1; else result = num + sum (n-1); return result; } © 2004 Pearson Addison-Wesley. All rights reserved

Recursive Programming main sum sum(3) sum(1) sum(2) result = 1 result = 3 result = 6 © 2004 Pearson Addison-Wesley. All rights reserved

Recursive Definitions Consider the following list of numbers: 24, 88, 40, 37 Such a list can be defined as follows: A LIST is a: number or a: number comma LIST That is, a LIST is defined to be a single number, or a number followed by a comma followed by a LIST The concept of a LIST is used to define itself © 2004 Pearson Addison-Wesley. All rights reserved

Recursive Definitions The recursive part of the LIST definition is used several times, terminating with the non-recursive part: number comma LIST 24 , 88, 40, 37 88 , 40, 37 40 , 37 number 37 © 2004 Pearson Addison-Wesley. All rights reserved

Towers of Hanoi The Towers of Hanoi is a puzzle made up of three vertical pegs and several disks that slide on the pegs The disks are of varying size, initially placed on one peg with the largest disk on the bottom with increasingly smaller ones on top The goal is to move all of the disks from one peg to another under the following rules: We can move only one disk at a time We cannot move a larger disk on top of a smaller one © 2004 Pearson Addison-Wesley. All rights reserved

Original Configuration Towers of Hanoi Original Configuration Move 1 Move 2 Move 3 © 2004 Pearson Addison-Wesley. All rights reserved

Towers of Hanoi Move 4 Move 5 Move 6 Move 7 (done) © 2004 Pearson Addison-Wesley. All rights reserved

Animation of the Towers of Hanoi http://www.cs.concordia.ca/~twang/ WangApr01/RootWang.html © 2004 Pearson Addison-Wesley. All rights reserved

Object-Oriented Design Chapter 6 Object-Oriented Design

Static Class Members Recall that a static method is one that can be invoked through its class name For example, the methods of the Math class are static: result = Math.sqrt(25) Variables can be static as well Determining if a method or variable should be static is an important design decision © 2004 Pearson Addison-Wesley. All rights reserved

The static Modifier We declare static methods and variables using the static modifier It associates the method or variable with the class rather than with an object of that class Static methods are sometimes called class methods and static variables are sometimes called class variables Let's carefully consider the implications of each © 2004 Pearson Addison-Wesley. All rights reserved

Static Variables Normally, each object has its own data space, but if a variable is declared as static, only one copy of the variable exists private static float price; Memory space for a static variable is created when the class is first referenced All objects instantiated from the class share its static variables Changing the value of a static variable in one object changes it for all others © 2004 Pearson Addison-Wesley. All rights reserved

Static Methods class Helper { public static int cube (int num) return num * num * num; } Because it is declared as static, the method can be invoked as value = Helper.cube(5); © 2004 Pearson Addison-Wesley. All rights reserved

Static Class Members The order of the modifiers can be interchanged, but by convention visibility modifiers come first Recall that the main method is static – it is invoked by the Java interpreter without creating an object Static methods cannot reference instance variables because instance variables don't exist until an object exists However, a static method can reference static variables or local variables © 2004 Pearson Addison-Wesley. All rights reserved

Static Class Members Static methods and static variables often work together The following example keeps track of how many Slogan objects have been created using a static variable, and makes that information available using a static method See SloganCounter.java (page 294) See Slogan.java (page 295) © 2004 Pearson Addison-Wesley. All rights reserved

Sections 6.1 & 6.2 (dry stuff that you can read on your own) © 2004 Pearson Addison-Wesley. All rights reserved

Program Development The creation of software involves four basic activities: establishing the requirements creating a design implementing the code testing the implementation These activities are not strictly linear – they overlap and interact © 2004 Pearson Addison-Wesley. All rights reserved

Requirements Software requirements specify the tasks that a program must accomplish what to do, not how to do it Often an initial set of requirements is provided, but they should be critiqued and expanded It is difficult to establish detailed, unambiguous, and complete requirements Careful attention to the requirements can save significant time and expense in the overall project © 2004 Pearson Addison-Wesley. All rights reserved

Design A software design specifies how a program will accomplish its requirements That is, a software design determines: how the solution can be broken down into manageable pieces what each piece will do An object-oriented design determines which classes and objects are needed, and specifies how they will interact Low level design details include how individual methods will accomplish their tasks © 2004 Pearson Addison-Wesley. All rights reserved

Implementation Implementation is the process of translating a design into source code Novice programmers often think that writing code is the heart of software development, but actually it should be the least creative step Almost all important decisions are made during requirements and design stages Implementation should focus on coding details, including style guidelines and documentation © 2004 Pearson Addison-Wesley. All rights reserved

Testing Testing attempts to ensure that the program will solve the intended problem under all the constraints specified in the requirements A program should be thoroughly tested with the goal of finding errors Debugging is the process of determining the cause of a problem and fixing it We revisit the details of the testing process later in this chapter © 2004 Pearson Addison-Wesley. All rights reserved

Identifying Classes and Objects The core activity of object-oriented design is determining the classes and objects that will make up the solution The classes may be part of a class library, reused from a previous project, or newly written One way to identify potential classes is to identify the objects discussed in the requirements Objects are generally nouns, and the services that an object provides are generally verbs © 2004 Pearson Addison-Wesley. All rights reserved

Identifying Classes and Objects A partial requirements document: The user must be allowed to specify each product by its primary characteristics, including its name and product number. If the bar code does not match the product, then an error should be generated to the message window and entered into the error log. The summary report of all transactions must be structured as specified in section 7.A. Of course, not all nouns will correspond to a class or object in the final solution © 2004 Pearson Addison-Wesley. All rights reserved

Identifying Classes and Objects Remember that a class represents a group (classification) of objects with the same behaviors Generally, classes that represent objects should be given names that are singular nouns Examples: Coin, Student, Message A class represents the concept of one such object We are free to instantiate as many of each object as needed © 2004 Pearson Addison-Wesley. All rights reserved

Identifying Classes and Objects Sometimes it is challenging to decide whether something should be represented as a class For example, should an employee's address be represented as a set of instance variables or as an Address object The more you examine the problem and its details the more clear these issues become When a class becomes too complex, it often should be decomposed into multiple smaller classes to distribute the responsibilities © 2004 Pearson Addison-Wesley. All rights reserved

Identifying Classes and Objects We want to define classes with the proper amount of detail For example, it may be unnecessary to create separate classes for each type of appliance in a house It may be sufficient to define a more general Appliance class with appropriate instance data It all depends on the details of the problem being solved © 2004 Pearson Addison-Wesley. All rights reserved

Identifying Classes and Objects Part of identifying the classes we need is the process of assigning responsibilities to each class Every activity that a program must accomplish must be represented by one or more methods in one or more classes We generally use verbs for the names of methods In early stages it is not necessary to determine every method of every class – begin with primary responsibilities and evolve the design © 2004 Pearson Addison-Wesley. All rights reserved

THE END © 2004 Pearson Addison-Wesley. All rights reserved