Lecture 7 Designing Classes

Slides:



Advertisements
Similar presentations
Chapter 8: Designing Classes
Advertisements

Lecture 9: More on objects, classes, strings discuss hw3 assign hw4 default values for variables scope of variables and shadowing null reference and NullPointerException.
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
Designing Classes Chapter 8. Classes Collection of objects Objects are not actions Class names – Nouns Method names – Verbs What Makes a Good Class Represent.
PACKAGES. PACKAGES IN JAVA A package is a collection of related classes and interfaces in Java Packages help in logical grouping of classes and interfaces.
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.
Java Programming Chapter 3 More about classes. Why?  To make classes easier to find and to use  to avoid naming conflicts  to control access  programmers.
Class Design CSC 171 FALL 2004 LECTURE 11. READING Read Chapter 7 It’s abstract But it should help with project #1.
CS-341 Dick Steflik Introduction. C++ General purpose programming language A superset of C (except for minor details) provides new flexible ways for defining.
CS-341 Dick Steflik Introduction. C++ General purpose programming language A superset of C (except for minor details) provides new flexible ways for defining.
C++ Classes in Depth. Topics Designing Your Own Classes Attributes and Behaviors Writing Classes in C++ Creating and Using Objects.
Applying OO Concepts Using Java. In this class, we will cover: Overriding a method Overloading a method Constructors Mutator and accessor methods The.
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.
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
Data Objects (revisited) Recall that values are stored in data objects, and that each data object holds one value of a particular type. Data objects may.
Packages F Package is a container for classes F A package is a grouping of related types (classes and interfaces) providing access protection and name.
Week 12 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Java Classes Appendix C © 2015 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 2.
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.
GP3, Martin Lillholm 1 Introductory Programming (GP) Spring 2006 Lecture 3 We start at 13:00 Slides are available from the course home page Feel free to.
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.
Defining Classes II. Today’s topics  Static methods  Static variables  Wrapper classes  References  Class parameters  Copy constructor.
Chapter 10 Defining Classes. The Internal Structure of Classes and Objects Object – collection of data and operations, in which the data can be accessed.
Chapter 4 Introduction to Classes, Objects, Methods and strings
Programmeren 1 6 september 2010 HOORCOLLEGE 2: INTERACTIE EN CONDITIES PROGRAMMEREN 1 6 SEPTEMBER 2009 Software Systems - Programming1 Programming.
Summing Up Object Oriented Design. Four Major Components: Abstraction modeling real-life entities by essential information only Encapsulation clustering.
CS0007: Introduction to Computer Programming Classes: Documentation, Method Overloading, Scope, Packages, and “Finding the Classes”
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.
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Structures and Classes Version 1.0. Topics Structures Classes Writing Structures & Classes Member Functions Class Diagrams.
Design Patterns Software Engineering CS 561. Last Time Introduced design patterns Abstraction-Occurrence General Hierarchy Player-Role.
Interfaces F What is an Interface? F Creating an Interface F Implementing an Interface F What is Marker Interface?
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 3 A First Look at Classes and Objects.
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
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.
April 20, 1998CS102-02Lecture 4-1 A Method to the Madness CS Lecture 4-1 Java's Work Horses.
CS 116 Lecture 1 John Korah Contains content provided by George Koutsogiannakis & Matt Bauer.
In this class, we will cover: Overriding a method Overloading a method Constructors Mutator and accessor methods The import statement and using prewritten.
Chapter 7 Designing Classes. Chapter Goals  To learn how to choose appropriate classes to implement  To understand the concepts of cohesion and coupling.
Topics Instance variables, set and get methods Encapsulation
© 2004 Pearson Addison-Wesley. All rights reserved September 5, 2007 Packages & Random and Math Classes ComS 207: Programming I (in Java) Iowa State University,
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Lecture 4 CS140 Dick Steflik. Reading Keyboard Input Import java.util.Scanner – A simple text scanner which can parse primitive types and strings using.
CSSE 220 Day 17 Introduction to your Vector Graphics project
Topic: Packages Course : JAVA PROGRAMMING Paper Code: ETCS-307
3 Introduction to Classes and Objects.
Objects as a programming concept
Intro To Classes Review
Chapter 3: Using Methods, Classes, and Objects
Chapter 11 – Interfaces and Polymorphism
Methods The real power of an object-oriented programming language takes place when you start to manipulate objects. A method defines an action that allows.
Lecture 23 Polymorphism Richard Gesick.
Defining Classes II.
Defining Your Own Classes Part 1
MSIS 655 Advanced Business Applications Programming
Defining Classes and Methods
Applying OO Concepts Using Java
Packages & Random and Math Classes
Defining Classes and Methods
Defining Classes and Methods
Classes, Objects and Methods
CS 112 Programming 2 Lecture 02 Abstract Classes & Interfaces (2)
Presentation transcript:

