CSE1720 Delegation Concepts (Ch 2)

Similar documents
Programming by Delegation

System.out.print(); Scanner.nextLine(); String.compareTo();

Advanced Java Programming

Using APIs. Chapter 3. Outline Fields Overall Layout. Java By Abstraction Chapter 3. Field Summary static double PI

Chap. 3. Creating Objects The String class Java Class Library (Packages) Math.random() Reading for this Lecture: L&L,

Code Listing H-1. (Car.java) H-2 Appendix H Packages

1Z Java SE 5 and 6, Certified Associate Exam Summary Syllabus Questions

CS 211: Existing Classes in the Java Library

Introduction... xv SECTION 1: DEVELOPING DESKTOP APPLICATIONS USING JAVA Chapter 1: Getting Started with Java... 1

AP Computer Science Unit 1. Writing Programs Using BlueJ

Checklist (for Today) What we are reinforcing with the exercises this class. recognizing the various different arithmetic operators (in situ)

Chapter Goals. Chapter 7 Designing Classes. Discovering Classes Actors (end in -er, -or) objects do some kinds of work for you: Discovering Classes

Fundamentals of Programming Data Types & Methods

Java Programming Course Overview. Duration: 35 hours. Price: $900

Introduction. Overview of the Course on Java. Overview of Part 1 of the Course

CS 335 Lecture 02 Java Programming

(800) Toll Free (804) Fax Introduction to Java and Enterprise Java using Eclipse IDE Duration: 5 days

CHAPTER 1: A GENERAL INTRODUCTION TO PROGRAMMING 1

CSCE3193: Programming Paradigms

Java: advanced object-oriented features

AP Computer Science Unit 1. Writing Programs Using BlueJ

Core Java SYLLABUS COVERAGE SYLLABUS IN DETAILS

