Java程序辅导

C C++ Java Python Processing编程在线培训 程序编写 软件开发 视频讲解

客服在线QQ:2653320439 微信:ittutor Email:itutor@qq.com
wx: cjtutor
QQ: 2653320439
. . . . . .
.
.. ..
.
.
Object-Oriented Programming
Lab 1 Exercises
Ewan Klein
School of Informatics
Inf1 :: 2009/10
Ewan Klein (School of Informatics) Object-Oriented ProgrammingLab 1 Exercises Inf1 :: 2009/10 1 / 11
. . . . . .
Introduction
Who’s who
Scheduled vs. drop-in labs
Ewan Klein (School of Informatics) Object-Oriented ProgrammingLab 1 Exercises Inf1 :: 2009/10 2 / 11
. . . . . .
Today’s Exercises
...1 HelloWorld
...2 PersonalGreeting
...3 Adder
...4 Divider
Ewan Klein (School of Informatics) Object-Oriented ProgrammingLab 1 Exercises Inf1 :: 2009/10 3 / 11
. . . . . .
Edit-Compile-Run
gedit is a good choice of lightweight text editor on DICE, and provides
syntax highlighting for Java.
You need to run the java command in the same folder as your compiled file.
.Commandline Primer..
.. ..
.
.
: mkdir myjava
: cd myjava
: javac HelloWorld.java
: ls
HelloWorld.class HelloWorld.java
: java HelloWorld
Hello World!
Ewan Klein (School of Informatics) Object-Oriented ProgrammingLab 1 Exercises Inf1 :: 2009/10 4 / 11
. . . . . .
PersonalGreeting
New stuff — uses variables and functions
Let’s look at variables
Ewan Klein (School of Informatics) Object-Oriented ProgrammingLab 1 Exercises Inf1 :: 2009/10 5 / 11
. . . . . .
Assigning Values to Variables
Schematic form for assigning to a variable:
var = value;
Important: = is the operator in an imperative, not a logical assertion.
.Variable Assignments..
.. ..
.
.
name = ”Bob”;
number = 33;
Ewan Klein (School of Informatics) Object-Oriented ProgrammingLab 1 Exercises Inf1 :: 2009/10 6 / 11
. . . . . .
Declaring Variables
But Java wants to know the type of the value!
Schematic form for declaring a variable:
type var;
.Variable Declarations..
.. ..
.
.
String name;
int number;
Important: first declare the variable (reserve some space in memory); then
assign a value of the appropriate type.
Ewan Klein (School of Informatics) Object-Oriented ProgrammingLab 1 Exercises Inf1 :: 2009/10 7 / 11
. . . . . .
Putting it Together
.Declarations plus Assignment..
.. ..
.
.
String name;
name = ”Bob”;
int number;
number = 33;
.Short Version..
.. ..
.
.
String name = ”Bob”;
int number = 33;
Ewan Klein (School of Informatics) Object-Oriented ProgrammingLab 1 Exercises Inf1 :: 2009/10 8 / 11
. . . . . .
Hello World with added variables
.Putting the message in a variable..
.. ..
.
.
public class HelloWorld {
public static void main ( String [] args ) {
String msg = ”Hello World!”;
System.out.println( msg );
}
}
Ewan Klein (School of Informatics) Object-Oriented ProgrammingLab 1 Exercises Inf1 :: 2009/10 9 / 11
. . . . . .
Using return value from a function
Suppose we have a function today() that returns a string giving today’s date.
We can assign this return value to a variable:
.Assigning a return value..
.. ..
.
.
String date = today();
Ewan Klein (School of Informatics) Object-Oriented ProgrammingLab 1 Exercises Inf1 :: 2009/10 10 / 11
. . . . . .
Finally
Adder and Divider should be fairly self-explanatory
Questions?
The lab 1 exercises:
http://www.inf.ed.ac.uk/teaching/courses/inf1/oop/Labs/build/
html/lab1.html
Or follow link from:
http://www.inf.ed.ac.uk/teaching/courses/inf1/oop/
Ewan Klein (School of Informatics) Object-Oriented ProgrammingLab 1 Exercises Inf1 :: 2009/10 11 / 11