Top Banner
Getting Started
41

Getting Started Programming Programming consists of two steps: design (the architects) coding (the construction workers) Programming requires: a programming.

Jan 01, 2016

Download

Documents

Ami Dickerson
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Getting Started Programming Programming consists of two steps: design (the architects) coding (the construction workers) Programming requires: a programming.

Getting Started

Page 2: Getting Started Programming Programming consists of two steps: design (the architects) coding (the construction workers) Programming requires: a programming.

Programming

• Programming consists of two steps: • design (the architects)• coding (the construction workers)

• Programming requires:• a programming language to express your ideas • a set of tools to design, edit, and debug your code • either

– a compiler to translate your programs to machine code – a machine to run the executable code

• or– an interpreter to translate and execute your program

Page 3: Getting Started Programming Programming consists of two steps: design (the architects) coding (the construction workers) Programming requires: a programming.

Algorithm design• Pancakes

800g flower4 tsp sucker1 tsp salt12 eggs16 dl milk12 sp water

1. Pour the milk in a bowl2. Pour the water in the bowl3. Add the salt4. Add the sucker5. As long as there are eggs left

1. Take an egg2. Break the egg3. Put in the bowl

6. Add the flower7. Turn the dew

Page 4: Getting Started Programming Programming consists of two steps: design (the architects) coding (the construction workers) Programming requires: a programming.

Coding• A Java program for a Pancake Robot

… double kiloFlower = 0.8; int tspSugar = 4; int tspSalt = 1; int noEggs = 12; int dlMilk = 16; int spWater = 12;

Robot.pourMilkInBowl(dlMilk); Robot.pourWaterInBowl(spWater); Robot.putSaltInBowl(tspSalt); Robot.putSugarInBowl(tspSugar); …

Variables are declared before they are used

The program is read sequentially from top to bottom

Page 5: Getting Started Programming Programming consists of two steps: design (the architects) coding (the construction workers) Programming requires: a programming.

Coding

Robot.putSugarInBowl(tspSugar);

While (noEggs > 0) { NoEggs = NoEggs -1; Robot.breakEgg(); Robot.putEggInBowl(); } Robot.putFlowerInBowl(kiloFlower); Robot.turnDew();…

Making decisions based on logical expressions

Page 6: Getting Started Programming Programming consists of two steps: design (the architects) coding (the construction workers) Programming requires: a programming.

What is Java?

• A programming language– Fully buzzword-compliant:

A simple, object oriented, distributed, interpreted, robust, secure, architecture neutral, portable, high performance, multithreaded, dynamic language.

From: Java: An OverviewJames Gosling, Sun Microsystems,

February 1995.

Page 7: Getting Started Programming Programming consists of two steps: design (the architects) coding (the construction workers) Programming requires: a programming.

What Else is Java?

• According to Gosling:– “An environment”– “A platform”– “A way of thinking”– …ok, whatever

• Java is a phenomenon– Took the world by storm in 1995 when

introduced with the HotJava web Browser– Quickly integrated with Netscape browser

Page 8: Getting Started Programming Programming consists of two steps: design (the architects) coding (the construction workers) Programming requires: a programming.

What is Java?

• Java is a general-purpose, high-level programming language.– The features of Java

• Java program is both compiled and interpreted.• Write once, run anywhere

• Java is a software-only platform running on top of other, hardware-based platforms.– Java Virtual Machine (Java VM)– The Java Application Programming Interface

(JAVA API)

Page 9: Getting Started Programming Programming consists of two steps: design (the architects) coding (the construction workers) Programming requires: a programming.

How Will Java Change My Life?

• Get started quickly

• Write less code

• Write better code

• Develop programs faster

• Avoid platform dependencies with 100% pure Java

• Write once, run anywhere

• Distribute software more easily

Page 10: Getting Started Programming Programming consists of two steps: design (the architects) coding (the construction workers) Programming requires: a programming.

Features of Java

