1 / 62

JAVA

JAVA. Why Java? Java was design for commercial reasons and it generated mammoth interest in the business community because of another Internet related development, the World Wide Web. Rule for Java Language. Case sensitive Class name and file name must be the same

cmendez
Download Presentation

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 • Why Java? • Java was design for commercial reasons and it generated mammoth interest in the business community because of another Internet related development, the World Wide Web.

  2. Rule for Java Language • Case sensitive • Class name and file name must be the same • Class name can not have space • Extension must be .java

  3. Common Error in Java • Omitting Semicolons • Misspelling words

  4. There are two common Error in Java Application Run as a stand alone program with java run-time Ex. Hello.java as you try Applet Run with java-enabled browser (IE, Netscape, etc.)

  5. There are two common Error in Java • Jdk version 1.2.2 • Kawa 3.2.2

  6. Running Program Source Code Compiler Object Code Loader Library From Source Code to Running Program

  7. Begin Edit Program Compiler error ? Compile Program Yes No Test Program Run-time error ? Yes No END Edit-Compile-Debug Loop

  8. Typical Java Environment

  9. Introduction to Java Programming Hello.java //A first Java Program import java.applet.Applet; import java.awt.Graphics; public class Hello extends Applet { public void paint (Graphics g) { g.drawString ("Hello World",25,25); } }

  10. HTML file <html> <applet code="Hello.class" width=275 height=135> </applet> </html>

  11. How to run the program By • AppletViewer • Web Browser

  12. appletviewer appletviewer Hello.html

  13. Web Browser

  14. Hello2.java //Second Java Program import java.applet.Applet; import java.awt.Graphics; public class Hello2 extends Applet { public void paint (Graphics g) { g.drawString ("Hello World",25,25); g.drawString ("How are You?",25,40); } }

  15. Addition.java public boolean action (Event e, Object o) { number=Integer.parseInt(o.toString()); input.setText(""); sum=sum+number; showStatus(Integer.toString(sum)); return true; } } //Another Java Program import java.applet.Applet; import java.awt.*; public class Addition extends Applet { Label prompt; TextField input; int number; int sum; public void init() { prompt = new Label("Enter interger and Press Enter"); input = new TextField(10); add(prompt); add(input); sum=0; }

  16. Ranges of Data Type Data Types Type boolean char byte short int long float double Size in bits 1 16 8 16 32 64 32 64 Values true or false ‘\u0000’ to ‘\uFFFF’ -128 to +127 -32,768 to +32,767 -2,147,483,648 to +2,147,483,647 -9,223,372,036,854,775,808 to +9,223,372,036,854,775,807 -3.40292347E+38 to 3.40292347E+38 +1.79769313486231570E+308 to -1.79769313486231570E+308

  17. Constant With final keyword: public static final double PIE = 3.1428; /*constant value for PIE */

  18. Comment Syntax There are two methods for writing comments /* This is a commented text This is better if you want to comment more than one line */ // This is a commented text /*-------------------------------------------------------- This look good for the header of program ---------------------------------------------------------- */

  19. Increment and Decrement Assignment Sample Explanation Assings operator Expression Assume: int c = 3, d = 5, e = 4, f = 6, g = 12; += c += 7 c = c + 7 10 to c -= d -= 4 d = d - 4 1 to d *= e *= 5 e = e * 5 20 to e /= f /= 3 f = f / 3 2 to f %= g %= 9 g = g % 9 3 to g

  20. Increment and Decrement OperatorCalled Sample Explanation expression ++ preincrement ++a Increment a by 1 then use the new value of a in the expression in which a resides. ++ postincrement a++ Use the current value of a in the expression in which a resides, then increment a by 1. -- predecrement --b Decrement b by 1 then use the new value of b in the expression inwhich b resides. -- postdecrement b-- Use the current value of b in the expression in which b resides, thenDecrement b by 1.

  21. Precedence of Order Operator Associativity Type ()left to right parentheses ++--+-(type) right to left unary * / %left to right multiplicative + -left to right additive < <= >>=left to right relational == !=left to right equality ?:right to left conditional = += -=*= /=%=right to leftassignment

  22. Common Escape Characters Escape Sequence Description \n Newline. Position the cursor to the beginning of the next line. \t Horizontal tab. Move the cursor to the next tab stop. \r Carriage return. Position the cursor to the beginning of the current line; Do not advance to the next line. \\ Backslash. Used to print a backslash character. \’ Single quote. Used to print a single quote character. \” Double quote. Used to print a double quote character.

  23. Comparison.java public void paint(Graphics g) { g.drawString("The Comparison results are :"70,75); if (num1 == num2) g.drawString(num1 +"=="+num2,100,90); if (num1 != num2) g.drawString(num1 +"!="+num2,100,105); if (num1 < num2) g.drawString(num1 +"<"+num2,100,120); if (num1 > num2) g.drawString(num1 +">"+num2,100,135); if (num1 <= num2) g.drawString(num1 +"<="+num2,100,150); if (num1 >= num2) g.drawString(num1 +">="+num2,100,165); } public boolean action (Event e, Object o) { if (event.target==input2) { num1=Integer.parseInt(input1.getText()); num2=Integer.parseInt(input2.getText()); repaint(); } return true; } } //Another Comparison Java Program import java.applet.Applet; import java.awt.*; public class Comparison extends Applet { Label prompt1; TextField input1; Label prompt2; TextField input2; int num1,num2; public void init() { prompt1 = new Label("Enter an interger"); input1 = new TextField(10); prompt2 = new Label("Enter an interger and Press Enter"); input2 = new TextField(10); add(prompt1); add(input1); add(prompt2); add(input2); }

  24. Day 2 Object Orientation • powerful and natural paradigm for creating programs changes accompanying and aging for any system.

  25. Common Terms • Global variable is one that affects every part of the program. • Pointer is actually the address of some data in memory. • Passing a pointer means the address in memory is passed to subroutine. • A function or subroutine is a unit of program that does a certain task and returns a value that represents the outcome

  26. Object Orientation • Encapsulation • Inheritance • Polymorphism

  27. Object Orientation • Encapsulation: mode of protection over code and data.

  28. Common Terms • A class represents an abstraction for a set of objects that share the same structure and behavior. • An object is a single instance of a class that retains the structure and behavior as define by the class, sometimes objects are referred to as instance of class. • A method is a message to take some action on an object. These messages are like subroutine calls or procedure language (function).

  29. Additional Notes Methods and instance variables can be declared public, it is best to hide variables behind method. Encapsulated object can be hidden from other parts of the system. This helps protect the object from evolutionary decay.

  30. A Class Vs An Instance • A class is something that describes the general attributes of an object, including the types of each attributes and the methods that can operate on the object. • An instance is a particular incidence of a class objects.

  31. Example of class and instance • The class Date may have attributes • Doors, engine, wheels, model, style • And may have methods • six mercedes benz is instances of class car

  32. Example of class and instance2 • The class Date may have attributes • Day, Month, Year • And may have methods • Date(), Date(int,int,int), setDay(), setMonth(), setYear(), showDay(), showMonth(), showYear(), showall()

  33. Example of Date Class Object publicvoid setDay(int d) { if ((d<1)||(d>31)) { System.out.println("Error day"); } else { day = d; } } publicvoid setMonth(int m) { if ((m<1)||(m>12)) { System.out.println("Error month"); } else { month = m; } } publicvoid isLeap() { } } publicclass Date { int day,month; int year=2000; boolean leap; public Date() { } public Date (int d, int m, int y) { day = d; month = m; year = y; } publicvoid ShowDate() { System.out.println(day+"/"+month+"/"+year); } publicint getDay() { return day; }

  34. Example of class DateUser (instance of Date Class) publicclass DateUser { publicstaticvoid main (String args[]) { Date christmax = new Date(); // christmax.day=25; // christmax.month=12; // christmax.year=1999; christmax.setDay(25); Date valentine = new Date(14,2,2000); christmax.ShowDate(); valentine.ShowDate(); christmax.setDay(255); christmax.setDay(30); christmax.setMonth(12); christmax.ShowDate(); } }

  35. Inheritance • Objects related to each other in a hierarchical way.

  36. Polymorphism • Properties of one object, many shapes, is a simple concept that allows a method to have multiple implementations that are selected based on which type of object is passed into the method invocation, is known method overloading. • Overloading is a way for a single class to deal with different types in a uniform way.

  37. ASCII table

  38. Mathematical Table

  39. What is String? String is a sequence (array) of character, such as “Hello”. In Java String are enclosed in quotation marks, which are not themselves part of the string. char char test1 = ‘a’; // declaration and initialize(assignment) test1 = ‘b’; // assignment String String name = “John”; // declaration and initialize(assignment) name = “Carl”; // assignment Next slide is a sample program for String and char.

  40. What is String? import java.applet.Applet; import java.awt.*; publicclass Comparison extends Applet { char value_char = 'a'; String value_string = "Hello"; publicvoid init() { } publicvoid paint(Graphics g) { g.drawString("char = "+value_char,100,75); g.drawString("string = "+value_string,100,165); } publicboolean action (Event e, Object o) { } }

  41. What is String? How does String kept in memory? String is a sequence of char so it keep in memory like a chain of char or we can call it array which we can do some operation with them. You can separate the string by using the substring function. • Concatenation If you can separate the string, you should be able to glue it up again. String fname = “Harry”; String lname = “Hacker”; String name = fname + “ “ + lname;

  42. String Function

  43. . . . Condition ? No Yes Process . . . What is Decision Decision is one of a tools in programming language. It help you extract your idea in logical way and tell that idea to computer. IF format: if ( condition ) { statement; ... }

  44. Condition ? Yes No Process 1 Process 2 . . . What is Decision IF…ELSE format: if ( condition ) { statement; ... } else { statement; … }

  45. . . Condition? Process 1 Yes No Condition ? Process 2 Yes No Error . . What is Decision IF…ELSEIF format: if ( condition ) statement; elseif ( condition ) statement; elseif ( condition ) statement; … else statement;

  46. . . Condition? Process 1 Yes No Condition ? Process 2 Yes No Error . . What is Decision SWITCH format: switch ( variable ) { case constant 1 : statement; … break; case constant 2 : statement; … break; default: statement … break; }

  47. What is Decision IF…ELSEIF format: if ( variable == constant 1) statement; elseif ( variable == constant 2) statement; else statement; SWITCH format: switch ( variable ) { case constant 1 : statement; … break; case constant 2 : statement; … break; default: statement … break; }

  48. Relational Operators JAVA > >= < <= = = != Description Greater than Greater than or equal Less than Less than or equal Equal Not equal

  49. Java Applet • In Java Applet, method start in this sequence: • init() • start() • paint(Graphics g) • action(Event e, Object o) // control method (event driven)

  50. Hello Applet //A first Java Program import java.applet.Applet; import java.awt.Graphics; public class Hello extends Applet { public void paint (Graphics g) { g.drawString ("Hello World",25,25); } }

More Related