© 2006 Pearson Addison-Wesley. All rights reserved 1-1 Chapter 1 Review of Java Fundamentals.

Slides:



Advertisements
Similar presentations
COSC 2006 Data Structures I Instructor: S. Xu URL:
Advertisements

Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
CS102--Object Oriented Programming
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 15: Exception Handling.
Designing an ADT The design of an ADT should evolve naturally during the problem-solving process Questions to ask when designing an ADT What data does.
Chapter 16: Exception Handling C++ Programming: From Problem Analysis to Program Design, Fifth Edition.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 16: Exception Handling.
Slides prepared by Rose Williams, Binghamton University ICS201 Exception Handling University of Hail College of Computer Science and Engineering Department.
1 Lecture 11 Interfaces and Exception Handling from Chapters 9 and 10.
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
Chapter 3 Data Abstraction: The Walls. © 2005 Pearson Addison-Wesley. All rights reserved3-2 Abstract Data Types Modularity –Keeps the complexity of a.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
J.43 ARRAYS  A Java array is an Object that holds an ordered collection of elements.  Components of an array can be primitive types or may reference.
Slides prepared by Rose Williams, Binghamton University Chapter 13 Interfaces and Inner Classes.
© 2006 Pearson Addison-Wesley. All rights reserved4-1 Chapter 4 Data Abstraction: The Walls.
23-Jun-15 Strings, Etc. Part I: String s. 2 About Strings There is a special syntax for constructing strings: "Hello" Strings, unlike most other objects,
© 2006 Pearson Addison-Wesley. All rights reserved1-1 Chapter 1 Review of Java Fundamentals.
Strings, Etc. Part I: Strings. About Strings There is a special syntax for constructing strings: "Hello" Strings, unlike most other objects, have a defined.
Slides prepared by Rose Williams, Binghamton University Chapter 9 More Exception Handling.
© 2006 Pearson Addison-Wesley. All rights reserved4-1 Chapter 4 Data Abstraction: The Walls.
Chapter 1 Review of Java Fundamentals Lecture 2 Jenny Walter Fall 2008.
© 2006 Pearson Addison-Wesley. All rights reserved1-1 Chapter 1 Review of Java Fundamentals.
Copyright © 2003 ProsoftTraining. All rights reserved. Sun Certified Java Programmer Exam Preparation Guide.
Java Methods By J. W. Rider. Java Methods Modularity Declaring methods –Header, signature, prototype Static Void Local variables –this Return Reentrancy.
CMSC 202 Interfaces. 11/20102 Classes and Methods When a class defines its methods as public, it describes how the class user interacts with the method.
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
Java. Why Java? It’s the current “hot” language It’s almost entirely object-oriented It has a vast library of predefined objects It’s platform independent.
Chapter 12 Inheritance and Exceptions Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas,
1 Review of Java Higher Level Language Concepts –Names and Reserved Words –Expressions and Precedence of Operators –Flow of Control – Selection –Flow of.
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
CMSC 202 Exceptions. Aug 7, Error Handling In the ideal world, all errors would occur when your code is compiled. That won’t happen. Errors which.
The Java Programming Language
JAVA Tokens. Introduction A token is an individual element in a program. More than one token can appear in a single line separated by white spaces.
Java Programming: Guided Learning with Early Objects
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Chapter 14: Exception Handling. Objectives In this chapter, you will: – Learn what an exception is – Learn how to handle exceptions within a program –
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
© 2011 Pearson Addison-Wesley. All rights reserved 1-1 Chapter 1 Review of Java Fundamentals.
Methods in Java. Program Modules in Java  Java programs are written by combining new methods and classes with predefined methods in the Java Application.
Page: 1 การโปรแกรมเชิงวัตถุด้วยภาษา JAVA บุรินทร์ รุจจนพันธุ์.. ปรับปรุง 15 มิถุนายน 2552 Keyword & Data Type มหาวิทยาลัยเนชั่น.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Namespaces & Exceptions Adapted from Ch. 3 slides of Data Abstraction:
Introduction to Java Lecture Notes 3. Variables l A variable is a name for a location in memory used to hold a value. In Java data declaration is identical.
Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java.
Introduction to Java Chapter 7 - Classes & Object-oriented Programming1 Chapter 7 Classes and Object-Oriented Programming.
CMSC 341 Java Packages, Classes, Variables, Expressions, Flow Control, and Exceptions.
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 9 Java Fundamentals Objects/ClassesMethods Mon.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Chapter 15: Exception Handling C++ Programming: Program Design Including Data Structures, Fifth Edition.
© 2006 Pearson Addison-Wesley. All rights reserved1-1 Lecture 2 Java Fundamentals.
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
© 2006 Pearson Addison-Wesley. All rights reserved 1-1 Chapter 1 Review of Java Fundamentals.
Exceptions in the Java programming language J. W. Rider.
Data Abstraction: The Walls
Review of Java Fundamentals
Java Primer 1: Types, Classes and Operators
University of Central Florida COP 3330 Object Oriented Programming
Java Programming: From Problem Analysis to Program Design, 4e
Chapter 6 Methods: A Deeper Look
Java Programming Language
Review of Java Fundamentals Jenny Walter Fall 2008
MSIS 655 Advanced Business Applications Programming
Chapter 2: Basic Elements of Java
Chap 2. Identifiers, Keywords, and Types
Review of Java Fundamentals
CMSC 202 Exceptions.
Review of Java Fundamentals
Presentation transcript:

