March 2005Java Programming1. March 2005Java Programming2 Why Java? Platform independence Object Oriented design Run-time checks (fewer bugs) Exception.

Slides:



Advertisements
Similar presentations
Chapter 22 Implementing lists: linked implementations.
Advertisements

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.
Air Force Institute of Technology Electrical and Computer Engineering
CERTIFICATION OBJECTIVES Use Class Members Develop Wrapper Code & Autoboxing Code Determine the Effects of Passing Variables into Methods Recognize when.
IntroductionIntroduction  Computer program: an ordered sequence of statements whose objective is to accomplish a task.  Programming: process of planning.
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
Java Syntax Primitive data types Operators Control statements.
Programming in Java; Instructor:Alok Mehta Objects, Classes, Program Constructs1 Programming in Java Objects, Classes, Program Constructs.
Inheritance and interfaces A class C1 is derived from class C2, then C1 is called subclass, and C2 is called superclass Superclass-parent, base class Subclass.
CS 225 Java Review. Java Applications A java application consists of one or more classes –Each class is in a separate file –Use the main class to start.
Review Java.
Classes, Types and Objects Data Structures Chapter 1 Prof. Vidya Manian Dept. of Electrical and Comptuer Engineering ICOM 4035Java Programming BasicsECE,
Java Data Types  Everything is an Object  Except Primitive Data Types  For efficiency  Platform independent  Portable  “slow”  Objects are often.
Programming Principles Data types and Variables. Data types Variables are nothing but reserved memory locations to store values. This means that when.
Agenda Review User input Scanner Strong type checking Other flow-control structures switch break & continue Strings Arrays 2.
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
6/6/2005 CS 3345: Algorithm Analysis and Data Structures Summer 2005, UT-Dallas 1 Specific topics of the Java Language Related to This Class Classes and.
P Object type and wrapper classes p Object methods p Generic classes p Interfaces and iterators Generic Programming Data Structures and Other Objects Using.
Java Tutorial. Object-Oriented Programming Concepts Object –a representation of some item state  fields/members and should be encapsulated behavior 
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Lecture 2 Object Oriented Programming Basics of Java Language MBY.
Basic Java Programming CSCI 392 Week Two. Stuff that is the same as C++ for loops and while loops for (int i=0; i
Chap. 1 Classes, Types, and Objects. How Classes Are Declared [ ] class [extends ] [implements,, … ] { // class methods and instance variable definitions.
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 0. HAFTA Algorithms FOURTH EDITION Robert Sedgewick and Kevin Wayne Princeton University.
Page: 1 การโปรแกรมเชิงวัตถุด้วยภาษา JAVA บุรินทร์ รุจจนพันธุ์.. ปรับปรุง 15 มิถุนายน 2552 Keyword & Data Type มหาวิทยาลัยเนชั่น.
CompSci 100E 2.1 Java Basics - Expressions  Literals  A literal is a constant value also called a self-defining term  Possibilities: o Object: null,
Introduction to Java Lecture Notes 3. Variables l A variable is a name for a location in memory used to hold a value. In Java data declaration is identical.
Java Programming The Language of Now & the Future* Lecture 0 An Introduction to Java Syntax for Non-C Programmers John Morris Department of Electrical.
Java Programming Java Basics. Data Types Java has two main categories of data types: –Primitive data types Built in data types Many very similar to C++
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
Copyright Curt Hill Variables What are they? Why do we need them?
Objects & Classes Weiss ch. 3. So far: –Point (see java.awt.Point) –String –Arrays of various kinds –IPAddress (see java.net.InetAddress) The Java API.
CS 11 java track: lecture 2 This week: more on object-oriented programming (OOP) objects vs. primitive types creating new objects with new calling methods.
Java Language Basics By Keywords Keywords of Java are given below – abstract continue for new switch assert *** default goto * package.
Chapter 7: Class Inheritance F Superclasses and Subclasses F Keywords: super and this F Overriding methods F The Object Class F Modifiers: protected, final.
Java Nuts and Bolts Variables and Data Types Operators Expressions Control Flow Statements Arrays and Strings.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
SOEN 343 Software Design Section H Fall 2006 Dr Greg Butler
CSI 3125, Preliminaries, page 1 Data Type, Variables.
Duke CPS From C++ to Java l Java history: Oak, toaster-ovens, internet language, panacea l What it is ä O-O language, not a hybrid (cf. C++)
Spring 2009 Programming Fundamentals I Java Programming XuanTung Hoang Lecture No. 8.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
An Introduction to Java – Part 1 Erin Hamalainen CS 265 Sec 001 October 20, 2010.
CSC Java Programming, Spring, 2010 Week 2: Java Data Types, Control Constructs, and their C++ counterparts.
© 2006 Pearson Addison-Wesley. All rights reserved 1-1 Chapter 1 Review of Java Fundamentals.
JAVA Programming (Session 2) “When you are willing to make sacrifices for a great cause, you will never be alone.” Instructor: รัฐภูมิ เถื่อนถนอม
CompSci 100E JB1.1 Java Basics (ala Goodrich & Tamassia)  Everything is in a class  A minimal program: public class Hello { public static void main(String[]
Object Oriented Programming Lecture 2: BallWorld.
 It is a pure oops language and a high level language.  It was developed at sun microsystems by James Gosling.
Key Words / Reserved Words
JAVA MULTIPLE CHOICE QUESTION.
Chapter 4 Assignment Statement
Objects, Classes, Program Constructs
Chapter 3 Assignment Statement
Advanced Programming Behnam Hatami Fall 2017.
Overloading and Constructors
Starting JavaProgramming
null, true, and false are also reserved.
Introduction to Java Programming
An overview of Java, Data types and variables
The Building Blocks Classes: Java class library, over 1,800 classes:
FINAL EXAM Final Exam Wednesday, Dec 14: 8: :00 AM (Frny G140)
Java Programming Language
Chap 2. Identifiers, Keywords, and Types
Agenda Types and identifiers Practice Assignment Keywords in Java
Overloading Each method has a signature: its name together with the number and types of its parameters Methods Signatures String toString()
Problem 1 Given n, calculate 2n
Review of Java Fundamentals
Presentation transcript:

March 2005Java Programming1

March 2005Java Programming2 Why Java? Platform independence Object Oriented design Run-time checks (fewer bugs) Exception handling Garbage collection some drawbacks (speed, RAM )

March 2005Java Programming3 Object Oriented Design software is built from objects each object is an instance of a class classes are arranged in a hierarchy each class defines fields and methods

March 2005Java Programming4 Types basic (primitive) types boolean (true/false) char (16 bits), byte (8 bits) short (16), int (32), long (64) float (32 bits), double (64 bits) Objects “structured” types must be “instantiated”

March 2005Java Programming5 Instantiation basic types (space allocated automatically) int n; arrays (must allocate space explicitly) int[] col = new int[5]; Objects Gnome g; (allocates reference only) g = new Gnome(...); (calls constructor method) several constructors may be defined for the same class, with different parameters.

March 2005Java Programming6 arrays array of Objects = array of “references” two-step allocation Gnome[] party = new Gnome[5]; for( int i=0; i < party.length; i++ ) { party[i] = new Gnome(...);} 2-dimensional array = “array of arrays” int table[][] = new int[10][20]; triangular arrays (allocate each row separately) int[][] table = new int[10][]; for( int i=0; i < table.length; i++ ) { table[i] = new int[i]; }

March 2005Java Programming7 Number Objects (“wrappers”) basic type “wrapped” inside an Object Integer N = new Integer(1045); int n = N.intValue(); Double X = new Double( ); double x = X.doubleValue(); these classes provide useful methods Integer.parseInt( String s ) Double.isInfinite( double v )

March 2005Java Programming8 String class concatenation String s = “kilo” + “meters” several useful methods int length() char charAt( int index ) int compareTo( String other ) see also StringBuffer

March 2005Java Programming9 Methods return types parameters constructor methods main method local variables

March 2005Java Programming10 Point class public class Point { private double x; private double y; public double getX() { return( x ); } public double getY() { return( y ); } public void translate( double dx, double dy ) { x += dx; y += dy; }

March 2005Java Programming11 Modifiers (class/field/method) Special purpose static (all instances share same value) final (can’t reassign/override/subclass) abstract (can’t instantiate) Access control public (anyone can access) protected (same package or subclass) friendly (default - only same package) private (only same class can access)

March 2005Java Programming12 Expressions Literals null true, false integer 42, 176L, -52I Floating point Character ’\t’ String “status quo ante”

March 2005Java Programming13 Operator Precedence ~ ! (postfix, prefix, type conversion) * / % (multiply/divide) + - (add/subtract) > >>> (shift) >= (comparison) [also instanceof] == != (equality) & (bitwise-and) ^ (bitwise-xor) | (bitwise-or) && (and) || (or) ? : (conditional) = += -= *= /= %= >>= >>= &= ^= |= (assignment)

March 2005Java Programming14 Casting Numberical conversion int ifrac, i=3, j=4; double dfrac, d=3.2; dfrac = i / d; // ( i automatically cast to double ) dfrac = i / (double) j ; // ( i again cast to double ) ifrac = i / d; // (will cause a compilation error) ifrac = i / (int) d;// (loss of precision made explicit) String conversion String s = “” + 22; // (correct, but ugly) String s = Integer.toString(22); // (much better)

March 2005Java Programming15 Control Flow if( ) {... } else {... } switch loops for( ; ; ) {... } while( ) {... } do {... } while( ); return break / continue

March 2005Java Programming16 Input / Output Output - very simple System.out.println(“size:\t” + x ); Input - a bit more complicated BufferedReader InputStreamReader System.in (see, for example Copy.java )

March 2005Java Programming17 Packages classes can be collected in a package standard packages are provided, or you can create your own import a single class import java.io.InputStream; import an entire package import java.io.*;

March 2005Java Programming18 Next Time Object Oriented design Inheritance/Polymorphism Exception handling Interfaces & abstract classes Casting Design Patterns Javadoc