• Simple• Architecture-neutral• Object-Oriented• Distributed• Compiled • Interpreted• Statically Typed• Multi-Threaded• Garbage Collected

• Portable• High-Performance• Robust • Secure• Extensible• Well-Understood

Page 11: Getting Started Programming Programming consists of two steps: design (the architects) coding (the construction workers) Programming requires: a programming.

Some History• 1993 Oak project at Sun

– small, robust, architecture independent, Object-Oriented, language to control interactive TV.

– didn’t go anywhere• 1995 Oak becomes Java

– Focus on the web• 1996 Java 1.0 available• 1997 (March) Java 1.1 - some language changes, much larger library, new

event handling model • 1997 (September) Java 1.2 beta – huge increase in libraries including Swing,

new collection classes, J2EE• 1998 (October) Java 1.2 final (Java2!)• 2000 (April) Java 1.3 final• 2001 Java 1.4 final (assert)• 2004 Java 1.5 (parameterized types, enum, …) (Java5!)• 2005 J2EE 1.5

Page 12: Getting Started Programming Programming consists of two steps: design (the architects) coding (the construction workers) Programming requires: a programming.

java.applet, java.awt, java.io, java.lang, java.net, java.util

java.math, java.rmi, java.security, java.sql, java.text, java.beans

javax.accessibility, javax.swing, org.omg

javax.naming, javax.sound, javax.transaction

java.nio, javax.imageio,javax.net, javax.print,javax.security, org.w3c

javax.activity,javax.management

Java 1.08 packages212 classes

Java 1.123 packages504 classes

Java 1.259 packages1520 classes

Java 1.377 packages1595 classes

Java 1.4103 packages2175 classes

Java 1.5131 packages2656 classes

New Events

Inner class

Object Serialization

Jar Files

International

Reflection

JDBC

RMI

JFC/Swing

Drag and Drop

Java2D

CORBA

JNDI

Java Sound

Timer

Regular ExpLoggingAssertionsNIO

Page 13: Getting Started Programming Programming consists of two steps: design (the architects) coding (the construction workers) Programming requires: a programming.

Java Applications and Java … lets

• Stand-alone Applications– Just like any programming language

• Applet– Run under a Java-Enabled Browser

• Midlet– Run in a Java-Enabled Mobile Phone

• Servlet– Run on a Java-Enabled Web Server

• Switchlet– …

Page 14: Getting Started Programming Programming consists of two steps: design (the architects) coding (the construction workers) Programming requires: a programming.

Java Developer's Kit (I)

• Java's programming environment– Core Java API– compiler– interpreter– debugger– dis-assembler– profiler– more...

Page 15: Getting Started Programming Programming consists of two steps: design (the architects) coding (the construction workers) Programming requires: a programming.

Java Developer's Kit (II)

Java Compiler

Java Interpreter

Java Source Java Bytecode

Compile

Run

<file>.java <file>.classJava

Dis-assembler

Page 16: Getting Started Programming Programming consists of two steps: design (the architects) coding (the construction workers) Programming requires: a programming.

Prepare and Execute Java

Source Computer

Java Program Compilation Java ByteCode

Your computer

Java ByteCode Execution Restricted Env.

Verification

Internet

Page 17: Getting Started Programming Programming consists of two steps: design (the architects) coding (the construction workers) Programming requires: a programming.

Write Once, Run Anywhere

Page 18: Getting Started Programming Programming consists of two steps: design (the architects) coding (the construction workers) Programming requires: a programming.

ByteCode: Food for the VM• For most languages, compilation

produces machine code• Java compilation produces “bytecode”

– Intermediate code readable by the VM– Transferable across the Internet as applets

• VM interprets BC into instructions– Partly responsible for performance lag

• ByteCode produced on any platform may be executed on any other platform which supports a VM

Page 19: Getting Started Programming Programming consists of two steps: design (the architects) coding (the construction workers) Programming requires: a programming.

virtual machine

execution model of Java

source(text) compiler