© 2006 Pearson Addison-Wesley. All rights reserved 1-1 Chapter 1 Review of Java Fundamentals

© 2006 Pearson Addison-Wesley. All rights reserved 1-2 Program Structure Typical Java program consists of –User written classes –Java Application Programming Interface (API) classes Java application –Has one class with a main method Java program basic elements: –Packages –Classes –Data fields –Methods

© 2006 Pearson Addison-Wesley. All rights reserved 1-3 Packages Provide a mechanism for grouping related classes package statement –Indicates a class is part of a package Java assumes all classes in a particular package are contained in same directory Java API consists of many predefined packages

© 2006 Pearson Addison-Wesley. All rights reserved 1-4 Packages import statement –Allows you to use classes contained in other packages Package java.lang is implicitly imported to all Java code

© 2006 Pearson Addison-Wesley. All rights reserved 1-5 Figure 1-1 A simple Java Program

© 2006 Pearson Addison-Wesley. All rights reserved 1-6 Classes Data type that specifies data and methods available for instances of the class An object in Java is an instance of a class Class definition includes –Optional subclassing modifier ( abstract / final ) –Optional access modifier ( public / none) –Keyword class –Optional extends clause –Optional implements clause –Class body

© 2006 Pearson Addison-Wesley. All rights reserved 1-7 Classes Every Java class is a subclass of either –Another Java class –Object class new operator –Creates an object or instance of a class

© 2006 Pearson Addison-Wesley. All rights reserved 1-8 Figure 1-2 Figure 1-2 Components of a class

© 2006 Pearson Addison-Wesley. All rights reserved 1-9 Data Fields Class members that are either variables or constants Data field declarations can contain –Access modifiers –Use modifiers –Modules

© 2006 Pearson Addison-Wesley. All rights reserved 1-10 Figure 1-3 Modifiers used in data field declarations

© 2006 Pearson Addison-Wesley. All rights reserved 1-11 Methods Used to implement operations Should perform one well-defined task Method modifiers –Access modifiers and use modifiers Valued method –Returns a value –Body must contain return expression;

© 2006 Pearson Addison-Wesley. All rights reserved 1-12 Figure 1-4 Modifiers used in a method declaration

© 2006 Pearson Addison-Wesley. All rights reserved 1-13 Methods Syntax of a method declaration access-modifier use-modifiers return-type method-name (formal-parameter-list) { method-body } Arguments are passed by value –Except for objects and arrays A reference value is copied instead Java 1.5 allows a method to have a variable number of arguments of the same type –Using the ellipses (three consecutive dots)

© 2006 Pearson Addison-Wesley. All rights reserved 1-14 Methods Constructor –Special kind of method –Has the same name as the class and no return type –Executed only when an object is created –public method-name() –No return-type A class can contain multiple constructors

© 2006 Pearson Addison-Wesley. All rights reserved 1-15 How to Access Members of an Object Data fields and methods declared public –Name the object, followed by a period, followed by member name Members declared static –Use the class name, followed by a period, followed by member name

© 2006 Pearson Addison-Wesley. All rights reserved 1-16 Language Basics Java application –Collection of classes One class contains the main method Java programs can also be written as applets

© 2006 Pearson Addison-Wesley. All rights reserved 1-17 Useful Java Classes The Object class –Java supports a single class inheritance hierarchy With class Object as the root –More useful methods public String toString() public boolean equals(Object obj) protected void finalize() public int hashCode()

