Java程序辅导

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

客服在线QQ:2653320439 微信:ittutor Email:itutor@qq.com
wx: cjtutor
QQ: 2653320439
DCS/100: Procedural Programming
Week 2: Variables, Types and Assignment
Queen Mary, University of London
DCS/100: wk 2 – p.1/42
Last Week
From last week you should be able to:
write simple straight-line programs
that use basic input and output
explain the declaration of variables
DCS/100: wk 2 – p.2/42
A program from last week
class hello2 extends basic
{
public static void main (String param[])
throws Exception
{
input in = new input();
output out = new output();
String name;
out.writeln("What’s your name?");
name = in.readline();
out.writeln("Hello " + name);
in.close(); out.close();
}
}
DCS/100: wk 2 – p.3/42
This week
This week we will cover:
variables (again)
procedures and functions (again)
assignment (again)
types: Strings, integers, booleans, ...
expressions
DCS/100: wk 2 – p.4/42
By the end of the week
You should
be comfortable with simple I/O
use different types such as int and String
be able to write programs that do calculations and print
results.
DCS/100: wk 2 – p.5/42
Motivation
Write down the single most important personal reason for
you doing this course.
Why are you here?
DCS/100: wk 2 – p.6/42
Matters arising. . .
Do your exercises in different files, e.g. ex1.java,
ex2.java, . . .
At the end of the week put them in a new directory:
mkdir week1
mv *.java week1
DCS/100: wk 2 – p.7/42
Types: Exercise
Put the following into groups of similar things:
1 true “hello” 5 ’p’ 17 false 6.2 ’+’
“12345” 3.14 ’a’ 2.3 ’z’ “afghj”
What properties do they share?
What operations can they be used for?
DCS/100: wk 2 – p.8/42
Types
Grouping values into different types with similar properties
means:
the computer can make sure there is appropriate
storage space for them, and
make sure the instructions only tell it to do sensible
things with them.
so you dont try to, for example, multiply two strings
together.
DCS/100: wk 2 – p.9/42
Variables:
Storing things for use later
Variables are what computer languages use for storing
information in a way that lets the program get at it again
later.
A variable is like a named box into which you can tell the
computer to put a piece of data.
Different types of data need different shaped boxes:
integers, decimals, strings of characters, single characters,
true or false values, output channels etc..
DCS/100: wk 2 – p.10/42
Variables
If you want to use a box to store something in, you have to
do two things:
get a box of the right kind/size
put something in it
It’s the same with variables.
declare the variable
(tells the computer to create the type of box you asked
for)
initialize it
(tells the computer to put something in it)
DCS/100: wk 2 – p.11/42
Declaring Variables
You include a statement in the program introducing the
variable:
int X;
String film;
These introduce two variables called X and film.
The computer is told that X holds an integer (a number)
(int), and film holds a string of characters (String).
These declare the variables X and film
DCS/100: wk 2 – p.12/42
Declaring Variables
output out = new output();
output is the type of output channels
output out declares a new output channel variable
called out
new output() creates a new output channel linked to
the screen
output out = new output();
declares a new output channel variable called out and
initialises it with a new output channel linked to the
screen.
DCS/100: wk 2 – p.13/42
Keywords
There are some restrictions on what you can use as the
name of a variable.
They cannot start with a number for example.
There are also some reserved “keywords” that already
mean something else such as class and extends.
You cannot use a keywoird as a variable (the computer
would get confused!)
DCS/100: wk 2 – p.14/42
Exercise: What do the following do?
Explain what each of the following statements does.
String message;
int age;
char initial;
boolean finished;
input in;
DCS/100: wk 2 – p.15/42
Strings
String values like "The Matrix" are in quotes.
That is how you tell they are string values and not the
names of variables like film
An operation you can do on string values is to combine
them using +:
"The Matrix" + " is cool!" creates the string
"The Matrix is cool!"
film + " is cool!" creates the string made of
whatever is in variable film with the string
" is cool!"
DCS/100: wk 2 – p.16/42
Putting values in Variables
The commands:
X = 1;
film = "The Matrix";
puts 1 in X
and "The Matrix" in film
“Box X gets the number 1”
“Box film gets the string "The Matrix" ”
DCS/100: wk 2 – p.17/42
All at once
It’s bad manners (and dangerous) to leave variables around
without values in them. So you will often want to declare a
variable, and immediately store something in it.
Most languages let you do this all at once:
int X = 1;
String film = "The Matrix";
These declare the variable and intialise it to contain an
initial value.
DCS/100: wk 2 – p.18/42
Assignment
You store something in a variable by assignment
name = in.readline();
stores the result of the method call in.readline() in
the variable name.
age = age+1;
adds one to the value in variable age and stores it back
in age.
year = 2004 - age;
sets the variable year to be 2004 minus the value in
the variable age.
DCS/100: wk 2 – p.19/42
Variables: the box metaphor
You can think of a variable as a box in which data can be
stored.
int number;
creates a box called number which is the right size and
shape to store integers (whole numbers):
number
478
DCS/100: wk 2 – p.20/42
Variables: the box metaphor
String words;
creates a box called words which is the right size and
shape to store a string:
words
“Die Fledermaus”
DCS/100: wk 2 – p.21/42
Variables cont.
Since Strings can be any length, you have to think of a box
for them as being “stretchy”:
words
“Die Fledermaus ...”
DCS/100: wk 2 – p.22/42
Other types in Java
Other things you can put in variables:
double: floating point numbers
3.14159
char: single characters
’a’, ’b’, ’c’, . . .
Note that the character ’1’, is differnet to the integer 1
which is different to the floating point number 1.0. They
are different types, are stored differently and have
different operations performed on them.
DCS/100: wk 2 – p.23/42
Assignment
The operation for setting the contents of a variable is called
assignment:
number = 3*4;
puts 12 in the box number, obliterating whatever was there
before:
number
12
DCS/100: wk 2 – p.24/42
Assignment Copies Information
You can copy information from one variable (box) to
another:
number = age;
puts a copy of whatever was in the box age into box
number , obliterating whatever was in number before but
leaving age unchanged : Suppose age held 42 the above
would make number 42 too.
age
42
number
42
DCS/100: wk 2 – p.25/42
Assignment
number = number+1;
adds 1 to the contents of the box number
Before:
number
111
After:
number
112
DCS/100: wk 2 – p.26/42
Integer Operations
You can do the normal operations on integers as with a
calculator.
For example:
f = (c * 9 / 5) + 32;
multiplies c by 9, divides that by 5 then adds 32, putting the
result in f.
DCS/100: wk 2 – p.27/42
Exercise: What does this program do?
class calculate extends basic
{
public static void main (String param[]) throws Exception
{
input in = new input();
output out = new output();
int answer; int number;
out.writeln("Give me a number?");
number = in.readint();
answer = (number * number) / 3;
out.writeln("Answer is " + answer);
in.close(); out.close();
}
}
DCS/100: wk 2 – p.28/42
Exercise
Write a program that asks the user for 2 numbers and prints
out their average (adding together and dividing by two).
DCS/100: wk 2 – p.29/42
Assignment
General form:
 = ;
