Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 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 http://inismor.ucd.ie/~fintanc/BouncingBallApplet.html

2 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

3 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.

4 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?

5 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?

6 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() ) { 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; } // Everything between these // curly brackets is the // definition of the class Ball

7 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() ) { 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; } // 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.

8 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() ) { 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; } // 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. // 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).

9 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() ) { 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; } // 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. // 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)

10 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() ) { 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; } // 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. // 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. // 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).

11 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() ) { 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; } // Every time an Object from the Ball // class is created, each object will hold its // own versions of these variables & methods. // The only exception to this is the variable // called ‘floor’, which is labelled ‘static’. // ‘Static’ means that there is only one ‘floor’ // variable, which is a part of the whole class // of Balls, rather than there being a different // version of floor in in each different Ball.

12 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;

13 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.

14 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? 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.)

15 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.

16 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 }

17 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/http://java.sun.com/

18 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 Second stage: Tell the computer to carry out the compiled instructions >java BouncingBallPositions Ball A: 0,0 Ball B: 0,0 Ball A: 0,3 Ball B: 3,3 Ball A: 2,6 javac stands for java compiler Tells the computer to execute the compiled java instructions in BouncingBallPositions.class javac puts the compiled instructions in a file BouncingBallPositions.class

19 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); How our program works horizontalPosition = 0; verticalPosition = 0; horizontalSpeed = 3; verticalSpeed = 0; etc Ball object move() { … } hitFloor(){…} bounce() { … } horizontalPosition = 0; verticalPosition = 0; horizontalSpeed = 3; verticalSpeed = 3; etc Ball object move() { … } hitFloor(){…} bounce() { … } class Ball class BouncingBallPositions (contains method “main”) >JAVAC BouncingBallPositions.java 3;3; 2;2; 3;3; 5;5; 3;3; 6;6; 4;4; 6;6; 7;7; 8;8; 2;2; >JAVA BouncingBallPositions (this runs the main method in the BouncingBallPositions class) (This “compiles” our two classes) moveCount = 0; 1;1; 2;2;

20 A diagram of Java’s organisation Method Part of VariablesCommands Object Part of Class Member of / defined by Part of static variables and static methods are a part of class not object

21 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  Methods  Objects  Classes  Extending a class  Over-riding a method  Implementing an interface These are the core ideas in Java programming. Don’t worry if you don’t understand: we will look at them all slowly and carefully in future lectures. 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.


Download ppt "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."

Similar presentations


Ads by Google