Object- Oriented Programming (CS243)

Slides:



Advertisements
Similar presentations
Chapter 1: Computer Systems
Advertisements

1 Java Programming Basics SE-1011 Dr. Mark L. Hornick.
Basic Java programs with println statements. 2 Compile/run a program 1.Write it –code or source code: the set of instructions in a program 2.Compile it.
Building Java Programs Chapter 1 Introduction to Java Programming.
Portions Copyright 2008 by Pearson Education CSE 142 Lecture 1 Course Introduction; Basic Java.
The Java Programming Language
Copyright 2008 by Pearson Education Building Java Programs Chapter 1 Lecture 1-1: Introduction; Basic Java Programs reading: self-check: #1-14.
Outline Java program structure Basic program elements
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 1: Introduction to Java Programming.
COMP 14: Intro. to Intro. to Programming May 23, 2000 Nick Vallidis.
Copyright 2013 by Pearson Education Building Java Programs Chapter 1 Lecture 1-1: Introduction; Basic Java Programs reading:
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
1 Building Java Programs Chapter 1: Introduction to Java Programming These lecture notes are copyright (C) Marty Stepp and Stuart Reges, They may.
Copyright 2008 by Pearson Education Building Java Programs Chapter 1 Lecture 1-1: Introduction; Basic Java Programs reading: self-check: #1-14.
Prepared by Uzma Hashmi Instructor Information Uzma Hashmi Office: B# 7/ R# address: Group Addresses Post message:
1 Identifiers  Identifiers are the words a programmer uses in a program  An identifier can be made up of letters, digits, the underscore character (
Topic 2 Java Basics “When a programming language is created that allows programmers to program in simple English, it will be discovered that programmers.
Java: Chapter 1 Computer Systems Computer Programming II.
Java Language and SW Dev’t
CS 106 Introduction to Computer Science I 01 / 25 / 2010 Instructor: Michael Eckmann.
1 Module Objective & Outline Module Objective: After completing this Module, you will be able to, appreciate java as a programming language, write java.
1 Computer Systems -- Introduction  Chapter 1 focuses on:  the structure of a Java application  basic program elements  preparing and executing a program.
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.
Intro and Review Welcome to Java. Introduction Java application programming Use tools from the JDK to compile and run programs. Videos at
CHAPTER 3 GC Java Fundamentals. 2 BASICS OF JAVA ENVIRONMENT  The environment  The language  Java applications programming Interface API  Various.
Copyright 2009 by Pearson Education Building Java Programs Chapter 1 Lecture 1-1: Introduction; Basic Java Programs reading: self-check: #1-14.
Chapter 2: Java Fundamentals
Object Oriented Programming Lecture 3. Introduction  In discussing Java, some items need to be clarified  The Java programming language  The Java virtual.
Building Java Programs Chapter 1 Introduction to Java Programming Copyright (c) Pearson All rights reserved.
Building Java Programs Chapter 1 Introduction to Java Programming.
1 WELCOME TO CIS 1068! Instructor: Alexander Yates.
© 2012 Pearson Education, Inc. All rights reserved types of Java programs Application – Stand-alone program (run without a web browser) – Relaxed.
Copyright 2010 by Pearson Education CSE 142, Fall 2011 Building Java Programs Chapter 1 Lecture 1-1: Introduction; Basic Java Programs reading:
Advanced Programming CS206 Introduction 1. Programming Language Code A programming language is a language that uses specially defined words, grammar,
Copyright 2010 by Pearson Education CSE 142, Spring 2012 Building Java Programs Chapter 1 Lecture 1-1: Introduction; Basic Java Programs reading: 1.1 -
© 2011 Pearson Education, publishing as Addison-Wesley Chapter 1: Computer Systems Presentation slides for Java Software Solutions for AP* Computer Science.
Welcome to CSE 142! Zorah Fung University of Washington, Summer Building Java Programs Chapter 1 Lecture 1-1: Introduction; Basic Java Programs.
SESSION 1 Introduction in Java. Objectives Introduce classes and objects Starting with Java Introduce JDK Writing a simple Java program Using comments.
Copyright 2010 by Pearson Education APCS Building Java Programs Chapter 1 Lecture 1-1: Introduction; Basic Java Programs reading:
1 Problem Solving  The purpose of writing a program is to solve a problem  The general steps in problem solving are: Understand the problem Dissect the.
Zorah Fung University of Washington, Winter 2016
Working with Java.
Welcome to CSE 142! Whitaker Brand and Benson Limketkai
CSCI 161 – Introduction to Programming I William Killian
CSE 190D, Winter 2013 Building Java Programs Chapter 1
Advanced Programming CS206
Benson Limketkai and Marty Stepp University of Washington, Spring 2010
null, true, and false are also reserved.
Introduction to Java Programming
CS-0401 INTERMEDIATE PROGRAMMING USING JAVA
An overview of Java, Data types and variables
Chapter 1: Computer Systems
Benson Limketkai University of Washington, Spring 2011
Building Java Programs
Whitaker Brand University of Washington, Winter 2018
Units with – James tedder
Units with – James tedder
Brett Wortzman University of Washington, Summer 2011
Advanced Programming CS206
Topic 2 Java Basics “When a programming language is created that allows programmers to program in simple English, it will be discovered that programmers.
Building Java Programs
Building Java Programs
Welcome to CSE 142!.
CSE 142, Summer 2012 Building Java Programs Chapter 1
CSE 142, Spring 2012 Building Java Programs Chapter 1
Zorah Fung University of Washington, Spring 2015
CSE 142, Winter 2014 Building Java Programs Chapter 1
Zorah Fung University of Washington, Winter 2016
Presentation transcript:

Object- Oriented Programming (CS243) Java Programming

Programming languages Some influential ones: FORTRAN science / engineering COBOL business data LISP logic and AI BASIC a simple language

JAVA C 1970 C++ 1979 JAVA 1991 http://java.sun.com/ C# 2000

Text Book Introduction to Java Programming, Comprehensive Version (10th Edition) 

TIOBE Programming Community Index for Feb2015 Programming Language 1 Java 2 C++ 3 C# 4 Python 5 PHP 6 JavaScript 7 Visual Basic .NET 8 Perl 9 Objective-C 10 Assembly language 11 Ruby 12 Delphi 13 Visual Baseic 14 Pascal 15 Swift 16 Matlab 17 PL/SQL http://www.tiobe.com/index.php/content/paperinfo/tpci/index.html

What can JAVA Do??? Web Application Desktop Application Network Application

Java SE version history Editions Java Card for smartcards Java Platform, Micro Edition (Java ME) — targeting environments with limited resources. Java Platform, Standard Edition (Java SE) — targeting workstation environments. Java Platform, Enterprise Edition (Java EE) — targeting large distributed enterprise or Internet environments. Java SE version history JDK 1.0 (January 23, 1996) JDK 1.1 (February 19, 1997) J2SE 1.2 (December 8, 1998) J2SE 1.3 (May 8, 2000) J2SE 1.4 (February 6, 2002) J2SE 5.0 (September 30, 2004) Java SE 6 (December 11, 2006) Java SE 7 (July 2011) Java SE 7 (Expected 2009)

Advantages of JAVA Java is simple. Java is object-oriented. Java is platform-independent. Java embraces Distributed computing. Java is secure. Java is reliable. Java is multithreaded. Automatic memory management Java is free! Disadvantages of JAVA Slower memory-consuming Single-pattern language (Every thing is an object) statically typed language Easy to decompile (need java obfuscator) Advantages of JAVA JAVA offers a number of advantages to developers. Java is simple: Java was designed to be easy to use and is therefore easy to write, compile, debug, and learn than other programming languages. The reason that why Java is much simpler than C++ is because Java uses automatic memory allocation and garbage collection where else C++ requires the programmer to allocate memory and to collect garbage. Java is object-oriented: Java is object-oriented because programming in Java is centered on creating objects, manipulating objects, and making objects work together. This allows you to create modular programs and reusable code. Java is platform-independent: One of the most significant advantages of Java is its ability to move easily from one computer system to another.  The ability to run the same program on many different systems is crucial to World Wide Web software, and Java succeeds at this by being platform-independent at both the source and binary levels. Java is distributed: Distributed computing involves several computers on a network working together. Java is designed to make distributed computing easy with the networking capability that is inherently integrated into it.  Writing network programs in Java is like sending and receiving data to and from a file. For example, the diagram below shows three programs running on three different systems, communicating with each other to perform a joint task. Java is interpreted: An interpreter is needed in order to run Java programs. The programs are compiled into Java Virtual Machine code called bytecode.  The bytecode is machine independent and is able to run on any machine that has a Java interpreter. With Java, the program need only be compiled once, and the bytecode generated by the Java compiler can run on any platform. Java is secure: Java is one of the first programming languages to consider security as part of its design. The Java language, compiler, interpreter, and runtime environment were each developed with security in mind. Java is robust: Robust means reliable and no programming language can really assure reliability. Java puts a lot of emphasis on early checking for possible errors, as Java compilers are able to detect many problems that would first show up during execution time in other languages. Java is multithreaded: Multithreaded is the capability for a program to perform several tasks simultaneously within a program. In Java, multithreaded programming has been smoothly integrated into it, while in other languages, operating system-specific procedures have to be called in order to enable multithreading. Multithreading is a necessity in visual and network programming. Disadvantages of JAVA Performance: Java can be perceived as significantly slower and more memory-consuming than natively compiled languages such as C or C++. Look and feel: The default look and feel of GUI applications written in Java using the Swing toolkit is very different from native applications. It is possible to specify a different look and feel through the pluggable look and feel system of Swing. Single-paradigm language: Java is predominantly a single-paradigm language. However, with the addition of static imports in Java 5.0 the procedural paradigm is better accommodated than in earlier versions of Java. http://en.wikipedia.org/wiki/Type_system#Static_typing

Assessment Scheme Type of assessment Marks? Week Number Quiz at lecture 5 marks Week 4 Quiz at section Week 5 Practical exam at lab 10 marks Week 7 Written Exam at lecture Week 11 Marks Week 12 Assignment 10 Marks Week 15 Written Exam 40 Marks Final

Java Basic Information Source code is first written in plain text files ending with the .java extension. JAVA source files are compiled into .class files by the java compiler. The .class file contains bytecodes which is the machine language of the Java Virtual Machine(Java VM). The JVM runs your application by running the .class file. Through the Java VM, the same application is capable of running on multiple platforms. class MyProgram { …….. } MyProgram.java ………… ……… ………. MyProgram.class C# .class=intermediate language file .exe JVM=Common Language Runtime (CRM) classes has .cs extention Java is both compiled and interpreted. At first, the Java source code (in .java files) is compiled into the so-called Bytecode (.class files). The Bytecode is a pre-compiled, platform independent version of your program. The .class files can be used on any operating system. When the Java application is started, the Bytecode is interpreted by the Java Virtual Mashine. Because the Bytecode is pre-compiled, Java does not have the disadvantages of classical interpreted languages, like BASIC. Read more: http://wiki.answers.com/Q/Is_java_interpreted#ixzz1HgWHbMTb A bytecode program may be executed by parsing and directly executing the instructions, one at a time. This kind of bytecode interpreter is very portable. Some systems, called dynamic translators, or "just-in-time" (JIT) compilers, translate bytecode into machine language as necessary at runtime: this makes the virtual machine unportable, but doesn't lose the portability of the bytecode itself. For example, Java and Smalltalk code is typically stored in bytecoded format, which is typically then JIT compiled to translate the bytecode to machine code before execution. This introduces a delay before a program is run, when bytecode is compiled to native machine code, but improves execution speed considerably compared to direct interpretation of the source code—normally by several measures.

Compiling/running a program Write it. code or source code: The set of instructions in a program. Compile it. compile: Translate a program from one language to another. byte code: The Java compiler converts your code into a format named byte code that runs on many computer types. Run (execute) it. output: The messages printed to the user by a program. source code compile byte code run output

your first application! The Java SE Development Kit 7 (JDK 7). IDE( Integrated Development Environment). 1-JDK 7 http://java.sun.com/javase/downloads/index.jsp JDK 7 : Java SE 7 Java SE 7 Documentation 2-IDE www.textpad.com www.eclipse.org/‎ www.netbeans.org Alternative :JDK7 & NetBeans Bundle

Creating Your First Application Create a source file Compile the source file into a .class file :The Java programming language compiler (javac) takes your source file and translates its text into bytecodes. (. class files are produced only if there are no compilation errors) Run the program The Java application launcher tool (java) uses the Java virtual machine to run your application. Your first application, HelloWorldApp, will simply display the greeting "Hello world!". To create this program, you Create a source file A source file contains code, written in the Java programming language, that you and other programmers can understand. You can use any text editor to create and edit source files. Compile the source file into a .class file The Java programming language compiler (javac) takes your source file and translates its text into instructions that the Java virtual machine can understand. The instructions contained within this file are known as bytecodes. Run the program The Java application launcher tool (java) uses the Java virtual machine to run your application. will:  javac java

A Simple Java Program Modifier Keyword Identifier for Class name // this program prints “Hello World” to screen public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World!"); } } Comment Main method declaration Statement Calling the method for printing to screen package cs243; HelloWorld.java

Syntax syntax: The set of legal structures and commands that can be used in a particular language. Every basic Java statement ends with a semicolon ; The contents of a class or method occur between { and } syntax error (compiler error): A problem in the structure of a program that causes the compiler to fail. Examples: Missing semicolon Too many or too few { } braces Illegal identifier for class name Class and file names do not match ...

compiler error A problem in the structure of a program that causes the compiler to fail and prevent the creation of the .class file. 1 public class Hello { 2 pooblic static void main(String[] args) { 3 System.owt.println("Hello, world!") 4 } 5 } Hello.java:2: <identifier> expected pooblic static void main(String[] args) { ^ Hello.java:5: ';' expected } 2 errors compiler output:

Structure of a Java program class: a program public class name { public static void main(String[] args) { statement; ... } Every executable Java program consists of a class, that contains a method named main, that contains the statements (commands) to be executed. method: a named group of statements statement: a command to be executed

The main method The main method is called automatically when the class is executed The main method signature is public static void main (String [] args) The argument of main method must be an array of string ,however it can take any name The main must be public ,void and static

A Java program Its output: Hello, world! This program produces public class Hello { public static void main(String[] args) { System.out.println("Hello, world!"); System.out.println(); System.out.println("This program produces"); System.out.println("four lines of output"); } Its output: Hello, world! This program produces four lines of output

System.out.println A statement that prints a line of output on the console. pronounced "print-linn" sometimes called a "println statement" for short Two ways to use System.out.println : System.out.println("text"); Prints the given message as output. System.out.println(); Prints a blank line of output. 20

Names and identifiers You must give your program a name. public class GangstaRap { Naming convention: capitalize each word (e.g. MyClassName) Your program's file must match exactly (GangstaRap.java) includes capitalization (Java is "case-sensitive") identifier: A name given to an item in your program. must start with a letter or _ or $ subsequent characters can be any of those or a number legal: _myName TheCure ANSWER_IS_42 $bling$ illegal: me+u 49ers side-swipe Ph.D's

Keywords keyword: An identifier that you cannot use because it already has a reserved meaning in Java. abstract default if private this boolean do implements protected throw break double import public throws byte else instanceof return transient case extends int short try catch final interface static void char finally long strictfp volatile class float native super while const for new switch continue goto package synchronized i.e., You may not use char or while for the name of a class.

Strings string: A sequence of characters to be printed. Restrictions: Starts and ends with a " quote " character. The quotes do not appear in the output. Examples: "hello" "This is a string. It's very long!" Restrictions: May not span multiple lines. "This is not a legal String." May not contain a " character. "This is not a "legal" String either." 23

Escape sequences escape sequence: A special sequence of characters used to represent certain special characters in a string. \t tab character \n new line character \" quotation mark character \\ backslash character Example: System.out.println("\\hello\nhow\tare \"you\"?\\\\"); Output: \hello how are "you"?\\

Questions What println statements will generate this output? This program prints a quote from the Gettysburg Address. "Four score and seven years ago, our 'fore fathers' brought forth on this continent a new nation." A "quoted" String is 'much' better if you learn the rules of "escape sequences." Also, "" represents an empty String. Don't forget: use \" instead of " ! '' is not the same as "