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.

Slides:



Advertisements
Similar presentations
Introduction to Java 2 Programming Lecture 3 Writing Java Applications, Java Development Tools.
Advertisements

OO Programming in Java Objectives for today: Overriding the toString() method Polymorphism & Dynamic Binding Interfaces Packages and Class Path.
Chapter 1 Object-Oriented Concepts. A class consists of variables called fields together with functions called methods that act on those fields.
Generics and the ArrayList Class
Games and Simulations O-O Programming in Java The Walker School
 2005 Pearson Education, Inc. All rights reserved Introduction.
ECE122 L4: Creating Objects February 8, 2007 ECE 122 Engineering Problem Solving with Java Lecture 4 Creating and Using Objects.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
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.
Using the Java API
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.
Chapter 14 Generics and the ArrayList Class Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
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.
3.1 Documentation & Java Language Elements Purpose of documentation Assist the programmer with developing the program Assist other programers who.
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
University of Limerick1 Work with API’s. University of Limerick2 Learning OO programming u Learning a programming language can be broadly split into two.
Applications in Java Towson University *Ref:
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.
1 CSC 221: Introduction to Programming Fall 2012 Functions & Modules  standard modules: math, random  Python documentation, help  user-defined functions,
Recap (önemli noktaları yinelemek) from last week Paradigm Kay’s Description Intro to Objects Messages / Interconnections Information Hiding Classes Inheritance.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Week1 Using the Library (the Java API) Classes are grouped together in packages –To use a class you have to know which package it is in –Every package.
Chapter 4: Loops and Files
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,
Objects and Classes Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Copyright © 2006 by Maria Litvin, Gary.
CHAPTER 10: CORE MECHANICS Definitions and Mechanisms.
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Chapter 14 Generics and the ArrayList Class Slides prepared by Rose Williams, Binghamton University Copyright © 2008 Pearson Addison-Wesley. All rights.
Methods in Java. Program Modules in Java  Java programs are written by combining new methods and classes with predefined methods in the Java Application.
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.
1 JAVA API & Packages Starring: Java Documentation Co-Starring: BlueJ IDE.
1 FUNCTIONS - I Chapter 5 Functions help us write more complex programs.
Chapter 9: A Second Look at Classes and Objects
Chapter 3 Introduction To Java. OBJECTIVES Packages & Libraries Statements Comments Bytecode, compiler, interpreter Outputting print() & println() Formatting.
CS0007: Introduction to Computer Programming Classes: Documentation, Method Overloading, Scope, Packages, and “Finding the Classes”
Methods: A Deeper Look. Template for Class Definition public class { } A.Import Statement B.Class Comments C.Class Name D.Data members E.Methods (inc.
Introduction to Java Chapter 7 - Classes & Object-oriented Programming1 Chapter 7 Classes and Object-Oriented Programming.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
CS170 ygao JAVA, C4Slide 1. CS170 ygao JAVA, C4Slide 2.
Chapter 7: Class Inheritance F Superclasses and Subclasses F Keywords: super and this F Overriding methods F The Object Class F Modifiers: protected, final.
The Math Class Methods Utilizing the Important Math Operations of Java!
Modularity Computer Science 3. What is Modularity? Computer systems are organized into components called modules. The extent to which this is done is.
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
1 Chapter 6 Programming with Objects and Classes F OO Programming Concepts F Creating Objects and Object Reference Variables –Differences between primitive.
Java Software Solutions Foundations of Program Design Sixth Edition by Lewis & Loftus Chapter 3: Using Classes and Objects Coming up: Creating Objects.
Chapter 5 Defining Classes II Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
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.
In this class, we will cover: Overriding a method Overloading a method Constructors Mutator and accessor methods The import statement and using prewritten.
1 Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
SourceAnatomy1 Java Source Anatomy Barb Ericson Georgia Institute of Technology July 2008.
M1G Introduction to Programming 2 2. Creating Classes: Game and Player.
Object Oriented Programming Object and Classes Lecture 3 MBY.
Using Classes and Objects We can create more interesting programs using predefined classes and related objects Chapter 3 focuses on: object creation and.
© 2004 Pearson Addison-Wesley. All rights reserved September 5, 2007 Packages & Random and Math Classes ComS 207: Programming I (in Java) Iowa State University,
Today Javadoc. Packages and static import. Viewing API source code. Upcoming Topics: –protected access modifier –Using the debugger in Eclipse –JUnit testing.
Lecture 9: Object and Classes Michael Hsu CSULA. 2 OO Programming Concepts Object-oriented programming (OOP) involves programming using objects. An object.
What is an object?. What Makes an Object? An object has identity (it acts as a single whole). Every object has a name that identifies what it is. Ex.
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.
Chapter 4 - Finishing the Crab Game
Classes, Libraries & Packages
MSIS 655 Advanced Business Applications Programming
Object Oriented Programming in java
Chapter 7 Objects and Classes
Presentation transcript:

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. We will explore how to take advantage of the huge number of pre-made classes provided with Java. We will also learn how to read the APIs that come with those classes so that you will be able to teach yourself how to use that class. We will also learn how to read the APIs that come with those classes so that you will be able to teach yourself how to use that class. In this chapter, we will start with APIs, explore a few useful classes and their APIs, In this chapter, we will start with APIs, explore a few useful classes and their APIs, And we will finish by learning how to write our own APIs so that other people can use our classes. And we will finish by learning how to write our own APIs so that other people can use our classes.

What is an API? - It is a pre-made class provided with Java. - There are large libraries of classes available to use with Java.

What is an API? API stands for Application Programming Interface (API). API stands for Application Programming Interface (API). It lists the classes and constructors so that we know which methods are available. It lists the classes and constructors so that we know which methods are available. APIs do not tell us how the programmer dealt with a problem or what kind of formulas they used internally, APIs do not tell us how the programmer dealt with a problem or what kind of formulas they used internally, It just tells us what methods we can access, how to interact with those methods, and what those methods will return back to us. It just tells us what methods we can access, how to interact with those methods, and what those methods will return back to us.

Where do you find APIs? You can always access the Java APIs at java.sun.com You can always access the Java APIs at java.sun.comjava.sun.com The Java APIs are organized both by package and by class. The Java APIs are organized both by package and by class. Packages are groups of related classes that are “packaged” together. Packages are groups of related classes that are “packaged” together.

Final and Static Final and Static are both Keywords. Final and Static are both Keywords. When used with a primitive data type, final means that the value of the variable will never change. When used with a primitive data type, final means that the value of the variable will never change. A program that repeatedly uses a constant identifier, can be quickly modified by assigning a value to it at the top of the program. A program that repeatedly uses a constant identifier, can be quickly modified by assigning a value to it at the top of the program. If the value of the constant is hard coded everywhere it is used, then changing the tax rate would involve changing that value everywhere it appears in the program. This involves searching through your program and finding every single time that value is used, and then changing it to the new value. So, using final values will not only save time but can also reduce the number of errors in your program. If the value of the constant is hard coded everywhere it is used, then changing the tax rate would involve changing that value everywhere it appears in the program. This involves searching through your program and finding every single time that value is used, and then changing it to the new value. So, using final values will not only save time but can also reduce the number of errors in your program.

Constants Once a final variable is given a value within a program, that value may never change in that run. Once a final variable is given a value within a program, that value may never change in that run. In order to change that value, you must change it within your code, recompile, and run the program again. In order to change that value, you must change it within your code, recompile, and run the program again.

Constants Objects and methods can also take a final keyword. However, they behave differently than primitive data types. We have not yet discussed the concepts that these final objects and methods affect, but we will cover them in Lesson A11 - Inheritance. Objects and methods can also take a final keyword. However, they behave differently than primitive data types. We have not yet discussed the concepts that these final objects and methods affect, but we will cover them in Lesson A11 - Inheritance.

Constants Using the keyword static means that the data member or method is attached to the class rather than to an object of that class. Using the keyword static means that the data member or method is attached to the class rather than to an object of that class. With a static method, you only need to type the name of the class followed by the name of the method. With a static method, you only need to type the name of the class followed by the name of the method. You can think of static methods as belonging to the class itself, whereas non-static methods are attached to the objects created from that class. You can think of static methods as belonging to the class itself, whereas non-static methods are attached to the objects created from that class.

More Design Considerations While we may make a hundred objects from one class, any static variables of that class will in fact be shared by each of those objects. While we may make a hundred objects from one class, any static variables of that class will in fact be shared by each of those objects. If one object changes the value of that static variable, then the value is changed for all of the other objects of that type. This is because the variable and its value do not belong to any of the objects individually, but to the class itself. If one object changes the value of that static variable, then the value is changed for all of the other objects of that type. This is because the variable and its value do not belong to any of the objects individually, but to the class itself.

More Useful Classes The Point2D class is useful for storing locations on a two dimensional space. It also contains several methods that can be used to do certain calculations. There are two sub-classes of Point2D, but you will generally only want to use Point2D.Double. The Point2D class is useful for storing locations on a two dimensional space. It also contains several methods that can be used to do certain calculations. There are two sub-classes of Point2D, but you will generally only want to use Point2D.Double.

More Useful Classes The java.util package is full of utility classes that you will find very useful. The java.util package is full of utility classes that you will find very useful. You will learn several of the util classes in detail, but for now we will just concentrate on this one, java.util.Random. You will learn several of the util classes in detail, but for now we will just concentrate on this one, java.util.Random. Random is probably the most fun of all the classes in the java.util package. Any sort of game or in depth simulation (such as inspecting a transportation system to see how efficient it is) will generally need some sort of randomness in order to work the way we want. Random is probably the most fun of all the classes in the java.util package. Any sort of game or in depth simulation (such as inspecting a transportation system to see how efficient it is) will generally need some sort of randomness in order to work the way we want.

RANDOM Class Constructor SummaryRandom() Creates a new random number generator. Constructor SummaryRandom() Creates a new random number generator. Method SummarydoublenextDouble() Returns the next pseudorandom, uniformly distributed double value between 0.0 and 1.0 from this random number generator's sequence.intnextInt(int n) Returns a pseudorandom, uniformly distributed int value between 0 (inclusive) and the specified value (exclusive), drawn from this random number generator's sequence. Method SummarydoublenextDouble() Returns the next pseudorandom, uniformly distributed double value between 0.0 and 1.0 from this random number generator's sequence.intnextInt(int n) Returns a pseudorandom, uniformly distributed int value between 0 (inclusive) and the specified value (exclusive), drawn from this random number generator's sequence. Used for games. Used for games.

MATH Class The Math class in the java.lang package contains class methods for commonly used mathematical functions. Java loads the java.lang package automatically, so no special actions are required to access these. The Math class in the java.lang package contains class methods for commonly used mathematical functions. Java loads the java.lang package automatically, so no special actions are required to access these. The Math class is most useful for complex mathematical formulas The Math class is most useful for complex mathematical formulas The class is large and can be viewed online. The class is large and can be viewed online.

Creating an API The basics of creating your own APIs are pretty simple. The basics of creating your own APIs are pretty simple. When you add comments in your code, you can use the tag /**...*/ before each class, variable, constructor, and method to create block comments. When you add comments in your code, you can use the tag /**...*/ before each class, variable, constructor, and method to create block comments. These comments work within your code in essentially the same way as the regular block comment tag /*...*/. However, once we run the Javadoc tool, APIs will be created based on these comments. These comments work within your code in essentially the same way as the regular block comment tag /*...*/. However, once we run the Javadoc tool, APIs will be created based on these comments.

LABS Taxes Taxes Regular Polygon Regular Polygon