Crash Course Review Only. Please use online Jasmit Singh 2

Similar documents
Chapter 1 Introduction to Java

Software Practice 1 - Inheritance and Interface Inheritance Overriding Polymorphism Abstraction Encapsulation Interfaces

Introduction to Programming Using Java (98-388)

Getting started with Java

13 th Windsor Regional Secondary School Computer Programming Competition

CompSci 125 Lecture 02

CS 11 java track: lecture 1

An overview of Java, Data types and variables

Introduction to Java

Sri Vidya College of Engineering & Technology

Introduction to Visual Basic and Visual C++ Introduction to Java. JDK Editions. Overview. Lesson 13. Overview

Java Programming. Atul Prakash

Programming Language Concepts: Lecture 2

Java Professional Certificate Day 1- Bridge Session

OVERRIDING. 7/11/2015 Budditha Hettige 82

CS 231 Data Structures and Algorithms, Fall 2016

Lecture 2. COMP1406/1006 (the Java course) Fall M. Jason Hinek Carleton University

Outline. Parts 1 to 3 introduce and sketch out the ideas of OOP. Part 5 deals with these ideas in closer detail.

Goals. Java - An Introduction. Java is Compiled and Interpreted. Architecture Neutral & Portable. Compiled Languages. Introduction to Java

Programming overview

Program Fundamentals

SRIVIDYA COLLEGE OF ENGG & TECH INTRODUCTION TO OOP AND JAVA FUNDAMENTALS SVCET. Ada 95 Fortran 2003 PHP since v4, greatly enhanced in v5

Introduction Basic elements of Java

CS 251 Intermediate Programming Methods and Classes

CS 251 Intermediate Programming Methods and More

Data Structures. Data structures. Data structures. What is a data structure? Simple answer: a collection of data equipped with some operations.

PROGRAMMING FUNDAMENTALS

STRUCTURING OF PROGRAM

Getting Started with Java. Atul Prakash

