Вы находитесь на странице: 1из 11

MSTU4031: Object

Oriented Design
OO Requirements
► Identify objects that are necessary for
the context of a desired software
application
► A class is the data AND the code!
► Methods affect change upon the data

MSTU4031 2
Design Example
► Your are given the following request:

For the first phase of the library system your are to


collect information about books. What information
or attributes describe a book? Let’s list them:

MSTU4031 3
Requirements Analysis

<<identify nouns>>
For the first phase of the library system your are to
collect information about books. What information
or attributes describe a book? Let’s list them:

MSTU4031 4
What is a class?
Object – An instance of a class

Class – The program; the code. The data, and code to act on that data

How are classes defined? Through analysis.


Ask yourself what is the data? How should it be grouped together?
Class

Data

Operations

MSTU4031 5
Analysis Outcome

Book Critique RoadMap


title : String 2. What is the data?
author: String
genre : String 3. What are the
isbn: String methods to alter
publisher : String the data?
pubDate : date
4. Consider input
cost : float
… and output

MSTU4031 6
Model

Library Book
… title : String
author: String

MSTU4031 7
► public class BookMain
► { // open BookMain class
► /**
► Tests the methods of the Book class
► @param args not used
► */
► public static void main (String[] args)

► { // open main method


Object Type

► //creating an object of type book
► Book hardcover= new Book();

► …
► …
Object reference Create a ‘new’ one
► …

MSTU4031 8
► public class BookMain
► { // open BookMain class
► /**
► Tests the methods of the Book class
► @param args not used
► */
► public static void main (String[] args)

► { // open main method

► //creating an object of type book Constructor


► Book hardcover= new Book();

► //calls constructor w/parameters title, pages, author and setting those


parameters
► Book paperback = new Book ("Democracy and Eduation", 378, "John
Dewey");

► //Setting the author of the hardcover book


► hardcover.setAuthor("Henry David Thoreau");
Object Reference
Method in Class Book Parameter

MSTU4031 9


public class Book
{
/**
Constructs the initial book without a title, author, or page numbers.
Default constructor, entry way into the program
*/
public Book ()
{
Constructor Construtor’s Parameters
}

/**
Constructs the initial book with a title, author, and page numbers.
@param inputtitle
@param inputauthor
@param inputpages
*/
public Book (String inputtitle, int inputpages, String inputauthor)
{
booktitle = inputtitle;
bookpages = inputpages;
bookauthor = inputauthor;
} Parameter passed to Method

/**
Sets book title
@param inputtitle the title of the book
*/
public void setBookTitle (String inputtitle)
{
Return Type booktitle = inputtitle;

} MSTU4031 10
Method Name Local Private Variable
References
► JDK online documentation.
► Java Forums

MSTU4031 11

Вам также может понравиться