Download Today! “HelloWorld” in BlueJ “HelloWorld” in BlueJ, Cont

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
5/3/2006
Today!
“HelloWorld” in BlueJ
1. Find “BlueJ” in the start menu, but start the
“Select VM” program instead (you will only need
to do this once for every installation of BlueJ.)
2. Make sure the JDK selected is version 1.5.0… or
better. If it is not, you may have to open the
bottom of the window and browse or search for a
newer JDK. If you can’t find one, you will need
to install a newer JDK from java.sun.com.
3. Close this window, and start BlueJ as you
normally would.
• Build “HelloWorld” yourself in BlueJ and Eclipse.
• Look at all the Java keywords.
• Primitive Types.
Spring 2006
CISC101 - Prof. McLeod
1
Spring 2006
CISC101 - Prof. McLeod
2
“HelloWorld” in BlueJ, Cont.
“HelloWorld” in BlueJ, Cont.
4. Click on “Project”, then “New Project…”.
5. Browse to a convenient folder (where you can
find the project again), and type in a name for
the Project – don’t use any spaces (always a
good idea with Java filenames and folders!).
6. Click on “Create” to close the New Project
window. The new project name is now at the top
of the window.
7. Click on the “New Class” button at the left.
8. Type in the class’s name: HelloWorld and leave
the “class” checked off.
9. Click on “OK” to close the New Class window.
10. You can now see a new little box representing
your class. You can enlarge the box and/or drag
it around the screen (entertaining, but not
useful!).
11. Double click on the little box, to open the code
editor window.
12. Delete all the junk in the editor window by
pressing <Cntrl>A followed by <Del>. We don’t
need all this stuff! (code “auto-generation”).
Spring 2006
Spring 2006
CISC101 - Prof. McLeod
3
“HelloWorld” in BlueJ, Cont.
CISC101 - Prof. McLeod
4
“HelloWorld” in BlueJ, Cont.
13. Type in the class’ container code:
15. You can choose to add a comment at the top of
your program, and another comment by the “}”:
public class HelloWorld {
// My very first Java program!
// By me!
public class HelloWorld {
}
14. Save your program by typing <Cntrl>s or go to
“Class” then choose “Save”. (Save your code
often!!!)
Spring 2006
CISC101 - Prof. McLeod
} // end HelloWorld
5
Spring 2006
CISC101 - Prof. McLeod
6
1
5/3/2006
“HelloWorld” in BlueJ, Cont.
“HelloWorld” in BlueJ, Cont.
16. Add the container for the main method:
// My very first Java program!
// By me!
public class HelloWorld {
public static void main (String[] args) {
} // end main
} // end HelloWorld
Spring 2006
CISC101 - Prof. McLeod
7
17. Save your program again. Note message at
bottom right corner – saying “Saved”.
18. Click on the “Compile” at the top left. Fix any
compilation errors.
19. Just in case you did not have any – make an
error (like removing a “}” or “{“) and compile
again to see what happens. Click on the “?” to
see a semi-useful message about the
compilation error.
20. Fix all errors and re-compile.
Spring 2006
“HelloWorld” in BlueJ, Cont.
25. Our program did what we told it to do – nothing!!
26. Go back to the editor window and add the
following output statement:
System.out.println("Hello World!");
27. Save, compile, and then run your program, as
before. (Always, always – Save before
running!)
23. Click on “OK” without changing any parameters
in the little text box.
24. Nothing happens right?
CISC101 - Prof. McLeod
9
Spring 2006
“HelloWorld” in BlueJ, Cont.
10
1. The first time you start Eclipse, you will see the
“Welcome” page. You should check out some of
the information here – particularly one of the
simple “Tutorials” and the “Overview”.
2. For now, click on the “Workbench” link at the top
right.
3. Before proceeding, and for the first time only,
you should check the configuration of the
program, to make sure it is running the correct
Java JDK.
29. On your own, from the project window, choose
“Help” then “BlueJ Tutorial”, if you wish to know
more.
CISC101 - Prof. McLeod
CISC101 - Prof. McLeod
“HelloWorld in Eclipse
28. You should see the following little exciting
window – called the “Terminal” or “Console”
window:
Spring 2006
8
“HelloWorld” in BlueJ, Cont.
21. Leave the editor window open, but move to the
original “Project” window. Note how the box
looks different if your class has not been
compiled. You cannot run your program until it
has been compiled.
22. Right click on your little box and choose “void
main(String[] args)” to run your program.
Spring 2006
CISC101 - Prof. McLeod
11
Spring 2006
CISC101 - Prof. McLeod
12
2
5/3/2006
“HelloWorld in Eclipse, Cont.
4. In the top menu bar, go to “Window” then
“Preferences”.
5. Click on the “+” beside “Java” and then choose
“Installed JRE’s”. You should see the window on
the following slide:
Spring 2006
CISC101 - Prof. McLeod
13
Spring 2006
CISC101 - Prof. McLeod
14
15
Spring 2006
CISC101 - Prof. McLeod
16
“HelloWorld in Eclipse, Cont.
6. Make sure the 1.5.0… JRE is checked off.
7. Now choose “Compiler” and make sure the
“Compiler Compliance Level” is set to “5.0”.
8. You should see the following window:
Spring 2006
CISC101 - Prof. McLeod
“HelloWorld in Eclipse, Cont.
“HelloWorld in Eclipse, Cont.
9. There are lots of other options in “Preferences”
but we do not have to worry about any of them
right now!
10. Click on “OK” to close the Preferences window.
This might take a little while and if you are asked
to re-compile, say “OK”.
11. Just as in BlueJ, you need to create a new
project. Projects can contain many java files,
however. Click on the little folder thing at the top
left:
12. Click on “Project”
– you will see the
Project creation
wizard:
13. Click on the
“Next >” button.
Spring 2006
Spring 2006
CISC101 - Prof. McLeod
17
CISC101 - Prof. McLeod
18
3
5/3/2006
“HelloWorld in Eclipse, Cont.
“HelloWorld in Eclipse, Cont.
14. On this screen
enter a name for
your project. You
will see:
15. Click on “Finish”.
Spring 2006
CISC101 - Prof. McLeod
16. Your new project will now be listed at the left in
the “Package Explorer”. Click on the “+” beside
the project’s name.
17. There is a link to the JRE System Library, that
we are going to ignore.
18. Click on that little folder thing at the top left, and
choose “Class” this time to start the New Java
Class wizard. You should see the window on the
next slide:
19
Spring 2006
CISC101 - Prof. McLeod
20
“HelloWorld in Eclipse, Cont.
19. Type in
“HelloWorld” for
the name of your
class. Make sure
the method stub
checkbox for
“main” is checked
(to make your life
easier!).
Spring 2006
CISC101 - Prof. McLeod
20. Click on “Finish” and your new class will show up
in the editor window.
21. Delete comments and/or add your own.
22. Add the System.out.println() line inside the main
method, as you did before.
23. You will notice some advanced editor features
when you hit period for example, and other
things such as auto indentation.
24. Save your program using <Cntrl>s or by clicking
on the little floppy disk button at the top left.
21
Spring 2006
“HelloWorld in Eclipse, Cont.
CISC101 - Prof. McLeod
22
“HelloWorld in Eclipse, Cont.
25. Click on the little green arrow thing at the top, as
shown on the next slide.
26. Note that Eclipse compiles your program when it
saves it.
Spring 2006
CISC101 - Prof. McLeod
23
Spring 2006
CISC101 - Prof. McLeod
24
4
5/3/2006
“HelloWorld in Eclipse, Cont.
Development Environments
27. Provided you have not made any compiler
errors, you should see your output in the
Console window at the bottom.
28. Make some deliberate errors in your code to see
what happens. Eclipse pre-compiles your
program as you write it, and will not run it until
you have fixed all the errors it has detected.
• You can choose BlueJ or Eclipse (or any other
tool you like!).
Spring 2006
Spring 2006
CISC101 - Prof. McLeod
25
• I will usually use Eclipse for class demonstrations.
• (I’m not going to try the command prompt
again…)
Java Keywords
double
int
super
assert
else
interface
switch
boolean
enum
long
synchronized
break
extends
native
this
byte
for
new
throw
case
final
package
throws
catch
finally
private
transient
char
float
protected
try
class
goto
public
void
const
if
return
volatile
continue
implements
short
while
default
import
static
do
instanceof
• The goto keyword is reserved but does not do
anything in java.
• On the next slide I have highlighted the
keywords that we will be using in this course.
strictfg
CISC101 - Prof. McLeod
27
Java Keywords for CISC101
Spring 2006
CISC101 - Prof. McLeod
28
Java Keywords for CISC101, by Category
abstract
double
int
super
assert
else
interface
switch
boolean
enum
long
synchronized
break
extends
native
this
byte
for
new
throw
case
final
package
throws
catch
finally
private
transient
char
float
protected
try
class
goto
public
void
const
if
return
volatile
continue
implements
short
while
default
import
static
do
instanceof
Spring 2006
26
Java Keywords, Cont.
abstract
Spring 2006
CISC101 - Prof. McLeod
Primitive Types Conditionals
Loops
Structural
boolean
byte
if
else
do
for
class
import
char
double
float
int
switch
case
while
continue
break
new
public
return
static
long
short
final
void
strictfg
CISC101 - Prof. McLeod
29
Spring 2006
CISC101 - Prof. McLeod
30
5
5/3/2006
Other “Things” in Java Programs
•
•
•
•
•
Expressions
Comments
Variable names
Literal values
“Punctuation” like: ; . , ( ) { } [ ]
Operators like: + - / % > < == = ! && ||
• A line of code in java often contains expressions.
• They are combinations of any or all of:
–
–
–
–
• We will also see lots of other object names along
with their “members” – attributes or methods.
Spring 2006
CISC101 - Prof. McLeod
Variables
Literal Values
Operators
Method Invocations
• A line of code in java is terminated by a “;”
31
Spring 2006
Variables
• To create a variable, you must state what type it is
going to be.
• For example, to create a variable called aNum of
type int, you would use the following line of code:
int aNum;
•A name for a piece of memory used to store
something.
• You could assign a value to aNum using
something like:
•The “something” can be a primitive type value or
a pointer to an object (a memory reference).
aNum = 200;
•(Don’t worry about pointers, yet!!)
CISC101 - Prof. McLeod
32
Variables, Cont.
• Java is a “declarative” language.
• This means that you have to declare a variable
before you can use it.
• What is a variable anyways?
Spring 2006
CISC101 - Prof. McLeod
33
Spring 2006
Back to Expressions
CISC101 - Prof. McLeod
34
Expressions
• What is a literal value?
• We just saw one: 200
• Now, let’s consider each of these elements again,
in more detail: variables, literal values, operators.
• What is an operator?
• We just saw one of those too: =
• Other operators, for example, are: + - / *
• And, what is an expression?
• We just saw one of those too!: aNum = 200;
Spring 2006
CISC101 - Prof. McLeod
35
Spring 2006
CISC101 - Prof. McLeod
36
6
5/3/2006
Java Primitive Types
Primitive Types - Cont.
• “Primitive Type” variables are those that are not
“Objects” in Java.
• Primitive Type Variables fall into the categories of
integer types, real types, characters and
booleans.
Integer
Real
byte
float
short
double
Character Boolean
char
boolean
int
long
Spring 2006
CISC101 - Prof. McLeod
37
Spring 2006
An Integer Primitive Type Variable
CISC101 - Prof. McLeod
38
More Integer Primitive Types
• Declare using the “int” keyword.
• Other primitive types: byte, short and long:
• For byte, from -128 to 127, inclusive (1 byte).
• For short, from -32768 to 32767, inclusive (2
bytes).
• (For int, from -2147483648 to 2147483647,
inclusive (4 bytes). )
• For long, from -9223372036854775808 to
9223372036854775807, inclusive (8 bytes).
• From -2147483648 to 2147483647, inclusive (4
bytes).
• (A “byte” is 8 bits, where a “bit” is either 1 or 0.)
• For example:
int aNum;
Spring 2006
CISC101 - Prof. McLeod
39
Spring 2006
Aside - Storage of Integers
CISC101 - Prof. McLeod
40
Storage of Integers - Cont.
• Computers like to store numbers in binary - a
memory location is either “on” or “off”.
• An “un-signed” 8 digit binary number can range
from 00000000 to 11111111
• 00000000 is 0 in base 10.
• 11111111 is 1x20 + 1x21 + 1x22 + … + 1x27 =
255, base 10.
Spring 2006
CISC101 - Prof. McLeod
•
•
•
•
41
So, how can a negative binary number be stored?
Use the “two’s complement” system of storage.
Make the most significant bit a negative number:
So, the lowest “signed” binary 8 digit number is
now: 10000000, which is -1x27, or -128 base 10.
Spring 2006
CISC101 - Prof. McLeod
42
7
5/3/2006
Storage of Integers - Cont.
Storage of Integers - Cont.
• For example, the binary number
binary
base 10
10000000
-128
10000001
-127
11111111
-1
00000000
0
00000001
1
01111111
127
Spring 2006
CISC101 - Prof. McLeod
10010101 is
1x20 + 1x22 + 1x24 - 1x27
= 1 + 4 + 16 - 128
= -107 base 10
• Now you can see how the primitive integer type,
byte, ranges from -128 to 127.
43
Spring 2006
Storage of Integers - Cont.
• An int is stored in 4 bytes using “two’s
complement”.
• An int ranges from:
01111111
+00000001
• This would be equivalent to adding 1 to 127 in
base 10 - the result would normally be 128.
• In base 2, using two’s complement, the result of
the addition is 10000000, which is -128 in base
10!
• So integer numbers wrap around, in the case of
overflow - no warning is given!
CISC101 - Prof. McLeod
44
Storage of Integers - Cont.
• Suppose we wish to add 1 to the largest byte
value:
Spring 2006
CISC101 - Prof. McLeod
10000000 00000000 00000000 00000000
to
01111111 11111111 11111111 11111111
or -2147483648 to 2147483647 in base 10
45
Spring 2006
A Double Primitive Type Variable
CISC101 - Prof. McLeod
46
The Other Real Primitive Type
• Declare using the “double” keyword.
• Also have float:
• For double, (8 bytes) roughly ±4.9 x 10-308 to
±1.7 x 10308 to 15 significant digits.
• For float, (4 bytes) roughly ±1.4 x 10-38 to ±3.4
x 1038 to 7 significant digits.
• For double, (8 bytes) roughly ±4.9 x 10-308 to
±1.7 x 10308 to 15 significant digits.
• For example:
double aVal;
Spring 2006
CISC101 - Prof. McLeod
47
Spring 2006
CISC101 - Prof. McLeod
48
8
5/3/2006
Integer Literals
Double Literals
• A literal integer value is assumed to be of type
“int”.
• If you want the literal to be a long, then you must
add the letter “L” to the end of the literal value.
• Java assumes a literal number like “2.5” or
“3.45e-7” is a double literal.
• If you want a literal to be recognized as a float
type, then you must add the letter “F” to the end of
the number.
Spring 2006
Spring 2006
CISC101 - Prof. McLeod
49
char Primitive Type
CISC101 - Prof. McLeod
50
char Primitive Type - Cont.
• Declared as:
• A char primitive type can also refer to a “Unicode”
character. For example:
char aChar;
char aChar = ‘\u03C0’;
• Char literals look like:
Where 03C0 is the hexadecimal (base 16) value for
the Unicode character: π
• Since two bytes are used to designate the
character value, it is possible to refer to 216 =
65,536 different characters.
‘a’ ‘B’ ‘1’ ‘ ’ (a space)
Spring 2006
CISC101 - Prof. McLeod
51
Aside - Unicode Characters
Spring 2006
CISC101 - Prof. McLeod
52
A Boolean Primitive Type Variable
• Declare using the “boolean” keyword.
• The ASCII code system only goes to 255 since it
only uses one byte.
• At the moment, just over 49,000 of the Unicode
positions are in use.
• See www.unicode.org
• Either true or false
• For example:
boolean aFlag;
• boolean literals are: true, false
Spring 2006
CISC101 - Prof. McLeod
53
Spring 2006
CISC101 - Prof. McLeod
54
9
5/3/2006
Aside - String’s
Variable Declaration
• To declare a variable, use the Java “keyword”
appropriate for the type of variable you are
declaring followed by a variable name you have
created, followed by a semicolon.
• String’s are not primitive data types, but are
Objects.
• We will discuss the difference later…
• A String can be declared in the same way as a
primitive type using the keyword: String.
• Examples:
• For example:
int aNum;
double totalVolume;
String userPrompt;
String response;
Spring 2006
CISC101 - Prof. McLeod
55
Spring 2006
Legal Variable Names
Legal names:
myData
x1
AnotherName
TWO_PI
– Names beginning with lowercase letters are variables
or methods.
– Names beginning with uppercase letters are class
names.
– Successive words within a name are capitalized.
– Names in all capital letters are constants.
57
Spring 2006
Literal Values
-1.0
333444891
3.457E-10
• boolean, for example:
• String literals:
“Hello!” “
Spring 2006
CISC101 - Prof. McLeod
58
• int and double variables initially are given a
value of zero unless they are initialized to a value.
• Java may prevent you from using variables that
are not initialized.
• So, it is often necessary to initialize your variables
before use, for example:
• double numbers, for example:
4.5
Constant
Variable Declaration - Cont.
• int’s, for example:
0
Class names
_toUpper
1Day
CISC101 - Prof. McLeod
-142
Variable or method names
Illegal names:
• (We’ll get to “constants” shortly).
12
56
Legal Variable Names - Cont.
• Java names may contain any number of letters,
numbers and underscore (“_”) characters, but
they must begin with a letter.
• Standard Java Naming Convention:
Spring 2006
CISC101 - Prof. McLeod
-3.4E45
true
int numDaysInYear = 365;
double avgNumDaysInYear = 365.25;
String greetingLine = “Hello there!”;
spaces”
CISC101 - Prof. McLeod
59
Spring 2006
CISC101 - Prof. McLeod
60
10
5/3/2006
Variable Declaration - Cont.
Constants
• The Java keyword, final can be used to make
sure a variable value is no longer “variable”.
• It becomes a constant, because Java will not
allow your program to change its value once it has
been declared:
• All these statements could be carried out in two
lines, for example:
int numDaysInWeek = 7;
Is the same as:
final int NUM_DAYS_IN_YEAR = 365;
int numDaysInWeek;
numDaysInWeek = 7;
Spring 2006
CISC101 - Prof. McLeod
final double MM_PER_INCH = 25.4;
61
• The standard arithmetic operators in Java are:
– Addition (+)
– Subtraction (-)
– Multiplication (*)
– Division (/)
– Modulo (or “remainder”) (%)
62
• Not only can “+” operate on numeric values, but it
can also handle String’s on either or both sides.
• If one side is not a String, it will be changed to
one, and then it will be concatenated to the
String on the other side:
4 + “you” evaluates to “4you”
“apples” + “oranges” + 99 evaluates to
“applesoranges99”
3 + 7 + “little piggies” evaluates to
“10little piggies”
• All of these operations apply to all numeric data
types.
• All require values on both sides of the operator.
CISC101 - Prof. McLeod
CISC101 - Prof. McLeod
Aside - Strings and the “+” Operator
Arithmetic Operators
Spring 2006
Spring 2006
63
Spring 2006
Logical Operators
CISC101 - Prof. McLeod
64
Logical Operators - Cont.
• Return either true or false. All these require values on
both sides of the operator.
• “Truth” tables for the “And” and “Or” operators:
boolean result = testa && testb;
•
•
•
•
•
•
==
!=
>
<
>=
<=
equals to
not equals to
greater than
less than
greater than or equal to
less than or equal to
&&
testa
testb true
false
true
false
true
false false false
||
testa
testb true
false
true
true
true
false true
• && logical “And”
• || logical “Or”
false
boolean result = testa || testb;
Spring 2006
CISC101 - Prof. McLeod
65
Spring 2006
CISC101 - Prof. McLeod
66
11
5/3/2006
Assignment Operator
Expressions
• Expressions are combinations of variables, literal
values, and operators.
• For example:
set equal to
=
• This is all we need to be able to build Java
expressions.
• There are other assignment operators (+=, -=, *=,
/=), but we’ll look at these later.
• We will also consider “unary” operators later too.
int aNum = 4 + 3 * 7; // aNum
int aNum = (4 + 3) * 7;
//
(4 > 7) || (10 > -1)
//
(5.5 >= 5.0) && (4.0 != 1.0)//
double circ = 3.14 * 2.0 * r;
is 25
aNum is 49
yields true
yields true
– (“//” precedes a comment in Java)
Spring 2006
CISC101 - Prof. McLeod
67
Spring 2006
CISC101 - Prof. McLeod
68
Precedence Rules
• Operator precedence rules determine which
operations take place in what order:
–
–
–
–
–
–
First *, /, %
Then +, Then <, >, <=, >=
Then ==, !=
Then &&, ||
Last =
• Use “( )” to control order of operations, as the
expression inside “( )” will be evaluated before
stuff outside of “( )”.
Spring 2006
CISC101 - Prof. McLeod
69
12