Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 Libraries and APIs Don’t reinvent the wheel

2 What’s an API? API – Application Programmers Interface –We want to use code someone else has written –API’s tell us how to use prewritten classes Constructors Methods – Example: DrawingTool –We don’t need to know how the original programmer made it work. –Use the import statement

3 final The value of the variable will never change final double TAXRATE = 0.0825 Convention is the identifier is written in CAPITALS Do this so we only need to change the number in one place.

4 static Variable or method belongs to the entire class, not an individual instance Often times you use the class name when referencing it (you don’t have to create an instance to use it – i.e. no new) –Color.RED –Dialog.message(“hello); –System.out.println(“hi); –Math.pow(3,4); –Math.PI

5 Static Example public class Person{ //number of people in world private static int numPeople; public Person(){ numPeople++; }

6 DrawingTool Example public drawCircle (double r); postcondition If the object is in drawing mode, a circle of radius r is drawn around the current location using the current width and color.

7 Point2D doubledistancedistance(Point2D pt) Returns the distance from this Point2D to a specified Point2D.Point2D double distance = rat1.distance(rat2);

8 The Java API Java 1.5 API We only need a small portion of these classes listed P.609 of the book

9 Random Constructor Summary RandomRandom() Creates a new random number generator. Method Summary nextIntnextInt(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.

10 A Number From 0 to 10 Random chooser = new Random(); int num = chooser.nextInt(11);

11 Dice How many sides does a die (one of a pair of dice) have? Would this work? Random random = new Random(); int die = random(6);

12 Dice Answer Random random = new Random(); int die = random(6) + 1;

13 Math Let’s look at the real API –Java 1.5 APIJava 1.5 API

14 Field Summary Variables that you can access –Math.PI –double circ = 2 * Math.PI * r;

15 Method Summary Methods that you can call 2^3 int answer = Math.pow(2, 3);

16 Javadoc When you write your own code, you can use javadoc to create your own API (html files)


Download ppt "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."

Similar presentations


Ads by Google