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.

Slides:



Advertisements
Similar presentations
Chapter 8: Designing Classes
Advertisements

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 9 – Inheritance Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.
Chapter 2 – An Introduction to Objects and Classes Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.
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,
Chapter 9 Designing Classes. Chapter Goals To learn how to choose appropriate classes to implement To understand the concepts of cohesion and coupling.
Chapter Goals To learn how to choose appropriate classes to implement
GUIs Part 4 CS221 – 4/17/09. Professional Assignments Assignment #2 – Download and install Visual Studio 2008 Professional Trial Software –
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.
Chapter 3 Using Classes and Objects. 2 Creating Objects  A variable holds either a primitive type or a reference to an object  A class name can be used.
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.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Chapter 8 – Designing Classes.
Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. Chapter Seven: Arrays and Array Lists.
Chapter 2: Objects and Primitive Data Classes and Objects String, Random, Math, NumberFormat, DecimalFormat and Wrapper Classes.
Chapter 4 – Fundamental Data Types. Chapter Goals To understand integer and floating-point numbers To understand integer and floating-point numbers To.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Chapter 8 – Designing Classes.
1 The String Class Every character string is an object in Java, defined by the String class Every string literal, delimited by double quotation marks,
Chapter 7 Objects and Classes 1 Fall 2012 CS2302: Programming Principles.
ICOM 4015: Advanced Programming Lecture 8 Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Reading: Chapter Eight:
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.
Static. Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. A static method does not operate on an object double dY.
Methods in Java. Program Modules in Java  Java programs are written by combining new methods and classes with predefined methods in the Java Application.
Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. Chapter Eight: Designing Classes.
Lecture Notes – Classes and Objects (Ch 7-8) Yonglei Tao.
Copyright © 2013 by John Wiley & Sons. All rights reserved. Slides by Rick Giles OBJECT- ORIENTED DESIGN CHAPTER 12.
CSS446 Spring 2014 Nan Wang.  A Java program consists of a collection of classes.  Package is a set of related classes. 2.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Application Programming Interfaces. Java comes with a bunch of classes that are already written. Java comes with a bunch of classes that are already written.
Chapter 8 – Designing Classes Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.
Chapter 9 – Designing Classes Goals Learning to design classes Learning to design classes Preconditions and postconditions for methods Preconditions.
Array length = maximum number of elements in array Usually, array is partially filled Need companion variable to keep track of current size Uniform naming.
1 Chap. 3 Creating Objects The String class Java Class Library (Packages) Math.random() Reading for this Lecture: L&L, 3.1 – 3.6.
Strings. Useful methods of String char charAt(int index) int compareTo(String anotherString) boolean endsWith(String suffix) int indexOf(multiple) int.
Packages. The main feature of OOP is its ability to support the reuse of code: –Extending the classes –Extending interfaces The features in basic form.
Java Software Solutions Foundations of Program Design Sixth Edition by Lewis & Loftus Chapter 3: Using Classes and Objects Coming up: Creating Objects.
Programming in Java (COP 2250) Lecture 7 Chengyong Yang Fall, 2005.
Chapter 7 – Arrays and Array Lists Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. 1.
April 20, 1998CS102-02Lecture 4-1 A Method to the Madness CS Lecture 4-1 Java's Work Horses.
SourceAnatomy1 Java Source Anatomy Barb Ericson Georgia Institute of Technology July 2008.
Week 13 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Chapter 7 Designing Classes. Chapter Goals  To learn how to choose appropriate classes to implement  To understand the concepts of cohesion and coupling.
Fall 2006Adapded from Java Concepts Slides1 Designing Classes Advanced Programming ICOM 4015 Lecture 8 Reading: Java Concepts Chapter 8.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Chapter Three - Implementing Classes.
Chapter 9 – Designing Classes. Chapter Goals Discovering and learning to Design classes Discovering and learning to Design classes Preconditions and postconditions.
Outline Creating Objects The String Class The Random and Math Classes Formatting Output Enumerated Types Wrapper Classes Components and Containers Images.
© 2004 Pearson Addison-Wesley. All rights reserved September 5, 2007 Packages & Random and Math Classes 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.
1 Creating Objects  A variable holds either a primitive type or a reference to an object  A class name can be used as a type to declare an object reference.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Objects and Classes. F OO Programming Concepts F Creating Objects and Object Reference Variables –Differences between primitive data type and object type.
Chapter 8 – Designing Classes
Variables in Java A variable holds either
Chapter 8 – Designing Classes
Chapter 8 Designing Classes
Lecture 7 Designing Classes
Classes, Libraries & Packages
Classes (review): A Java program consists of classes.
Chapter Three - Implementing Classes
Chapter 8 – Designing Classes
Chapter 8 – Designing Classes
Introduction to Computer Science and Object-Oriented Programming
Chapter 8 – Designing Classes
JAVA CLASSES.
Using java libraries CGS3416 spring 2019.
Interfaces,Packages and Threads
Presentation transcript:

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 collection of data that is closely, tightly related Name for a class should be a noun that describes concept Concepts from mathematics: Point Rectangle Ellipse Concepts from real life: BankAccount CashRegister Discovering Classes

Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. The “nouns” of your design Actors (end in -er, -or) – objects do some kinds of work for you: Scanner Random // better name: RandomNumberGenerator Actor Entities representing a collection of information: BankAccount Rectangle Actor Utility classes – no objects, only static methods and constants: Math Discovering Classes

Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. The “verbs” of your design The perform some sort of action on the object / noun: getArea()// Rectangle getBalance()// BankAccount move()// Actor Program starters/testers/printers: only have a static main method Discovering Classes Methods

Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Every method must be in a class A static method is not invoked on an object Why write a method that does not operate on an object ? Common reason: encapsulate some computation that involves only numbers. Numbers aren’t objects, you can’t invoke methods on them. E.g. x.sqrt() can never be legal in Java Static Methods

Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Example: public class Financial { public static double percentOf(double p, double a) { return (p / 100) * a; } // More financial methods can be added here. } Call with class name instead of object: double tax = Financial.percentOf(taxRate, total); Static Methods

Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. If a method manipulates a class that you do not own, you cannot add it to that class (for example the Rectangle class) A static method solves this problem: public class Geometry { public static double area(Rectangle rect) { return rect.getWidth() * rect.getHeight(); } // More geometry methods can be added here. } double rectArea = Geometry.area(myRectangle); main is static — there aren’t any objects yet Static Methods

Suppose Java had no static methods. How would you use the Math.sqrt method for computing the square root of a number x? Answer: Math m = new Math(); y = m.sqrt(x); Self Check 8.12 Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.

Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. A static variable belongs to the class, not to any object of the class: public class BankAccount {... private double balance; private int accountNumber; private static int lastAssignedNumber = 1000; } If lastAssignedNumber was not static, each instance of BankAccount would have its own value of lastAssignedNumber Static Variables

Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. public BankAccount() { // Generates next account number to be assigned lastAssignedNumber++; // Updates the static variable accountNumber = lastAssignedNumber; // Sets the instance variable } Static Variables

Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. A Static Variable and Instance Variables

Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Exception: Static constants, which may be either private or public: public class BankAccount {... public static final double OVERDRAFT_FEE = 5; // Refer to it as BankAccount.OVERDRAFT_FEE } Math.PI; Minimize the use of static variables (static final variables are ok) Static Variables

Name two static variables of the System class. Answer: System.in and System.out. Self Check 8.14 Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.

Harry tells you that he has found a great way to avoid those pesky objects: Put all code into a single class and declare all methods and variables static. Then main can call the other static methods, and all of them can access the static variables. Will Harry’s plan work? Is it a good idea? Answer: Yes, it works. Static methods can access static variables of the same class. But it is a terrible idea. As your programming tasks get more complex, you will want to use objects and classes to organize your programs. Self Check 8.15 Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.

Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. PackagePurposeSample Class java.lang Language support Math java.util Utilities Random java.io Input and output PrintStream java.awt Abstract Windowing Toolkit Color java.applet Applets Applet java.net Networking Socket java.sql Database Access ResultSet javax.swing Swing user interface JButton omg.w3c.dom Document Object Model for XML documents Document Packages Package: Set of related classes Important packages in the Java library:

Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. To put classes in a package, you must place a line package packageName ; as the first instruction in the source file containing the classes Package name consists of one or more identifiers separated by periods Organizing Related Classes into Packages

Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. For example, to put the Financial class introduced into a package named com.horstmann.bigjava, the Financial.java file must start as follows: package com.horstmann.bigjava; public class Financial {... } Default package has no name, no package statement Organizing Related Classes into Packages

Syntax 8.2 Package Specification Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.

Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Can always use class without importing: java.util.Scanner in = new java.util.Scanner(System.in); Tedious to use fully qualified name Import lets you use shorter class name: import java.util.Scanner;... Scanner in = new Scanner(System.in) Can import all classes in a package: import java.util.*; Never need to import java.lang You don’t need to import other classes in the same package Importing Packages

Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Use packages to avoid name clashes java.util.Timer vs. javax.swing.Timer Package names should be unambiguous Recommendation: start with reversed domain name: com.horstmann.bigjava edu.sjsu.cs.walters : for Britney Walters’ classes ( ) Path name should match package name: com/horstmann/bigjava/Financial.java Package Names

Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Base directory: holds your program's Files Path name, relative to base directory, must match package name: com/horstmann/bigjava/Financial.java Package and Source Files