1 / 48

Java Programming

Java Programming. Introduction to programming. Keywords. Class Method Compile Comments myWorld. Hello World. World – is the stage of your game m yWorld class – contains code to set the stage i.e. what actors to display at the start of the game

butch
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. Keywords • Class • Method • Compile • Comments • myWorld

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

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

  5. 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:

  6. 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 matks

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

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

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

  10. Task: What are these different data types?

  11. 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: private 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

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

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

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

  15. Programming Task 2: Animal

  16. Learning ObjectivesIf statements All • Know what a method is used for Most • Recognise different types of methods Some • Test and refine your program

  17. 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 A boolean method returns true or false public booleanfoundLeaf() { if(lives >0) { return true; } else { return false; } } A void method is a set of instructions only public void act() { move(); } A int method returns a number public int lives() { return lives = 3; } Task: What method have you been adding code to in the myWorld class?

  18. Programming Task 3: Drawing Shapes

  19. Programming Task 4: Box Class

  20. Learning ObjectivesIf statements All • Know what a IF statement is used for Most • Test and refine your program Some • Fully comment code to show understanding

  21. 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(expression) { System.out.println(“True”); } 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; }

  22. Task: Using operators in expressions

  23. Programming task 5: 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!!!”

  24. Learning ObjectivesIf statements All • Know what a IF statement is used for • Know what a method is used for • Use a while loop Most • Test and refine your program Some Fully comment code to show understanding

  25. Loops/Iterations • While loop – this will continue until a condition is met Try adding this to the act() method inti = 0; while (i < 4){ setLocation(300, 300); Greenfoot.delay(5); setLocation(100,100); Greenfoot.delay(5); setLocation(600, 100); Greenfoot.delay(5); } i++; System.out.println(i); Task • Why does this program not work?? Use the system.out.println to see what is happening to the variable - What is happening to the variable? • Improve the program while (condition) { statement; Statement; }

  26. For Loop – Another way to loop a set of instructions inthowMany = 4; for(inti=0; i<howMany; i++) { setLocation(300, 300); Greenfoot.delay(5); setLocation(100,100); Greenfoot.delay(5); setLocation(600, 100); Greenfoot.delay(5); }

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

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

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

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

  31. 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()

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

  33. 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()

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

  35. 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()

  36. Animal Inheritance Program

  37. 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)

  38. 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 }

  39. Packages in Java • Java has a huge number of classes in it • Related classes are grouped into packages • java.lang – the essential classes • java.io – the classes for input and output • java.awt – some classes for doing graphics and user interfaces • java.util – classes that can hold a collection of objects

  40. Worlds and Actors • Greenfoot has two main classes • World • Place to hold and display actors • Has a width and height and cell size • Of type integer (int) • Has a background image • Actor • Actors know how to act • Actors have a x and y location in the world

  41. Learning ObjectivesDevelop your programming skills All • Identify some parts of a Greenfoot program – variable, if statements & controls • Identify the difference between a class and method Most • Identify a range of parts of a Greenfootprogram • Identify the difference between a global and local variable Some • Test and refine your program

  42. Wombats Game • See help sheets • Quiz sheet

  43. Resources • Joy of Code • Greenroom • GreenFoot

  44. Programming Terminology/ comments • Declare a variable – int lives; • Assign a variable – lives = 3; • Re-assign/reset a variable to new value • Identifier – is a programmer-defined name for a variable, method, class or interface. • Syntax error i.e. case sensitive, missing a semi-colon, missing a curly bracket, structure of the if else • Call a method • A new instance of an object/actor in the myWorld interface • increment operator i.e. ++ or – • Methods can require parameters i.e. setCounter( intByHowMany) • Methods can return values

  45. What is aggregation and composition? Composition final class Car { private final Engine engine; Car(EngineSpecs specs) { engine = new Engine(specs); } voidmove() { engine.work(); } } Aggregation final class Car { private Engine engine; voidsetEngine(Engineengine) { this.engine = engine; } voidmove() { if (engine != null) engine.work(); } } • In the case of composition, the Engine is completely encapsulated by the Car. There is no way for the outside world to get a reference to the Engine. The Engine lives and dies with the car. • With aggregation, the Car also performs its functions through an Engine, but the Engine is not always an internal part of the Car. Engines may be swapped, or even completely removed. Not only that, but the outside world can still have a reference to the Engine, and tinker with it regardless of whether it's in the Car.

  46. Java Resources • For Java basics • Tutorial • http://java.sun.com/docs/books/tutorial/java/index.html • Practice-it site • http://webster.cs.washington.edu:8080/practiceit/ • API • http://java.sun.com/javase/6/docs/api/ • Coding bat site • http://codingbat.com

  47. Greenfoot Resources • Greenroom for teachers • http://greenroom.greenfoot.org/door • Greenfoot webinars by Michael Kölling • https://sas.elluminate.com/p.jnlp?psid=2010-04-19.1045.M.4032652CF7B7C595BD3164F7B34250.vcr • https://sas.elluminate.com/p.jnlp?psid=2010-04-21.1235.M.4032652CF7B7C595BD3164F7B34250.vcr • https://sas.elluminate.com/p.jnlp?sid=2009238&psid=2010-04-26.1237.M.4032652CF7B7C595BD3164F7B34250.vcr • Greenfoot webinar by Barb Ericson • https://sas.elluminate.com/p.jnlp?psid=2009-09-23.1704.M.710CE1263F2BF8D1FB814C40272355.vcr

More Related