Class Libraries and Packages

Similar documents
Object-Oriented Programming

Programs, Statements, Variables

Arrays. Arrays. Wolfgang Schreiner Research Institute for Symbolic Computation (RISC) Johannes Kepler University, Linz, Austria

Objects and Classes. Objects and Classes

Methods. Methods. Wolfgang Schreiner Research Institute for Symbolic Computation (RISC) Johannes Kepler University, Linz, Austria

Computational Expression

Introduction to Programming Using Java (98-388)

CHETTINAD COLLEGE OF ENGINEERING & TECHNOLOGY JAVA

Java Intro 3. Java Intro 3. Class Libraries and the Java API. Outline

1. Java is a... language. A. moderate typed B. strogly typed C. weakly typed D. none of these. Answer: B

B2.52-R3: INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING THROUGH JAVA

OBJECT ORIENTED PROGRAMMING TYm. Allotted : 3 Hours Full Marks: 70

1 Shyam sir JAVA Notes

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

Packages & Random and Math Classes

PIC 20A The Basics of Java

Getting started with Java

Abstract Data Types. Abstract Data Types

(A) 99 (B) 100 (C) 101 (D) 100 initial integers plus any additional integers required during program execution

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

Objectives of CS 230. Java portability. Why ADTs? 8/18/14

USING LIBRARY CLASSES

Introduction to Java

Example packages from Java s standard class library:

Java Bytecode (binary file)

ITI Introduction to Computing II

Lecture 2. COMP1406/1006 (the Java course) Fall M. Jason Hinek Carleton University

Interpreted vs Compiled. Java Compile. Classes, Objects, and Methods. Hello World 10/6/2016. Python Interpreted. Java Compiled

An overview of Java, Data types and variables

(A) 99 ** (B) 100 (C) 101 (D) 100 initial integers plus any additional integers required during program execution

Crash Course in Java. Why Java? Java notes for C++ programmers. Network Programming in Java is very different than in C/C++

Array. Prepared By - Rifat Shahriyar

CS321 Languages and Compiler Design I. Winter 2012 Lecture 2

Data Types Reference Types

A variable is a name for a location in memory A variable must be declared

Lab5. Wooseok Kim

CSCE3193: Programming Paradigms

Formatting Output & Enumerated Types & Wrapper Classes

Getting started with Java

Praktische Softwaretechnologie

Lab5. Wooseok Kim

Introduction to Programming (Java) 4/12

1. An operation in which an overall value is computed incrementally, often using a loop.

CSC 1214: Object-Oriented Programming

Java Foundations Certified Junior Associate

CS313D: ADVANCED PROGRAMMING LANGUAGE

CS 251 Intermediate Programming Methods and Classes

CS 251 Intermediate Programming Methods and More

3. Java - Language Constructs I

cis20.1 design and implementation of software applications I fall 2007 lecture # I.2 topics: introduction to java, part 1

Java How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved.

CS 231 Data Structures and Algorithms, Fall 2016

CSC Java Programming, Fall Java Data Types and Control Constructs

Methods (Deitel chapter 6)

Inheritance 2. Inheritance 2. Wolfgang Schreiner Research Institute for Symbolic Computation (RISC) Johannes Kepler University, Linz, Austria

CT 229 Fundamentals of Java Syntax

Methods (Deitel chapter 6)

INDEX. A SIMPLE JAVA PROGRAM Class Declaration The Main Line. The Line Contains Three Keywords The Output Line

Chapter 2: Basic Elements of Java

CS 335 Lecture 02 Java Programming

The RISC ProgramExplorer Tutorial and Manual 1

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

CS260 Intro to Java & Android 03.Java Language Basics

PIC 20A Number, Autoboxing, and Unboxing

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

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.

S.E. Sem. III [CMPN] Object Oriented Programming Methodology

Binghamton University. CS-140 Fall Data Types in Java

LECTURE 2 (Gaya College of Engineering)

Overview The Java Modeling Language (Part 1) Related Work

Selected Questions from by Nageshwara Rao

Creating Classes and Objects

Classes, Structs and Records. Internal and External Field Access

THE CONCEPT OF OBJECT

OOSD. Introduction to JAVA. Giuseppe Lipari Scuola Superiore Sant Anna Pisa. September 29, 2010

Review for Test 1 (Chapter 1-5)

Lecture Notes CPSC 224 (Spring 2012) Today... Java basics. S. Bowers 1 of 8

PROGRAMMING FUNDAMENTALS

Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub

BASIC COMPUTATION. public static void main(string [] args) Fundamentals of Computer Science I

Java Identifiers, Data Types & Variables

Course information. Petr Hnětynka 2/2 Zk/Z

User Defined Classes Part 2. CS 180 Sunil Prabhakar Department of Computer Science Purdue University

Java Primer. CITS2200 Data Structures and Algorithms. Topic 2

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

COMP 250 Winter 2011 Reading: Java background January 5, 2011

The Java programming environment. The Java programming environment. Java: A tiny intro. Java features

CS 11 java track: lecture 1

CS2141 Software Development using C/C++ C++ Basics

Lecture 14 CSE11 Fall 2013 For loops, Do While, Break, Continue

data_type variable_name = value; Here value is optional because in java, you can declare the variable first and then later assign the value to it.

Chapter 2: Using Data

boolean, char, class, const, double, else, final, float, for, if, import, int, long, new, public, return, static, throws, void, while

Section 2.2 Your First Program in Java: Printing a Line of Text

Course information. Petr Hnětynka 2/2 Zk/Z

Full file at

Packages: Putting Classes Together

