Presentation is loading. Please wait.

Presentation is loading. Please wait.

310417 2000 WEEK 2 1 Software Concepts -- Introduction Now we can begin to examine the basic ideas behind writing programs Chapter 2 focuses on: history.

Similar presentations


Presentation on theme: "310417 2000 WEEK 2 1 Software Concepts -- Introduction Now we can begin to examine the basic ideas behind writing programs Chapter 2 focuses on: history."— Presentation transcript:

1 310417 2000 WEEK 2 1 Software Concepts -- Introduction Now we can begin to examine the basic ideas behind writing programs Chapter 2 focuses on: history the structure of a Java application basic program elements preparing and executing a program basic object-oriented programming concepts helpful support for writing software Java applets

2 310417 2000 WEEK 2 2 History James Gosling Oak Java, May 20, 1995, Sun World HotJava The first Java-enabled Web browser

3 310417 2000 WEEK 2 3 Java Development Tools JDK1.0, JDK1.0.2, JDK1.1, JDK1.2 Inprise JBuilder (RAD) Microsoft Visual J++ Symantec Café (RAD) Rouge Wave JFactory Sun Java Workshop IBM Visual Age for Java (RAD)

4 310417 2000 WEEK 2 4 Java Program Structure See Welcome.java A program is made up of one or more classes A class contains one or more methods A method contains program statements A Java application always executes the main method

5 310417 2000 WEEK 2 5 Welcome.java //This application program prints Welcome to Java! public class Welcome { public static void main (String[] args) { System.out.println("Welcome to Java!"); } RunSource

6 310417 2000 WEEK 2 6 White Space Spaces, blank lines, and tabs are collectively called white space and are used to separate words and symbols in a program Extra white space is ignored A valid Java program can be formatted many different ways See Welcome2.java Programs should be formatted to enhance readability, using consistent indentation

7 310417 2000 WEEK 2 7 Comments Comments in a program are also called inline documentation They should be included to explain the purpose of the program and describe processing steps Java comments can take two forms: // comment runs to the end of the line /* comment runs to terminating symbol, even across line breaks */

8 310417 2000 WEEK 2 8 Identifiers Identifiers are the words a programmer uses in a program Most identifiers have no predefined meaning except as specified by the programmer An identifier can be made up of letters, digits, the underscore character (_), and the dollar sign They cannot begin with a digit Java is case sensitive, therefore Total and total are different identifiers

9 310417 2000 WEEK 2 9 Reserved Words Some identifiers, called reserved words, have specific meanings in Java and cannot be used in other ways abstract boolean break byte byvalue case cast catch char class const continue default do double else extends false final finally float for future generic goto if implements import inner instanceof int interface long native new null operator outer package private protected public rest return short static super switch synchronized this throw throws transient true try var void volatile while

10 310417 2000 WEEK 2 10 Literals A literal is an explicit data value used in a program Integer literals: 25 69 -4288 Floating point literals: 3.14159 42.075 -0.5 String literals: "The result is: " "To thine own self be true."

11 310417 2000 WEEK 2 11 The Java API The Java Application Programmer Interface (API) is a collection of classes that can be used as needed The println and print methods are part of the Java API; they are not part of the Java language itself Both methods print information to the screen; the difference is that println moves to the next line when done, but print does not See Countdown.java

12 310417 2000 WEEK 2 12 String Concatenation and Addition The + operator serves two purposes When applied to two strings, they are combined into one ( string concatenation ) When applied to a string and some other value (like a number), that value is converted to a string and they are concatenated When applied to two numeric types, they are added together arithmetically See Antarctica.java and Sum.java

13 310417 2000 WEEK 2 13 Programming Languages There are four basic programming language levels: machine language assembly language high-level language fourth-generation language Each CPU has its own specific machine language The other levels were created to make programming easier

14 310417 2000 WEEK 2 14 Programming Languages A program must be translated into machine language before it can be executed on a particular type of CPU This can be accomplished in several ways A compiler is a software tool which translates source code into a specific target language Often, that target language is the machine language for a particular CPU type The Java approach is somewhat different

15 310417 2000 WEEK 2 15 Java Translation and Execution The Java compiler translates Java source code into a special representation called bytecode Java bytecode is not the machine language for any traditional CPU Another software tool, called an interpreter, translates bytecode into machine language and executes it Therefore the Java compiler is not tied to any particular machine Java is considered to be architecture-neutral

16 310417 2000 WEEK 2 16 Java Translation and Execution Java source code Machine code Java bytecode Java interpreter Bytecode compiler Java compiler

17 310417 2000 WEEK 2 17 Java Translation and Execution Executing the compiler in a command line environment: > javac Welcome.java This creates a file called Welcome.class, which is submitted to the interpreter to be executed: > java Welcome The.java extension is used at compile time, but the.class extension is not used with the interpreter Other environments do this processing in a different way

