Introduction to java Part I By Shenglan Zhang.

Slides:



Advertisements
Similar presentations
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Advertisements

Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 3: Flow Control I: For Loops.
STRING AN EXAMPLE OF REFERENCE DATA TYPE. 2 Primitive Data Types  The eight Java primitive data types are:  byte  short  int  long  float  double.
CSCI 160 Midterm Review Rasanjalee DM.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
Introduction to Computers and Programming Lecture 5 Boolean type; if statement Professor: Evan Korth New York University.
Java Intro. A First Java Program //The Hello, World! program in Java public class Hello { public static void main(String[] args) { System.out.println("Hello,
Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
Hello, world! Dissect HelloWorld.java Compile it Run it.
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 Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
The string data type String. String (in general) A string is a sequence of characters enclosed between the double quotes "..." Example: Each character.
CS 11 java track: lecture 1 Administrivia need a CS cluster account cgi-bin/sysadmin/account_request.cgi need to know UNIX
JAVA Tokens. Introduction A token is an individual element in a program. More than one token can appear in a single line separated by white spaces.
Java means Coffee Java Coffee Beans The name “JAVA” was taken from a cup of coffee.
CS591x A very brief introduction to Java. Java Developed by Sun Microsystems was intended a language for embedded applications became a general purpose.
From C++ to Java A whirlwind tour of Java for C++ programmers.
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.
 JAVA Compilation and Interpretation  JAVA Platform Independence  Building First JAVA Program  Escapes Sequences  Display text with printf  Data.
