Chapter 3 - Java Programming With Supplied Classes1 Chapter 3 Java Programming With Supplied Classes.

Slides:



Advertisements
Similar presentations
 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Advertisements

CERTIFICATION OBJECTIVES Use Class Members Develop Wrapper Code & Autoboxing Code Determine the Effects of Passing Variables into Methods Recognize when.
Java Software Solutions
ECE122 L4: Creating Objects February 8, 2007 ECE 122 Engineering Problem Solving with Java Lecture 4 Creating and Using Objects.
Lecture 27 Exam outline Boxing of primitive types in Java 1.5 Generic types in Java 1.5.
Chapter Day 6. © 2007 Pearson Addison-Wesley. All rights reserved2-2 Agenda Day 5 Questions from last Class?? Problem set 1 DUE Problem set 2 posted 
1 Chapter 2 Introductory Programs. 2 Getting started To create and run a Java program –Create a text file with a.java extension for the source code. For.
Chapter 3 Using Classes and Objects 5 TH EDITION Lewis & Loftus java Software Solutions Foundations of Program Design © 2007 Pearson Addison-Wesley. All.
Review CSC 171 FALL 2004 LECTURE 21. Topics Objects and Classes Fundamental Types Graphics and Applets Decisions Iteration Designing Classes Testing and.
Chapter 3 Using Classes and Objects. 2 Creating Objects  A variable holds either a primitive type or a reference to an object  A class name can be used.
Static Class Members Wrapper Classes Autoboxing Unboxing.
Java Data Types  Everything is an Object  Except Primitive Data Types  For efficiency  Platform independent  Portable  “slow”  Objects are often.
Java Programming, 2E Introductory Concepts and Techniques Chapter 2 Creating a Java Application and Applet.
Chapter 11 Abstract Classes and Interfaces 1. Abstract method New modifier for class and method: abstract An abstract method has no body Compare: abstract.
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
Java Programming, Second Edition Chapter Four Advanced Object Concepts.
Java Programming, 3e Concepts and Techniques Chapter 2 - Part 2 Creating a Java Application and Applet.
Chapter 2: Objects and Primitive Data Classes and Objects String, Random, Math, NumberFormat, DecimalFormat and Wrapper Classes.
Introduction to Java Programming. History F James Gosling and Sun Microsystems F Oak F Java, May 20, 1995, Sun World F HotJava –The first Java-enabled.
Chapter 1: Introducing JAVA. 2 Introduction Why JAVA Applets and Server Side Programming Very rich GUI libraries Portability (machine independence) A.
1 The String Class Every character string is an object in Java, defined by the String class Every string literal, delimited by double quotation marks,
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
1 Programming Java Java Basics. 2 Java Program Java Application Program Application Program written in general programming language Applet Program running.
1 Generics Chapter 21 Liang, Introduction to Java Programming.
Fall 2015CISC124 - Prof. McLeod1 CISC124 Have you filled out the lab section survey? (As of last night 54 left to fill out the survey.) TA names have been.
Class Libraries Chapter 1 1 Source Intro to Java Programming Y. Daniel Liang.
Chapter 7: Characters, Strings, and the StringBuilder.
CS-2851 Dr. Mark L. Hornick 1 Generic Java Classes Implementing your own generic classes.
Chapter 14 Abstract Classes and Interfaces. Abstract Classes An abstract class extracts common features and functionality of a family of objects An abstract.
Abstract Classes and Interfaces Chapter 9 CSCI 1302.
Using Classes and Objects. We can create more interesting programs using predefined classes and related objects Chapter 3 focuses on: Object creation.
Chapter 5 Objects and Classes Inheritance. Solution Assignments 3 & 4 Review in class…..
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6: Transition to Java Programming with Alice and Java First Edition.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 9 Inheritance and.
Chapter 7: Class Inheritance F Superclasses and Subclasses F Keywords: super and this F Overriding methods F The Object Class F Modifiers: protected, final.
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
Creating a Java Application and Applet
Duke CPS From C++ to Java l Java history: Oak, toaster-ovens, internet language, panacea l What it is ä O-O language, not a hybrid (cf. C++)
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Agenda Comments Identifiers Keywords Syntax and Symentics Indentation Variables Datatype Operator.
Java Programming, Second Edition Chapter Twelve Advanced Inheritance Concepts.
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.
1 Wrapper Classes  Sometimes we want to use a primitive type as an object, so there are wrapper classes that will help us.  In particular, we need to.
Java Software Solutions Foundations of Program Design Sixth Edition by Lewis & Loftus Chapter 3: Using Classes and Objects Coming up: Creating Objects.
3-1 Creating Objects A variable holds either a primitive type or a reference to an object A class name can be used as a type to declare an object reference.
Object-Oriented Application Development Using VB.NET 1 Chapter 4 VB.NET Programming with Supplied Classes.
In this class, we will cover: Overriding a method Overloading a method Constructors Mutator and accessor methods The import statement and using prewritten.
Chapter 4: More Object Concepts. Objectives Understand blocks and scope Overload a method Avoid ambiguity Create and call constructors with parameters.
© 2006 Pearson Addison-Wesley. All rights reserved 1-1 Chapter 1 Review of Java Fundamentals.
Know Your Java. Java is special Java source code Byte code/ native code Object code on windows Object code on Dos Object code on Lynux.
Object Oriented Programming Object and Classes Lecture 3 MBY.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
Object Oriented Programming Lecture 2: BallWorld.
1 Creating Objects  A variable holds either a primitive type or a reference to an object  A class name can be used as a type to declare an object reference.
 It is a pure oops language and a high level language.  It was developed at sun microsystems by James Gosling.