CS 335 Java Programming Controls. Fall 2007

Basic Operations jgrasp debugger Writing Programs & Checkstyle

COMP-202: Foundations of Programming. Lecture 5: Arrays, Reference Type, and Methods Sandeep Manjanna, Summer 2015

Transcription:

Class Libraries and Packages Wolfgang Schreiner Research Institute for Symbolic Computation (RISC) Johannes Kepler University, Linz, Austria Wolfgang.Schreiner@risc.jku.at http://www.risc.jku.at Wolfgang Schreiner RISC

Class Libraries Class library: a set of classes supporting program development. Java API: class library shipped with every Java system. Class libraries are grouped into several packages. Package: a set of classes with related functionality. Some 50 packages in the Java API. java.applet Create applets java.awt Abstract Windowing Toolkit java.io Input and output functions java.lang General language support java.net Computing across the network java.util General utilities http://docs.oracle.com/javase/8/docs/api Wolfgang Schreiner 1

Class Names A class name is only unique within a package. Classes in java.lang are automatically available. Extension of the Java language. Example: String, Integer,... Classes in other packages can be referenced by qualified names: package.class Example: java.util.vector Typical: import of classes from other packages. Wolfgang Schreiner 2

Class Import Classes can be imported into a class declaration. Class name can be used without qualification by package name. import package.class ; import package.*; class MyClass {... } import package.class ; Class can be used without qualification. import java.util.vector; import package.*; All classes of package can be used without qualification. import java.util.*; Wolfgang Schreiner 3

Writing Packages Place all classes of a package into a subdirectory of the same name. /home/ws/ Main.java cs1/ Input.java A.java Package cs1. Classes Input and A. Compilation: javac cs1/input.java Usage: import cs1.*; class Main {... } Wolfgang Schreiner 4

Classes in Packages Classes in packages must be especially declared. package cs1; import...; public class Input {... } Prefix package package ; Class belongs to package. Keyword public exports the class. Class can be used from classes in other packages. Without public, class can be only used within package. Place your program classes into a separate package. Wolfgang Schreiner 5

Nested Packages Packages may be arbitarily nested. /home/ws/ Main.java cs1/ assert/ A.java Package cs1.assert Declaration: package cs1.assert; public class A {... } Compilation: javac cs1/assert/a.java Usage: import cs1.assert.*; Wolfgang Schreiner 6

The Package java.lang Extension of the Java language. Boolean A wrapper for type boolean Byte A wrapper for type byte Character A wrapper for type char and utility methods Double A wrapper for type double and utility methods Float A wrapper for type float and utility methods Integer A wrapper for type int and utility methods Long A wrapper for type long and utility methods Math Various numerical (floating point) operations Short A wrapper for type short String Sequences of characters System Several useful class fields and class methods Study the classes in this package in detail. Wolfgang Schreiner 7

Example System.out.println(string ) Class System in package java.lang Class name is automatically imported. Class variable out in class System static PrintStream out Class PrintStream is in package java.io. Object method println in class PrintStream. void println(string) Use the hypertext manual to look up the definitions. Wolfgang Schreiner 8

Wrapper Classes For every primitive type, there is a wrapper class. int val = 7; Integer i = new Integer(val); int value = i.intvalue(); Wraps an int value into an Integer object. Extracts the int value from the Integer object. Autoboxing: automatic wrapping/unwrapping when needed. When an object pointer rather than an atomic value is needed. Thus the use of wrapper classes is often hidden from the user. Other: Byte, Short, Long, Character, Float, Double, Boolean. Wolfgang Schreiner 9

Example: Separating Words We write a program that reads a string which contains multiple words (sequences of letters) separated by other characters. The program then prints all words of the text in separate lines in the order of their occurrence in the text. For instance, the input One, two, and three! shall result in output One two and three For this purpose, we may use the Character method public static boolean isletter(char ch) which returns true if and only if character ch denotes a letter. Wolfgang Schreiner 10

Main Method public static void main(string[] args) { String text = Input.readString(); //... error check omitted printwords(text); } // printwords(text) // prints each word of text on a separate line // Input: text is not null // Effect: let a word be a sequence of letters. // This method prints all word of text in the // order of their occurrences on separate lines. public static void printwords(string text) Wolfgang Schreiner 11

Core Method public static void printwords(string text) { int i = 0; final int end = text.length(); while (i < end) { int a = wordbegin(text, i); assert(a == end Character.isLetter(text.charAt(a))); if (a == end) break; int b = wordend(text, a); assert(b == end!character.isletter(text.charat(b))); System.out.println(text.substring(a, b)); i = b+1; } } Wolfgang Schreiner 12

Specification of Auxiliary Methods // p = wordbegin(text, i) // p is the position of the first letter at or after i // Input: text is not null, i < length(text) // Output: i <= p <= length(text) // text at j is not a letter for every i <= j < p. // if p < length(text), then text at p is a letter. static int wordbegin(string text, int i) // p = wordend(text, i) // p is the position of the first non-letter after i // Input: text is not null, i < length(text) // Output: i < p <= length(text) // text at j is a letter for every i < j < p. // if p < length(text), then text at p is not a letter. static int wordend(string text, int i) Wolfgang Schreiner 13

Implementation of Auxiliary Methods static int wordbegin(string text, int i) { int a = i; final int end = text.length(); while (a < end &&!Character.isLetter(text.charAt(a))) a = a+1; return a; } static int wordend(string text, int a) { int b = a+1; final int end = text.length(); while (b < end && Character.isLetter(text.charAt(b))) b = b+1; return b; } Wolfgang Schreiner 14