Chapter 2: Java Fundamentals
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.
Session Three Review & Conditional Control Flow. Java File Hierarchy Projects Packages Classes Methods.
An Introduction to Java – Part 1 Dylan Boltz. What is Java?  An object-oriented programming language  Developed and released by Sun in 1995  Designed.
Introduction to Java Java Translation Program Structure
Mixing integer and floating point numbers in an arithmetic operation.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
Core Java Introduction Byju Veedu Ness Technologies httpdownload.oracle.com/javase/tutorial/getStarted/intro/definition.html.
1 Basic Java Constructs and Data Types – Nuts and Bolts Looking into Specific Differences and Enhancements in Java compared to C.
CSI 3125, Preliminaries, page 1 Compiling the Program.
Chapter 2 Input, Variables and Data Types. JAVA Input JAVA input is not straightforward and is different depending on the JAVA environment that you are.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
Developed at Sun Microsystems in 1991 James Gosling, initially named “OAK” Formally announced java in 1995 Object oriented and cant write procedural.
Spring 2009 Programming Fundamentals I Java Programming XuanTung Hoang Lecture No. 8.
CSCI S-1 Section 4. Deadlines for Homework 2 Problems 1-8 in Parts C and D – Friday, July 3, 17:00 EST Parts E and F – Tuesday, July 7, 17:00 EST.
Boolean expressions, part 1: Compare operators. Compare operators Compare operators compare 2 numerical values and return a Boolean (logical) value A.
Java Part I By Wen Fei, HAO. Program Structure public class ClassName { public static void main(String[] args) { program statements } user defined methods.
An Introduction to Java – Part 1 Erin Hamalainen CS 265 Sec 001 October 20, 2010.
Chapter 1 Java Programming Review. Introduction Java is platform-independent, meaning that you can write a program once and run it anywhere. Java programs.
1 Lecture # 2. * Introducing Programming with an Example * Identifiers, Variables, and Constants * Primitive Data Types * Byte, short, int, long, float,
Object Oriented Programming Lecture 2: BallWorld.
SESSION 1 Introduction in Java. Objectives Introduce classes and objects Starting with Java Introduce JDK Writing a simple Java program Using comments.
CPSC 233 Tutorial January 21 st /22 nd, Linux Commands.
 It is a pure oops language and a high level language.  It was developed at sun microsystems by James Gosling.
Primitive data types Lecture 03. Review of Last Lecture Write a program that prints the multiplication table of 5. class MultiplicationTable { public.
Session Three Review & Conditional Control Flow. Java File Hierarchy Projects Packages Classes Methods.
CIS3931 – Intro to JAVA Lecture Note Set 2 17-May-05.
Intro to ETEC Java.
Programming in Java Sachin Malhotra, Chairperson, PGDM-IT, IMS Ghaziabad Saurabh Chaudhary, Dean, Academics, IMS Ghaziabad.
Chapter No. : 1 Introduction to Java.
Yanal Alahmad Java Workshop Yanal Alahmad
Website: SSD1 Unit 2 Intro to Java Presentation 2.3 Website:
Programming Language Concepts (CIS 635)
Intro to Java.
An Introduction to Java – Part I
Chapter 2.
CMSC 202 Static Methods.
Introduction to Programming in Java
5 Variables, Data Types.
An Introduction to Java – Part I, language basics
The Boolean (logical) data type boolean
Java Tokens & Data types
Java Intro.
Chap 1 Chap 2 Chap 3 Chap 5 Surprise Me
elementary programming
Fundamental OOP Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
In this class, we will cover:
Introduction to Object-Oriented Concepts in Java
Review of Java Fundamentals
Presentation transcript:

Introduction to java Part I By Shenglan Zhang

Program Structure public class ClassName { public static void main(String[] args) program statements } user defined methods public class ClassName { public ClassName(){ Initilization of instance fields } user defined methods

Example public class HelloWorld { public static void main(String[ ] args) System.out.println(“Hello, World”); }

Explanation Public classes are accessible from any class. Classes, which are not declared public are accessible only within their package Public methods may be called from any class. (Encapsulation) Static methods do not operate on objects. (Math.arctan(x)) The keyword void indicates that the method does not return a value. The array args of the main method stores command line arguments. (Take arguments when the program runs from command line, java blahblah 1 2)

Source Code, Compilation and Execution: The source code and the public class have to have the same name. There only can be one public class in a Java program. The source code must have .java A bytecode file is created during compilation and has the extension .class The Java interpreter executes when we type command java followed by the class name

Data Types Integers: int, short, long, byte Floating-Point Types: float, double The Character Type: char The Boolean Type: boolean (values: true, false)

Operators Arithmetic Operators: +, -, *, /, % Relational Operators: ==, !=, <, >, <=, >= Logical Operators: &&, ||, ! **Compares references, not values. Use “==“ if you want to know if two references are to the same object

Strings Strings are a standard class in Java. Constant strings are enclosed in double quotes. String Concatenation: + Example: String a = “Hello”; String b = “World”; int n = 5; System.out.println(a+b); Will print: HelloWorld System.out.println(a+n); Will print: Hello5

Variables Variables in Java need to be declared. You need to initialize variables before you use them. Examples: int n=5; (without int, compiler will complain) System.out.println(n);

If/else Statement While Loops For Loops ** Enhanced for loop : For (int e:Array){ System.out.println(e); }

Selected Member Functions: int compareTo(String other) - Negative if the implicit argument comes before the explicit argument (in alphabetical order) - Positive if the explicit argument comes before the implicit argument - 0 if the strings are equal

boolean equals(Object other) True if the implicit argument equals the explicit argument Example: System.out.println(a.equals(a)); Will print true. int length() Returns the length of the string

Arrays Declaration: int[ 5 ] arr; Initialization: Int[ ] arr = new int[ 5]; Int[ ] arr = {1 ,2, 3, 4, 5}; Arr.length returns the length of the array.

Selected Member Functions: static void sort(type [] a) Sorts an array using a QuickSort algorithm static int binarySearch(type [] a, type v) a must be a sorted array ( v and a has the same type. Ex: int ) static boolean equals(type [] a, Object other) argument other is an object. returns true if other is an array of the same type as a, if it has the same length, and if the corresponding elements match

Java Sort Method of Arrays Class public class SortJava { public static void main(String[] args) for (int i=0; i<numbers.length; i++) numbers[i]=(int)(Math.random()*n); Arrays.sort(numbers); for(int i=0; i<numbers.length; i++) System.out.println(numbers[i]); }

Quick Sort Algorithm in Java The code is contained in the directory java/quick_sort on dunx1 Structure: public class QuickSortJava { public static void main(String[] args) … } public static void sort(int[] v, int left, int right) public static void swap(int[] v, int i, int j) public static int rand(int left, int right)