Download A tour around Java

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
A tour around Java
General introduction to aspects of the language
(these will be covered in more detail later)
After this tour you should have a general
understanding of how Java programs work and of
some concepts in java programming
We’ll base this tour on parts of the bouncing ball
applet at http://inismor.ucd.ie/~fintanc/BouncingBallApplet.html
Parts of a programming language
• A programming language contains commands (telling
the computer what to do)
• A programming language contains variables (allowing
the computer to remember things)
• A programming language has some way of organising
commands and variables
• In Java, commands and variables are organised into
methods, objects, and classes
Organising commands: methods
• A ‘method’ is a sequence of commands grouped together
into a single block and given a name.
• The name of the method can be used to ‘call’ or carry out
all of those commands: the method name is shorthand for
the commands contained in that method.
• Every command in java is always part of a method.
• Methods can also contain variables. A variable is like a
box used to store and remember information. Different
types of variable can store different types of information.
• Variables also have names: a variable’s name is used to
refer to the information stored in that variable.
Organising methods: Objects
• An ‘object’ is made up of a number of methods. Just like
a method, an object can also contain variables.
• We design objects to represent the things that are
important in our program.
• The different methods in an object represent the different
things we want our object to do.
• The variables in an object are boxes holding information
about that particular object.
What objects might there be in the BouncingBall
program?
What variables might these objects have?
What methods might these objects have?
Organising objects: classes
• A class defines a set of objects that all contain the same
methods and the same variables.
• The class definition lists these methods and variables.
• Every time we want to get an object that has that particular
list of methods and variables, we ask java create a new
member of the class using a special command (“new”).
• Every object created in this way contains its own copy of
the methods and variables that are defined in that class.
What class might there be in the BouncingBall
program?
What methods will be listed in this class?
What variables will be listed in this class?
class Ball {
int Diameter = 20;
int horizontalPosition = 0; int verticalPosition = 0;
int horizontalSpeed = 3;
int verticalSpeed = 0;
int verticalAcceleration = 2; static int floor = 500;
// Everything between these
// curly brackets is the
// definition of the class Ball
public void move() {
horizontalPosition = horizontalPosition + horizontalSpeed;
verticalPosition = verticalPosition + verticalSpeed;
if (hitsFloor() ) {
bounce();
}
verticalSpeed = verticalSpeed + verticalAcceleration;
}
public boolean hitsFloor() {
if ( (verticalPosition + Diameter) >= floor)
{ return true; }
else { return false; }
}
public void bounce() {
verticalSpeed = 0 - verticalSpeed;
verticalPosition = floor - Diameter;
}
}
class Ball {
int Diameter = 20;
int horizontalPosition = 0; int verticalPosition = 0;
int horizontalSpeed = 3;
int verticalSpeed = 0;
int verticalAcceleration = 2; static int floor = 500;
// these variables hold info about
// the ball we're looking at: speed,
// horizontal and vertical position,
// etc. The ‘int’ type means these
// variables can hold integers.
public void move() {
horizontalPosition = horizontalPosition + horizontalSpeed;
verticalPosition = verticalPosition + verticalSpeed;
if (hitsFloor() ) {
bounce();
}
verticalSpeed = verticalSpeed + verticalAcceleration;
}
public boolean hitsFloor() {
if ( (verticalPosition + Diameter) >= floor)
{ return true; }
else { return false; }
}
public void bounce() {
verticalSpeed = 0 - verticalSpeed;
verticalPosition = floor - Diameter;
}
}
class Ball {
int Diameter = 20;
int horizontalPosition = 0; int verticalPosition = 0;
int horizontalSpeed = 3;
int verticalSpeed = 0;
int verticalAcceleration = 2; static int floor = 500;
// these variables hold info about
// the ball we're animating: speed,
// horizontal and vertical position,
// etc. The ‘int’ type means these
// variables can hold integers.
public void move() {
horizontalPosition = horizontalPosition + horizontalSpeed;
verticalPosition = verticalPosition + verticalSpeed;
if (hitsFloor() ) {
bounce();
}
verticalSpeed = verticalSpeed + verticalAcceleration;
}
public boolean hitsFloor() {
if ( (verticalPosition + Diameter) >= floor)
{ return true; }
else { return false; }
}
public void bounce() {
verticalSpeed = 0 - verticalSpeed;
verticalPosition = floor - Diameter;
}
}
// this method moves
// the ball, changing its
// horizontal and
// vertical position and
// vertical speed (due to
// falling acceleration).
// “public” means it can
// be used by other
// programs (they can
// move the ball).
class Ball {
int Diameter = 20;
int horizontalPosition = 0; int verticalPosition = 0;
int horizontalSpeed = 3;
int verticalSpeed = 0;
int verticalAcceleration = 2; static int floor = 500;
// these variables hold info about
// the ball we're animating: speed,
// horizontal and vertical position,
// etc. The ‘int’ type means these
// variables can hold integers.
public void move() {
horizontalPosition = horizontalPosition + horizontalSpeed;
verticalPosition = verticalPosition + verticalSpeed;
if (hitsFloor() ) {
bounce();
}
verticalSpeed = verticalSpeed + verticalAcceleration;
}
// this method moves
// the ball, changing its
// horizontal and
// vertical position and
// vertical speed (due to
// falling acceleration).
// “public” means it can
// be used by other
// programs (they can
// move the ball).
public boolean hitsFloor() {
if ( (verticalPosition + Diameter) >= floor)
{ return true; }
// this method tells us if the ball has hit the
else { return false; }
// ‘floor’. “boolean” means it returns an
}
// answer which is either true or false.
// (a “void” means nothing is returned)
public void bounce() {
verticalSpeed = 0 - verticalSpeed;
verticalPosition = floor - Diameter;
}
}
class Ball {
int Diameter = 20;
int horizontalPosition = 0; int verticalPosition = 0;
int horizontalSpeed = 3;
int verticalSpeed = 0;
int verticalAcceleration = 2; static int floor = 500;
// these variables hold info about
// the ball we're animating: speed,
// horizontal and vertical position,
// etc. The ‘int’ type means these
// variables can hold integers.
public void move() {
horizontalPosition = horizontalPosition + horizontalSpeed;
verticalPosition = verticalPosition + verticalSpeed;
if (hitsFloor() ) {
bounce();
}
verticalSpeed = verticalSpeed + verticalAcceleration;
}
public boolean hitsFloor() {
if ( (verticalPosition + Diameter) >= floor)
{ return true; }
else { return false; }
}
public void bounce() {
verticalSpeed = 0 - verticalSpeed;
verticalPosition = floor - Diameter;
}
}
// this method moves
// the ball, changing its
// horizontal and
// vertical position and
// vertical speed (due to
// falling acceleration).
// “public” means it can
// be used by other
// programs (they can
// move the ball).
// this method tells us if the ball has hit the
// ‘floor’. “boolean” means it returns an
// answer which is either true or false.
// (a “void” means nothing is returned)
//
//
//
//
//
this method causes the ball to
bounce, by reversing its vertical
speed so it goes up instead of
down. It also places the ball exactly
on the floor when bouncing.
class Ball {
int Diameter = 20;
int horizontalPosition = 0; int verticalPosition = 0;
int horizontalSpeed = 3;
int verticalSpeed = 0;
int verticalAcceleration = 2; static int floor = 500;
public void move() {
horizontalPosition = horizontalPosition + horizontalSpeed;
verticalPosition = verticalPosition + verticalSpeed;
if (hitsFloor() ) {
// Every time an Object from the Ball
bounce();
// class is created, each object will hold its
}
// own versions of these variables & methods.
verticalSpeed = verticalSpeed + verticalAcceleration;
}
// The only exception to this is the variable
// called ‘floor’, which is labelled ‘static’.
public boolean hitsFloor() {
if ( (verticalPosition + Diameter) >= floor) // ‘Static’ means that there is only one ‘floor’
// variable, which is a part of the whole class
{ return true; }
// of Balls, rather than there being a different
else { return false; }
// version of floor in in each different Ball.
}
public void bounce() {
verticalSpeed = 0 - verticalSpeed;
verticalPosition = floor - Diameter;
}
}
Using the bouncing balls
•
•
•
We have created a class that defines bouncing balls, but we haven’t
seen how do things with these bouncing balls.
Rather than showing you how to draw two bouncing balls on the
screen, I’m going to do something simpler:
 Write a program which prints the changing co-ordinate positions of two
