Importing methods in the Java library. Previously discussed Method = a collection of statements that performs a complex (useful) task A method is identified.

Slides:



Advertisements
Similar presentations
Escape Sequences \n newline \t tab \b backspace \r carriage return
Advertisements

Basic Java Constructs and Data Types – Nuts and Bolts
1 Packages: Putting Classes Together. 2 Introduction The main feature of OOP is its ability to support the reuse of code: Extending the classes (via inheritance)
18 January 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems.
Java Packages CSci 1130 Intro to Computer Programming with Java Instructor Tatyana Volk.
Building Java Programs Interactive Programs w/ Scanner What is a class? What is an object? What is the difference between primitive and object variables?
MOD III. Input / Output Streams Byte streams Programs use byte streams to perform input and output of 8-bit bytes. This Stream handles the 8-bit.
Q1 Review. Other News US News top CS Universities global-universities/computer- science?int=994b08.
Self Check 1.Which are the most commonly used number types in Java? 2.Suppose x is a double. When does the cast (long) x yield a different result from.
INTRODUCTION Chapter 1 1. Java CPSC 1100 University of Tennessee at Chattanooga 2  Difference between Visual Logic & Java  Lots  Visual Logic Flowcharts.
Chapter 2: Using Objects Part 1. To learn about variables To understand the concepts of classes and objects To be able to call methods To learn about.
CS1010 Programming Methodology
 2005 Pearson Education, Inc. All rights reserved Introduction.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
CMT Programming Software Applications
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.
1 Key Concepts:  Why C?  Life Cycle Of a C program,  What is a computer program?  A program statement?  Basic parts of a C program,  Printf() function?
Chapter 2 storing numbers and creating objects Pages in Horstmann.
1 Data types, operations, and expressions Overview l Format of a Java Application l Primitive Data Types l Variable Declaration l Arithmetic Operations.
Chapter 2  Using Objects 1 Chapter 2 Using Objects.
Guide To UNIX Using Linux Third Edition
Introduction to Methods
The switch statement: an N-way selection statement.
Using Java's Math & Scanner class. Java's Mathematical functions (methods) (1)
© The McGraw-Hill Companies, 2006 Chapter 1 The first step.
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.
Floating point variables of different lengths. Trade-off: accuracy vs. memory space Recall that the computer can combine adjacent bytes in the RAM memory.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA ‏ Packages.
The character data type char
Java Packages and Libraries M Taimoor Khan
The string data type String. String (in general) A string is a sequence of characters enclosed between the double quotes "..." Example: Each character.
The Java Programming Language
The basics of the array data structure. Storing information Computer programs (and humans) cannot operate without information. Example: The array data.
 2005 Pearson Education, Inc. All rights reserved. 1 Methods Called functions or procedures in other languages Modularize programs by separating its tasks.
CHAPTER 3 GC Java Fundamentals. 2 BASICS OF JAVA ENVIRONMENT  The environment  The language  Java applications programming Interface API  Various.
Floating point numerical information. Previously discussed Recall that: A byte is a memory cell consisting of 8 switches and can store a binary number.
What does a computer program look like: a general overview.
FIRST JAVA PROGRAM. JAVA PROGRAMS Every program may consist of 1 or more classes. Syntax of a class: Each class can contain 1 or more methods. public.
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
Working with arrays (we will use an array of double as example)
Introduction to programming in the Java programming language.
Jens Dalsgaard Nielsen Jan Dimon Bendtsen Dept. of Electronic Systems Basic Programming INS-basis GF, PDP and HST.
CSci 111 – computer Science I Fall 2014 Cynthia Zickos WRITING A SIMPLE PROGRAM IN JAVA.
More About Objects and Methods Chapter 5. Outline Programming with Methods Static Methods and Static Variables Designing Methods Overloading Constructors.
Mixing integer and floating point numbers in an arithmetic operation.
1 Principles of Computer Science I Prof. Nadeem Abdul Hamid CSC 120 – Fall 2005 Lecture Unit 2 - Using Objects.
Class Libraries Chapter 1 1 Source Intro to Java Programming Y. Daniel Liang.
© 2004 Pearson Addison-Wesley. All rights reserved ComS 207: Programming I Instructor: Alexander Stoytchev
Using methods in the Java library. Previously discussed Method = a collection of statements that performs a complex (useful) task A method is identified.
The character data type char. Character type char is used to represent alpha-numerical information (characters) inside the computer uses 2 bytes of memory.
Introduction to Methods. Previously discussed There are similarities in make up of that can help you remember the construct of a class a class in the.
The life time of local variables (in a method). Local variables Local variable: A local variable is used to store information that is relevant for the.
Methods. Overview Class/Library/static vs. Message/non-static return and void Parameters and arguments.
1 Basic Java Constructs and Data Types – Nuts and Bolts Looking into Specific Differences and Enhancements in Java compared to C.
Arithmetic expressions containing Mathematical functions.
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.
Agenda Comments Identifiers Keywords Syntax and Symentics Indentation Variables Datatype Operator.
Methods.
Invoking methods in the Java library. Jargon: method invocation Terminology: Invoking a method = executing a method Other phrases with exactly the same.
SUMMARY OF CHAPTER 2: JAVA FUNDAMENTS STARTING OUT WITH JAVA: OBJECTS Parts of a Java Program.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
Introduction to Computer Science What is Computer Science? Getting Started Programming.
Working with Java.
“Packages in Java”.
Chapter 1: Computer Systems
Anatomy of a Java Program
Introduction to Java Brief history of Java Sample Java Program
Chap 4. Programming Fundamentals
Instructor: Alexander Stoytchev
Presentation transcript:

Importing methods in the Java library

Previously discussed Method = a collection of statements that performs a complex (useful) task A method is identified by a method name Class = a container for methods Methods that serves a similar purpose are stored in the same class A class is identified by a class name

Previously discussed (cont.) Schematically:

Organization of the Java library The Standard Java library consists of a number of packages Each package consists of a number of classes (that provide similar functionality)

Organization of the Java library (cont.) The standard Java library is named java A package named xxx inside the standard Java library is named java.xxx

Organization of the Java library (cont.) Some commonly used packages: java.lang: Provides classes that are fundamental to the design of the Java programming language. Official website: e-summary.html e-summary.html java.lang: Contains the collections framework, legacy collection classes, event model, date and time facilities, internationalization, and miscellaneous utility classes (a string tokenizer, a random- number generator, and a bit array). Official website: -summary.html

Organization of the Java library (cont.) Schematical representation of Java's Standard library:

Organization of the Java library (cont.) A class named yyy inside the package java.xxx is named java.xxx.yyy Example: The class Math inside the package java.lang is known as java.lang.Math The class Double inside the package java.lang is known as java.lang.Double The class Stack inside the package java.util is known as java.util.Stack The class Scanner inside the package java.util is known as java.util.Scanner

Organization of the Java library (cont.) Note: It is a Java convention that the name of a Java class begins with a upper case letter It is also a Java convention that the name of a method begins with a lower case letter

Importing a method that is available in Java's standard library: importing a class Rule of usage: The import clauses must occur before any class definitions If a Java program wants to use a method in the Java library, the Java program must first import the containing class

Importing a method that is available in Java's standard library: importing a class (cont.) Syntax to import a class from the Java library: import className ;

Importing a method that is available in Java's standard library: importing a class (cont.) Examples: import java.lang.Math; import java.lang.Double; import java.util.Stack; import java.util.Scanner; // After the import clauses, you can write // the class definition // This program can now use all methods defined inside // the classes Math, Double, Stack and Scanner public class MyProgram { public static void main(String[] args) { double a; a = Math.sqrt(2.0); // Save computed value in variable System.out.println(a); // You can print the saved value later }

Importing all classes in a package Some complex Java program may use many different methods contained in many different classes in the same package It would be a pain to write a long list of import clauses Example: import java.lang.Math; import java.lang.Double; import java.lang.Integer;...

Importing all classes in a package (cont.) There is a short hand to import all classes contained in a package: import java.lang.* ; // import all class in java.lang package import java.util.* ; // import all class in java.util package

Frequently used methods: java.lang According to the Rule of usage: We must import java.lang.Math if we want to use the method Math.sqrt() If a Java program wants to use a method in the Java library, the Java program must first import the containing class

Frequently used methods: java.lang (cont.) We should have written: import java.lang.Math; // We MUST import this class to use Math.sqrt public class Abc { double a, b, c, x1, x2; // Define 5 variable a = 1.0; b = 0.0; c = -4.0; x1 = ( -b - Math.sqrt( b*b - 4*a*c ) ) / (2*a); x2 = ( -b + Math.sqrt( b*b - 4*a*c ) ) / (2*a); System.out.print("a = "); System.out.println(a); System.out.print("b = "); System.out.println(b); System.out.print("c = "); System.out.println(c); System.out.print("x1 = "); System.out.println(x1); System.out.print("x2 = "); System.out.println(x2); }

Frequently used methods: java.lang (cont.) But.... because: all classes in the java.lang package are automatically included in every Java program (the Java compiler is programmed to do this) That is why we did not need to import java.lang.Math in our program. The package java.lang contains classes that are fundamental to the design of the Java programming language.

Summary: importing methods in the Java library Rule of usage: If a Java program wants to use a method in the Java library, the Java program must first import the containing class All classes in the java.lang package have already been imported into a Java program (You can use methods in these classes without the import clause)

Summary: importing methods in the Java library (cont.) Analogy: The reason you need to import the method is because the Java compiler needs to know the details of the method before it can make the correct translation. You can think of methods inside your Java program and methods inside the Java library as subjects (people) from different automonous entities (e.g., countries) When you want to "bring" a method from the Java library into your program, you need to issue an import "license"