Classes and Objects. Agenda. Quiz 7/1/2008. The Background of the Object-Oriented Approach. Class. Object. Package and import



Similar documents
Fundamentals of Java Programming

Chapter 2 Introduction to Java programming

Handout 1. Introduction to Java programming language. Java primitive types and operations. Reading keyboard Input using class Scanner.

Introduction to Java Applications Pearson Education, Inc. All rights reserved.

An Overview of Java. overview-1

Contents. 9-1 Copyright (c) N. Afshartous

J a v a Quiz (Unit 3, Test 0 Practice)

Java and J2EE (SCJA Exam CX ) 50 Cragwood Rd, Suite 350 South Plainfield, NJ 07080

java.util.scanner Here are some of the many features of Scanner objects. Some Features of java.util.scanner

Chulalongkorn University International School of Engineering Department of Computer Engineering Computer Programming Lab.

Building Java Programs

Scanner. It takes input and splits it into a sequence of tokens. A token is a group of characters which form some unit.

Introduction to Java

6.088 Intro to C/C++ Day 4: Object-oriented programming in C++ Eunsuk Kang and Jean Yang

SOFTWARE ENGINEERING PROGRAM

RARITAN VALLEY COMMUNITY COLLEGE ACADEMIC COURSE OUTLINE. CISY 105 Foundations of Computer Science