Computer Components. Software{ User Programs. Operating System. Hardware

Lecture 1: Overview of Java

Midterm Exam CS 251, Intermediate Programming March 6, 2015

JAVA: A Primer. By: Amrita Rajagopal

1. Download the JDK 6, from

Java Bytecode (binary file)

THE CONCEPT OF OBJECT

Course Outline. Introduction to java

5/3/2006. Today! HelloWorld in BlueJ. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont.

Contents. I. Classes, Superclasses, and Subclasses. Topic 04 - Inheritance

Answer1. Features of Java

Introduction to Java

CHAPTER 1. Introduction to JAVA Programming

INDEX. A SIMPLE JAVA PROGRAM Class Declaration The Main Line. The Line Contains Three Keywords The Output Line

S.E. Sem. III [CMPN] Object Oriented Programming Methodology

The Java language has a wide variety of modifiers, including the following:

3 CREATING YOUR FIRST JAVA APPLICATION (USING WINDOWS)

BIT Java Programming. Sem 1 Session 2011/12. Chapter 2 JAVA. basic

Java is a high-level programming language originally developed by Sun Microsystems and released in Java runs on a variety of

Java Intro 3. Java Intro 3. Class Libraries and the Java API. Outline

Lecture 02, Fall 2018 Friday September 7

Java Foundations: Unit 3. Parts of a Java Program

1 Shyam sir JAVA Notes

C02: Overview of Software Development and Java

Chapter 6 Introduction to Defining Classes

Application Development in JAVA. Data Types, Variable, Comments & Operators. Part I: Core Java (J2SE) Getting Started

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

Outline. Overview. Control statements. Classes and methods. history and advantage how to: program, compile and execute 8 data types 3 types of errors

CS260 Intro to Java & Android 03.Java Language Basics

Programming Basics. Digital Urban Visualization. People as Flows. ia

Chapters 1-4 Summary. Syntax - Java or C? Syntax - Java or C?

STATIC, ABSTRACT, AND INTERFACE

Building Java Programs. Introduction to Programming and Simple Java Programs

Programming Language Concepts: Lecture 1

Outline. Java Models for variables Types and type checking, type safety Interpretation vs. compilation. Reasoning about code. CSCI 2600 Spring

Day 2. COMP 1006/1406A Summer M. Jason Hinek Carleton University

CSE 142 Su 04 Computer Programming 1 - Java. Objects

Java Programming. MSc Induction Tutorials Stefan Stafrace PhD Student Department of Computing

Lecture Notes CPSC 224 (Spring 2012) Today... Java basics. S. Bowers 1 of 8

Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub

QUESTIONS FOR AVERAGE BLOOMERS

CMSC 341. Nilanjan Banerjee

Object Oriented Programming

Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

Computer Programming, I. Laboratory Manual. Final Exam Solution

Lecture 3. COMP1006/1406 (the Java course) Summer M. Jason Hinek Carleton University

Programming with Java

Fundamentals of Object Oriented Programming

Computer Components. Software{ User Programs. Operating System. Hardware

Classes, Objects, and OOP in Java. June 16, 2017

CS Week 5. Jim Williams, PhD

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

CSC Java Programming, Fall Java Data Types and Control Constructs

BASIC COMPUTATION. public static void main(string [] args) Fundamentals of Computer Science I

1. Java is a... language. A. moderate typed B. strogly typed C. weakly typed D. none of these. Answer: B

Chapter 4 Defining Classes I

Java: Classes. An instance of a class is an object based on the class. Creation of an instance from a class is called instantiation.

Java Identifiers, Data Types & Variables

15CS45 : OBJECT ORIENTED CONCEPTS

9 Working with the Java Class Library

The Sun s Java Certification and its Possible Role in the Joint Teaching Material

Computing Science 114 Solutions to Midterm Examination Tuesday October 19, In Questions 1 20, Circle EXACTLY ONE choice as the best answer

History of Java. Java was originally developed by Sun Microsystems star:ng in This language was ini:ally called Oak Renamed Java in 1995

WA1278 Introduction to Java Using Eclipse

Learning the Java Language. 2.1 Object-Oriented Programming

Problem Grade Total

Last Name: Circle One: OCW Non-OCW

Programming. Syntax and Semantics

NATIONAL UNIVERSITY OF SINGAPORE

The Java programming environment. The Java programming environment. Java: A tiny intro. Java features

Transcription:

@ Jasmit Singh 1

Crash Course Review Only Please use online resources @ Jasmit Singh 2

Java is an object- oriented language Structured around objects and methods A method is an action or something you do with the object An object has member variables (Instance variables) that define the different properties of the object Java code is written as.java files Compiled into.class files using javac Java run time environment Virtual Machine java 3

Compile-time Java Source (.java) Run-time Class Loader Java Class Libraries Java Compiler Java Bytecodes move locally or through network Java Interpreter Just in Time Compiler Java Virtual machine Java Bytecode (.class ) Runtime System Operating System Hardware 4

JDK freely download from http://www.oracle.com Any text editors can be used to create a.java file Vi/vim, emacs, notepad, wordpad Eclipse IDE Eclipse http://www.eclipse.org Android Development Tools (ADT) is a plugin for Eclipse 5

Write java class HelloWorld containing a main() method and save in file HelloWorld.java The file name MUST be the same as class name Compile with: javac HelloWorld.java Creates compiled.class file: HelloWorld.class Run the program: java HelloWorld Notice: use the class name directly, no.class! 6

File name: HelloWorld.java /* Our first Java program HelloWorld.java */ public class HelloWorld { //main() } Command line arguments public static void main ( String[] args ) { System.out.println( Hello world!" ); } Standard output, print with new line 7

Step 1 l File > New > Java Project l Project Name : HelloWorld Step 2 l File > New > Class l source folder : HelloWorld/src l Package : com.example l Name : HelloWorld l check "public static void main (String[] args) 8

Step 3 l Add your code System.out.println( Hello world! ); Step 4 l Run > Run As > Java Application 9

Fundamental unit of Java program All java programs are classes A class is a template or blueprint for objects A class describes a set of objects that have identical characteristics (data elements) and behaviors (methods). Existing classes provided by JRE User defined classes Each class defines a set of fields (variables), methods or other classes 10

A class describes the attributes and behaviors of an object. An interface defines behaviors that a class implements. You cannot instantiate an interface. An interface does not contain any constructors. All of the methods in an interface are abstract. An interface cannot contain instance fields. The only fields that can appear in an interface must be declared both static and final. An interface is not extended by a class; it is implemented by a class. An interface can extend multiple interfaces. @ Jasmit Singh 11

An interface is implicitly abstract. You do not need to use the abstract keyword when declaring an interface. Each method in an interface is also implicitly abstract, so the abstract keyword is not needed. Methods in an interface are implicitly public. @ Jasmit Singh 12

File name: Animal.java /* File name : Animal.java */ interface Animal { } public void eat(); public void travel(); 13

/* File name : MammalInt.java */ public class MammalInt implements Animal{ public void eat(){ System.out.println("Mammal eats"); } public void travel(){ System.out.println("Mammal travels"); } public int nooflegs(){ return 0; } } public static void main(string args[]){ MammalInt m = new MammalInt(); m.eat(); m.travel(); } @ Jasmit Singh 14

Fields (instance variables) in a class are private and providing access to the fields via public methods. Anyone outside the class cannot directly access or modify the fields. Also referred to as data hiding. Users of a class do not know how the class stores its data. Example? 15

Each sub- class acquires all properties of its super- class Information is made manageable in a hierarchical order Keywords extend and implements Animal Mammal Reptile Human Being Dog Lizard @ Jasmit Singh 16

Interface same despite different data types Ability of an object to take on many forms draw() method Circle Shape Triangle Parallelogram Penatgon Hexagon @ Jasmit Singh 17

Abstract class is one that cannot be instantiated All other functionality of the class still exists fields, methods, and constructors Parent class contains common functionality of a collection of child classes, but the parent class itself is too abstract to be used on its own. @ Jasmit Singh 18

An object is an instance of a class An object has state, behavior and identity Internal variable: store state Method: produce behavior Unique address in memory: identity All objects of the same type share code they all have same object type, but can have different field values. 19

public class Dog{ String breed; int age; String color; void barking(){} void hungry(){} void sleeping(){} } Variables Method class: keyword 20

Dog roxy = new Dog(); //declaration roxy.age= 2; //set properties roxy.breed= Pit Bull ; roxy.color= Brown ; Dog sam = new Dog(); sam.age= 5; //set properties sam.breed= Cocker Spanial ; sam.color= White ; roxy.barking(); sam.sleeping(); // call method 21

roxy breed: Roxy age: 2 color: Brown sam breed: Sam age: 5 color: White References Objects allocated in memory 22

Primitive type Size Minimum Maximum Wrapper type boolean 1-bit Boolean char 16-bit Unicode 0 Unicode 2 16-1 Character byte 8-bit -128 +127 Byte short 16-bit -2 15 +2 15-1 Short int 32-bit -2 31 +2 31-1 Integer long 64-bit -2 63 +2 63-1 Long float 32-bit IEEE754 IEEE754 Float double 64-bit IEEE754 IEEE754 Double 23

A package defines the path to a class Uniquely identifies a class with respect to other classes that may have the same class name - avoid conflicts between classes Packages also reflect the folder structure. Give packages meaningful names. Grouping similar classes together makes it easier to search and utilize different classes when programming. @ Jasmit Singh 24

Default (no modifier): Visible to the package Private: Visible to class only Public: Visible to World Protected: Visible to package and all sub- classes 25

Java methods and variables can be declared static These exist independent of any object static methods can be called even if no objects of that class have been created static data is shared by all instances (i.e., one value per class instead of one per instance 26

Operator Meaning == equal to, as in (a == b)!= not equal to, as in (x!= y) > greater than, as in (z > y) < Less than, as in (z<y) <= Less than equal to, as in (z<=y) >= Greater than equal to, as in (z>=y) ((a==b) (a==c)) // true only if A is equal to B or equal to C && ((a==b)&& (a==c)) // true only if A is equal to B and equal to C @ Jasmit Singh 27

A number of different code paths possible that branch from a single variable value A default option to execute if none other cases apply Each case can be terminated with a break statement @ Jasmit Singh 28

char singlechar= z ; switch(singlechar) { case 'a': case 'e': case 'i': case 'o': case 'u': // singlechar is a vowel! Execute this code! break; default: // singlechar is a consonant! Execute this code instead! break; } @ Jasmit Singh 29

Example of If If else If elseif else switch @ Jasmit Singh 30

Type while //loop continuously provided that a statement is true Example int numitemstoprocess = 3; while(numitemstoprocess > 0){ // process an item numitemstoprocess- - ; } do while // conditional loop expression AFTER the first iteration do{ // check for items to process, update numitemstoprocess as required // process item, updated numitemstoprocess } while (numitemstoprocess > 0); for // loop for a specific number of iterations for(int i = 1; i <=100; i++){ // print i } @ Jasmit Singh 31

Examples of Loops @ Jasmit Singh 32

If, else // check for a condition if(ivar==0) { // variable is zero } else if (ivar > 0) { // variable is a positive number } else { // variable is a negative number } InstanceOf // test if an object is of a specific type void checkfortextview(view v) { if(v instanceof TextView) { // This is a TextView control } } else { // This is not a TextView control } @ Jasmit Singh 33

Data structure to store an ordered list of items char avowels[] = {'a','e','i','o','u'}; Size of array int length = avowels.length; Can store primitive types or Objects Dog a = new Dog(); Dog b = new Dog(); Dog c= new Dog(); Dog dogs[] = {a,b,c}; or Dog[] dogs = new Dogs[3]; dogs[1] = new Dog(); dogs[2] = new Dog(); dogs[3] = new Dog(); @ Jasmit Singh 34

Interface for a collection of objects Having them work similarly with a high degree of interoperability High performance implementations Allow for adaptation by extending easily Examples: Interfaces - List, Maps, Sets, SortedMap Classes LinkedList, ArrayList, TreeSet, HashMap, TreeMap @ Jasmit Singh 35

ArrayList<Dog> dogs= new ArrayList<Dog>(); Dog roxy = new Dog( Roxy, 5); Dog sam = new Dog( Sam, 10); dogs.add(roxy); dogs.add(sam); System.out.println( Number of dogs is +dogs.size()); dogs.remove(sam) @ Jasmit Singh 36

HashMap<String,Student> classgrades= new HashMap<String,Student>(); Student st1 = new Student( Charles,3.8); Student st2 = new Student( Raechel, 2.5); Student st3 = new Student( Andrea, 4.0); classgrade.put( CHARLES, st1); classgrade.put( RAECHEL, st2); classgrade.put( ANDREA, st3); System.out.println( Rachel s class grade is + classgrades.get( RAECHEL ).getgrade()); @ Jasmit Singh 37

38