Presentation is loading. Please wait.

Presentation is loading. Please wait.

Programming Languages

Similar presentations

Presentation on theme: "Programming Languages"— Presentation transcript:

1 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

2 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

3 Translation of Java Programs
Java programs are designed to be able to run on any kind of computer when downloaded from the Web. With most languages, that would mean downloading source code for the program and having a compiler translate it into the machine code for your machine. The user would have to tell the machine to compile the source code before running the program. Java uses bytecodes to solve this problem. The kinds of Java programs referred to here are called applets. We will actually ignore applets in this subject, but will concentrate on applications. Applets may be touched upon in CSE1203, and students can do the CSE1434 elective later if they want to learn more about them. Demonstrate running an applet on the web by bringing up again.

4 Java Bytecodes The Java compiler translates the source code to bytecodes, the machine code for an imaginary machine. The bytecodes are downloaded, then translated by an interpreter on the local machine to its own machine code. This may sound like no improvement, but it is easier to write a bytecode interpreter for a particular machine than a compiler for source programs. Each type of machine has its own bytecode interpreter for Java. These are downloadable from the Web, and included in web browsers. The interpreter is called the Java virtual machine. I make an analogy here to a two-stage translation process in natural language, e.g. translating a document written in English into written Mandarin first, and then verbally into each of the languages and dialects spoken in China. Then show how this analogy matches up with the next slide.

5 Executing a Java Program
Editor Compiler source code On remote computer? bytecodes Internet Bytecode Interpreter (Maybe) An editor is like a word processor except that it is not expecting to produce a document so doesn’t word wrap, etc. It may be sensitive to some language, e.g. emacs puts in a closing parenthesis when you open one. You can use any editor to create your Java programs, as long as it produces text files as output. For example, DOS Edit or, Notepad and PFE are OK. We will use BlueJ as our environment in the labs, and you can use it at home. This is an environment designed and written by Michael Kolling and John Rosenberg of Monash University. The BlueJ editor is Java-sensitive and colour codes parts of the program to make it easier to understand. The compiler translates the program (if it can) into bytecodes which are able to be translated into machine code for different machines by different Java interpreters. The class loader's job is to take the bytecodes and load them into memory. The verifier's job is to make sure that the bytecodes are OK when they are downloaded from the web ready to run on the machine. This is supposed to make it impossible that they will actually contain a virus. Of course, the bytecodes may not be downloaded from the Internet. They may have been produced by compiling a program written on the local machine. It doesn't matter. The programs we will write for this subject are like this. There are actually two kinds of Java programs: applications and applets, and there is a difference in the way they are executed, but we will leave it at this for the moment. Bytecode Verifier On local computer copy of bytecodes bytecodes

6 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 problem into manageable pieces Design a solution (an algorithm) Consider alternatives to the solution and refine it Implement the solution Test the solution and fix any problems that exist

7 Algorithms An algorithm is the set of instructions to be obeyed to perform a particular task in order to solve a problem. Examples: a recipe the part of a recipe that makes the sauce, or the icing a section in a car repair manual a knitting pattern Programming is about problem solving. It's fun and challenging. Expand on these examples. Other examples area piece of written music, an algorithm for calculating the date of Easter, or whether a year is a leap year. Ask if they can suggest some everyday processes for which there are algorithms.

8 An Algorithm (Chocolate Pudding)
1/2 cup self-raising flour 1 tablespoon baking powder 3 tablespoons raw sugar 3 tablespoons cocoa 1/4 cup milk 1 tablespoon melted butter Sauce 1/2 cup raw sugar 200ml hot water Mix sauce ingredients together in a 1.5 litre casserole. Mix flour, baking powder and remaining sugar and cocoa together in a bowl, then stir in milk and butter. Put this mixture on top of sauce mixture in casserole. Bake for 40 minutes in a moderate oven. Point out the parts that are data and the parts that are processing. Computers don't understand English, and wouldn't be able to carry out these instructions even if they could. However, if we wanted to express this like a computer program (imagine you are telling a robot how to make this pudding), we would have to be clearer about it. See next slide.