Lecture 7 Designing Classes CS140 Dick Steflik

A Good Class Should represent a single concept from a problem domain domain = mathematics; point, rectangle, elipse an abstraction of a real-life entity BankAccount, CashRegister

A cohesive Public Interface Minimize Dependencies Separating Accessors and Mutators Minimize Side Effects

Cohesive Public Interface The methods of a class are like members of a soccer team, they all have to be working for the same thing Ex: Cash register shouldn't have to keep track of coin values, this is better done by a Coin class. this way Coin class could be easily adapted to different currencies and the cash register would still work

Minimize Dependancies Never do I/O in an object method not all environments support the same I/O ATMs done have console output; no console Most consumer electronics products have limited I/o , especially for console output In GUI apps you have the possibilities of AWT, Swing or Android GUI, this requies special I/O for each

Separating Accessors and Mutators An accessor retrieves a piece of state data A mutator actually modifies the state by changing some piece of state data A class that has only accessor methods is called immutable String is immutable, i.e. there isn't a method that can modify the String use for classes that represent values: strings, colors, currency... In a mutator class separate the mutator and accessor methods, make each method do just one thing

Minimizing Side Effects Call by value Call by Referance In Java, numbers and object references are passed by value; a Java method can mutate an object when it receives an object reference as an arguement

Side Effects An effect (intended or not) that is secondary to the one intended. Side effects are not desirable. they make code harder to debug are often undocumented features class myclass{ private int abc, def; int getVal (){ def++; return abc;}} in addition to returning the value of abc there is a side effect of def being modified, unless this is very well documented it will not be noticed by most people looking at the code

side effects (more) public double getTotalBalance(Arraylist<String> accounts) { double sum = 0; while (studentNames.size() > 0) { BankAccount account = accounts.remove(0); sum = account.getBalance(); } return sum; }} this does return the total balance but also causes accounts to be emptied out. This may or may not be what was intended but is secondary to the primary purpose of the function

Static Variables accessible by all instances of a class belong to the class not to any instance of the class could be used for instance communication class airplane{ private static bool flag = false; private int myId = 1000; private static int lastId = 1000; private static int controller = 0; private int message = 0; public airplane() { myId = lastId++ ; ) public void takeControl { if (!flag) { flag = true; controller = myId; flag = false;} }

Static Methods methods that work on values not objects For instance Math.sqrt(x) takes a double as its argument (which is a primitive not an object) and returns a double. The Math is the name of the static class and sqrt is a static method in that class. No object called Math has been instantiated. You can make your own static functions but it should be minimized

Packages A package is a collection of related classes A Java package is a mechanism for organizing Java classes into namespaces . Java packages can be stored in compressed files called JAR files, allowing classes to download faster as a group rather than one at a time. Programmers also typically use packages to organize classes belonging to the same category or providing similar functionality. JAR (Java Archive) files are a modified version of a .zip file Packages are also used to avoid naming collisions (using the same variable name in two different places). By having each person in a project place their classes into their own package the package names will qualify identical names in different packages.

Packages (more) A class can always be referred to by its fully qualified name java.util.Scanner -or- by importing the package java.util.Scanner Scanner -or- by importing java.util.* Scanner The fully qualified package name actually implies a directory structure To make your own package, place your code into your desired directory structure, compile it then use the jar program in your java/bin directory to make a .jar file jar cf myPackage.jar *.class (this creates a file called myPackage.jar from all of the class files in the directory structure)