© 2006 Pearson Addison-Wesley. All rights reserved 1-18 Useful Java Classes String classes –Class String Declaration examples: –String title; –String title = “Walls and Mirrors”; Assignment example: –Title = “Walls and Mirrors”; String length example: –title.length(); Referencing a single character –title.charAt(0); Comparing strings –title.compareTo(string2);

© 2006 Pearson Addison-Wesley. All rights reserved 1-19 Useful Java Classes String classes (continued) –Class String Concatenation example: String monthName = "December"; int day = 31; int year = 02; String date = monthName + " " + day + ", 20" + year;

© 2006 Pearson Addison-Wesley. All rights reserved 1-20 Useful Java Classes String classes (continued) –Class StringBuffer Creates mutable strings Provides same functionality as class String More useful methods –public StringBuffer append(String str) –public StringBuffer insert(int offset, String str) –public StringBuffer delete(int start, int end) –public void setCharAt(int index, char ch) –public StringBuffer replace(int start, int end, String str)

© 2006 Pearson Addison-Wesley. All rights reserved 1-21 Useful Java Classes String classes (continued) –Class StringTokenizer Allows a program to break a string into pieces or tokens More useful methods –public StringTokenizer(String str) –public StringTokenizer(String str, String delim) –public StringTokenizer(String str, String delim, boolean returnTokens) –public String nextToken() –public boolean hasMoreTokens()

© 2006 Pearson Addison-Wesley. All rights reserved 1-22 Java Exceptions Exception –Handles an error during execution Throw an exception –To indicate an error during a method execution Catch an exception –To deal with the error condition

© 2006 Pearson Addison-Wesley. All rights reserved 1-23 Catching Exceptions Java provides try-catch blocks –To handle an exception Place statement that might throw an exception within the try block –Must be followed by one or more catch blocks –When an exception occurs, control is passed to catch block Catch block indicates type of exception you want to handle

© 2006 Pearson Addison-Wesley. All rights reserved 1-24 Catching Exceptions try-catch blocks syntax try { statement(s); } catch (exceptionClass identifier) { statement(s); } Some exceptions from the Java API cannot be totally ignored –You must provide a handler for that exception

© 2006 Pearson Addison-Wesley. All rights reserved 1-25 Catching Exceptions Figure 1-9 Flow of control in a simple Java application

© 2006 Pearson Addison-Wesley. All rights reserved 1-26 Catching Exceptions Types of exception –Checked exceptions Instances of classes that are subclasses of java.lang.Exception Must be handled locally or thrown by the method Used when method encounters a serious problem –Runtime exceptions Occur when the error is not considered serious Instances of classes that are subclasses of java.lang.RuntimeException

© 2006 Pearson Addison-Wesley. All rights reserved 1-27 Catching Exceptions The finally block –Executed whether or not an exception is thrown –Can be used even if no catch block is used –Syntax finally { statement(s); }

© 2006 Pearson Addison-Wesley. All rights reserved 1-28 Throwing Exceptions throws clause –Indicates a method may throw an exception If an error occurs during its execution –Syntax public methodName throws ExceptionClassName throw statement –Used to throw an exception at any time –Syntax throw new exceptionClass(stringArgument); You can define your own exception class

© 2006 Pearson Addison-Wesley. All rights reserved 1-29 Summary Java packages –Provide a mechanism for grouping related classes import statement –Required to use classes contained in other packages Object in Java is an instance of a class Class –Data type that specifies data and methods available –Data fields are either variables or constants –Methods implement object behavior Method parameters are passed by value

© 2006 Pearson Addison-Wesley. All rights reserved 1-30 Summary Comments in Java –Comment lines –Multiple-line comments Java identifier –Sequence of letters, digits, underscores, and dollar signs Primitive data types categories –Integer, character, floating point, and boolean Java reference –Used to locate an object

© 2006 Pearson Addison-Wesley. All rights reserved 1-31 Summary Define named constant with final keyword Java uses short-circuit evaluation for logical and relational expressions Array –Collection of references that have the same data type Selection statements –if and switch Iteration statements –while, for, and do

© 2006 Pearson Addison-Wesley. All rights reserved 1-32 Summary String –Sequence of characters –String classes: String, StringBuffer, StringTokenizer Exceptions –Used to handle errors during execution Files are accessed using Scanner class or streams