1 / 95

INF160 IS Development Environments AUBG, COS dept

INF160 IS Development Environments AUBG, COS dept. Lecture 03 Title: Tutorial Introduction to JAVA (Extract from Syllabus). Lecture Contents:. Java program – general definition/description Java program – basic structure Java program – demo example

Download Presentation

INF160 IS Development Environments AUBG, COS dept

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. INF160 IS Development Environments AUBG, COS dept Lecture 03 Title: Tutorial Introduction to JAVA (Extract from Syllabus)

  2. Lecture Contents: • Java program – general definition/description • Java program – basic structure • Java program – demo example • Detailed description of Java as a conventional programming language

  3. To students with skills in Java • Write a program - Tester for factorial, greatest common divisor, Fibonacci series number • PRELUDE: • To compute Fibonacci series number you need a method like this header • int Fibonacci( int n ) • { • … • } • To compute factorial you need a method like this header • int Factorial( int n ) • { • … • } • To compute greatest common divisor you need a method like this header • int GCD( int m, int n ) • { • … • }

  4. Program – definition/description • Program: An organized list of instructions that, when executed, causes the computer to behave in a predetermined manner. • Java program: Collection of one or more classes, one of which has a method titled public static void main(String[] args) • Java class: mechanism that allows to combine data and operations on that data /named methods/into a single unit. • Java method: Set of instructions designed to accomplish a specific task. • In OOD the final Java program is a collection of interacting objects /instances of classes/.

  5. Review On VBASIC ‘Review:VB Program – basic structure <Imports declarations> <Module heading> Sub Main() <definition/declaration statements> <executable statements> End Sub <Module end delimiter> ‘ Review: VB program – demo example Imports System Module thisModule Sub Main() Console.WriteLine(“Hello, World!”) Console.ReadKey() End Sub End Module

  6. Review On C++ Native code // Review: C++ Program – basic structure <preprocessor directives> using namespace <region>; void main() { <definition/declaration statements> <executable statements> } // Review: C++ Program – demo example #include <iostream> using namespace std; void main( ) { cout << “\nHello, World!”; system(“pause”); }

  7. Review On C++ Managed code // Review: C++ Program – basic structure <preprocessor directives> using namespace <region>; void main() { <definition/declaration statements> <executable statements> } // Review: C++ Program – demo example #include "stdafx.h" using namespace System; int main( ) { Console::WriteLine("Hello World"); Console::ReadKey(); return 0; }

  8. Review On C# // Review: C# Program – basic structure using System; namespace YourNamespace { class YourClass { } interface IYourInterface { } delegate int YourDelegate(); class YourMainClass { static void Main(string[] args) { //Your program starts here... } } } // Review: C# Program – demo example using System; namespace MyProject { public class Hello { public static void Main(string[] args) { Console.WriteLine("Hello world in C#"); Console.ReadLine(); } } }

  9. New: JAVA // New: Java Program – basic structure package package_name; import predefined_package; class YourClass { method declarations } class YourMainClass { static void main(String[] args) { //Your program starts here... } } // New:Java Program – demo example package MyJavaProg; import java.util.Scanner; public class Hello { public static void main(String[] args) { System.out.println("Hello world in Java"); } }

  10. Lecture Contents (new): • Why Java (instead intro) • Anatomy of a Java program • Sample source text • Packages • Comments • Names – identifiers, reserved words, modifiers • Classes – data fields, methods (statements, blocks of statements ) • Components of a Java program • Data – by category • Data – by type • Expressions (incl. operands & operators) • Statements • Routines (member functions - methods) • I/O facilities • Data collections – arrays, etc • Demo programs - examples

  11. . Detailed description of Java as Conventional Prog Lan

  12. Why Java? The answer is that Java enables users to develop and deploy applications on the Internet for servers, desktop computers, and small hand-held devices. The future of computing is being profoundly influenced by the Internet, and Java is the Internet programming language. • Java is a general purpose programming language. • Java is the Internet programming language. • Java programs may be applications or applets or servlets. Applications are standalone programs, similar to .NET Console and Windows applications. Applets are similar to applications, but they do not run as standalone programs. - Instead, applets adhere to a set of conventions that lets them run within a Java-compatible browser (client-side). - You can only run an applet from an HTML page.

  13. Anatomy of a Java program: Sample source text Sample Run: Java Programming: From Problem Analysis to Program Design, 3e Java Programming: From Problem Analysis to Program Design, 4e 13

  14. The Basics of a Java Program Java program: collection of classes There is a main method (namedmainand specified as public static void) in every Java application program. Token: smallest individual unit of a program. Also called lexical unit or lexeme. Java Programming: From Problem Analysis to Program Design, 3e Java Programming: From Problem Analysis to Program Design, 4e 14

  15. Java program – basic structure components Packages, Classes, Methods, and the import Statement Package: collection of related classes Class: consists of methods Method: designed to accomplish a specific task Java Programming: From Problem Analysis to Program Design, 3e Java Programming: From Problem Analysis to Program Design, 4e 15

  16. The package Package as a term: Collection of related classes Logical entity Really locates one file or many files Package as reserved word: Syntax: package <name>; Always first line in the source code May be omitted: program without explicit package name Java Programming: From Problem Analysis to Program Design, 3e Java Programming: From Problem Analysis to Program Design, 4e 16

  17. Classes Classes are the building blocks of Java programs The class is the essential Java construct. A class is a template or blueprint for objects. Syntax: For now, though, understand that a program is defined by using one or more classes. Classes consist of methods /member functions/. Java Programming: From Problem Analysis to Program Design, 3e

  18. Methods What is System.out.println? It is a method: a collection of statements that performs a sequence of operations to display a message on the console. It is used by invoking a statement with a string argument. The string argument is enclosed within parentheses. In this case, the argument is "Welcome to Java!“. You can call the same println method with a different argument to print a different message. Java Programming: From Problem Analysis to Program Design, 3e

  19. main Method The main method provides the control of program flow. The Java interpreter executes the application by invoking the main method. The main method looks like this: public static void main(String[] args) { // Statements; } Java Programming: From Problem Analysis to Program Design, 3e

  20. import Statement Used to import the components of a package into a program Reserved word importjava.io.*; Imports the (components of the) packagejava.io into the program Primitive data types and the classString Part of the Java language Don’t need to be imported Java Programming: From Problem Analysis to Program Design, 3e Java Programming: From Problem Analysis to Program Design, 4e 20

  21. Import statement A Java program is never entirely self-contained but instead must execute in the “world” provided by the Java runtime system. Use of the import statement: import java.lang.*; This statement makes the methods in the library package java.lang available to the class following it. java.lang is just one package in the Java library or API. A Java package is similar to a C# namespace. Java Programming: From Problem Analysis to Program Design, 3e

  22. Types of statements Declaration/definition statements Executable statements System.out.println("Welcome to Java!"); Every statement in Java ends with a semicolon (;). Java Programming: From Problem Analysis to Program Design, 3e Java Programming: From Problem Analysis to Program Design, 4e 22

  23. Blocks A pair of braces in a program forms a block that groups components of a program. Java Programming: From Problem Analysis to Program Design, 3e

  24. Block Styles Use end-of-line style for braces. Java Programming: From Problem Analysis to Program Design, 3e

  25. Names - Identifiers • Names of entities (like variables, named constants, methods, classes, objects etc.) • An identifier is a sequence of characters that consist of letters, digits, underscores (_), and dollar signs ($). • An identifier must start with a letter, an underscore (_), or a dollar sign ($). It cannot start with a digit. • An identifier cannot be a reserved word. • An identifier can be of any length. • Java is case sensitive – ion, Ion, IOn, ION Java Programming: From Problem Analysis to Program Design, 3e

  26. Naming Conventions • Choose meaningful and descriptive names. • Class names: • Capitalize the first letter of each word in the name. For example, the class name ComputeArea. • Named Constants: • Capitalize all letters in constants, and use underscores to connect words. For example, PI and MAX_VALUE • Variables and method names: • Use lowercase. If the name consists of several words, concatenate all in one, use lowercase for the first word, and capitalize the first letter of each subsequent word in the name. For example, the variables radius and area, and the method computeArea. Java Programming: From Problem Analysis to Program Design, 3e

  27. Reserved Words Reserved words or keywords are words that have a specific meaning to the compiler and cannot be used for other purposes in the program. For example, class, public, static, void. are reserved words. Modifiers Java uses certain reserved words called modifiers that specify the properties of the data, methods, and classes and how they can be used. Examples of modifiers are public and static. Other modifiers are private, final, abstract, and protected. Java Programming: From Problem Analysis to Program Design, 3e

  28. Data Types Data type:set of values together with a set of operations/operators on those values. Data type is composed of two components: Set of values. Values may describe/present using Literals (explicit constants) Named constants Variables Set of operators on those values Java Programming: From Problem Analysis to Program Design, 3e Java Programming: From Problem Analysis to Program Design, 4e 28

  29. Data Types Data Types Classified: 8 Primitive Data Types boolean, byte, short, int, long, float, double, char Abstract/Reference Data Types (classes, objects) String, Object, Frame, Person, Animal, … Array types (also reference types) Java Programming: From Problem Analysis to Program Design, 3e Java Programming: From Problem Analysis to Program Design, 4e 29

  30. Primitive Data Types • Classified in three categories: • Integral, which is a data type that deals with integers (or numbers without a decimal part) and characters • Floating-point, which is a data type that deals with decimal numbers • Boolean, which is a data type that deals with logical values Java Programming: From Problem Analysis to Program Design, 3e Java Programming: From Problem Analysis to Program Design, 4e 30

  31. Values and Memory Allocation for Integral Data Types Java Programming: From Problem Analysis to Program Design, 3e Java Programming: From Problem Analysis to Program Design, 4e 31

  32. Integral data types • char – Unicode – 2 bytes/char, unlikely ASCII, EBCDIC – 1 byte/char • Mostly presentation of symbolic data • byte, short, int, long • Mostly presentation of numeric data fixed point Java Programming: From Problem Analysis to Program Design, 3e

  33. Primitive Data Types Floating-point data types float: precision = 6 or 7 decimal places Single precision (4 bytes) double: precision = 15 decimal places Double precision (8 bytes) Java Programming: From Problem Analysis to Program Design, 3e Java Programming: From Problem Analysis to Program Design, 4e 33

  34. Primitive Data Types boolean: two values true false Memory allocated to boolean data type is 1 bit Java Programming: From Problem Analysis to Program Design, 3e Java Programming: From Problem Analysis to Program Design, 4e 34

  35. Primitive Data Types Primitive Data Types presented by category as: Literals Named constants Variables Java Programming: From Problem Analysis to Program Design, 3e Java Programming: From Problem Analysis to Program Design, 4e 35

  36. Literals (Constants) Integer literals: 23 -67 +447 0177 0x1a Floating-point literals: 12.34 +25.60 -3.15 5. .166 3.14e-2 2.78e+3 5.23e4 Character literals: 'a' '5' '+' '\n' Java Programming: From Problem Analysis to Program Design, 3e Java Programming: From Problem Analysis to Program Design, 4e 36

  37. Named constants Named constant Cannot be changed during program execution Declared by using the reserved word final Initialized when it is declared Example: final double CENTIMETERS_PER_INCH = 2.54; final int NO_OF_STUDENTS = 20; final charBLANK = ' '; final doublePAY_RATE = 15.75; Java Programming: From Problem Analysis to Program Design, 3e Java Programming: From Problem Analysis to Program Design, 4e 37

  38. Variables Variable (name, value, data type, size) Content may change during program execution Must be declared before it can be used May not be automatically initialized If new value is assigned, old one is destroyed Value can only be changed by an assignment statement or an input (read) statement Example: double amountDue; int counter; char ch; Java Programming: From Problem Analysis to Program Design, 3e Java Programming: From Problem Analysis to Program Design, 4e 38

  39. The class String Used to manipulate strings String Sequence of zero or more characters Enclosed in double quotation marks Null or empty strings have no characters Numeric strings consist of integers or decimal numbers?? /digits/ Length is the number of characters in a string Java Programming: From Problem Analysis to Program Design, 3e Java Programming: From Problem Analysis to Program Design, 4e 39

  40. Declaring and Initializing Variables int first; first = 13; char ch; ch = ‘E’; double value; value = 35.68; ------------------------------------------------------------- int first = 13; char ch = ‘E’; double value = 35.68; Java Programming: From Problem Analysis to Program Design, 3e

  41. (Non-)Pointers in Java • In Java, all primitive variables are value variables. (Real, actual) • it is impossible to have an integer pointer or a pointer to any variable of one of the primitive data types • All object variables are actually reference variables (pointers, memory addresses) to objects. • it is impossible to have anything but references to objects. You can never have a plain object variable Java Programming: From Problem Analysis to Program Design, 3e

  42. Character Data Type Four hexadecimal digits. char letter = 'A'; (ASCII) char numChar = '4'; (ASCII) char letter = '\u0041'; (Unicode) char numChar = '\u0034'; (Unicode) NOTE: The increment and decrement operators can also be used on char variables to get the next or preceding Unicode character. For example, the following statements display character b. char ch = 'a'; System.out.println(++ch); Java Programming: From Problem Analysis to Program Design, 3e

  43. Unicode Format Java characters use Unicode, a 16-bit encoding scheme established by the Unicode Consortium to support the interchange, processing, and display of written texts in the world’s diverse languages. Unicode takes two bytes, preceded by \u, expressed in four hexadecimal numbers that run from '\u0000' to '\uFFFF'. So, Unicode can represent 65535 + 1 characters. Unicode \u03b1 \u03b2 \u03b3 for three Greek letters Java Programming: From Problem Analysis to Program Design, 3e

  44. Escape Sequences for Special Characters Description Escape Sequence Unicode Backspace \b\u0008 Tab \t\u0009 Linefeed \n\u000A Carriage return \r\u000D Backslash \\\u005C Single Quote \'\u0027 Double Quote \"\u0022 Java Programming: From Problem Analysis to Program Design, 3e

  45. Expressions Java Programming: From Problem Analysis to Program Design, 3e Java Programming: From Problem Analysis to Program Design, 4e 45

  46. Expressions - operands • Operands • Literals • Named Constants • Variables – regular(scalar) or indexed • Method call • Sub expression like ( … ) Java Programming: From Problem Analysis to Program Design, 3e

  47. Arithmetic Operators and Operator Precedence Five Arithmetic Operators + addition - subtraction * multiplication / division % mod (modulus) Unary operator: operator that has one operand Binary operator:operator that has two operands Java Programming: From Problem Analysis to Program Design, 3e Java Programming: From Problem Analysis to Program Design, 4e 47

  48. Order of Precedence 1. * / % (same precedence) 2. + - (same precedence) Operators in 1 have a higher precedence than operators in 2 When operators have the same level of precedence, operations are performed from left to right Java Programming: From Problem Analysis to Program Design, 3e Java Programming: From Problem Analysis to Program Design, 4e 48

  49. Increment and Decrement Operators ++ increments the value of its operand by 1 -- decrements the value of its operand by 1 Syntax Pre-increment: ++variable Post-increment: variable++ Pre-decrement: --variable Post-decrement: variable-- Java Programming: From Problem Analysis to Program Design, 3e Java Programming: From Problem Analysis to Program Design, 4e 49

  50. Mixed Expressions Operands of different types Examples 2 + 3.5 6 / 4 + 3.9 Integer operands yield an integer result; floating-point numbers yield floating-point results If both types of operands are present, the result is a floating-point number Precedence rules are followed Java Programming: From Problem Analysis to Program Design, 3e Java Programming: From Problem Analysis to Program Design, 4e 50

More Related