Objects and Classes. F OO Programming Concepts F Creating Objects and Object Reference Variables –Differences between primitive data type and object type.
More About Objects and Methods
Variables in Java A variable holds either
Java Programming: Guided Learning with Early Objects
JAVA MULTIPLE CHOICE QUESTION.
Yanal Alahmad Java Workshop Yanal Alahmad
More About Objects and Methods
CS230 Tutorial Week 3.
Classes, Libraries & Packages
Java Programming Language
The Building Blocks Classes: Java class library, over 1,800 classes:
More About Objects and Methods
Programs and Classes A program is made up from classes
Chapter 13 Abstract Classes and Interfaces Part 01
Presentation transcript:

Chapter 3 - Java Programming With Supplied Classes1 Chapter 3 Java Programming With Supplied Classes

Chapter 3 - Java Programming With Supplied Classes2 Topics Packages and classes supplied w/ JDK The String class and its methods Declaring and accessing a String array The Vector, Calendar, and Date classes Data wrapper classes Writing and executing an applet Controlling font and color

Chapter 3 - Java Programming With Supplied Classes3 Using the Packages and Classes Supplied with Java Java Development Kit (JDK) –JDK 1.4 Consists of 135 packages, 2,991 pre-defined classes and interfaces ~ JDK 1.3: 76 and 1,842 –Package –Group of related classes (class library) –Keywords import –Gives the compiler access to the classes package –Assign classes to a particular package

Chapter 3 - Java Programming With Supplied Classes4

5 Using the String Class String Class –Member of java.lang package Automatically imported by Java compiler –Two ways to instantiate a String object Similar to primitives: –String a = Hello World”; Use the keyword new: –String s = new String(“Hello World”);

Chapter 3 - Java Programming With Supplied Classes6

7 Using the String Class Method types –Instance methods (nonstatic) Associated with a specific instance of the class Use reference variable to invoke e.g., s.length() –Class methods (static) Not associated with any instance Use class name to invoke Contain keyword static in their headers e.g., Student.getTotalNumberOfStudents()

Chapter 3 - Java Programming With Supplied Classes8

