Presentation is loading. Please wait.

Presentation is loading. Please wait.

How do we make our Welcome.java program do something? The java in our Welcome.java file won’t do anything by itself. We need to tell the computer to execute.

Similar presentations


Presentation on theme: "How do we make our Welcome.java program do something? The java in our Welcome.java file won’t do anything by itself. We need to tell the computer to execute."— Presentation transcript:

1 How do we make our Welcome.java program do something? The java in our Welcome.java file won’t do anything by itself. We need to tell the computer to execute the program in the file. This is a two-stage process. First stage: Compile the program into a concise set of instructions your computer can carry out directly >javac Welcome.java Second stage: Tell the computer to carry out the compiled instructions >java Welcome Welcome to Java javac stands for java compiler Tells the computer to execute the compiled java instructions in Welcome.class javac puts the compiled instructions in a new file Welcome.class

2 Translating from high to low..... Human speaks English Programmer encodes problem in a high-level Language (Java) Compiler Translates Java into compiled code Computer executes machine code instructions

3 public class Welcome { public static void main(String[] args){ System.out.println(“Welcome to java”); } Java source code in file Welcome.java 3%(*$&£.... (*&£*&£ *(&^^!__£+ ~@{W}{~@W}{~@ (£()*&(*H###~}{}{} (*(*(*(*£ Java bytecode in file Welcome.class 10010101001010010001111110001010 Program instructions carried out JAVAC compiles the source code JAVA virtual machine executes code

4 Writing your first program Open a new file in Notepad (or any other simple editor; not Word, tho!). You’ll find Notepad under Programs\Accessories. Type the Welcome.java code into this new file. Save the file in the Java folder in your home directory, with the name Welcome.java You have now written and saved your first draft of the program. Next you need to compile your program (convert it into a form that can do something), and once compiled, to run it (see what it does). Exit Notepad.

5 Compiling your program Open the command prompt on your computer. You’ll find this under Programs\Accessories. The command prompt is a window that allows you to type commands directly to the computer. Type cd H:\Java  at the command prompt (this command puts you in the Java folder of your Home ( H: ) directory) Type dir  to get a dir ectory of all files in H:\Java (you will see Welcome.java)

6 Compiling your program (continued) Type dir  to get a dir ectory of all files in H:\Java (you will now see Welcome.java and Welcome.class, if there were no compilation errors) Type javac Welcome.java  The javac command compiles your high-level Welcome.java file into low-level bytecode program in a file Welcome.class If there are any “grammatical” errors in your Welcome.java program (and there often are!) javac will print them out.

7 Running your compiled program Type java Welcome  to run the Welcome program The java command gets the file Welcome.class and executes the bytecode commands in that file. If your Welcome.java program compiled correctly (with no errors), you will now have a Welcome.class file that can be run. If you give commands like java Welcome.class or java Welcome.java they won’t work because the java command will look for Welcome.class.class or Welcome.java.class If your Welcome program is written correctly, when run it’ll display Welcome to java on the command prompt

8 Methods: a first introduction Two ways to make tea: 1: boil water; put teabag into cup;... etc...... 2: tell your younger brother: makeTea(1 milk, 0 sugar); A method is a short way of calling a bunch of other statements

9 Built-in classes and methods Printing things on the screen is actually quite complicated (we’d need to know how to draw the characters, where to put them, how to move to a new line, and so on.) Java has built-in System.out classes that other programmers have written to handle all these complications. These classes contain methods like println or print that we can call to execute the series of statements needed to print something on the screen. There are a lot of such built-in classes and methods in Java that do all sorts of useful things. We will learn many of them

10 More on methods System.out.println("Hi there"); Method: makeTea; println Carried out by: Younger brother; System.out Arguments: 1 Milk, 0 sugar; "Hi there" We need to know 1) the method we want to execute; 2) the class that can carry out that method; 3) the arguments the method takes.

11 Programs and classes Each class describes a thing that does certain actions. Large programs can contain multiple classes describing different things. public class Foo { public static void main(String[] args) { // your code in here } This would be placed in a file called Foo.java. A java file always has the exact same name as the class containing main. Smallest possible Java program: 1 class containing 1 (main) method & 0 statements

12 How do we develop programs? understand the problem design an algorithm (semi-formal) implement the algorithm (write Java) compile and test Debug (fix mistakes)

13 Edit... Compile... Debug cycle Write/edit Java code Compile and run Debug (find the mistakes)

14 Homework Read Liang, pp 14..20 (top of page) Do review questions, p. 26: 1.4 1.7 1.8 1.10 1.12 You will also be doing programming exercises in your practicals.


Download ppt "How do we make our Welcome.java program do something? The java in our Welcome.java file won’t do anything by itself. We need to tell the computer to execute."

Similar presentations


Ads by Google