Download Introduction to Programming and Java

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Lecture 1.
Introduction to Programming
and Java
MIT- AITI 2003
What is a Computer Program?
• For a computer to be able to do anything (start up,
play a song, run a word processor), it must first be
given the instructions to do so.
• The instructions are written by humans in different
computer languages like C, C++, Java, etc.
• The computer does not understand these
languages. They must first be converted
(compiled) to code that the machine can run
(0101010101111)
How Are Programs Designed?
• Programs are written in a step by step
instruction method which gets run every
time you “run the program”.
• For example, a program can be compared to a script for
a play
– A script is a set of instructions that calls for specific:
• Dialogue
• Acting directions
• Scenery
– A program is also a set of instructions that might call for specific:
• Displays
• Commands
– Additionally, a script may also produce a different looking play
each time it is performed because of:
• Different actors
• Different theaters
• Different directors
– Similarly, the same computer program can behave differently
because of:
• Different user input
• Different software/hardware
• Different platforms
How Do Computers Interpret Code?
• Some programming languages are converted to machine code by a
compiler and then can run anytime. However, because different
platforms (PCs, Macs, Suns) interpret the same machine code
differently, you must compile separately for each platform
Programming Language
compiler
compiler
compiler
PC
Sun
MAC
• Other programming languages only need
to be compiled in one way and then an
interpreter (software) on the machine
converts it to the appropriate machine
code. This is how Java works.
PC
Java Program
Java Bytecode
compiler
MAC
Interpreter
Sun
• Advantages:
– Only need to compile once and then it works
on every platform
• Disadvantages:
– Software or drivers must be installed on the
computers in order to run the programs
Welcome to Java
• The programming language you will learn
in this course is called Java.
A little background about Java:
– It was developed in 1990 by Sun
Microsystems engineer James Gosling for
use in small appliances
– Unexpectedly, its popularity grew because of
its compatibility with the emerging World Wide
Web.
Advantages of Java
• It is effective for programming on the web
• It is small and simple therefore making for
faster programs
• It is secure for added protection against
hacking
• It is portable; it can run on any machine
without re-compiling
Programs in Java
• The most common Java programs are
applications and applets.
• Applications are standalone programs.
• Applets are similar to applications, but
they don't run standalone. Instead, applets
adhere to a set of conventions that lets
them run within a Java-compatible
browser.
Writing Your First Application
• Open your text-editor and type the following piece of Java
code exactly:
class HelloWorld1 {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
• Save this file as HelloWorld1.java (watch capitalization)
in the following directory: C:\Java
Compiling and Running Your First
Application
• Open the command prompt in Windows
• To run the program that you just wrote, type at the command
prompt:
cd C:\Java
• Your command prompt should now look like this:
C:\Java>
• To compile the program that you wrote, you need to run the Java
Development Tool Kit Compiler as follows:
At the command prompt type:
C:\Java> javac HelloWorld1.java
• You have now created your first compiled Java program named
HelloWorld.class
• To run your first program, type the following at the command prompt:
C:\Java>java HelloWorld1
Although the file name includes the .class extension , this part of the name must be
left off when running the program with the Java interpreter.
You’ve created your first
Java application!!
Writing Your First Applet
• Open your text-editor and type the following piece of Java code
exactly:
import java.applet.Applet;
import java.awt.Graphics;
public class HelloWorld2 extends Applet {
public void paint(Graphics g) {
g.drawString("Hello world!", 50, 25);
}
}
• Save this file as HelloWorld2.java (watch capitalization)
in the following directory: C:\Java
Compiling Your First Applet
• Open the command prompt in Windows
• To run the program that you just wrote, type at
the command prompt:
cd C:\Java
• Your command prompt should now look like this:
C:\Java>
• To compile the program that you wrote, you
need to run the Java Development Tool Kit
Compiler as follows:
At the command prompt type:
C:\Java> javac HelloWorld2.java
Running Your First Applet
• In order to run your first Java applet, you must first write
an HTML page that the applet will run in
• Open your text-editor and type the following piece of
HTML code exactly:
<HTML>
<HEAD>
<TITLE>Applet HTML Page</TITLE>
</HEAD>
<BODY>
<HR WIDTH="100%">Applet HTML Page<HR WIDTH="100%">
<P>
<APPLET code=“HelloWorld2.class" width=350 height=200></APPLET>
</P>
</BODY>
</HTML>
• Save this file as HelloWorld2.html (watch capitalization)
in the following directory: C:\Java
• Finally, to run your first applet, type the following at the
command prompt:
C:\Java> appletviewer HelloWorld2.html
You will learn more about HTML in following lectures
You’ve created your first
Java applet!!