CPU

bytecodeinterpreterbytecodeinterpreter

dynamicloading

JITcompiler

JITcompiler

compiledcode

compiledcode

JVML

verifier

bytecode(aka. class file)

Page 20: Getting Started Programming Programming consists of two steps: design (the architects) coding (the construction workers) Programming requires: a programming.

The JIT

• Just-In-Time compiler

• Translates bytecode into machine code at runtime– 1-time overhead when run initiated– Performance increase 10-30 times

• Now the default for most JVM’s– Can be turned off if desired– JIT can apply statistical optimizations

based on runtime usage profile

Page 21: Getting Started Programming Programming consists of two steps: design (the architects) coding (the construction workers) Programming requires: a programming.

Not just one JVM, but a whole family

• JVM (J2EE & J2SE)– Well-known Java Virtual Machine.

• CVM, KVM (J2ME)– Small devices.– Reduces some VM features to fit resource-constrained

devices.

• JCVM (Java Card)– Smart cards.– It has least VM features.

• And there are also lots of other JVMs

Page 22: Getting Started Programming Programming consists of two steps: design (the architects) coding (the construction workers) Programming requires: a programming.

Java Platform & VM & Devices

Page 23: Getting Started Programming Programming consists of two steps: design (the architects) coding (the construction workers) Programming requires: a programming.

Java VM and API

• Java API and Virtual Machine insulate the Java program from hardware dependencies.

• Java API

Page 24: Getting Started Programming Programming consists of two steps: design (the architects) coding (the construction workers) Programming requires: a programming.

Java API

• Collection of ready-made software components that provide many useful capabilities.

• Grouped into libraries (packages) of related components.

• Core API– Essentials: Object,

String, Input and Output...

– Applets– Networking– Internationalization– Security– Software Components– Object Serialization– Java Database

Connectivity (JDBC)

Page 25: Getting Started Programming Programming consists of two steps: design (the architects) coding (the construction workers) Programming requires: a programming.

Java 2 Platform

Page 26: Getting Started Programming Programming consists of two steps: design (the architects) coding (the construction workers) Programming requires: a programming.

The “Hello World” Application

Page 27: Getting Started Programming Programming consists of two steps: design (the architects) coding (the construction workers) Programming requires: a programming.

Create a Java Source File

public class HelloWorld {public static void main(String[] args) {System.out.println("Hello World!");}

}

Page 28: Getting Started Programming Programming consists of two steps: design (the architects) coding (the construction workers) Programming requires: a programming.

Compile and Run

• Compile– javac HelloWorld.java

• One file named HelloWorld.class is created if the compilation is succeeds.

• Run– java HelloWorld

examples

Page 29: Getting Started Programming Programming consists of two steps: design (the architects) coding (the construction workers) Programming requires: a programming.

Programming tasks

Write code

compile

Correct errorsRun program

Page 30: Getting Started Programming Programming consists of two steps: design (the architects) coding (the construction workers) Programming requires: a programming.

The Simplest Java Application: Hello,World!

• Since Java is object-oriented, programs are organized into modules called classes, which may have data in variables and subroutines called methods.

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

Each program is enclosed in a class definition.Each program is enclosed in a class definition.

main() is the first method that is run.main() is the first method that is run.

The notation class.method or The notation class.method or package.class.method is how package.class.method is how to refer to a public method to refer to a public method (with some exceptions).(with some exceptions).

Syntax is similar to C - braces Syntax is similar to C - braces for blocks, semicolon after for blocks, semicolon after each statement. One each statement. One difference: upper and lower difference: upper and lower case matter!case matter!

Page 31: Getting Started Programming Programming consists of two steps: design (the architects) coding (the construction workers) Programming requires: a programming.

Command Line Programming

• First you have to code and edit your program in the text editor of your choice and remember to save your file in the form of SomeClass.java.

• Then you must go to the command line (hence Command Line Programming) and set the Path for your Java Virtual Machine if you have not done so already.

