Java Java 1995 developed in Java Sun Microsystems

  • Slides: 22
Download presentation
Java • Java (1995) developed in Java Sun Microsystems by James Gosling, Bill Joy

Java • Java (1995) developed in Java Sun Microsystems by James Gosling, Bill Joy and Guy Steele • Now owned by Oracle • Influenced by C++ • Implements classes, encapsulation, simple inheritance, polymorphism, interfaces, garbage collection 1

The java Model • When compiling a Java Program an intermediate code is generated

The java Model • When compiling a Java Program an intermediate code is generated (defined by Sun) which is named bytecode 2

The Java Model • This bytecode is portable to different platforms 3

The Java Model • This bytecode is portable to different platforms 3

Types of java Files • All files define either a class or an interface

Types of java Files • All files define either a class or an interface • Class: – Defines and implements a type of object – Variables (representation) • Static variables: common for all objects of the class • Dynamic variables: one for each object – Procedures (functions) • Statics (a special one: main) • And dynamics (are performed on an object) • Interface – Defines the signature of a procedure 4

A basic file to run a java program public class My. Class { static

A basic file to run a java program public class My. Class { static public void main(String[] args) { here come the instructions that will be performed when the resulting class file is interpreted } } • Program must be written in a file named My. Class. java (My. Class is the class name, chosen by the programmer, starts with uppercase by convention • When compiled generates My. Class. class, which is interpreted by the JVM (java command) • If something is changed in the program then it should be compiled again to reflect the changes 5

Problem Write a program to let the computer compute the percentage of men and

Problem Write a program to let the computer compute the percentage of men and women according to the following dialogue with a person (user): Number of men 19 user Number of women 12 user % of men = 61. 29032258064516 % of women = 38. 70967741935484

import java. util. Scanner; public class Problem 1 { public static void main(String[] args)

import java. util. Scanner; public class Problem 1 { public static void main(String[] args) { Scanner sc = new Scanner(System. in); System. out. println("Number of men "); int m = sc. next. Int(); System. out. println("Number of women "); int w = sc. next. Int(); double p = 100. 0 * m / (w + m); System. out. print("% of men = "); System. out. println(p); System. out. print("% of women = "); System. out. println(100 - p); sc. close(); } }

Evaluation of mathematical expressions 1. Unary operators ( +3, -4 ) 2. “multiplicative” operators

Evaluation of mathematical expressions 1. Unary operators ( +3, -4 ) 2. “multiplicative” operators ( *, / ) 3. “additive” operators( +, - ) Notes In case of operators of same priority the evaluation is done from left to right. Ex. : 100. 0*m/(w+m) is equivalent to (100. 0*m)/(w+m) Parenthesis are used to o Modify the evaluation order Ex. : 100. 0*m/(w+m) o Confirm the evaluation order Ex. : (100. 0*m)/(w+m) Type of the result Common type Ex: 1 / 2 is 0 (integer), 1. 0 / 2. 0 is 0. 5 (real) real if operands are of different types. Ex: 1. 0/2 is 0. 5 Note. the result of m/(w+m)*100. 0? Why?

System. out. println(p); Semantic: writes the value (or expression) of p and “skips” to

System. out. println(p); Semantic: writes the value (or expression) of p and “skips” to the next line System. out. print(p); Semantic: writes the value (or expression) of p and “stays” in the same line (what is written after this appears on the same line) Syntax: System. out. println(arithmetic expression); Examples: variable (p) constant (100) expression (a*b, (a/b))

Example 2 Program that reads an integer number which contains a date in the

Example 2 Program that reads an integer number which contains a date in the format yyyymmdd and prints it as dd/mm/yyyy import java. util. *; //uses Scanner public class Change. Date. Format { static public void main(String[] args) { Scanner U = new Scanner(System. in); System. out. print("date as yyyymmdd : "); int n = U. next. Int(); int y=n/10000, d=n%100, m=n%10000/100; System. out. print("date as dd/mm/aaaa : "); System. out. print(d+"/"+m+"/"+y); } } 11

Types, Values and Variables • A java variable can store 2 types of values:

Types, Values and Variables • A java variable can store 2 types of values: – Primitives: variable corresponds to a mnemonic name of a Primitives place in memory where the value is stored – References: variable corresponds to a name of the place in References memory that contains the address where the data is stored • Primitive variables and references are strongly "typed" 12

Primitive Data Type Values boolean true or false char Unicode character (16 bits) byte

Primitive Data Type Values boolean true or false char Unicode character (16 bits) byte Integer of 8 bits (with sign) short Integer of 16 bits (with sign) int Integer of 32 bits (with sign) long Integer of 64 bits (with sign) float Floating number (real) of 32 bits (with sign) double Floating number (real) of 64 bits (with sign) 13

Type conversion (cast) • Java is strongly typed – Checks type compatibility during compilation

Type conversion (cast) • Java is strongly typed – Checks type compatibility during compilation – Allows casting among types 14

Implicit conversion • When a numeric value of smaller range is assigned to a

Implicit conversion • When a numeric value of smaller range is assigned to a variable of a wider range byte short int long float double smaller wider • Example int i = 1000; double d = i; 15

Explicit cast (primitive types) • May cause loss of precision double d = 20.

Explicit cast (primitive types) • May cause loss of precision double d = 20. 5; long l = (long) d; System. out. println(l); 20 • May cause loss of digits long l = 1000000; short s; s = (short) l; System. out. println(s); 16960 16

Solution 1 Scanner U = new Scanner(System. in); System. out. print("side of the square?

Solution 1 Scanner U = new Scanner(System. in); System. out. print("side of the square? "); double x = U. next. Double(); double r = x / 2; System. out. print("area="); System. out. println(x * x - 3. 1416 * r); System. out. print("perímeter="); System. out. println(4 * x + 2 * 3. 1416 * r); U. close(); Solution 2. . . System. out. println("area=" + (x * x - Math. PI * r)); System. out. println("perímeter=" + (4 * x + 2 * Math. PI * r));

Solution 3 (with some useful new things) double x = U. next. Double(), r

Solution 3 (with some useful new things) double x = U. next. Double(), r = x / 2; System. out. println("area=" + (Math. pow(x, 2) - Math. PI * Math. pow(r, 2))); System. out. println("perimeter=" + (4 * x + 2 * Math. PI * r)); double x=…, r=…; • Equivalent to: double x=…; double r=…; Math. PI • Pre-defined value of л (3. 14159265358979) • real constant (cannot be changed) Math. pow(r, 2) • computes and returns r 2 • Math. pow: predefined function • syntax: Math. pow(x, y), x and y: expressions • semantic: xy, result is a real value (Math. pow(2, 2) is 4. 0)

Proposed Problem 1. Write a program to let the computer compute the velocity of

Proposed Problem 1. Write a program to let the computer compute the velocity of a moving object according to the following dialogue with a person (user): Meters ? 50 user Seconds ? 10 user Velocity = 18. 0 km/hour

Proposed Problem 2. Write a program to let the computer compute the % of

Proposed Problem 2. Write a program to let the computer compute the % of Armenians in Armenia, Artsakh and the Diaspora according to the following dialogue with a person (user): In Armenia ? 2800000 user In Artsakh? 150000 user In Diaspora ? 8000000 user % in Armenia = X% % in Artsakh = Y% % in Diaspora = Z%

Proposed Problem 3. Write a program to let the computer read a number of

Proposed Problem 3. Write a program to let the computer read a number of two digits and print the number with the digits inverted (if user gives 78 the computer writes 87, if user writes 90 the computer writes 09): Two digits number ? 79 user Number with inverted digits = 97