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

Similar documents
Programs as Models. Procedural Paradigm. Class Methods. CS256 Computer Science I Kevin Sahr, PhD. Lecture 11: Objects

Object-Based Programming. Programming with Objects

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

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

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

Using Classes and Objects. Chapter

JAVA: A Primer. By: Amrita Rajagopal

AP Computer Science Unit 1. Writing Programs Using BlueJ

AP Computer Science Unit 1. Writing Programs Using BlueJ

Packages & Random and Math Classes

CS 211: Existing Classes in the Java Library

CITS1001 week 6 Libraries

Introduction to Java Unit 1. Using BlueJ to Write Programs

Topics. The Development Process

CSCI 2010 Principles of Computer Science. Basic Java Programming. 08/09/2013 CSCI Basic Java 1

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

4. Java language basics: Function. Minhaeng Lee

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

Using Classes and Objects

Using Classes and Objects

Formatting Output & Enumerated Types & Wrapper Classes

ECE 122. Engineering Problem Solving with Java

ECE 122. Engineering Problem Solving with Java

Programming with Java

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

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

Classes and Objects Miscellany: I/O, Statics, Wrappers & Packages. CMSC 202H (Honors Section) John Park

Advanced Computer Programming

CS/ENGRD 2110 FALL Lecture 7: Interfaces and Abstract Classes

c) And last but not least, there are javadoc comments. See Weiss.

Classes and Objects Part 1

Using Java Classes Fall 2018 Margaret Reid-Miller

Lesson 10: Quiz #1 and Getting User Input (W03D2)

COMP6700/2140 Std. Lib., I/O

Java Methods. Lecture 8 COP 3252 Summer May 23, 2017

JAVA Ch. 4. Variables and Constants Lawrenceville Press

Introduction to Java Applications

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

CSCE 120: Learning To Code

Midterms Save the Dates!

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

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

Example packages from Java s standard class library:

CompSci 125 Lecture 05. Programming Style, String Class and Literals, Static Methods, Packages

Object Oriented Programming. Java-Lecture 1

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

Classes and Objects. CGS 3416 Spring 2018

CS/ENGRD 2110 SPRING Lecture 7: Interfaces and Abstract Classes

Object-Oriented Programming Concepts

More About Objects and Methods. Objectives. Outline. Chapter 6

More About Objects and Methods

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

2.8. Decision Making: Equality and Relational Operators

CMPSCI 187: Programming With Data Structures. Lecture 6: The StringLog ADT David Mix Barrington 17 September 2012

CSE1720 Delegation Concepts (Ch 2)

Classes and Objects. COMP1400/INFS1609 Week 8. Monday, 10 September 12

COSC 123 Computer Creativity. Introduction to Java. Dr. Ramon Lawrence University of British Columbia Okanagan

Computational Expression

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

Using Classes and Objects

CS 302: INTRODUCTION TO PROGRAMMING IN JAVA. Lecture 16

Università degli Studi di Bologna Facoltà di Ingegneria. Principles, Models, and Applications for Distributed Systems M

More About Objects and Methods

CEN 414 Java Programming

Encapsulation. Administrative Stuff. September 12, Writing Classes. Quick review of last lecture. Classes. Classes and Objects

Lecture Transcript While and Do While Statements in C++

4. Java Project Design, Input Methods

Lab5. Wooseok Kim

CS61BL. Lecture 1: Welcome to CS61BL! Intro to Java and OOP Testing Error-handling

ECE 122. Engineering Problem Solving with Java

Full file at

Lecture 12 Modular Programming with Functions

