1 / 22

Java

Java. Java (1995) developed in 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. The java Model.

lmarjorie
Télécharger la présentation

Java

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. Java • Java (1995) developed inSun Microsystems byJames Gosling, Bill Joy andGuy Steele • Now owned by Oracle • Influenced by C++ • Implements classes, encapsulation, simple inheritance, polymorphism, interfaces, garbage collection

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

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

  4. 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

  5. A basic file to run a java program public class MyClass { 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 MyClass.java (MyClass is the class name, chosen by the programmer, starts with uppercase by convention • When compiled generates MyClass.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

  6. 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

  7. import java.util.Scanner; • public class Problem1 { • public static void main(String[] args) { • Scanner sc = new Scanner(System.in); • System.out.println("Number of men "); • int m = sc.nextInt(); • System.out.println("Number of women "); • int w = sc.nextInt(); • 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(); • } • }

  8. 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 • Modify the evaluation order Ex.: 100.0*m/(w+m) • 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?

  9. 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))

  10. 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 ChangeDateFormat{ static public void main(String[] args) { Scanner U = new Scanner(System.in); System.out.print("date as yyyymmdd : "); int n = U.nextInt(); 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: • Primitives: variable corresponds to a mnemonic name of a place in memory where the value is stored • References: variable corresponds to a name of the place in memory that contains the address where the data is stored • Primitive variables and references are strongly "typed"

  12. Primitive Data

  13. 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 variable of a wider range byte short int long float double smaller wider • Example inti = 1000; double d = i;

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

  16. Solution 1 • Scanner U = new Scanner(System.in); • System.out.print("side of the square?"); • double x = U.nextDouble(); • double r = x / 2; • System.out.print("area="); • System.out.println(x * x - 3.1416 * r * 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 * r)); • System.out.println("perímeter=" + (4 * x + 2 * Math.PI * r));

  17. Solution 3 (with some useful new things) • double x = U.nextDouble(), 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 r2 • 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)

  18. Proposed Problem1 . 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

  19. Proposed Problem2 . 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%

  20. Proposed Problem3 . 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

More Related