Towards writing a first pro gram
Download
Report
Transcript Towards writing a first pro gram
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
javac stands for java compiler
javac puts the compiled instructions
in a new file Welcome.class
Second stage: Tell the computer to carry out the compiled instructions
>java Welcome
Welcome to Java
Tells the computer to execute the compiled
java instructions in Welcome.class
Translating from high to low.....
Human
Programmer
speaks English
encodes problem
in a high-level
Language (Java)
Compiler
Translates Java into
compiled code
Computer
executes machine
code instructions
public class Welcome {
public static void main(String[] args){
System.out.println(“Welcome to java”);
}
}
Java source code
in file
Welcome.java
JAVAC compiles the source code
3%(*$&£.... (*&£*&£
*(&^^!__£+ ~@{W}{~@
(£()*&(*H###~}{}{}
(*(*(*(*£
Java bytecode
in file
Welcome.class
JAVA virtual machine executes code
Program
instructions carried out
10010101001010010001111110001010
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
Exit Notepad.
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).
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
Type dir
at the command prompt
(this command puts you in the Java
folder of your Home (H:) directory)
to get a directory of all files in H:\Java
(you will see Welcome.java)
Compiling your program (continued)
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.
Type dir
to get a directory of all files in H:\Java
(you will now see Welcome.java and
Welcome.class, if there were no
compilation errors)
Running your compiled program
If your Welcome.java program compiled correctly (with no
errors), you will now have a Welcome.class file that can be run.
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 program is written correctly, when run it’ll display
Welcome to java
on the command prompt
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
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.
Computer labs start next week (27th)
Where are the labs?
Go up the stairs in the computer science building to the
first floor.
Turn right and go down to the end of the corridor; the
first year labs are facing you. Go in and pick a
computer.
You must go at the time allocated for your group, as
listed on the following slides.
Lab Times each Week
You must go to the labs at the time for your option.
Arts: Monday 4-6
Actuarial & Finance: Thursday 4-6
Denominated entry computer science have a choice:
Denominated Computer Science : Monday 2-4
Denominated Computer Science : Tuesday 2-4
IMPORTANT: If you are a Denominated Computer Science student
who is doing CHEMISTRY, you MUST sign up for the MONDAY 2-4
labs. This is because your chemistry labs will be on Tuesday.
General Science lab times
General Science option F, J, or M : Thursday 2-4
General Science option I or O :
Wed 2-4
General Science option H or K : Mon 2-4 or Tues 2-4
(Students doing option H must go to the computer lab time that
does not clash with their chemistry time)
General Science option G : Tues 4-6 or Wed 4-6
(Students doing option G must go to the computer lab time that
does not clash with their chemistry time)
What you need in computer labs
To use the computer labs, you need two things.
1) Your student account and password (this will have
been given to you on registration).
(Your student account will look like a random sequence of letters
and numbers, maybe something like [email protected].)
2) Your student number (this will be on your Student
Card). If you haven’t registered yet, simply bring along
your CAO number.
3) You need both account+password and student number!