1 / 59

Java Programming

Java Programming. Introduction to programming. Programming Terms . Greenfoot Java programming language Actor World Compile Run Comments. Programming Terms. Actor Sequence Repetitions Decision – making Variables Operations Input Output Process Class Methods. Hello World.

quasar
Download Presentation

Java Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Java Programming Introduction to programming

  2. Programming Terms • Greenfoot • Java programming language • Actor • World • Compile • Run • Comments

  3. Programming Terms • Actor • Sequence • Repetitions • Decision – making • Variables • Operations • Input • Output • Process • Class • Methods

  4. Hello World World – is the stage of your game myWorld class – contains code to set the stage i.e. what actors to display at the start of the game Actor – is a object in the world. When you add a actor it creates a actor class and you add code to control the events and actions for that actor. A Class contains methods Method is used to perform a set of instructions / blocks of code for a specific purpose You can write your own methods or use methods that are part of Greenfoot/JAVA classes.

  5. Learning ObjectivesHello World program and Greenfoot interface All • Identify different parts of the Greenfoot interface • Know how to compile a program and test for errors • Create a simple “hello world” program • Know how to add comments Most • Modify a program Some • Fully annotate the program

  6. New program • Choose the option new scenario • Right click on world and create a new subclass • The new class name it “myWorld” • Set is as no image • Click on myWorld • It should look like this:

  7. Programming task 1: First Program • A program is a set of instructions Print “hello world” Print “my name is….” Type in the following: Then click on compile If the code is has no errors … at the bottom of the screen it will say “No syntax errors” Note: When you want to display text (String) you always use quotation marks. If you want to display numbers (int) you don’t need to use quotation marks

  8. Adding Comments //Create a new world with 8x8 cells and //with a cell size of 60x60 pixels

  9. Learning ObjectivesData types, variables & constants All • Identify at least 2 data types used in a program • Know what a variable is used for • Create a program that uses at least one variable Most • Know about the scope of variables different variables in a program Some • Test and refine your program

  10. Data types • Variables can hold data with different data types i.e. numbers and text • A number is int and text is string Complete the next task to find out about different data types you can use in a program

  11. Task: What are these different data types?

  12. Variables TRY this “Hello world” is datatype String myText is the name of the variable 1) You have to declare the variable at the top of the class: public String myText; 2) Then add this code to myWorld() method You have to assign a value to the variable: myText = “Hello world”; 3) Display mytext System.out.println(myText); • A variable is used to hold data and you can use this stored data for a number of reasons: • display the data • change the data • check the data Task: Using the Hello World Program: 1) Declare a variable called myText2 2) Assign a value “Goodbye world” 3) Display mytext2

  13. Scope of variables • Variable can be either public or private – this defines the scope of a variable • public variables can be used and changed at anytime from other classes. • private variables can only be used and changed with in a single class – you will learn about methods later • Local variable is used in a method and you don’t need to use public or private. It only can be used and changed in a single method • Global variables last for the whole duration of the entire program. Different methods in different classes can be used to change the variable. A global variable is used for score, lives etc.

  14. int variables and calculation Try the following code: //add this variable at the top of the class public int calculation; calculation = 3*2+12; System.out.println(calculation); Try your own calculations and check that the program works NB use / for divide

  15. Print screen your code and label how you have done the following: Declared a variable and set the data type Assigned a value to a variable Output data – System.out.println() input

  16. int variables and calculation Try the following code: //add this variable at the top of the class private int calculation; private int number1; private int number2; number1 = 5; number2 = 6; calculation = number1*number2; System.out.println(calculation); Try your own calculations and check that the program works NB use / for divide

  17. Constants A constant in a program is a value that always stays the same static final int levels = 3;

  18. Programming Task 2: Animal

  19. Print screen your code and label how you have done the following: Declared a variable and set the data type Assigned a value to a variable Output data – System.out.println() input

  20. Learning Objectives– methods/procedures All • Know what a method is used for • Program a method Most • Recognise different types of methods • Make your own method Some • Test and refine your program

  21. Methods Methods are blocks of code that can be used and reused at anytime in a program. A method is a block of code that will perform a set of specific instructions and/or return a value Other type of methods: A boolean method returns true or false public booleanfoundLeaf() { return true; } A int method returns a number public int lives() { return lives = 3; } A method that will carry out a set of instructions public void act() { move(); } Constructor method – what will happen when the object is constructed public box() { super (600,400,1); } Task: What method have you been adding code to in the myWorld class?

  22. Print screen your code and label how you have done the following: Declared a variable and set the data type Assigned a value to a variable input Calling a method Output

  23. Programming Task 3: Making a box

  24. Programming Task 3: Box Class

  25. Learning Objectives– Selection/ decision making All • Know what a IF statement is used for • Program a simple if statement • Know what a operator is and use at least one in your program Most • Use a nested if • Test and refine your program Some • Fully comment code to show understanding

  26. Using a simple IF statement to make decisions Try this: Add to myWorld() //declaring a variable in a method you don’t need private int x= 5; int y=8; if(x==y) { System.out.println(“The two numbers are equal”); } else { System.out.println(“False”); } Task: Using the following expressions in the table, use this program to workout if the expression is true or false if(expression) { statements; } else if(expression) { statements; } else { statements; }

  27. Task: Using operators in expressions

  28. Programming task 4: Your first puzzle • Create a program that will tell if something is true or false • Use a variable to store answer • Print “What is the capital of France?” • If answer is Paris print “correct” • Else if answer is London print “No way!!! What were you thinking!!” • Else another answer print “Do your research and listen more in geography!!!”

  29. Print screen your code and label how you have done the following: Declared a variable and set the data type Assigned a value to a variable input Output Operators Selection Constructor method Extension Task Test out you if statement by changing the answer. Change it to London and then try another capital city – print screen your evidence.

  30. Learning Objectives- Actors All • Know how to use actors • Know how to add a new actor object to the stage Most • Use a while loop and call a method • Test and refine your program Some Fully comment code to show understanding

  31. Creating Shapes Task 5 • See help sheet

  32. Loops/Iterations:Creating objects in random positions In myWorld create a new method: public void randomShapes(inthowMany) { for(int i=0; i<howMany; i++) { rectangle rect = new rectangle(100, 90); int x = Greenfoot.getRandomNumber(getWidth()); int y = Greenfoot.getRandomNumber(getHeight()); addObject(rect, x, y); } }

  33. Call the randomShapes(inthowMany) Try Calling the method in myWorld public myWorld (){ randomShapes(5); } Then Try calling the method in the act method public void act() { randomShapes(5); } What happends?

  34. Moving objects In the rectangle actor add the act class public void act() { movingObjects(); } public void movingObjects(){ myWorldmyworld = (myWorld) getWorld(); int x = Greenfoot.getRandomNumber(500); int y = Greenfoot.getRandomNumber(500); setLocation(x, y); }

  35. Programming task 6: Play Random number gameSee Help Sheet

  36. Kangaroo Challenge Challenge 1 Get the kangaroo to move(5) or setLocation(3, 6); Challenge 2: Get the kangaroo to move and then turn(90) Challenge3: Move the kangaroo using the arrow keys Challenge 4: Use a if statement to turn the kangaroo in the opposite direction when it hits the edge Challenge 5: Add collision code

  37. Print kangaroo code • Name on your work • Label different parts of the code • Controls • Collision • Calling a method

  38. Object-oriented programming • Java programs have interacting objects • Each object has data (properties) and behavior (methods) • The class describes the data and behavior that all objects of that class have: • It classifies the objects • It also creates the objects • Object-oriented also allows for inheritance • One class inherits object fields and methods from another class

  39. Goals of OO programming • Spread out responsibility • Objects should do what you expect them to do • No one object does everything • Make reusable classes • Minimize the effect of changes • Make it easier to create new things out of classes that have been tested

  40. Objects and Classes • Classes define what objects know and can do • There is one Balloon class • Objects do the action • There can be many objects of the same class • There are many balloon objects Objects Classes

  41. Here’s an example of a class Car class • A class can have properties and methods (behaviors) Car Class Properties: colour, model, engine capacity, lights etc Methods: move(), stop(), accelerate(), turn()

  42. Your turn …. Animal Class Properties: Methods: Shape Class: Properties: Methods:

  43. Your turn …. Shape Class: Properties: Number of Sides Colour of outline Colour fill Line thickness Methods: drawShape() FillColour SetLineThickness() Animal Class Properties: Type Legs Weight Methods: Move() Eat() Sleep()

  44. Homework….Think of another 2 classes ………… Class Properties: Methods: ………….. Class: Properties: Methods:

  45. Here’s am example of inheritance Animal Class Common Properties: type, name, legs, colour Common Methods: Eats(), moves() Cat Class Specific Methods: meows() Dog Class Specific Methods: barks()

  46. Animal Inheritance Program

  47. Constructors • Used to initialize the data for an object • Like the width, height, and cell size for a new world • A class can have more than one constructor • As long as they take different parameters (passed values)

  48. The Java Class Structure import package.class; // as needed public class Nameextends OtherName { // properties – the data for each object // constructors – initialize the fields // methods – behavior for the objects // optional main method for testing }

More Related