CSS446 Spring 2014 Nan Wang.  To learn how to choose appropriate classes for a given problem  To understand the concept of cohesion  To minimize dependencies.

Slides:



Advertisements
Similar presentations
New features in JDK 1.5 Can these new and complex features simplify Java development?
Advertisements

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 7.9Arrays of Pointers Arrays can contain pointers For.
Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view class.
Classes and Objects. What is Design? The parts of the software including – what information each part holds – what things each part can do – how the various.
Chapter 8: Designing Classes
Objects, Variables & Methods Java encapsulates data and action modules that access the data in one container, called an object. Object members that.
Designing Classes Chapter 8. Classes Collection of objects Objects are not actions Class names – Nouns Method names – Verbs What Makes a Good Class Represent.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Chapter Three - Implementing Classes.
Chapter 8 Designing Classes. Assignment Chapter 9 Review Exercises (Written)  R8.1 – 8.3, 8.5 – 8.7, 8. 10, 8.11, 8.13, 8.15, 8.19, 8.20 Due Friday,
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
CS 106 Introduction to Computer Science I 11 / 13 / 2006 Instructor: Michael Eckmann.
Chapter Goals To learn how to choose appropriate classes to implement
Chapter 7 Designing Classes Goals  To learn how to choose appropriate classes to implement  To understand the concepts of cohesion and coupling  To.
CSM-Java Programming-I Spring,2005 Class Design Lesson - 4.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
1 Classes Overview l Classes as Types l Declaring Instance Variables l Implementing Methods l Constructors l Accessor and Mutator Methods.
Object Oriented Programming CSC 171 FALL 2001 LECTURE 11.
Class Design CSC 171 FALL 2004 LECTURE 11. READING Read Chapter 7 It’s abstract But it should help with project #1.
ECE122 L6: Problem Definition and Implementation February 15, 2007 ECE 122 Engineering Problem Solving with Java Lecture 6 Problem Definition and Implementation.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
Chapter 5 Black Jack. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 5-2 Chapter Objectives Provide a case study example from problem statement.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
Using Objects Object: an entity in your program that you can manipulate Attributes Methods Method: consists of a sequence of instructions that can access.
1 Chapter 8 Objects and Classes. 2 Motivations After learning the preceding chapters, you are capable of solving many programming problems using selections,
Chapter 4 Objects and Classes.
CS 106 Introduction to Computer Science I 04 / 11 / 2008 Instructor: Michael Eckmann.
Black Jack MVC By Jeremy DiPaolo. Introduction Goal: To create a Black Jack game that takes advantage of the MVC framework. Uses many of the components.
Week 4 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Week 12 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Mason Vail.  A data type definition – “blueprint for objects”  Includes properties and/or methods ◦ “instance” data / methods – specific to one object.
A Singleton Puzzle: What is Printed? 1 public class Elvis { public static final Elvis INSTANCE = new Elvis(); private final int beltSize; private static.
Chapter 7 Objects and Classes 1 Fall 2012 CS2302: Programming Principles.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. A class represents a single concept from the problem domain Name.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Chapter 8 – Designing Classes.
Chapter 7 Arrays. A 12-element array Declaring and Creating Arrays Arrays are objects that occupy memory Created dynamically with keyword new int c[]
CH 8 : Enhancing Classes - Review QUICK REVIEW : A Class defines an entity’s (Object’s) data and the actions or behaviors (Methods) associated with that.
Java Objects and Classes. Overview n Creating objects that belong to the classes in the standard Java library n Creating your own classes.
CSSE501 Object-Oriented Development. Chapter 4: Classes and Methods  Chapters 4 and 5 present two sides of OOP: Chapter 4 discusses the static, compile.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
Designing a Card Game Solitaire--Thirteens. Game.
Java - Classes JPatterson. What is a class? public class _Alpha { public static void main(String [] args) { } You have been using classes all year – you.
Summing Up Object Oriented Design. Four Major Components: Abstraction modeling real-life entities by essential information only Encapsulation clustering.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. A class represents a single concept from the problem domain, or a.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
C Lecture Notes 1 Structures & Unions. C Lecture Notes Introduction Structures –Collections of related variables (aggregates) under one name Can.
IT108 Objects and Classes Part I George Mason University Revised 4/3/2012.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 6 Objects and Classes.
Java Classes Chapter 1. 2 Chapter Contents Objects and Classes Using Methods in a Java Class References and Aliases Arguments and Parameters Defining.
Chapter 9 – Designing Classes Goals Learning to design classes Learning to design classes Preconditions and postconditions for methods Preconditions.
Topic 7 Interfaces I once attended a Java user group meeting where James Gosling (one of Java's creators) was the featured speaker. During the memorable.
Object Oriented Programming. OOP  The fundamental idea behind object-oriented programming is:  The real world consists of objects. Computer programs.
 2000 Prentice Hall, Inc. All rights reserved Introduction Structures –Collections of related variables (aggregates) under one name Can contain.
StructureStructure. Outline Introduction Structure Definitions Initializing Structures Accessing Members of Structures Using Structures with Functions.
1 Enums (Chapter 4) To enumerate is: to name things one after another in a list Java has a type, called an enum, where a programmer specifies a finite.
Chapter 7 Designing Classes. Chapter Goals  To learn how to choose appropriate classes to implement  To understand the concepts of cohesion and coupling.
Chapter 9 – Designing Classes. Chapter Goals Discovering and learning to Design classes Discovering and learning to Design classes Preconditions and postconditions.
PRG 420 Entire Course FOR MORE CLASSES VISIT PRG 420 Week 1 Individual Assignment Hello world PRG 420 Week 2 Individual Assignment.
XuanTung Hoang 1 Something to discuss Feedbacks on Midterm Exam Final exam and term project  Final exam requires solid knowledge/skills in Java  Be more.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Lecture 3: Introduction to Object and Classes Michael Hsu CSULA.
Lecture 3: Introduction to Object and Classes Michael Hsu CSULA.
CSSE 220 Day 17 Introduction to your Vector Graphics project
PRG 420 NERD Marvelous Learning / prg420nerd.com
Lecture 7 Designing Classes
PRG 420 Education for Service-- prg420.com. PRG 420 Week 1 Coding a Simple “Hello, world!” Java™ Program(New Syllabus) For more course tutorials visit.
Chapter Three - Implementing Classes
Outline Writing Classes Copyright © 2012 Pearson Education, Inc.
JAVA CLASSES.
Dr. R Z Khan Handout-3 Classes
Introduction to Computer Science and Object-Oriented Programming
Presentation transcript:

CSS446 Spring 2014 Nan Wang

 To learn how to choose appropriate classes for a given problem  To understand the concept of cohesion  To minimize dependencies and side effects  To learn how to find a data representation for a class  To understand static methods and variables  To learn about packages 2

 A class should represent a single concept from a problem domain.  E.g.  Point  Rectangle  Ellipse ◦ Other classes are abstractions of real-life entities:  BankAccount  CashRegister  Card Game 3

 The name for such a class should be a noun that describes the concept. In fact, a simple rule of thumb for getting started with class design is to look for nouns in the problem description.  Card  DeckOfCards 4

 Objects of an actor class carry out certain tasks for you  A Scanner Object: scans a stream for numbers and strings.  A Random object: generates random numbers. It is a good idea to choose class names for actors that end in “-er” or “-or”. (A better name for the Random class might be RandomNumberGenerator.)  E.g.  Random myRandomNumberGenerator = new Random(); 5

 Utility class has no objects, but it contains a collection of related static methods and constants.  E.g. ◦ Math Class ◦ Arrays Class  import java.util.Arrays 6

 Their sole purpose is to start a program.  DeckOfCardsTest.java 7

 What is a simple rule of thumb for finding classes?  Your job is to write a program that plays chess. Might ChessBoard be an appropriate class? How about MovePiece?  What classes will be in your card game? 8

 Providing a Cohesive public interface  A class should represent a single concept. All interface features should be closely related to the single concept that the class represents.  Such a public interface is said to be cohesive. 9 The members of a cohesive team have a common goal.

 What methods are needed in Card Class  What are in DeckOfCards? 10

11

12

 A mutator method changes the state of an object. ◦ setFace()  An accessor method asks an object to compute a result, without changing the state. ◦ getFace() 13

 Designed to have only accessor methods and no mutator methods at all.  E.g String Class ◦ String name = "John Q. Public"; ◦ String uppercased = name.toUpperCase(); // name is not changed  An immutable class has a major advantage: It is safe to give out references to its objects freely. If no method can change the object’s value, then no code can modify the object at an unexpected time. 14

15

16 In Java, a method can never change the contents of a variable that is passed to a method.

 A value properly belongs to a class, not to any object of the class.  We want to assign bank account numbers sequentially. That is, we want the bank account constructor to construct the first account with number 1001, the next with number 1002, and so on. To solve this problem, we need to have a single value of lastAssigned­Number that is a property of the class, not any object of the class. 17

but all objects share a single copy of the lastAssignedNumber variable  Every BankAccount object has its own balance and accountNumber instance variables, but all objects share a single copy of the lastAssignedNumber variable. 18

19 private static final String[] faces = { "Ace", "Deuce", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King" }; private static final String[] suits = { "Hearts", "Diamonds", "Clubs", "Spades" };

20 private static final int ONEPAIR = 2; private static final int TWOPAIR = 4; private static final int THREEKIND = 6; private static final int STRAIGHT = 8; private static final int FULLHOUSE = 10; private static final int FLUSH = 12; private static final int FOURKIND = 14; private static final int STRAIGHTFLUSH = 16;

 A class defines methods that are not invoked on an object.  the sqrt method in the Math class. Because numbers aren’t objects, you can’t invoke methods on them. For example, if x is a number, then the call x.sqrt() is not legal in Java. Therefore, the Math class provides a static method that is invoked as Math.sqrt(x). No object of the Math class is constructed. The Math qualifier simply tells the compiler where to find the sqrt method. 21

22

23

24 public static void main( String[] args ) { DeckOfCards myDeckOfCards = new DeckOfCards(); myDeckOfCards.shuffle(); // place Cards in random order Card[] hand1 = new Card[ 5 ]; // store first hand Card[] hand2 = new Card[ 5 ]; // store second hand … }

25

26  Due Date: 1/23/14 (Next Thursday)  Submit your zipped package to  Grading: ◦ 100 points for implementation of simple rules  E.g. if there are same pairs in both hands, they tie with out continuing comparison of next card. ◦ 50 bonus points for implementation of complete rules.  Do not tell me you do not know how to play card game, I do not believe it

27