Docsity

Java Programming for Beginners: Understanding APIs, Random and Math Classes - Prof. D. Pal, Study notes of Electrical and Electronics Engineering

Toggle sidebar
bg1
1
Introduction to Programming
with Java, for Beginners
API
Random Class
Math Class
Dynamic vs. Static
ESE112
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
ESE112
Using Library (API)
๎š„We will use API documentation for Java Version 5
๎š„With the help of Javadocs we can use already
implemented code
๎š„Find the documentation for the Random class
๎š„If you scroll down the lower left panel and click on
the link labeled Random, the large "main" panel on
the right will display the documentation for the
Random class
ESE112
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 ?
pf3
pf4
pf5

Partial preview of the text

Download Java Programming for Beginners: Understanding APIs, Random and Math Classes - Prof. D. Pal and more Study notes Electrical and Electronics Engineering in PDF only on Docsity!

Introduction to Programming

with Java, for Beginners^ APIRandom ClassMath ClassDynamic vs. Static

ESE

Java Library

ย„

Java provides a huge library or collection ofuseful programs

ย„

A gold mine of well-tested code that can saveyou countless hours of development time

ย„

This huge library information is provided in

API -

Application Programming Interface

ESE

Using Library (API)

ย„

We will use API documentation for Java Version 5

ย„

With the help of Javadocs we can use alreadyimplemented code

ย„

Find the documentation for the Random class

ย„^

If you scroll down the lower left panel and click onthe link labeled

Random

, the large "main" panel on

the right will display the documentation for theRandom class

ESE

Random Class

ย„

A class to create Random numbers

ย„

Constructor Summary shows the objects of thistype can be created

ย„^

E.g. Random r = new Random();

ย„

Method Summary shows that it can generaterandom 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?

ESE

Packages and import Statements

ย„^

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 class code is stored in the directorypath

java/util/

somewhere on your machine

-^

"util", or utility

package

ย„^

Inorder to use implemented work, need to tell Java compilerwhere class is located^ ย„

Use

import

statement

-^

import java.util.Random;

-^

Another way is to use the asterisk "wildcard character": importjava.util.*;

ESE

Math Class

Math Class Interface ย„^

Field Summary: Has two constants PI and E

ย„

Constructor Summary: has no publicconstructor

ย„

Methods Summary: many methods all whichare static

ย„

Method Details: e.g. sqrt() takes a double andreturns a double

ESE

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 Javalanguage and hence can be used directly

  • No need of an import statement

Math.PI3.141592653589793> Math.E2.718281828459045> Math.sqrt(25)5.0> Math.pow(2,10) 1024 > Math.cos(0)1.0> Math.cos(2 * Math.PI)1.

ESE

How the Math Class is Implemented

public class Math{

public static final double PI = 3.141592653589793;

public static double sin(double d){ .. }public static double sqrt(double d) { .. }.. } > Math.PI3.141592653589793> Math.sqrt(25)5.

ESE

Static Methods with OO class

ย„^

A method may be declared with the

static

keyword

ย„^

Static methods live at

class level

, not at

object level

ย„^

Static methods can

access

static variables and

methods, but not dynamic ones^ ย„

How could it choose which one? We have not created anyobjects yet ย„^

Example:public static int getNumSold(){

return numTicketsSold; }

ESE

Static Variables & Methods in General

ย„

A static method that is public can be accessed

ย„^

ClassName.methodName(args) ย„^

double result = Math.sqrt(25.0); ย„^

int sold = Ticket.getNumberSold(); ย„^

boolean b = isHappy(10);

ย„

A static variable that is public may be accessed

ย„^

Using

ClassName.variableName

  • E.g. Math.PI, Math.E ย„^

Static variables act as global variable i.e. accessiblewithin any static method

ESE

Ticket class Interactions

> Ticket.getNumberSold() 0 > Ticket t1 = new Ticket();> t1.getTicketNum() 1 > t1.getInfo()"ticket # 1; 1 ticket(s) sold."> t1.getNumberSold() 1 > Ticket t2 = new Ticket();> t2.getTicketNum() 2 > t2.getInfo()"ticket # 2; 2 ticket(s) sold."> t1.getInfo()"ticket # 1; 2 ticket(s) sold."> Ticket.getNumberSold() 2

ESE

Main method is static

ย„

To have standalone Java Application we need amethod:

public static void main(String args[])

ย„

The main method belongs to the class in whichit is written

ย„^

Hence it is static i.e. does not belong to any object

ย„

Note: Instance variable cannot be referencedfrom main unless the object is created

ESE

Example

public class Point {

int x;int y;public static void main(String args[]) {

x = 5;y = 10; } }

all are wrong

ESE

Solution

public class Point {

int x;int y;public void setX(int val) {

x = 5; } โ€ฆ.public static void main(String args[]) {

Point p = new Point();p.setX(5);โ€ฆ

} }

Remember that if no constructoris written, java creates a defaultconstructor and initializes theinstance variables to their defaultvalues

ESE

When to use static with OOP

A variable should be static if

ย„

It logically describes the class as a whole

ย„

There should be only one copy of it

A method should be static if:

ย„

It does not use or affect the object thatreceives the message (it uses only itsparameters)

ESE

Static & Dynamic Rules Recap

ย„^

static

variables and methods belong to the class in general, not to individual objects

ย„^

The absence

of the keyword

static

before non-local variables

and methods means

dynamic

(one per object/instance)

ย„^

A dynamic method can access all dynamic

and

static variables

and methods in the same class

ย„^

A static method can not access a dynamic variable

(How could

it choose or which one?)

ย„^

A static method can not call a dynamic method (becausedynamic method might access an instance variable)