9 A Good Algorithm An algorithm should be precise unambiguous correct
efficient maintainable We can write an algorithm in English (or any other language), but it tends to be verbose and ambiguous, so we use a programming language (or a "pretend" programming language called pseudocode). Precise means that it tells you exactly what you have to do, in sufficient detail. This includes knowing what your input data is, e.g. what ingredients you need for a recipe, or what input you need for a calculation. You might need to make sure that the input is correct (this is called validation). Unambiguous means that there are no instructions that might mean more than one thing. Correct means that it does the job completely and accurately. Efficient means that it does not waste time or space (the computer's memory). This is not so important at this stage of our learning, but in some applications it is very important, e.g. real-time systems. Maintainable means that it is easy to understand and change, without introducing errors. Explain the other words here that may be unfamiliar to students, e.g. verbose. It is worth mentioning at this point that one of the first things that students have to learn in a new subject is the terminology of that subject. It's no use our avoiding big words in order not to frighten them. Without the right language, it is very difficult to express concepts. That's what language is for. Tell students that, if they don't understand a word you use, they should put their hands up immediately and ask what it means.

10 What is a Computer Program?
An algorithm that can be executed on a computer is a program. A program usually contains several algorithms. A program is DATA plus PROCESSING. Examples the set of calculations required to work out your tax payable (algorithms for separate parts, too, e.g. gross income, deductions, Medicare) the calculation to work out whether this is a leap year Programming is about solving problems. Compare this to a program for a concert, for example. The program contains a list of what is to happen, in the order in which it will occur. Point out the data and processing in instructions to change a car tyre. A knitting pattern specifies how many balls of wool of what thickness you need, and what size knitting needles. I wonder how many students know how to knit these days :-). There may not be many students who have filled in a tax form yet, either.

11 The Java Programming Language
A programming language specifies the words and symbols that we can use to write a program A programming language employs a set of rules that dictate how the words and symbols can be put together to form valid program statements Java was created by Sun Microsystems, Inc. It was introduced in 1995 and has become quite popular It is an object-oriented language

12 A Sample Program // The first program in CSCI 201 // Aug 20, 2001
public class HelloWorld { public static void main (String [] args) System.out.println ("Hello, Welcome to CSCI 201"); System.out.println ("Get ready to program!!!"); }

13 Java Program Structure
In the Java programming language: A program is made up of one or more classes A class contains one or more methods A method contains program statements These terms will be explored in detail throughout the course A Java application always contains a method called main See (page 26)

14 Java Program Structure
// comments about the class public class MyProgram { } class header class body Comments can be added almost anywhere

15 Java Program Structure
// comments about the class public class MyProgram { } // comments about the method public static void main (String[] args) { } method header method body

16 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 They do not affect how a program works Java comments can take two forms: // this comment runs to the end of the line /* this comment runs to the terminating symbol, even across line breaks */

17 Identifiers Identifiers are the words a programmer uses in a program
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

18 Identifiers Sometimes we choose identifiers ourselves when writing a program (such as Lincoln) Sometimes we are using another programmer's code, so we use the identifiers that they chose (such as println) Often we use special identifiers called reserved words that already have a predefined meaning in the language A reserved word cannot be used in any other way

19 Reserved Words The Java reserved words: 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

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

21 Another Example import java.awt.*; import java.applet.*;
public class Einstein extends Applet { public void paint(Graphics page) { page.drawRect(50, 50, 40, 40); page.drawRect(60, 80, 225, 30); page.drawOval(75, 65, 20, 20); page.drawLine(35, 60, 100,120); page.drawString("Out of clutter, find Simplicity.", , 70); page.drawString("-- Albert Einstein", 130, 100); }

22 Problem Solving Many software projects fail because the developer didn't really understand the problem to be solved We must avoid assumptions and clarify ambiguities As problems and their solutions become larger, we must organize our development into manageable pieces This technique is fundamental to software development We will dissect our solutions into pieces called classes and objects, taking an object-oriented approach

Download ppt "Programming Languages"

Similar presentations

Ads by Google