static int min(int a, int b) Returns the smaller of two int values. static double pow(double a,

Fundamental Concepts and Definitions

After a lecture on cosmology and the structure of the solar system, William James was accosted by a little old lady.

COMP 110 Programming Exercise: Simulation of the Game of Craps

Object Oriented Programming. Java-Lecture 6 - Arrays

Lab5. Wooseok Kim

First Java Program - Output to the Screen

University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner

Object-Oriented Principles and Practice / C++

I. Variables and Data Type week 3

! Rest of Chap 2 ! 2.3-4, ! Rest of Chap 4 ! ! Things that do not vary. ! unlike variables. ! will never change. !

PIC 20A The Basics of Java

The first applet we shall build will ask the user how many times the die is to be tossed. The request is made by utilizing a JoptionPane input form:

false, import, new 1 class Lecture2 { 2 3 "Data types, Variables, and Operators" 4

First Programs. CSE 1310 Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington

CS 106 Introduction to Computer Science I

Computer Science 210: Data Structures

First Programs. CSE 1310 Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington

CS/ENGRD 2110 SPRING 2018

AS COMPUTERS AND THEIR USER INTERFACES have become easier to use,

CS110: PROGRAMMING LANGUAGE I

26. Interfaces. Java. Fall 2009 Instructor: Dr. Masoud Yaghini

CSCI 1103: File I/O, Scanner, PrintWriter

More About Objects and Methods

Everything is an object. Almost, but all objects are of type Object!

Professor Hugh C. Lauer CS-1004 Introduction to Programming for Non-Majors

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

CS 335 Lecture 02 Java Programming

Transcription:

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

Intro to Libraries We ve barely scratched the surface of Java, but before we proceed with programming concepts, we need to talk about the Java API. We would also like to be able to start using existing libraries in the Java SDK as quickly as possible. To that aim, this outline will provide just enough to illustrate basic core usage of existing Java class libraries. The Java API can be found at https://docs.oracle.com/javase/8/docs/api/

User vs. Builder With any re-usable programming construct, we have two points-of-view that should always be considered: The builder is responsible for declaring and defining how some module works. The user (or caller) is somebody (i.e. some portion of code, often some other module) that makes use of an existing module to perform a task. For the purposes of this topic (Using Java Libraries), we are looking at things from the user s perspective. In other words, what do we need to know to use an existing Java library from the SDK, along with it s various already-defined features. We will look at how to build things like functions, classes, interfaces, etc. later on.

What s in the Java SDK? There are multiple kinds of library constructs to consider, including: classes interfaces packages classes and interfaces with generic type parameters Classes and interfaces are grouped into packages. packages are named into categories and subcategories, separated by the dot-operator. Examples of packages: java.lang java.util java.util.concurrent

What s in the Java SDK? If a class is inside a package, we can refer to the whole name by referring to the package name, dot-operator, then class name. Examples: java.lang.string java.util.scanner classes and interfaces can contain: fields (i.e. data variables) methods (i.e. member functions) Right now, we will focus on the usage of class libraries.

The import Statement If you are using any item from the package java.lang, you don t need to do anything special. Everything from java.lang is automatically imported for use into every Java program you write. For a class out of any other package, you need to put an import statement at the top of your file, to let the Java tools (compiler and loader) know what libraries to pull in. Basic form: import <package name>.<class name>; Examples: import java.util.scanner; import javax.swing.jframe; import java.awt.geom.generalpath;

The import Statement Wildcards - if you are going to be using many classes from the same package, you can tell the compiler to import all classes from a single package with the * wildcard character (meaning all ), in place of a single class name. Examples: import javax.swing.*; // imports all classes in javax.swing package import java.util.*; // imports all classes in java.util package Note that in this last one, for example, it does not import all classes in the sub-package java.util.concurrent. It only imports classes directly inside the base package that is specified.

API Descriptions The API description for a Java class gives all of the information you need to be able to syntactically use it correctly. Starts with description of the class, in a general documentation format. Field Summary This section lists data fields that you might want to use Often, these are constants, but not always This chart lists the variable names, descriptions, and their types Constructor Summary This section lists the constructor methods that are available for this class Constructors are related to the creation of objects This chart provides the parameter list for each constructor option

API Descriptions Method Summary This section lists the methods that are available for this class For general class usage, this will typically be the most relevant set of features that you will want to call upon This chart provides the full prototype, or declaration, of each method first column shows the return type, and whether the method is static or not (more on this later) Second column provides method name, as well as list of expected parameters, and a short description For all of these items, the names (of the variables, constructors, and methods) are also links to more detailed descriptions of the items, which are further down the page.

static fields and methods Some fields and methods are declared as static In the Field Summary and/or Method Summary, this information would show up in the left column. If a variable or method is not declared with the word static, then we call it an instance variable or method. To call upon variables or methods from a class, we use the dot-operator syntax. There is a difference between static and instance items, though. For a static variable or method, we use this format: classname.fieldname // fields classname.methodname(arguments) // methods

java.lang.math Library API: java.lang.math Note that all fields and methods in this class are static class Math has two fields, which are common mathematical constants. Sample usage: double area = Math.PI * radius * radius; // compute area of a circle Sample calls to static methods from Math: area = Math.PI * Math.pow(radius, 2); // area of circle, using power method y = Math.abs(x); // computes absolute value of x System.out.print(Math.random()); // prints random value in range [0,1) int die = (int)(math.random() * 6) + 1; // roll a standard 6-sided die

Instance Fields and Methods Recall that an instance field or method is one that is not declared to be static. Instance is the default. To call upon instance fields or methods in a class library, you have to create one or more objects from that class A class is a blueprint for building objects. Syntax for building an object: classname variable = new classname(parameter(s)); In this format, the first part is the declaration of a reference variable classname variablename new is a keyword of the language, and that part of the statement builds a new object, and runs a special initialization function called a constructor. This is what the parameters are for.

Examples Scanner input = new Scanner(System.in); JButton mybutton = new JButton("Click Me"); String s1 = new String(); Once you have declared one or more objects, call upon fields and methods with the dot-operator, as before, but for instance members, use the object s name (i.e. the reference variable) on the left of the dot: objectname.fieldname // fields objectname.methodname(arguments) // methods Example uses: int x = input.nextint(); mybutton.settext("stop clicking me!"); System.out.print(s1.toUpperCase());

java.util.random Library API: java.util.random This library is for generating pseudo-random numbers How computers do random number generation It s really a pseudo-random generator Start with a seed value The seed is used as the input to an algorithm, which generates a seemingly randomized number Each random value generated becomes the seed for the next one Start with the same seed, and you ll get the same random numbers!

Some Examples Creating objects of this type: Random r1 = new Random(); // uses the system time to create seed Random r2 = new Random(1234); // uses 1234 as the seed In the above statements, r1 and r2 refer to objects of type Random they both can generate a pseudo-random sequence of values Sample calls to these objects: int x = r1.nextint(); // gets a random integer int y = r1.nextint(10); // gets a random integer from 0-9 double z = r1.nextdouble(); // gets a random double in range [0,1)