1 / 30

JAVA P rogramming B asic

JAVA P rogramming B asic. Chapter 2. Chapter 2. 2.0 Java Programming Basic 2.1 Structured language vs OOP language 2.2 Introduction to JAVA Application 2.3 Data Types 2.4 Control Structures 2.5 Array 2.6 Packages. Structured language vs OOP language. Structured language.

jersey
Download Presentation

JAVA P rogramming B asic

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 Programming Basic Chapter 2

  2. Chapter 2 • 2.0 Java Programming Basic • 2.1 Structured language vs OOP language • 2.2 Introduction to JAVA Application • 2.3 Data Types • 2.4 Control Structures • 2.5 Array • 2.6 Packages

  3. Structured language vs OOP language Structured language Object-oriented programming A programming language where the program may be broken down into blocks or procedures which can be written without detailed knowledge of the inner workings of other blocks, thus allowing a top-down designapproach. The use of a class of programming languages and techniques based on the concept of an “object” which is a data structure (abstract data type) encapsulated with a set of routines, called “methods”, which operate on the data. Operations on the data can only be performed via these methods, which are common to all objects that are instances of a particular “class”. Thus the interface to objects is well defined, and allows the code implementing the methods to be changed so long as the interface remains the same. Taken from dictionary.die.net

  4. Structured Programming • Dividing a problem into smaller subproblem structured design. • Each subproblem is then analyzed, and solution or the subproblem is obtained. • The solution to all the subproblemare then combined to solve the overall problem. • The process of implementing a strutted design is called structured programming. • Is also known as: • Top-down design, • Stepwise refinement, • Modular programming

  5. Object-oriented Programming • In Object-oriented Design (OOD) • The first step in the problem solving process: • To identify the components called objects, form the basic of the solution • Determine how these objects interact with one another. • e.g: You want to write a program that automates the video rental process for a local video stores. Two main object: • The video • The customer

  6. Object-oriented Programming • After identifying the objects, the next step is: • To specify the relevant data for each object and possible operations to be performed on the data. • e.g: video objects The data might be :- movie name, actors, producer. The operation might be :- check the name of the movie, reduce the number of copies in stock by one after a copy is rented.

  7. Object-oriented Programming • In OOD • The final program: • Is a collection of interacting objects • A programming language that implements OOD is called an OOP. • To work with objects you need to know how to combine data and operations on that data into a single unit is called class.

  8. Object-oriented Programming • In OOP: • Object • Consists of: • Data • The operations on those data. • Combines: • Data • Operations on the that data into a single unit.

  9. Introduction of JAVA Application History of JAVA • Initially designed at Sun Microsystems by James Gosling in 1991 as a language that was embedded in consumer electronic items. The language was named as OAK. • In 1993, the growth of internet phenomena, OAK was adapted into internet to create dynamic and interactive web pages. • Sun created web browser named HotJava, was programmed using OAK language called JAVA. • JAVA not only used for web application but also as a general purpose programming language

  10. Introduction of JAVA Application

  11. Characteristics of JAVA

  12. Characteristics of JAVA

  13. Program in PC Machine language myProg.cpp Source File Program in SUN Machine language Program in MAC Machine language C++ Compiler for PC C++ Compiler for SUN C++ Compiler for MAC C++ and JAVA • Executing C++ Program

  14. JAVA Interpreter JAVA Complier myProg.java myProg.class Source File (Java Program) JVM/JRE Library files The compiled program is in the form of bytecode – the same for all machines C++ and JAVA • Executing JAVA program

  15. C++ vs JAVA

  16. C++ vs JAVA

  17. C++ vs JAVA

  18. Identifiers • Identifier is a name given to variable, constant and method name other than reserved word • It can be alphabet, numeric, underscore(_) or dollar sign ($) • The name must begin with alphabet, underscore(_) or dollar sign($) • It is case sensitive • Cannot be JAVA reserved word • Example valid identifier name : • total • _total • $total • total5

  19. JAVA Reserved Words

  20. Data Types • Primitive data types • Simple data types, also referred to as built-in types • Different category • Boolean type – boolean • Integral type – byte, int, long, char • Floating type –float, double • Reference data type • Either with a class, array or interface • Refers to object • Different category • Class types – such as String • Array types • Interface types • Constant data types • Declare using keyword final eg. final double PI = 3.142; • Constant declared inside a class using the keyword static eg. static final double PI = 3.142;

  21. String • String class is part of java.langpackage which is automatically included during compilation • Some operation used in String class • toUpperCase() • toLowerCase() • length() • charAt() • substring() • replace()

  22. Example of String Operations String name; name = “Abraham Lincoln”; name = name.toUpperCase(); //ABRAHAM LINCOLN name = name.toLowerCase();//abrahamlincoln int x = name.length(); //return the how many characters =15 name = name.charAt(2); //return a character in position 2 Name = name.substring(5); //return the whole word starting position 5 // = ham Lincoln Name = name.substring(5, 3)//return the word of length 3 starting // from position 5 = ham **position in a string always starts with 0

  23. Control Structure • Selection structure • Make decision – branching statement • Similar to C++ • if statement • if…else statement • nested if statement • switch statement • Iteration structure • Repetition – similar to C++ • for statement • while statement • do…while statement • nested loop • break and continue

  24. Array • An array is a collection of data value • An array is an indexed value of the same type • Array of primitive data types • Declaration datatype variablename [];or datatype [] variablename; • Array creation e.g. variablename = new dataype[size of array];

  25. Example double[] rainfall; //declare an array rainfall = new double[12]; //create array

  26. Packages • Consist of one or more individual classes that are stored in the same directory. • Those classes packaged are usually related in providing similar objects and methods • It facilitates software reuse by allowing programs to import classes from other packages libraries to be shared by many users • For example to use PrintStreamclass in contained in java.io package, you need to include keyword import in front of the package name import java.io.*; Then you can use the print() and println() method provided by the PrintStream class

  27. Input Statement • Using I/O console • Using Scanner class • Create a Scanner object by passing an input stream to the constructor Scanner s = new Scanner(System.in); • You can use these method to read the data from the keyboard : • Integer data value = nextInt() • Decimal data value = nextDouble(); • String data value = next(); • Example : Scanner s = new Scanner(System.in); int num = s.nextInt(); String name = s.next();

  28. Input Statement • Using dialog box • You need to use javax.swing.JOptionPane package • The method used is showInputDialog, and it has String parameter which is a message displayed on the monitor. • The method returns String value if user enters a value and null value if user cancels the input • The String value can be converted to numeric by the used of these method • Integer = Integer.parseInt() • Double = Double.parseDouble() • Foat = Float.parseFloat() • Example import javax.swing.*; -- String input = JOptionPane.showInputDialog(null, “Enter an integer number “); int num = Integer.parseInt();

  29. Output Statement • Using I/O Console • By using System.out object through the method print() or println() • Example int total = 4 +5; System.out.print(“The total is “); System.out.println(total); System.out.println(“The total is “ + total);

  30. Output Statement • Using dialog box • You need to use javax.swing.JOptionPane package • The method used is showMessageDialog, and it has String parameter which is a message displayed on the monitor screen • Example import javax.swing.*; -- JOptionPane.showMessageDialog(null,”Hello World!”); String s1 = “JAVA is fun”; JOptionPane.showMessageDialog(null, s1);

More Related