Nelson Baloian Universidad de Chile. Goals of intensive lecture To learn: –What is JAVA ? –What is JAVA good for ? –What is JAVA bad for ? –Which programming.

Slides:



Advertisements
Similar presentations
Introduction to Programming G51PRG University of Nottingham Revision 1
Advertisements

1 pritisajja.info Unlocking the World of Java Programming….. Priti Srinivas Sajja February, 2014 Visit pritisajja.info for detail Future Technology for.
 2005 Pearson Education, Inc. All rights reserved Introduction.
Client Side Programming Using Java Applet Outcomes: You will be expected to know: – Java Applets and HTML file; –bytecode and platform independent programs;
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
1 Java Basics. 2 Compiling A “compiler” is a program that translates from one language to another Typically from easy-to-read to fast-to-run e.g. from.
Programming Applets How do Applets work ? This is an HTML page This is the applet’s code It has a link to an applet.
Principles of Object-Oriented Software Development The language Java.
Introduction to Java CS 331. Introduction Present the syntax of Java Introduce the Java API Demonstrate how to build –stand-alone Java programs –Java.
Java An introduction. Example 1 public class Example1 { public static void main (String [] args) { System.out.println (“This is the first example”); int.
CS100A, Fall 1997, Lectures 221 CS100A, Fall 1997 Lecture 22, Tuesday 18 November Introduction To C Goal: Acquire a reading knowledge of basic C. Concepts:
Slide 1 of 40. Lecture A The Java Programming Language Invented 1995 by James Gosling at Sun Microsystems. Based on previous languages: C, C++, Objective-C,
COMP 110 Introduction to Programming Mr. Joshua Stough September 10, 2007.
1 Lecture#8: EXCEPTION HANDLING Overview l What exceptions should be handled or thrown ? l The syntax of the try statement. l The semantics of the try.
Hello, world! Dissect HelloWorld.java Compile it Run it.
1 The First Step Learning objectives write Java programs that display text on the screen. distinguish between the eight built-in scalar types of Java;
Lecture 1: Overview of Java. What is java? Developed by Sun Microsystems (James Gosling) A general-purpose object-oriented language Based on C/C++ Designed.
CSE 1301 J Lecture 2 Intro to Java Programming Richard Gesick.
CMSC 341 Introduction to Java Based on tutorial by Rebecca Hasti at
Programming in Java; Instructor:Moorthy Introduction, Objects, Classes, Libraries1 Programming in Java Introduction.
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
1 Course Lectures Available on line:
LESSON 2 CREATING A JAVA APPLICATION JAVA PROGRAMMING Compiled By: Edwin O. Okech [Tutor, Amoud University]
DAT602 Database Application Development Lecture 5 JAVA Review.
Java Introduction Lecture 1. Java Powerful, object-oriented language Free SDK and many resources at
Welcome to the Lecture Series on “Introduction to Programming With Java”
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Java Fundamentals Asserting Java Chapter 2: Introduction to Computer Science ©Rick Mercer.
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Fundamentals (Comments, Variables, etc.)
The Java Programming Language
CSC204 – Programming I Lecture 4 August 28, 2002.
Basic Java Programming CSCI 392 Week Two. Stuff that is the same as C++ for loops and while loops for (int i=0; i
CS 11 java track: lecture 1 Administrivia need a CS cluster account cgi-bin/sysadmin/account_request.cgi need to know UNIX
Introduction to Computer Systems and the Java Programming Language.
Java Programming, Second Edition Chapter One Creating Your First Java Program.
Java means Coffee Java Coffee Beans The name “JAVA” was taken from a cup of coffee.
Basics of Java IMPORTANT: Read Chap 1-6 of How to think like a… Lecture 3.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Netprog: Java Intro1 Crash Course in Java. Netprog: Java Intro2 Why Java? Network Programming in Java is very different than in C/C++ –much more language.
CHAPTER 3 GC Java Fundamentals. 2 BASICS OF JAVA ENVIRONMENT  The environment  The language  Java applications programming Interface API  Various.
 JAVA Compilation and Interpretation  JAVA Platform Independence  Building First JAVA Program  Escapes Sequences  Display text with printf  Data.
Chapter 2: Java Fundamentals
1 Introduction to Java. 2 What is Java? A programming language. A platform –A virtual machine (JVM) definition. –Runtime environments in diverse hardware.
CIS 260: App Dev I. 2 Programs and Programming n Program  A sequence of steps designed to accomplish a task n Program design  A detailed _____ for implementing.
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 2: Variables & Data Types.
National Taiwan University Department of Computer Science and Information Engineering National Taiwan University Department of Computer Science and Information.
© 2004 Pearson Addison-Wesley. All rights reserved ComS 207: Programming I Instructor: Alexander Stoytchev
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 5P. 1Winter Quarter C Programming Basics Lecture 5.
Core Java Introduction Byju Veedu Ness Technologies httpdownload.oracle.com/javase/tutorial/getStarted/intro/definition.html.
STRUCTURE OF JSP PRESENTED BY: SIDDHARTHA SINGH ( ) SOMYA SHRIVASTAV ( ) SONAM JINDAL ( )
An Introduction to Java – Part 1 Erin Hamalainen CS 265 Sec 001 October 20, 2010.
COP 2551 Introduction to Object Oriented Programming with Java Topics –Introduction to the Java language –Code Commenting –Java Program Structure –Identifiers.
Dr. Abdullah Almutairi Spring PHP is a server scripting language, and a powerful tool for making dynamic and interactive Web pages. PHP is a widely-used,
CS 106 Introduction to Computer Science I 01 / 24 / 2007 Instructor: Michael Eckmann.
Review A program is… a set of instructions that tell a computer what to do. Programs can also be called… software. Hardware refers to… the physical components.
Object Oriented Programming Lecture 2: BallWorld.
CSE 110: Programming Language I Matin Saad Abdullah UB 1222.
SESSION 1 Introduction in Java. Objectives Introduce classes and objects Starting with Java Introduce JDK Writing a simple Java program Using comments.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
 It is a pure oops language and a high level language.  It was developed at sun microsystems by James Gosling.
Sung-Dong Kim, Dept. of Computer Engineering, Hansung University Java - Introduction.
Introduction to programming in java
Chapter No. : 1 Introduction to Java.
Lecture Note Set 1 Thursday 12-May-05
Programming Language Concepts (CIS 635)
An Introduction to Java – Part I, language basics
Names of variables, functions, classes
Chap 1. Getting Started Objectives
Presentation transcript:

Nelson Baloian Universidad de Chile

Goals of intensive lecture To learn: –What is JAVA ? –What is JAVA good for ? –What is JAVA bad for ? –Which programming resources offers JAVA to support internetworking programming ? –How does JAVA programming looks like ?

Why is JAVA so popular ? Object oriented: the modern programming paradigm Robust: avoids programming features which can frequently cause errors, permits recovering from errors. Aware of the Network: easy for programming in a TCP/IP network programming Muti-platform It is free and has very good documentation

What is Object Orientation ? Data used by the program are objects Objects are a collection of variables and methods Method that modifies the values of the object’s variables Method that returns the value of the object’s variables

Examples of Objects A clock: the variables are the position of the pointers A telephone book: the variables are a list of names and telephone numbers Set the pointers’ value What time is it ? Set a name with a telephone number change the telephone number delete a pair name-telephone Given a name ask the telephone number

Classes and Objects Every object is of a certain class The class define the type of the object, this means –what variables contains an object of this class –which methods can be applied to an object of this class Class Clock Object A of class clock Object B of class clock Object C of class clock

An Example about use of Objects in Java (1) Clock A = new Clock( ); A.setTime(3,35); int i; i = A.getHour( ); Put pointers in 3 and 35 Get the value of the hour-pointer

An Example about use of Objects in Java (2) PhoneBook B = new PhoneBook( ); B.addEntry(“Eduardo Vera”, ); B.getNumber(“Eduardo Vera” ); Adds the name and the phone number given Gets the number of the person Creates a new Phone Book Eduardo Vera Eduardo Vera

Why Object Orientation ? A strategy to develop bigger programs with few errors and at lower cost (time) by building over existing components

Robust (compared with C) The basic instructions in Java are similar to the basic instructions in C BUT it has not the instructions and characteristics which make C less stable like: –Pointer arithmetic –assignation in conditions like in if (a = 9) –garbage collection (liberating memory which is not used any more) It permits to program a set of instructions within a try and catch block try { instructions which may cause an error } catch (Error e) { instructions to react to the error }

Network savvy Java appears when the internet and the WWW are already popular and in rapid expansion (1995) It has a library with classes which make possible to easily communicate 2 programs running in a TCP/IP network (internet) INTERNET

Examples Provides a library with classes which make possible to easily communicate 2 programs running in a TCP/IP network (internet) –Find IP Address given the host’s name –Establish URL connections with www servers –Build TCP & UDP sockets –Implement RMI by implementing remote objects

JAVA is an interpreted Language Java compiler (specific for each platform) javac P1.java P1.javaP1.class Java interpreter (specific for each platform) also called Java Virtual Machine java P1 (class) Program’s output

What kinds of programs can I develop with JAVA ? Stand alone programs: written with a text editor, compiled and interpreted Applets: programs that run inside www page. Servlets: programs invoked by a www page running on the server side Java script JSP

Java Applets Java program running on the client Html Animator.class Applets are java programs which are downloaded with the HTML page. Animator.class

Java Script Java program running on the client Html & Script The code of the java program is written directly in the HTML page the code

Java Servlets The code of the java program which runs on the server and can dynamically produce HTML content according to the particular situation (the client of a bank) MyServlet.class HTML-page with a reference to a servlet HTML from page HTML from servlet

In JAVA Programming means to write classes What components (attributes) should have an object of this class ? How can I create a new Object (constructor) ? What methods can I perform on an object of this class ? There may be a static method which can be called from “outside”. Is my new class the extension of an existing one ?

How do I write a simple stand alone program ? public class Classname { public static void main(String args[ ]) { HERE JAVA INSTRUCTIONS } // end of the main method } // end of the class Write a file with the program (the name should be Classname.java) Compile it with the command javac Classname.java This will create a new file Classname.class Execute the command java Classname and the instructions written in the main method will be executed.

public class HelloWorld { public static void main(String args[ ]) { System.out.println(“Hello World ” ); } // end of the main method } // end of the class Every program should start with a class declaration: public class ClassName (class name is given by the programmer) The instruction which will be executed should be written inside a method called main(String args[ ]) The instructions should be written inside the brackets {} The “Hello World” example

public class HelloWorld { public static void main(String args[ ]) { System.out.println(“Hello World ” ); } // end of the main method } // end of the class After a double slash // the rest of the line is considered comments and are not executable instructions The instruction System.out.println(“Hello World ” ); writes a line on the screen with everything that is inside the brackets This program should be written in a file named HelloWorld.java The “Hello World” example

public class Program2 { public static void main(String args[ ]) { int myNumber1 = 5; double myNumber2 = 9.0; System.out.println( “ number is ” +myNumber1); System.out.println( ” number plus 1 is ” +(myNumber1+1)); System.out.println( ” number minus 3 is ” +(myNumber1-3)); System.out.println( ” double of number is ” +(myNumber1*2)); System.out.println( ” half of number is ” +(myNumber1/2)); System.out.println( ” My other number is ” +myNumber2); System.out.println( ” The half is ” + (myNumber2/2)); System.out.println( “ The square root is ” + Math.sqrt(myNumber2)); } More about programming...

import java.io.*; public class Program3 { public static void main(String args[ ]) throws IOException { BufferedReader inKbd = new BufferedReader( new InputStreamReader(System.in)); String inputLine; System.out.print( “ please enter your name: ” ); inputLine = inKbd.readLine(); System.out.println( “ Hajimemashite, ” + inputLine); } Reading a line of characters from keyboard It is only possible to read a line of characters with this method It is necessary to write throws IOException at the beginning of the main method. Otherwise it will not even pass compilation For using input/output classes it is necessary to import the classes from the java.io.* library

import java.io.*; public class Program4 { public static void main(String args[ ]) throws IOException { BufferedReader inKbd = new BufferedReader( new InputStreamReader(System.in)); String inputLine; System.out.print(“I am Jalisco, enter your number ! ”); inputLine = inKbd.readLine(); int yourNumber = Integer.parseInt(inputLine); System.out.println(“My number is ”+ (yourNumber+1)+” I win !”); } Reading a number from keyboard In Java2 there is also Double.parseDouble(aString) If the content of the String does not correspond to a and error will occur during the program execution

Primitive types in Java integer: int, long, short, byte Const. 1, -1, 1024, 1L real: float, double Const. 1.0, , 1.5e4, 1.0f character: char Const. ‘a’, ‘X’, logic value: boolean Const. true, false String: “Hola“,“21 of Jannuary 2000 ” Not a Primitive

Declarations int i;//declaration int i = 1;//declaration and //initialization double pi = ; char c = ‘a’; boolean genki_desu = true; Declarations are necessary to reserve memory for storing a value in a certain part of the memory and referencing it with a name (not address) A declaration of variable can appear in any part of the program but always before the variable is used.

Expressions & Assignation Aritmethics : sum + 20 * c / (mod % 3) Boolean: a > b, b >= c, c != 4, a == 0 String: “Hello “+ name + “ today is the “+ day + “of”+month Casts: (int) pi (pi = 3.1) (int) (Math.random()*100)+1) other: a == 1 ? a+1 : a-1 Assignation: a = 1; Assignation as operator: a = b = c = d = 0;

Controlling the sequence of instructions Conditional execution of instructions: if (condition) instruction; if(condition) instruction; else instruction; It is always possible to write more than one instruction after the if and the else by grouping them inside curly brackets { }

Problem Write a program that reads 2 numbers from the keyboard. The first one is the amount a customer has to pay at a bookstore. The second is the sum the customer gives to pay for the books. The computer should respond if the sum is not enough, if it exact the amount to pay or if then customer should receive change. In case the customer should receive change, the computer must give the exact amount of 5000 and 1000 bills and 500, 100, 50, 10, 5 and 5 coins. Enter the value to pay:3561 Enter the value given by the customer :10000 give 1 of 5000 give 1 of 1000 give 4 of 100 give 3 of 10 give 1 of 5 give 4 of 1

Doing Loops The basic instruction for the loop is the while while (condition) instruction; There are others, but they are variations do instruction; while (condition); for (instr1; condition; instr2) instruction;

Example Loops The while loop int i = 1; while (i <= 10) { System.out.println(”5 X ” + i + ” = ”+(i*5)) i = i + 1; } The do while loop int i = 1; do { System.out.println(”5 X ” + i + ” = ”+(i*5)); i = i + 1; } while (i <= 10); The for loop for (i = 1; i <= 10; i = i +1) System.out.println(”5 X ” + i + ” = ”+(i*5));

Another Example: the MCD public class MCD { //computing of the maximum common //divider of 15 & 24 public static void main(String args[ ]){ int x = 15, y = 24; while (x != y) { if (x < y) y = y - x; else x = x - y; } System.out.println (“The MCD of 15 & 24 is “ + x); }