balls as they move 10 times.
Here’s a rough outline of what this program will do:
create two ball objects to bounce (call them ‘A’ and ‘B’);
give one ball an increased speed (so it moves ahead of the other);
do the following {
print the current position of ball A;
print the current position of ball B;
move ball A to its new location using its move() method;
move ball B to its new location using its move() method;
increase the count of the number of moves done so far.
} while the number of moves done is still less than 10;
Showing positions of bouncing balls
public class showBouncingBallPositions{
public static void main(String[ ] args){
Ball A = new Ball();
Ball B = new Ball();
B.speed = 3;
int moveCount = 0;
do {
System.out.print( “Ball A:” + A.horizontalPosition+ “,” + A.verticalPosition);
System.out.print( “ Ball B:” + B.horizontalPosition+ “,” + B.verticalPosition);
System.out.println();
// start a new line (for the next time we print)
A.move();
B.move();
// move the bouncing balls
moveCount = moveCount+1;
} while (moveCount < 10);
}
}
The block of code after the do command will be repeated while the number in the
moveCount variable is less than 10. Notice that moveCount increases by 1 each
time this block of code is carried out. This “loop” will stop when moveCount hits 10.
Creating objects from a class
• For our showBouncingBallPositions class, we want to
have two ball objects, created from the Ball class.
• The easiest way to use these two Ball objects is to put
them into variables (boxes).
Ball A = new Ball();
Ball B = new Ball();
• (just as we had ‘int’ shaped variables to hold integers, here
we have Ball shaped variables to hold balls).
• We can use variables and methods from either of these
Ball objects (Ball A or Ball B) by saying things like
B.verticalSpeed = 3
A.move()
A.horizontalPosition
What does this mean?
What does this mean?
What does this mean?
To get at part of an object, we say the name of the object we
want to use (A or B), then a dot, then the part of that object we
want to use (its speed variable, its move method etc.)
using classes in Java
•
In Java programs we can write our own classes (like the
Ball class). We can also use any of a very large number of
useful predefined classes that come along with java.
•
Here are 2 ways of using classes in Java:
1) Creating new objects from a class
• then calling the methods and variables in those objects;
2) Accessing methods or variables through a class directly;
•
If something is “static” it means that there is only one of
those in the class, and it can only be accessed through
the class (not through objects created from that class).
•
In the Ball class, “floor” is static.
•
In the showBouncingBallPositions class, the
method “main” is static.
Summary of the entire program
Two classes: Ball class, and showBouncingBallPositions class.
class Ball {
int Diameter = 20;
int horizontalPosition = 0; int verticalPosition = 0;
// variables that represent properties
int horizontalSpeed = 3;
int verticalSpeed = 0;
// of ball at any given time
int verticalAcceleration = 2; int floor = 500;
public void move() { … }
// code that changes horizontal & vertical position, and speed
public boolean hitsFloor() { … } // code that checks if ball position equals floor position
public void bounce() { …. }
// code that reverses vertical speed to bounce ball
}
public class showBouncingBallPositions{
public static void main(String[] args){
Ball A = new Ball();
// code that creates two ball objects
Ball B = new Ball();
// and puts them in Ball variables A and B
B.verticalSpeed = 3;
int moveCount = 0;
do {
// start a loop…
….
// print out positions for ball A and ball B (as in earlier slide)
A.move();
B.move();
// call the move() methods in th two balls
moveCount = moveCount+1;
} while (moveCount < 10); // the loop continues until balls have moved 10 times
}
}
Calling methods from a predefined class
There are a lot of predefined classes available in Java.
These contain useful methods, variables, and objects.
We can use static variables and methods from those classes by
specifying the name of class we are interested in, followed by a
dot (‘.’), and then by the name of the variable or method we want.
Consider
System.out.print();
This means there is a pre-defined class called System , which
contains inside it a static variable called out, which has a method
called print(). Out is an object for printing things out.
How do I know about the System class? The ‘Java API’ lists and
explains all the predefined classes in Java. I looked up “System”
there to find out what it could do.
The Java API is at http://java.sun.com/
Running our program
The java in our BouncingBallPositions.java file won’t do anything
by itself. We need to tell the computer to execute the program.
This is a two-stage process.
First stage: Compile the program into a concise set of instructions
your computer can carry out directly
>javac BouncingBallPositions.java
javac stands for java compiler
javac puts the compiled instructions
in a file BouncingBallPositions.class
Second stage: Tell the computer to carry out the compiled instructions
>java BouncingBallPositions
Ball
Ball
Ball
Ball
Ball
A:
B:
A:
B:
A:
0,0
0,0
0,3
3,3
2,6
Tells the computer to execute the
compiled java instructions in
BouncingBallPositions.class
How our program works
>JAVAC BouncingBallPositions.java
class Ball
class BouncingBallPositions
(contains method “main”)
>JAVA BouncingBallPositions
(this runs the main method in the
BouncingBallPositions class)
(This “compiles” our two classes)
Ball object
horizontalPosition = 6
30;
;
verticalPosition = 2
0;
;
horizontalSpeed = 3;
20;
; etc
verticalSpeed = 4
move() { … }
hitFloor(){…}
bounce() { … }
Ball object
horizontalPosition = 6
30;
;
verticalPosition = 8
0;
3
;
horizontalSpeed = 3;
53;
; etc
verticalSpeed = 7
move() { … }
hitFloor(){…}
bounce() { … }
moveCount = 2
0;
1;
main method
Ball A = new Ball();
Ball B = new Ball();
B.verticalSpeed = 3;
int moveCount = 0;
do {
System.out.print( “Ball A:” + A.horizontalPosition+ “,” + A.verticalPosition);
System.out.print( “
Ball B:” + B.horizontalPosition+ “,” + B.verticalPosition);
System.out.println();
A.move();
B.move();
moveCount = moveCount+1;
} while (moveCount < 10);
A diagram of Java’s organisation
Class
static variables and
static methods are a
part of class not object
Part of
Member of / defined by
Object
Part of
Method
Part of
Variables
Part of
Commands
Conclusions
• You should now have a general idea of what happens in
the BouncingBallApplet that allows it to work as it does.
• You should also have some sort of idea of the meaning of
 Variables
These are the core ideas
 Methods
in Java programming.
 Objects
Don’t worry if you don’t
 Classes
understand: we will look at
 Extending a class
them all slowly and
carefully in future lectures.
 Over-riding a method
 Implementing an interface
• Next we will look slowly and carefully at how to use
variables methods and objects in java programs.
• You should read chapter 1 of Horstmann.