• Then compile your program by typing:

javac SomeClass.java • If you have errors you must go back to your editor fix and resave

your file, which can be a pain. • Once all errors are gone you will get a file called SomeClass.class.• Once that happens you can type: java SomeClass.java and observe

your output.

Page 32: Getting Started Programming Programming consists of two steps: design (the architects) coding (the construction workers) Programming requires: a programming.

Integrated Development Environments (IDE)

• JCreator, Eclipse, and NetBeans use the following concepts to manage the files for a project:– A Workspace in which multiple projects are stored– A Project in which multiple files are stored– Source files with the extension .java containing human-readable

program statements• created by you in an editor

– Class files with the extension .class containing machine-readable program logic

• Created by the Java compiler

Page 33: Getting Started Programming Programming consists of two steps: design (the architects) coding (the construction workers) Programming requires: a programming.

NetBeans 4.1

examples

Page 34: Getting Started Programming Programming consists of two steps: design (the architects) coding (the construction workers) Programming requires: a programming.

The “Hello World” Applet

Page 35: Getting Started Programming Programming consists of two steps: design (the architects) coding (the construction workers) Programming requires: a programming.

Create a Java Source FileHelloWorldApplet.java

• import java.applet.Applet;import java.awt.Graphics;

public class HelloWorldApplet extends Applet { public void paint(Graphics g) { g.drawString(“Hello World!”, 5, 25); }}

Page 36: Getting Started Programming Programming consists of two steps: design (the architects) coding (the construction workers) Programming requires: a programming.

Compile the Source File

• javac HelloWorldApplet.java

• One file named HelloWorldApplet.class is created if the compilation is succeeds.

Page 37: Getting Started Programming Programming consists of two steps: design (the architects) coding (the construction workers) Programming requires: a programming.

Displaying your applet from a Web page.• Create an HTML file with an applet tag to display the results of

drawing the applet.

<html><head><title>Simple Hello Page</title></head><body>My Java applet says:<applet code=“HelloWorldApplet.class” width=150 height=25></applet></body></html>

Name of your applet class.

The browser will use a rectangle of width 150 pixels The browser will use a rectangle of width 150 pixels and height 25 pixels to display the applet within the and height 25 pixels to display the applet within the other html.other html.

Page 38: Getting Started Programming Programming consists of two steps: design (the architects) coding (the construction workers) Programming requires: a programming.

The Simplest Java Applet: Hello, World!• Java applets are part of the class hierarchy that can call methods to

display on a screen (within the browser window). One way to draw on the screen is to call the method drawString from the standard method paint.

import java.awt.Graphics;

public class HelloWorldApplet extends java.applet.Applet{ public void paint (Graphics g) { g.drawString(“Hello World!”, 5, 25); }}

The import statement allows the use of The import statement allows the use of methods from the Graphics class without methods from the Graphics class without the dot notation .the dot notation .

The paint method displays a graphics object The paint method displays a graphics object on the screen - one of the standard methods on the screen - one of the standard methods that takes the place of main for applets.that takes the place of main for applets.

Puts this as a subclass of Puts this as a subclass of Applet.Applet.

Page 39: Getting Started Programming Programming consists of two steps: design (the architects) coding (the construction workers) Programming requires: a programming.

No main method

• Yes, applets have a main method...

• ...but it's in the browser, not in your code!

• More about that later in the course …

Page 40: Getting Started Programming Programming consists of two steps: design (the architects) coding (the construction workers) Programming requires: a programming.

Say hello to Kara

..\kara\allkara-en.jar

Page 41: Getting Started Programming Programming consists of two steps: design (the architects) coding (the construction workers) Programming requires: a programming.

So really, why learn about programming?

• Programmers make lots of money. • Programming really is fun. • Programming is very intellectually rewarding. • Programming makes you feel superior to other

people. • Programming gives you complete control over an

innocent, vulnerable machine, which will do your evil bidding with a loyalty not even your pet dog can rival.