Java Library Java provides a huge library or collection of useful programs A gold mine of well-tested code that can save you countless hours of development.

Slides:



Advertisements
Similar presentations
Java Packages CSci 1130 Intro to Computer Programming with Java Instructor Tatyana Volk.
Advertisements

 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
 2005 Pearson Education, Inc. All rights reserved Introduction.
Dialogs. Displaying Text in a Dialog Box Windows and dialog boxes –Up to this our output has been to the screen –Many Java applications use these to display.
Introduction to Programming with Java, for Beginners Intro OOP with Java Java Program Structure.
Objectives Learn about objects and reference variables Explore how to use predefined methods in a program.
Overloaded Constructors constructors can be overloaded, like other methods – i.e., a class can define several constructors all constructors must have the.
Chapter 3 Using Classes and Objects. Creating Objects A variable holds either a primitive type or a reference to an object A class name can be used as.
More sophisticated behavior Using library classes to implement some more advanced functionality 4.0.
Chapter 2 storing numbers and creating objects Pages in Horstmann.
CS 225 Java Review. Java Applications A java application consists of one or more classes –Each class is in a separate file –Use the main class to start.
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.
Applying OO Concepts Using Java. In this class, we will cover: Overriding a method Overloading a method Constructors Mutator and accessor methods The.
Programming in Java; Instructor:Moorthy Introduction, Objects, Classes, Libraries1 Programming in Java Introduction.
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.
University of Limerick1 Work with API’s. University of Limerick2 Learning OO programming u Learning a programming language can be broadly split into two.
LESSON 2 CREATING A JAVA APPLICATION JAVA PROGRAMMING Compiled By: Edwin O. Okech [Tutor, Amoud University]
Chapter 2: Objects and Primitive Data Classes and Objects String, Random, Math, NumberFormat, DecimalFormat and Wrapper Classes.
Introduction to Programming Writing Java Beginning Java Programs.
Dale Roberts Procedural Programming using Java Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer and.
Java Packages and Libraries M Taimoor Khan
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,
Math With Java The Math Class. First, A Quick Review of Math Operators in Java Primitive Data type in Java that represent numbers: Primitive Data type.
A First Look at Java Chapter 2 1/29 & 2/2 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010.
Methods in Java. Program Modules in Java  Java programs are written by combining new methods and classes with predefined methods in the Java Application.
Math Class Part of the Java.lang package. This package is from object so you do not have to import the lang package. Static: Math Class is static.
10-Nov-15 Java Object Oriented Programming What is it?
More About Objects and Methods Chapter 5. Outline Programming with Methods Static Methods and Static Variables Designing Methods Overloading Constructors.
Classes and Objects in Java
1 JAVA API & Packages Starring: Java Documentation Co-Starring: BlueJ IDE.
Introduction to Programming Writing Java Beginning Java Programs.
CS0007: Introduction to Computer Programming Classes: Documentation, Method Overloading, Scope, Packages, and “Finding the Classes”
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. A class represents a single concept from the problem domain, or a.
Data Types – Reference Types Objective To understand what reference types are The need to study reference types To understand Java standard packages To.
Introduction to Java Chapter 7 - Classes & Object-oriented Programming1 Chapter 7 Classes and Object-Oriented Programming.
Lesson 6 – Libraries & APIs Libraries & APIs. Objective: We will explore how to take advantage of the huge number of pre-made classes provided with Java.
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
AP Computer Science A – Healdsburg High School 1 static Keyword in Java - Static variables - Static methods.
The Math class Java provides certain math functions for us. The Math class contains methods and constants that can be very useful. The Math class is like.
The Math Class Methods Utilizing the Important Math Operations of Java!
1 Java Library Lecture 9 by Dr. Norazah Yusof. 2 Java Library Java has pre-defined classes that consist of the basic language classes in Java (organized.
Java Classes Chapter 1. 2 Chapter Contents Objects and Classes Using Methods in a Java Class References and Aliases Arguments and Parameters Defining.
Reading input from the console input. Java's console input The console is the terminal window that is running the Java program I.e., that's the terminal.
Georgia Institute of Technology More on Creating Classes part 1 Barb Ericson Georgia Institute of Technology Oct 2005.
Libraries and APIs Don’t reinvent the wheel. What’s an API? API – Application Programmers Interface –We want to use code someone else has written –API’s.
CSE 1030: Implementing Static Features Mark Shtern.
JAVA COLLECTIONS LIBRARY School of Engineering and Computer Science, Victoria University of Wellington COMP T2, Lecture 2 Marcus Frean.
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.
Week 13 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
M1G Introduction to Programming 2 2. Creating Classes: Game and Player.
Slides prepared by Rose Williams, Binghamton University Console Input and Output.
Invoking methods in the Java library. Jargon: method invocation Terminology: Invoking a method = executing a method Other phrases with exactly the same.
Today Javadoc. Packages and static import. Viewing API source code. Upcoming Topics: –protected access modifier –Using the debugger in Eclipse –JUnit testing.
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.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Objects and Classes. F OO Programming Concepts F Creating Objects and Object Reference Variables –Differences between primitive data type and object type.
More Sophisticated Behavior
Java Course Review.
Methods Chapter 6.
Classes, Libraries & Packages
Introduction to Programming with Java, for Beginners
MSIS 655 Advanced Business Applications Programming
Introduction to Computer Science and Object-Oriented Programming
Applying OO Concepts Using Java
Object Oriented Programming in java
Using java libraries CGS3416 spring 2019.
Presentation transcript:

Introduction to Programming with Java, for Beginners Intro to Math Class Concept of static API Tour Intro Random Class

Java Library Java provides a huge library or collection of useful programs A gold mine of well-tested code that can save you countless hours of development time This huge library information is provided in API -Application Programming Interface

The Math Class Description provided in Java API Collection of common math functions (sin, cos, sqrt, etc.). And two constants: PI and E > Math.PI 3.141592653589793 > Math.E 2.718281828459045 > Math.sqrt(25) 5.0 > Math.pow(2,10) 1024 > Math.cos(0) 1.0 > Math.cos(2 * Math.PI)

How the Math Class Works public class Math{ public static final double PI = 3.141592653589793; public static double sin(double d){ .. } public static double sqrt(double d) { .. } private Math(){} .. } > Math.PI 3.141592653589793 > Math.sqrt(25) 5.0

What's different about Math Class It’s different from a typical Java class It is a “stateless” class We only need one Math class (not multiple instances) No need to instantiate it (hence, no public constructor) All of its variables and methods are static static means “applies to the class as a whole” vs. “applies to an individual instance”

Static Data or Methods A class can have data and methods of its own (not part of the objects) For example, Dog class can keep a count of the number of objects it has created class Dog{ static int dogCount; //not an instance variable … Dog(String dogName, int dogAge){ name = dogName; age = dogAge; dogCount++; //short form for dogCount = dogCount +1 }

Static (contd..) In Dr Java >Dog d1 = new Dog(“Fluffy”, 5); >Dog d2 = new Dog(“Fido”, 6); >Dog.dogCount 2 Dog.dogCount is asking Dog class for the count of the dogs created Since dogCount belongs to class and not the object Hence use the classname and not Object name to access variable dogCount More on static topic next time

Using Reading Library (API) We will use API documentation for Java Version 5 Find the documentation for the Math class If you scroll down the lower left panel and click on the link labeled Math, the large "main" panel on the right will display the documentation for the Math class

General Class Interface A class' public interface Notice the four headings in the main panel. Field Summary Has information about public variables Constructor and Method summaries Describe the public constructors and methods Method Detail Provides detail on method inputs (parameter(s))and output (return type) and some extra details The collection of all of a class' public fields, constructors, and methods constitute its "interface", that is, the public face that it shows the world. Note that private variables and methods are not a part of its interface.

Math Class Description Notice the phrase java.lang at the top of the main panel above the word Math This means that the Math class is part of the core Java language and hence can be used directly Math Class Interface Field Summary: Has two constants PI and E Constructor Summary: has no public constructor Methods Summary: many methods all which are static Method Details: e.g. sqrt() takes a double and returns a double

Packages and import Statements If a class is not part of java language i.e. java.lang, you'll see package name What is a package? Basically it's a directory that has a collection of related classes E.g. Random Class description contains: java.util.Random Indicating that the Random code is stored in a file called java/util/Random.class somewhere on your machine. The java/util directory/folder is known as the "util", or utility package. Since Random is not part of Java Language we need to tell Java where to find it by saying import java.util.Random; Another way is to use the asterisk "wildcard character": import java.util.*;

Random Class A class to create Random numbers Constructor Summary shows the objects of this type can be created E.g. Random r = new Random(); Method Summary shows that it can generate random values of types: integers, doubles etc. E.g. r.nextInt(6) – Generate a integer numbers between 0 (inclusive) and 6 (exclusive) How do I generate a number between 1 and 6 ?