evaluates , (that is works out its value)
and puts the result in ,
completely obliterating the previous contents.
DCS/100: wk 2 – p.30/42
Tracing: What does this do?
Draw a series of box pictures with a box for each variable to
work out what this code does...
int x,y;
x=1;
y=2;
x=y;
y=x;
out.writeln("x= "+x);
out.writeln("y= "+y);
DCS/100: wk 2 – p.31/42
Exercise: What does this do?
Draw a series of box pictures with a box for each variable to
work out what this code does...
int x,y,t;
x=1;
y=2;
t=x;
x=y;
y=t;
out.writeln("x= "+x);
out.writeln("y= "+y);
DCS/100: wk 2 – p.32/42
Remember
Before you use a variable you need to do two things:
Create the box (declare the variable):
int count;
Put something in the box(initialise the variable):
count = 0;
You can do both at the same time:
int count = 0;
DCS/100: wk 2 – p.33/42
Some rules about variables
Always call them by a name that tells you about their
function:
in the fahrenheit, celsius example a mathematician
might have
int f,c;
. . .
f = (c * 9 / 5) + 32;
a computer scientist would more likely have
int fahr,cel;
. . .
fahr = (cel * 9 / 5) + 32;
DCS/100: wk 2 – p.34/42
Some rules about variables
Always put your declarations in a sensible place, so you
can find them later.
For example: do them in an initialisation phase for that
part of the program.
Initialise variables where possible.
DCS/100: wk 2 – p.35/42
Functions and procedures
Methods divide up into functions and procedures.
A function “returns” a value that you can use later in the
computation:
mystring = in.readline();
age = in.readint();
A procedure simply does something:
in.readln();
DCS/100: wk 2 – p.36/42
Functions and procedures
mystring = in.readln();
is wrong because in.readln() does NOT produce a
string to store in mystring.
readln()
is a procedure, it does not return a result, it simply reads to
the end of the line.
DCS/100: wk 2 – p.37/42
Functions
The documentation for functions tells you what type of thing
is returned, and what types of things any arguments have to
be.
public char read() throws Exception
public String readline() throws Exception
Functions and procedures have a lot in common, and Java
treats them as the same kind of thing:
a procedure is a function that returns nothing.
public void writeln(String value) throws
Exception
DCS/100: wk 2 – p.38/42
The Type: boolean
We also have a type: boolean
It has two possible values: true and false
It is just like the other types
(int, char, double, String).
There are functions that return boolean.
You can have boolean variables.
DCS/100: wk 2 – p.39/42
Boolean Variables
int finished = true;
creates a box called finished which is the right size and
shape to store booleans and stores true in it:
finished
true
We will see more on the use of booleans in the next lecture.
DCS/100: wk 2 – p.40/42
Comments
It is essential to add explanations (“comments”) to
programs explaining what they do and how they work at
a high level.
This is so that subsequent programmers including
yourself can understand them.
Comments are ignored by java - they are not computer
instructions.
/* This is a comment */
DCS/100: wk 2 – p.41/42
By the end of the week
Once you have done the reading and the exercises you
should be able to:
write and run simple programs that
read input from the user and store it in a variable
do calculations on values stored
print out messages that include the results of
calculations.
store and manipulate different types of values.
Reading: Chapter 1 of Brinch Hansen (again), Chapter 3, 4 of
Computing Without Computers
DCS/100: wk 2 – p.42/42