9 Using the String Class NullPointerException –Results from attempting to invoke an instance method using a reference variable that hasn’t been initialized Immutable –Refers to the fact that Java Strings cannot be changed Methods that “change” a string value actually return a new String instance

Chapter 3 - Java Programming With Supplied Classes10 Creating a String Array Array declaration –String stringArray[] = new String[4]; Creates array of 4 String objects Array elements are String objects Array element instantiation –stringArray[0] = new String(“Hello”); Must be performed for each element

Chapter 3 - Java Programming With Supplied Classes11

Chapter 3 - Java Programming With Supplied Classes12 Creating a String Array Comparing String objects –Reference variables contain references, not data values –Cannot use comparison operator (==) –Must use String methods: equals equalsIgnoreCase

Chapter 3 - Java Programming With Supplied Classes13 Using the Vector Class Vector Class –Contained in java.util package must be imported –Array that is dynamically resizable Can contain different class data types Cannot contain primitive data types (need wrapper) –Declaration Vector v = new Vector(3); –Creates vector with three elements

Chapter 3 - Java Programming With Supplied Classes14

Chapter 3 - Java Programming With Supplied Classes15 Working With Dates Classes for Working with Date Values –In java.util package: Calendar Class –Contains methods and constants Date Class –An instance contains the actual date value

Chapter 3 - Java Programming With Supplied Classes16 Working With Dates Classes for Working with Date Values –In java.text package: DateFormat Class –An instance provides several data formats for display purposes

Chapter 3 - Java Programming With Supplied Classes17 Wrapper Classes –Contains primitive data inside an object instance –Reside in java.lang package –Named same as primitive counterpart with the first letter capitalized e.g., double  Double, float  Float, etc. Except for Integer: int  Integer Using Wrapper Classes

Chapter 3 - Java Programming With Supplied Classes18 Using Wrapper Classes Converting Primitive to Wrapper and Back –Primitive to wrapper: Instantiate the appropriate wrapper class using the primitive variable as the argument Double d; Double doubleWrapper = new Double( d ); –Wrapper to primitive: Use instance method named xxxValue (where xxx is the primitive data type) e.g., d = doubleWrapper.doubleValue( );

Chapter 3 - Java Programming With Supplied Classes19

Chapter 3 - Java Programming With Supplied Classes20 Using Wrapper Classes Converting String to Primitive and Back –String to primitive : Use instance method named parsexxx (where xxx is the primitive data type) –String s1 =new String(“2.2”); –doublePrimitive = s.parseDouble(s1); –Wrapper to primitive: Use instance method named toString that creates a String instance containing the primitive value –String s2 = Double.toString(doublePrimitive);

Chapter 3 - Java Programming With Supplied Classes21 Using Wrapper Classes Converting String to Wrapper and Back –String to wrapper: Use static wrapper method named valueOf that creates a wrapper instance from String instance –doubleWrapper = Double.valueof(s1); –Wrapper to String: Use wrapper method named toString that creates a String instance from the wrapper instance –s1 = doubleWrapper.toString( );

Chapter 3 - Java Programming With Supplied Classes22 Using the Applet Class Writing a Simple Applet –Import: Graphics Class from java.awt package Applet Class from java.applet package –Subclass of Panel: »GUI window without a title bar –Executed in a browser window Requires HTML file

Chapter 3 - Java Programming With Supplied Classes23

Chapter 3 - Java Programming With Supplied Classes24

Chapter 3 - Java Programming With Supplied Classes25 Using the Applet Class Controlling Color and Font –Font Class from java.awt package Specifies font name, style, and size –Color Class from java.awt package Specifies colors –Wild card character Used with import statements to specify multiple classes import java.awt.*

Chapter 3 - Java Programming With Supplied Classes26

Chapter 3 - Java Programming With Supplied Classes27

Chapter 3 - Java Programming With Supplied Classes28