System.out.println("\nEnter Product Number 1-5 (0 to stop and view summary) :

Chapter 6: Programming Languages

Design Patterns in Java

Java Interview Questions and Answers

More on Objects and Classes

CISC 181 Project 3 Designing Classes for Bank Accounts

java Features Version April 19, 2013 by Thorsten Kracht

Debugging. Common Semantic Errors ESE112. Java Library. It is highly unlikely that you will write code that will work on the first go

MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question.

Week 1: Review of Java Programming Basics

JDK 1.5 Updates for Introduction to Java Programming with SUN ONE Studio 4

Classes and Objects in Java Constructors. In creating objects of the type Fraction, we have used statements similar to the following:

Masters programmes in Computer Science and Information Systems. Object-Oriented Design and Programming. Sample module entry test xxth December 2013

PIC 10A. Lecture 7: Graphics II and intro to the if statement

You are to simulate the process by making a record of the balls chosen, in the sequence in which they are chosen. Typical output for a run would be:

Lecture 5: Java Fundamentals III

INTRODUCTION TO COMPUTER PROGRAMMING. Richard Pierse. Class 7: Object-Oriented Programming. Introduction

CSE 1020 Introduction to Computer Science I A sample nal exam

Design and UML Class Diagrams

6.1. Example: A Tip Calculator 6-1

Java 進 階 程 式 設 計 03/14~03/21

CS170 Lab 11 Abstract Data Types & Objects

Programming and Software Development CTAG Alignments

1.00 Lecture 1. Course information Course staff (TA, instructor names on syllabus/faq): 2 instructors, 4 TAs, 2 Lab TAs, graders

Topic 11 Scanner object, conditional execution

Java the UML Way: Integrating Object-Oriented Design and Programming

Chapter 2. println Versus print. Formatting Output withprintf. System.out.println for console output. console output. Console Input and Output

Chapter 1 Java Program Design and Development

Part I. Multiple Choice Questions (2 points each):

Moving from CS 61A Scheme to CS 61B Java

Building Java Programs

CS 111 Classes I 1. Software Organization View to this point:

Crash Course in Java

Iteration CHAPTER 6. Topic Summary

CSE 1223: Introduction to Computer Programming in Java Chapter 7 File I/O

Basics of Java Programming Input and the Scanner class

Java Programming Fundamentals

CSC 551: Web Programming. Fall 2001

Java (12 Weeks) Introduction to Java Programming Language

Summit Public Schools Summit, New Jersey Grade Level / Content Area: Mathematics Length of Course: 1 Academic Year Curriculum: AP Computer Science A

Introduction to Object-Oriented Programming

Object-Oriented Programming in Java

The Sun Certified Associate for the Java Platform, Standard Edition, Exam Version 1.0

Applets, RMI, JDBC Exam Review

Computer Programming I

Object-Oriented Databases

Glossary of Object Oriented Terms

Scanner sc = new Scanner(System.in); // scanner for the keyboard. Scanner sc = new Scanner(System.in); // scanner for the keyboard

Java Application Developer Certificate Program Competencies

AP Computer Science Java Subset

First Java Programs. V. Paúl Pauca. CSC 111D Fall, Department of Computer Science Wake Forest University. Introduction to Computer Science

Building Java Programs

The Interface Concept

Lecture 1 Introduction to Java

Chapter 1 Modeling and the UML

From Object Oriented Conceptual Modeling to Automated Programming in Java

Mobile App Design Project #1 Java Boot Camp: Design Model for Chutes and Ladders Board Game

Capabilities of a Java Test Execution Framework by Erick Griffin

CSCI 253. Object Oriented Programming (OOP) Overview. George Blankenship 1. Object Oriented Design: Java Review OOP George Blankenship.

13 File Output and Input

The Java Series. Java Essentials I What is Java? Basic Language Constructs. Java Essentials I. What is Java?. Basic Language Constructs Slide 1

Arrays. Atul Prakash Readings: Chapter 10, Downey Sun s Java tutorial on Arrays:

Chapter 2: Elements of Java

Design with Reuse. Building software from reusable components. Ian Sommerville 2000 Software Engineering, 6th edition. Chapter 14 Slide 1

Comp 248 Introduction to Programming

Thomas Jefferson High School for Science and Technology Program of Studies Foundations of Computer Science. Unit of Study / Textbook Correlation

Building Java Programs

Sample CSE8A midterm Multiple Choice (circle one)

The C Programming Language course syllabus associate level

Basic Java Constructs and Data Types Nuts and Bolts. Looking into Specific Differences and Enhancements in Java compared to C

Ch 7-1. Object-Oriented Programming and Classes

COSC Introduction to Computer Science I Section A, Summer Question Out of Mark A Total 16. B-1 7 B-2 4 B-3 4 B-4 4 B Total 19

How To Write A Program In Java (Programming) On A Microsoft Macbook Or Ipad (For Pc) Or Ipa (For Mac) (For Microsoft) (Programmer) (Or Mac) Or Macbook (For

Sources: On the Web: Slides will be available on:

Programming Languages CIS 443

IMPROVING JAVA SOFTWARE THROUGH PACKAGE STRUCTURE ANALYSIS

Interactive Programs and Graphics in Java

El Dorado Union High School District Educational Services

Software Testing. Definition: Testing is a process of executing a program with data, with the sole intention of finding errors in the program.

Transcription:

Classes and Objects 2 4 pm Tuesday 7/1/2008 @JD2211 1 Agenda The Background of the Object-Oriented Approach Class Object Package and import 2 Quiz Who was the oldest profession in the world? 1. Physician 2. Civil Engineer 3. Computer Scientist and Software Engineer 3 1

Complexity A physician, a civil engineer, and a computer scientist were arguing about what was the oldest profession in the world. The physician remarked, Well, in the Bible, it says that God created Eve from a rib taken out of Adam. This clearly required surgery, and so I can rightly claim that mine is the oldest profession in the world. 4 Complexity The civil engineer interrupted, and said, But even earlier in the book of Genesis, it states that God created the order of the heavens and the earth from out of the chaos. This was the first and certainly the most spectacular application of civil engineering. Therefore, fair doctor, you are wrong: mine is the oldest profession in the world. 5 Complexity The computer scientist leaned back in his chair, smiled, and then said confidently, Ah, but who do you think created the chaos? Object-Oriented Analysis and Design with Applications, by Grady Booch 6 2

Example of Complexity -- Feasibility of Testing to Specification Two inputs One has five values The other has seven values How many test cases are needed 5X7 = 35 30 inputs Each input has four different values How many test cases are required? If a program has 1.1 X 10 18 possible inputs and one test can be run every microsecond, how long would it take to execute all of the possible inputs? 7 Example of Complexity -- Feasibility of Testing to Code Read (kmax) // kmax is an integer between 1 and 18 for (k = 0; k < kmax; k++) do read (mychar) // mychar is the character A, B, or C switch (mychar) case A : block A; if (cond1) blockc; break; case B : block B; if (cond2) blockc; break; case C : block C: break; 8 Example of Complexity -- Feasibility of Testing to Code k < 18 [yes] A blocka mychar C cond1 true blockc false [no] blockb B true cond2 false blockd 9 3

Software Essential Difficulties No Silver Bullet: Essence and Accidents of Software Engineering by Frederick P. Brooks, Jr., 1986 http://www.csun.edu/~twang/java/nosilverbullet.p p df http://en.wikipedia.org/wiki/no_silver_bullet Essential difficulties High complexity Low conformity Changeability 10 Past Breakthrough (as of 1986) High-level language Time-sharing Unified programming environment 11 Hope for the Silver (as of 1986) Ada and other high-level language advance Object-oriented programming Artificial intelligence Expert system Graphical programming Program verification Environments and tools Workstation 12 4

Hope for the Silver (since1990 s ) Design pattern Architecture pattern Application frameworks Software product lines Software factory Component-based software Service-oriented architecture 13 Foundations of the Object Model Classes and objects are basic building blocks Structured design methods use algorithms or functions as their fundamental building blocks The object model has been influenced by a number of factors not just to programming languages, but also the design of database, user interface, and even computer architecture 14 Elements of the Object Model Abstraction Encapsulation Object Class Attribute Methods Relationships Inheritance Polymorphism 15 5

Abstraction Denotes the essential characteristics of an object that distinguish it from all other kinds of object, and thus provide crisply defined conceptual boundaries, relative to the perspective of the viewer. 16 Encapsulation Encapsulation is the process of compartmentalizing the elements of an abstraction that constitute its structure and behavior; encapsulation serves to separate the contractual interface of an abstraction and its implementation Encapsulation is often achieved through information hiding 17 Abstraction vs. Encapsulation Abstraction and encapsulation are complementary concepts Abstraction focuses upon the observable behavior of an object Encapsulation focuses upon the implementation that gives rise to this behavior 18 6

Object Is anything crisply defined An object represents an individual, identifiable item, unit, or entity, either real or abstract, with a well-defined role in the problem domain An object has state, behavior, and identity; the structure and behavior of similar objects are defined in tier common class 19 Attribute The object s attributes are the values it stores internally, which may be represented as primitive data or as other objects Collectively, the values of an object s attributes define its current state 20 Method The methods of an object define its potential behaviors A method is a group of programming statements that is given a name When a method is invoked, its statements are executed A set of methods is associated with an object 21 7

Class An object is defined as a class A class is the model or blueprint from which an object is created In general, a class contains no space to store data. Each object has space for its own data Once a class has been defined, multiple objects can be created from that class 22 Class In Java, there are ready-made classes and classes we have written ourselves When you use ready-made class, you do not normally need to know how the class methods are implemented, you just have to know how they are called 23 Examples of Classes Class Attributes Operations Student Flight Employee 24 8

Employee Class Attributes First name Last name Social security number Monthly salary Operations Set first name Set last name Set social security number Set monthly salary Get monthly salary Print employee information Employee firstname lastname socialsecuritynumber monthlysalary Employee setfirstname () setlastnname () setsocialsecurtynnumber () semmonthlysalary () getfirstname () getlastname () getsocialsecuritynumber () getmonthlysalary () stringemployeeinfo() Analysis level Employee UML class diagram 25 Employee Class Employee -firstname : String -lastname : String -socialsecuritynumber : String -monthlysalary : double +Employee (String first, String last, String ssn, double salary) +setfirstname (String first) : void +setlastnname (String last) : void +setsocialsecurtynnumber (String ssn) : void +semmonthlysalary (double salary) : void +getfirstname () : String +getlastname () : String +getsocialsecuritynumber () : String +getmonthlysalary () : double +stringemployeeinfo() : Striing Design level Employee UML class diagram 26 public class Employee private String firstname; private String lastname; private String socialsecuritynumber; double monthlysalary; //constructor public Employee (String first, String last, String ssn, double salary) public void setfirstname(string first) public void setlastnname(string last) public void setsocialsecurtynnumber(string ssn) 27 9

public String getfirstname() public String getlastname() public String getsocialsecuritynumber() public double getmonthlysalary() public String stringemployeeinfo() 28 Create a Main Class A main Class contains the main method, that contains a series of instructions to create objects and to tell those objects to do things. public class EmployeeDemo public static void main (String args[]) Employee myemployee = new Employee("George", "Bush", "111 22 3333", 50000.00);.. 29 Creating Objects -- String The standard class String is an important example of a ready-made class String is a special class String s1 = Java s1 is a reference variable which refers to String object, Java String s1 = new String ( Java ); 30 10

The new Operator and Constructor Has the same name as the class Returns a reference to a newly created object: Are not allowed to have a return type There may be several of them but then they must have a different number of parameters, or parameters of different types Unless we define our own constructors, a constructor without parameter is defined automatically (and this does nothing). 31 Package Java program is supported by a standard library A class library is a set of classes that supports the development of programs A complier or development environment often comes with a class library 32 Package (cont d) Class libraries can also be obtained separately though third-party vendor The String class is not an inherent part of the Java language. It is part of the Java standard class library. The class library is made up of several clusters of related classes, called Java APIs or application programming interfaces 33 11

Package (cont d) The classes of the Java standard class library are grouped into packages. Each class is part of particular package The String class is part of the java.lang package The System class is part of the java.lang package as well 34 Some Packages in the Java Standard Class Library Package java.awt Java.io Provides support to Draw graphics and create graphical user interfaces; AWT stands for Abstract Windows Toolkit Perform a wide variety of input and out functions java.lang General support; it is automatically ti imported dinto all Java programs Java.math Perform calculations with arbitrary high precision java.sql Interact with databases java.text Format text for output java.util General utilities javax.swing Create graphical user interfaces with components that executed the AWT capabilities 35 The import Declaration The classes of java.lang package are automatically available for use when writing Java program To use classes from any other package, however, we must use an import declaration For example, import java.io.* is used for data input 36 12

The Random Class The need for random numbers occurs frequently when writing programs Games The roll of a die, the shuffle of a deck of cards Flight simulators The Random class, which is a part of the java.util package, represents a random number generator 37 Some Methods of the Random Class Random() Constructor float nextfloat() Returns a random number between 0.0 0 (inclusive) and 1.0 (exclusive) int nextint() Returns a random number that ranges over all possible int value int nextint (int num) Returns a random number in the range 0 to num-1 38 Lab Assignments Write a program to produces several random numbers in various ranges: 1) over all possible int value; 2) from 0 to 9; 3) from 1 to 10; 4) from 20 to 34; 5) from -10 to 9; 6) between 0.0 and 1.0, and 7) between 1.0 and 6.0 Complete the Employee class in the slide and implement the main class to create an Employee object and print out the object. Construct a class Person. A person should have a name, an address and an age. Make use of the class String. Form a constructor and some appropriate methods for class Person (Exercise 2 on page 79). Also create a person object and print it by implementing the main class. 39 13