Example Program. public class ComputeArea {

CS111: PROGRAMMING LANGUAGE II

Defining Classes and Methods

AP Computer Science Unit 1. Programs

Chapter 2: Basic Elements of Java

Selected Java Topics

SYLLABUS JAVA COURSE DETAILS. DURATION: 60 Hours. With Live Hands-on Sessions J P I N F O T E C H

Java Training JAVA. Introduction of Java

Packages. Examples of package names: points java.lang com.sun.security drawing.figures

CS 112 Introduction to Programming

Introduction to Java Unit 1. Using BlueJ to Write Programs

Methods (Deitel chapter 6)

Methods (Deitel chapter 6)

JAVA: A Primer. By: Amrita Rajagopal

CS 152: Data Structures with Java Hello World with the IntelliJ IDE

[Course Overview] After completing this module you are ready to: Develop Desktop applications, Networking & Multi-threaded programs in java.

4. Java Project Design, Input Methods

We now start exploring some key elements of the Java programming language and ways of performing I/O

Project 1. Java Data types and input/output 1/17/2014. Primitive data types (2) Primitive data types in Java

Class Libraries. Readings and References. Java fundamentals. Java class libraries and data structures. Reading. Other References

Lab5. Wooseok Kim

presentation for Java Student Group, UFC, 03/13/2008 J. M. Silveira Neto Sun Campus Ambassador Universidade Federal do Ceará, Brazil

Computer Programming, I. Laboratory Manual. Experiment #2. Elementary Programming

Advanced Computer Programming

Constants. Why Use Constants? main Method Arguments. CS256 Computer Science I Kevin Sahr, PhD. Lecture 25: Miscellaneous

COMP 202. Built in Libraries and objects. CONTENTS: Introduction to objects Introduction to some basic Java libraries string

Assertions, pre/postconditions

Object Oriented Programming. Java-Lecture 1

Programming with Java

Lab5. Wooseok Kim

Course Content for Java J2EE

Tester vs. Controller. Elementary Programming. Learning Outcomes. Compile Time vs. Run Time

JAVA COURSES. Empowering Innovation. DN InfoTech Pvt. Ltd. H-151, Sector 63, Noida, UP

Introduction to Computer Science I

Faculty of Science COMP-202A - Foundations of Computing (Fall 2012) - All Sections Midterm Examination

Call: Core&Advanced Java Springframeworks Course Content:35-40hours Course Outline

Elementary Programming

12/22/11. Java How to Program, 9/e. public must be stored in a file that has the same name as the class and ends with the.java file-name extension.

Data Conversion & Scanner Class

Accurate study guides, High passing rate! Testhorse provides update free of charge in one year!

Chapter 4 Defining Classes I

BlueJ Demo. Topic 1: Basic Java. 1. Sequencing. Features of Structured Programming Languages

JAVA PROGRAMMING LAB. ABSTRACT In this Lab you will learn how to describe objects and classes and how to define classes and create objects

What we are reinforcing with the exercises this class

CSC Algorithms and Data Structures I. Midterm Examination February 25, Name:

CH. 2 OBJECT-ORIENTED PROGRAMMING

Packages & Random and Math Classes

Reading Input from Text File

1 Shyam sir JAVA Notes

CSE Lecture 24 Review and Recap. High-Level Overview of the Course!! L1-7: I. Programming Basics!

F I N A L E X A M I N A T I O N

Java Libraries. Lecture 6 CGS 3416 Fall September 21, 2015

Lab 8. An Introduction to Objects

Topics. The Development Process

Java SE7 Fundamentals

Core JAVA Training Syllabus FEE: RS. 8000/-

EECS1710. Checklist from last lecture (Sept 9, 2014) " get an EECS account (if you don t have it already) " read sections

Text User Interfaces. Keyboard IO plus

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

Another IS-A Relationship

EECS 1001 and EECS 1030M, lab 01 conflict

Computer Science is...

Java 8, Java 9, and Beyond!

Course Outline. Introduction to java

CEN 414 Java Programming

PSD1B Advance Java Programming Unit : I-V. PSD1B- Advance Java Programming

Internet Application Developer

CSC 1051 Algorithms and Data Structures I. Midterm Examination February 25, Name: KEY A

CS1150 Principles of Computer Science Objects and Classes

Eng. Mohammed Alokshiya

Introduction to Programming Using Java (98-388)

Eng. Mohammed S. Abdualal

Defining Classes and Methods. Objectives. Objectives 6/27/2014. Chapter 5

The Sun s Java Certification and its Possible Role in the Joint Teaching Material

Example packages from Java s standard class library:

Faculty of Science COMP-202B - Introduction to Computing I (Winter 2010) - All Sections Midterm Examination

Elementary Programming. CSE 114, Computer Science 1 Stony Brook University

Page 1

Transcription:

CSE1720 Delegation Concepts (Ch 2) Output (sec 2.2.5) Output to the console Output to a file (later section 5.3.2) Instead of System.out.println( Hi ); Use: output.println( Hi ); 1 2 Ready-Made I/O Components Input (sec 2.2.5) Use this template as a starting point for all your programs in this course: Output from the console Input from a file (later section 5.3.2) Use: Scanner input = new Scanner(System.in); int x = input.nextint(); 3 import java.util.scanner; import java.io.printstream; public class Template Scanner input = new Scanner(System.in); // use input.nextint/double for input // use output.println/print for output 4 1

2.1 Computing Paradigms import java.lang.system; public class Area int width = 8; int height = 3; int area = width * height; System.out.println(area); The code inside the rectangle computes the area of a circle. It handles both storage (of data) and computation (of area). Let us explore delegating one or both of these tasks. 5 2.1.1 Procedural Paradigm Keep storage but delegate computation to a class: int width = 8; int height = 3; int area = Rectangle1.computeArea(width, height); A method belongs to a class. It performs an action, and hence, its name is a verb (e.g., computearea() ) or a complete predicate (e.g., isenabled() ). The method name must be followed by a pair of parenthesis with any parameters needed sandwiched in between. The method name together with the types of its parameters make up the method signature. It is unique per class. The method's action culminates in a return. It can be void. Invocation syntax: class_name.method( ). It is like dialing the phone number of a company followed by someone's extension. 6 2.1.2 Modular Paradigm Delegate both storage and computation to a class: Rectangle2.width = 8; Rectangle2.height = 3; int area = Rectangle2.getArea(); An attribute belongs to a class. It holds data, and hence, its name is a noun (width). It has a type. Java treats attributes like variables except you do not declare them in your program (their class takes care of that) and the notion of scope does not apply to them. The attribute name is unique per class. Access syntax: class_name.attribute. Because the class name appears before the dot, we say that you invoke a method, or access an attribute, on the class. 7 2.1.3 Object-Oriented Paradigm Delegate both to an instance of a class: Rectangle3 r = new Rectangle3(); r.width = 8; r.height = 3; int area = r.getarea(); Create an instance (a.k.a object) of a class that can handle storage and computation and work with the instance as if it is a module. The instance has a name, r, known as the object reference. The attributes are accessed, and the methods are invoked, on the instance, not on the class. Think of the object (or instance) as a copy of the original class. Each object can store different values in its attributes; these values are known as the state of the object. 8 2

Case Study 2.2.4: the JDK UML (Unified Modeling Language) Top-level packages java.awt java.beans java.io java.lang java.math Provides support for drawing graphics. AWT = Abstract Windowing Toolkit Provide support for Java Beans. Provides support for f ile and other I/O operations. Provides the fundamental Java classes. This package is auto -imported by the compiler. Provides support for arbitrary -precision arithmetic The class diagram of a procedural class: type::lib::rectangle1 computearea(int, int): int java.net Provides su pport for network access. Provides support for RMI. java.rmi RMI = Remote Method Invocation java.security Provides support for the security framework. java.sql java.text java.util Provides support for databases access over JDBC JDBC = Java Database Connectivity, SQL = Structured Query Language Provides formatting for text, dates, and numbers. Miscellaneous utility classes including JCF. JCF = Java Collection Framework javax.crypto Provides support for cryptographic operations. The class diagrams of two modular classes: type::lib::rectangle2 width: int height: i nt PI: double java::lang::math javax.servlet Provides support for servlet and JSP development. JSP = Java Server Pages getarea(): int sqrt(double): double javax.swing Provides support for GUI development. GUI = Graphical User javax.xml Provides support for XML processing. XML = extensible Markup Language 9 10 UML (Non-Utility Classes) The class diagram of an object oriented class along with the object diagrams of two instances of it: r: Rectangle3 type::lib::re ctangle3 width: int height: int getarea(): int s: Rectangle3 2.2.5 Ready-Made I/O Components Keyboard Input: Scanner input = new Scanner(System.in); int width = input.nextint(); Screen Output: output.print(width); width = 8 height = 3 The class diagrams of an object-oriented class in the Java standard library width = 4 height = 1 java::util::date gettime(): long tostring(): String java::util::scanner nextint(): int nextdouble(): double nextline(): String input: Scanner java::io::printstream print(int): void print(string): void println(doubl e): void output: PrintStream 11 12 3

2.2.1 Application Architecture A Java application consists of several cooperating classes. One of the classes starts the application, and is known as the main class. The other classes are known as helpers or components. The main class for a desktop application (as opposed to an applet or servlet) is known as an app. It must have a method with the following header: The main class delegates to components. And as more ready-made components become available, application development will reduce to developing the main class. 13 2.2.2 The Client View The client is the developer of the main class. The implementer is the developer of a component. The client understands the big picture, the purpose of the application. The implementer focuses only on the inner details of one component. The client knows how to shop for components and how to read their specs; i.e. knows what each one does but not how it does it. This course focuses on being a client. It prepares you to write applications using components that are already available. Separation of concerns means the client and the implementer share info on a need-to-know basis. 14 The Client View Given a component, the client does not care what is inside it, only what it does. This is known as its interface or API (application programming interface). The class of a component thus encapsulates it. An attempt to look inside is breaking the encapsulation. The Client View A class is made up of features. A feature is an attribute or a method. The class of a component classifies each feature as either public or private depending, respectively, on whether the client needs or does not need to know about it. The API (interface) of a component lists only the headers of its public methods and the declarations of its public attributes (a.k.a. fields). CLIENT IMPLEMENTER feature attribute method private public = field public private interfac e 15 16 4

2.3.1 Risk Mitigation by Early Exposure If you are not sure about something during software development, confront it as early as possible. Making changes later is more difficulty than doing so now. For example, the Java compiler turns a potential logic error (like assigning a real value to an int variable) to a compile-time error. The risk of truncating the real value is exposed early. 17 2.3.3 Contracts Each method in a component comes with a contract that spells out the responsibilities of the client and the implementer. The client must supply parameters that satisfy the precondition of the method. The implementer must supply a return that satisfy the postcondition of the method. Liability: if pre=false, the client is at fault, if pre=true and post=false, then the implementer is at fault. If pre=post=true then everything is OK. Note: if a method has pre=true then its client does not have to 18 ensure anything. Contracts Methods in the Java standard library specify their pre and post as follows: - pre is always assumed to be true unless stated otherwise - post is specified under Returns and Throws and can be assumed to be true Example: This contract specifies pre=true (i.e. no condition on the parameter). The post states that the method will return the square root if x is nonnegative and will throw an exception otherwise double squareroot(double x) Returns the square root of the given argument. Parameters: x - an argument. Returns: the positive square root of x. Throws: an exception if x < 0. 19 5