18 310417 2000 WEEK 2 18 Syntax and Semantics The syntax of a language defines how you can put symbols, reserved words, and identifiers together to make a valid program The semantics of a language construct is the meaning of the construct; it defines its role in a program A syntactically correct program does not mean it is logically (semantically) correct A program will always do what we tell it to do, not what we meant to tell it to do

19 310417 2000 WEEK 2 19 Errors A program can have three types of errors The compiler will find problems with syntax and other basic issues ( compile-time errors ) If compile-time errors exist, an executable version of the program is not created A problem can occur during program execution, such as trying to divide by zero, which causes a program to terminate abnormally ( run-time errors ) A program may run, but produce incorrect results ( logical errors )

20 310417 2000 WEEK 2 20 Command Line Arguments See Name_Tag.java The main method accepts extra information on the command line when a program is executed > java Name_Tag Seree Each extra value is called command line argument In Java, command line arguments are always read as a list of character strings

21 310417 2000 WEEK 2 21 Name_Tag.java public class Name_Tag { public static void main (String[] args) { System.out.println ("Hello. My name is " + args[0]); } // method main } // class Name_Tag

22 310417 2000 WEEK 2 22 Software Engineering We should always strive to engineer our software to make it reliable and maintainable As the complexity of a program increases, its cost to develop and revise grows exponentially cost complexity

23 310417 2000 WEEK 2 23 Software Components Programs are easier to construct and modify when they are made up of separate components A software component can be thought of as any program element that transforms input into output Input Output Component 15 38 16 22 Compute average

24 310417 2000 WEEK 2 24 Software Components Components can be combined to make larger components

25 310417 2000 WEEK 2 25 Object-Oriented Programming Java is object-oriented language Programs are made from software components called objects An object contains data and methods An object is defined by a class Multiple objects can be created from the same class

26 310417 2000 WEEK 2 26 Object-Oriented Programming A class represents a concept and an object represents the realization of that concept Car My first car John's car Dad's car Class Objects

27 310417 2000 WEEK 2 27 Object-Oriented Programming Objects can also be derived from each other using a process called inheritance Objects, classes, and inheritance will be discussed in greater detail later Vehicle TrainCarAirplane

28 310417 2000 WEEK 2 28 Class Libraries The Java API is a class library, a group of classes that support program development Classes in a class hierarchy are often related by inheritance The classes in the Java API is separated into packages The System class, for example, is in package java.lang Each package contains a set of classes that relate in some way

29 310417 2000 WEEK 2 29 The Java API Packages Some packages in the Java API: java.applet java.awt java.beans java.io java.lang java.math java.net java.rmi java.security java.sql java.text java.util

30 310417 2000 WEEK 2 30 Importing Packages Using a class from the Java API can be accomplished by using its fully qualified name: java.lang.System.out.println (); Or, the package can be imported using an import statement, which has two forms: import java.applet.*; import java.util.Random; The java.lang package is automatically imported into every Java program

31 310417 2000 WEEK 2 31 Java Applets A Java applet is a Java program that is intended to be sent across a network and executed using a Web browser A Java application is a stand alone program Applications have a main method, but applets do not Applets are derived from the java.applet.Applet class See Confucius.java and No_Parking.java Links to applets can be embedded in HTML documents

32 310417 2000 WEEK 2 32 Java Applets Java source code Java bytecode Java compiler Java interpreter Web browser local computer remote computer

33 310417 2000 WEEK 2 33 A Simple Applet /* This is an example of Java applets */ import java.awt.Graphics; public class WelcomeApplet extends java.applet.Applet { public void paint (Graphics g) { g.drawString("Welcome to Java!",10,10); } Source

34 310417 2000 WEEK 2 34 Creating an HTML File: WelcomeApplet.html <applet code="WelcomeApplet.class" width = 100 height = 40>

35 310417 2000 WEEK 2 35 Viewing Java Applets

36 310417 2000 WEEK 2 36 Applet Viewer Utility appletviewer htmlfile.html Example: appletviewer WelcomeApplet.html Run Applet Viewer

37 310417 2000 WEEK 2 37 Applications vs. Applets Similarities Differences

38 310417 2000 WEEK 2 38 Security Restrictions on Applets Applets are not allowed to read from, or write to, the file system of the computer viewing the applets. Applets are not allowed to run any programs on the browser’s computer. Applets are not allowed to establish connections between the user’s computer and another computer except with the server where the applets are stored.


Download ppt "310417 2000 WEEK 2 1 Software Concepts -- Introduction Now we can begin to examine the basic ideas behind writing programs Chapter 2 focuses on: history."

Similar presentations


Ads by Google