CSM-Java Programming-I Spring,2005 Class Design Lesson - 4.

Slides:



Advertisements
Similar presentations
Introduction to Java 2 Programming Lecture 3 Writing Java Applications, Java Development Tools.
Advertisements

Core Java Lecture 4-5. What We Will Cover Today What Are Methods Scope and Life Time of Variables Command Line Arguments Use of static keyword in Java.
Java Programming 2 Dr. Priti Srinivas Sajja Introductory concepts of java programming as specified in PGDCA 203:Object Technology, S P University.
Lecture 9: More on objects, classes, strings discuss hw3 assign hw4 default values for variables scope of variables and shadowing null reference and NullPointerException.
Written by: Dr. JJ Shepherd
IMPLEMENTING CLASSES Chapter 3. Black Box  Something that magically does its thing!  You know what it does but not how.  You really don’t care how.
Designing Classes Chapter 8. Classes Collection of objects Objects are not actions Class names – Nouns Method names – Verbs What Makes a Good Class Represent.
Lecture 2: Object Oriented Programming I
Chapter 3 Implementing Classes. Instance Variables Instance variables store the data of an object; the fields of an object. Instance of a class: an object.
Chapter 2 – An Introduction to Objects and Classes Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.
Chapter Goals To learn how to choose appropriate classes to implement
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
Road Map Introduction to object oriented programming. Classes
Lecture 2 Classes and objects, Constructors, Arrays and vectors.
The Java Programming Language  Simple – but abstract  Safe  Platform-independent ("write once, run anywhere")  Has a Rich growing library  Designed.
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 Fundamental Data Types Lesson - 2.
CHAPTER 2 OBJECTS AND CLASSES Goals: To understand the concepts of classes and objects To realize the difference between objects and object references.
COMP 110 Introduction to Programming Mr. Joshua Stough October 8, 2007.
Enhancing classes Visibility modifiers and encapsulation revisited
Class Design CSC 171 FALL 2004 LECTURE 11. READING Read Chapter 7 It’s abstract But it should help with project #1.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
Applying OO Concepts Using Java. In this class, we will cover: Overriding a method Overloading a method Constructors Mutator and accessor methods The.
Lecture From Chapter 6 & /8/10 1 Method of Classes.
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.
UML Basics & Access Modifier
Programming Languages and Paradigms Object-Oriented Programming.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
CSM-Java Programming-I Spring,2005 Objects and Classes Overview Lesson - 1.
1 Lecture 2: Object Oriented Programming in Java.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
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.
Utilities (Part 2) Implementing static features 1.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
CSC1401 Classes - 2. Learning Goals Computing concepts Adding a method To show the pictures in the slide show Creating accessors and modifiers That protect.
Session 7 Methods Strings Constructors this Inheritance.
Odds and Ends. CS 21a 09/18/05 L14: Odds & Ends Slide 2 Copyright © 2005, by the authors of these slides, and Ateneo de Manila University. All rights.
CS0007: Introduction to Computer Programming Classes: Documentation, Method Overloading, Scope, Packages, and “Finding the Classes”
Rina System development with Java Instructors: Rina Zviel-Girshin Lecture 4.
Introduction to Java Chapter 7 - Classes & Object-oriented Programming1 Chapter 7 Classes and Object-Oriented Programming.
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
CMSC 341 Java Packages, Classes, Variables, Expressions, Flow Control, and Exceptions.
Java Class Structure. Class Structure package declaration import statements class declaration class (static) variable declarations* instance variable.
Chapter 8: Designing Classes Accessors, Mutators, and Immutable Classes(8.3) Side Effects(8.4) Parameter Passing (Advanced Topic 8.1) Preconditions and.
CSI 3125, Preliminaries, page 1 Overloading Methods In Java it is possible to define two or more methods within the same class that share the same name,
Chapter 9 – Designing Classes Goals Learning to design classes Learning to design classes Preconditions and postconditions for methods Preconditions.
Topic 8Classes, Objects and Methods 1 Topic 8 l Class and Method Definitions l Information Hiding and Encapsulation l Objects and Reference Classes, Objects,
1 Static Variable and Method Lecture 9 by Dr. Norazah Yusof.
In this class, we will cover: Overriding a method Overloading a method Constructors Mutator and accessor methods The import statement and using prewritten.
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.
Topics Instance variables, set and get methods Encapsulation
5.1 Basics of defining and using classes A review of class and object definitions A class is a template or blueprint for an object A class defines.
Internet Computing Module II. Syllabus Creating & Using classes in Java – Methods and Classes – Inheritance – Super Class – Method Overriding – Packages.
Chapter 9 – Designing Classes. Chapter Goals Discovering and learning to Design classes Discovering and learning to Design classes Preconditions and postconditions.
Chapter 3 Implementing Classes
Georgia Institute of Technology More on Creating Classes Barb Ericson Georgia Institute of Technology June 2006.
Basic Class Structure. Class vs. Object class - a template for building an object –defines the instance data that the object will hold –defines instance.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Lecture 3 John Woodward.
Exceptions, Interfaces & Generics
Implementing Classes Yonglei Tao.
Constructor Overloading
Introduction to Computer Science and Object-Oriented Programming
Applying OO Concepts Using Java
JAVA CLASSES.
AN INTRODUCTION TO OBJECTS AND CLASSES
Corresponds with Chapter 5
Chapter 7 Objects and Classes
Presentation transcript:

CSM-Java Programming-I Spring,2005 Class Design Lesson - 4

CSM-Java Programming-I Lesson-1 Objectives Review of last class Method Overloading Constructor Overloading Static revisited finalize Recursion Command-Line Arguments Arrays Class Design

CSM-Java Programming-I Lesson-1 Method Overloading In Java, it is possible for two or more methods to have the same name, as long as they have different number or types of parameters. This is called method overloading. When an overloaded method is invoked, the compiler compares the number and types of parameters to find the method that best matches the available signatures. The return type and the exceptions thrown are not counted for overloading of methods.

CSM-Java Programming-I Lesson-1 Method Overloading public class Example { public void show() { // does something; } public double show (double i) { // does something else return i/2.0; }

CSM-Java Programming-I Lesson-1 Constructor Overloading public class Box { int height; int width; int depth; public Box () { height = 0; …. } public Box(int bheight, int bwidth, int bdepth) { height = bheight; ……. }

CSM-Java Programming-I Lesson-1 Static Revisited When a member is declared static, it can be accessed before any objects of its class are created. Both methods and variables can be declared static. A static member is a member that is only one per class, rather than one in every object created from that class. A static method can access only static variables and static methods of the class. There is no this reference because there is no specific object being operated on. classname.staticmethod();

CSM-Java Programming-I Lesson-1 Garbage collection and finalize Java performs garbage collection and eliminates the need to free objects explicitly. When an object has no references to it anywhere, except in other objects that are also unreferenced, its space can be reclaimed. Before the object is destroyed, it might be necessary for the object to perform some actions. For example closing an opened file. In such a case define a finalize() method with the actions to be performed before the object is destroyed.

CSM-Java Programming-I Lesson-1 finalize When a finalize method is defined in a class, Java run time calls finalize() whenever it is about to recycle an object of that class. protected void finalize() { // code } A garbage collector reclaims objects in any order or never reclaim them.

CSM-Java Programming-I Lesson-1 Command-Line Arguments The data that follows the program name on the command- line when it is executed is a command-line argument. The command-line arguments are stored as strings in the String array passed to the main().

CSM-Java Programming-I Lesson-1 Command-Line Arguments public class Example { public static void main(String args[]) { for (int i=0; i < args.length; i++) System.out.println(“args[“ + i + “]: “ + args[i]); } > java Example my test 1 args[0]: my args[1]: test args[2]: 1

CSM-Java Programming-I Lesson-1 Arrays An array is a fixed-length collection of variables of the same type. Arrays can have one or more dimensions. An element in an array is accessed by its index. E.g.: a[i] int[] data = new int[10]; Arrays have a length field that says how many elements the array contains. In the above example: data.length =10

CSM-Java Programming-I Lesson-1 Arrays An array range is between 0 and length-1. An indexOutofBoundsException is thrown if you try to access elements outside the range of the array. An array can be initialized when they are declared. Eg: int singleDigitEvenNumbers[] = {2, 4, 6, 8 }; In the above example there is no need to do “new”. An array will be created large enough to hold all the elements that are initialized.

CSM-Java Programming-I Lesson-1 Recursion Recursion is defining something in terms of itself (a method to call itself). public class Factorial { public int fact(int n) { int result; if (n == 1) { return 1; } result = fact(n-1) * n; return result; }

CSM-Java Programming-I Lesson-1 Class Design Classes are collections of objects. Objects are entities not actions. Methods are actions. A class should be a single concept. Begin designing your class by identifying objects and the classes to which they belong.

CSM-Java Programming-I Lesson-1 Class Design Some examples of classes can be, Student, Employee, etc., Very occasionally, a class has no objects, but it contains a collection of related static methods and constants. -Math class is an example of such a class. It is called a utility class. If you can’t tell from the class name what an object of the class is supposed to do, then you are probably not on the right track.

CSM-Java Programming-I Lesson-1 Cohesion The public interface of a class should be cohesive (all features should be closely related to the single concept that the class represents).

CSM-Java Programming-I Lesson-1 Example /** A purse computes the total value of a collection of coins. */ public class Purse { private double total; /** Constructs an empty purse. */ public Purse() { total = 0; }

CSM-Java Programming-I Lesson-1 Example /** Add a coin to the aCoin the coin to add */ public void add(Coin aCoin) { total = total + aCoin.getValue(); } /** Get the total value of the coins in the the sum of all coin values */ public double getTotal() { return total; }

CSM-Java Programming-I Lesson-1 Example /** A coin with a monetary value. */ public class Coin { private double value; private String name; /** Constructs a aValue the monetary value of the aName the name of the coin */ public Coin(double aValue, String aName) { value = aValue; name = aName; }

CSM-Java Programming-I Lesson-1 Example /** Gets the coin the value */ public double getValue() { return value; } /** Gets the coin the name */ public String getName() { return name; }

CSM-Java Programming-I Lesson-1 Coupling A class depends on another class if it uses objects of that other class. If many classes of a program depend on each other, then we say that the coupling between classes is high. It is a good practice to minimize coupling between classes. Follow a consistent scheme for the method names and parameters.

CSM-Java Programming-I Lesson-1 Accessor and Mutator Methods A method that accesses an object and returns some information about it, without changing the object, is called an accessor method. Eg: getBalance() method in BankAccount class. A method whose purpose is to modify the state of an object is called a mutator method. Eg: withdraw() or deposit() Some classes are designed to have only accessor methods. Eg: String. – Immutable class.

CSM-Java Programming-I Lesson-1 Preconditions A precondition is a requirement that the caller of a method must obey. For example, the deposit method of the BankAccount class has a precondition amount >= 0 (amount parameter must not be negative) If a method violates this precondition it is not responsible for computing a correct result (might throw an exception).

CSM-Java Programming-I Lesson-1 Postconditions Postcondition makes a statement about the object's state after the deposit method is called. For example, the deposit method of the BankAccount class has a postcondition getBalance() >= 0 (balance returned after deposit is not negative). As long as the precondition is fulfilled, this method guarentees that the balance after the deposit is not negative.

CSM-Java Programming-I Lesson-1 Scope The scope of local variables extends from the point of declaration to the end of the block that encloses it. The scope of a variable in a for loop extends to the for loop but not beyond. for (int i = 1; i <= years; i++) { …… } // scope of i ends here.

CSM-Java Programming-I Lesson-1 Scope In Java, you cannot have two local variables with overlapping scope. Rectangle r = new Rectangle (5, 10, 20, 30); if (x >= 0) { double r = Math.sqrt(x); // Error- Can’t declare another variable called r here. …… }

CSM-Java Programming-I Lesson-1 Scope of Class Memebrs In Java, the scope of a local variable and an instance variable can overlap. You can solve this problem by using the this reference. public class Coin { double value; String name; public Coin (double value, String name) { this.value = value; this.name = name; }

CSM-Java Programming-I Lesson-1 Calling One Constructor from Another public class Coin { public Coin( double value, String name) { this.value = value; this.name = name; } public Coin() { this (1, “dollar”); } ….. } Such a constructor call can occur only as the first line in another constructor.

CSM-Java Programming-I Lesson-1 Packages A package is a set of related classes. They provide a structuring mechanism. To put classes in a package, you must place a line package packagename; package com.hotsmann.bigjava; as the first line in the source file containing classes. If you did not include package statement at the top of your source file, its classes are placed in the default package.

CSM-Java Programming-I Lesson-1 Importing packages The import directive lets you refer to a class of a package by its class name, without the package prefix. import java.awt.Color; There is no need to import the classes in the java.lang package explicitly. This package contains most basic Java classes. There is no need to import the other classes in the same package.

CSM-Java Programming-I Lesson-1 How to program with packages Step 1: Come up with a package name : Say homework1. Step 2: Pick a base directory: Say C:\CIS381\Assignments Step 3: Make a subdirectory from the base directory that matches your package name. mkdir C:\CIS381\Assignments\homework1 Step 4: Place your source files into the package subdirectory. Say your homework1 consists of BankAccount.java and BankAccountTest.java, then put them in C:\CIS381\Assignments\homework1

CSM-Java Programming-I Lesson-1 How to program with packages Step 5: Use the package statement in each of your source files. package homework1; as the first line of code in your BankAccount.java and BankAccountTest.java. Step 6: Change to the base directory to compile your files. cd \CIS381\Assigments javac homework1\BankAccount.java javac homework1\BankAccountTest.java

CSM-Java Programming-I Lesson-1 How to program with packages Step 7: Run your program from the base directory cd CIS381/Assignments java homework1.BankAccountTest