Download presentation
Presentation is loading. Please wait.
Published byBryan Cook Modified over 8 years ago
1
Review – Primitive Data What is primitive data? What are 3 examples of primitive data types? How is a piece of primitive data different from an object? Storing simple data like numbers int – to store an integer double – to store a decimal number boolean – to store a true or false It is not connected to data fields or methods
2
Review – Constructors What is a constructor? What part of the constructor definition should you look at when determining how to USE the constructor? Example: Climber Constructor Definition: public Climber(int x) HEADER { super(x, 1, Display.NORTH, 1); } How can you tell the difference between a method and a constructor? Specifies how to create an object Header They have the same name as the class When you USE the constructor, you must put 1 integer value in as a parameter: Climber c = new Climber(3);
3
Athlete Constructors 2 How many constructors does Athlete have? When we make an Athlete, how many parameters can we use? What happens to them? They get sent to the Robot constructor 0 or 4
4
Climber Constructor 1 How many constructors does Climber have? When we make a Climber, how many parameters can we use? What happens to it? It becomes x and then gets sent to the Athlete constructor 1 public class Climber extends Athlete { public Climber(int x) { super(x, 1, Display.NORTH, 1); } public void climbUpRight() { …
5
Computer Math Unit 1 Lab04
6
Class/Static Methods Often changes an entire environment – Example: Display.setSize(x,y) Can be defined and used in different classes: – openWorld, setSpeed, setSize are DEFINED in the Display class, but USED in the Lab00, Lab01, etc classes. – Invoked through CLASS name: Display.setSpeed(5);
7
Class/Static Methods contd Sometimes only needed for a specific program (we don’t need to use the method more than once. – In this case, defined and used inside of the same class – Unlike turnRight() which is defined in Athlete and used in Lab02 All class methods are marked by the keyword static Defined inside the class, before the main method
8
Instance Methods Invoked by an object name: karel.turnLeft() Change individual objects
9
Lab04 Take the Field We will define a class/static method called takeTheField The definition will go INSIDE of the class and above the main method (not inside of main). Why? Everything inside of main automatically runs. We do not want this method to run unless we tell it to.
10
Method Header takeTheField’s method header looks like this: public static void takeTheField(Athlete arg) What does “Athlete arg” mean? keywordsName of method Parameter/Argument When we use the method, we must put in the name of an Athlete as a parameter
11
Method Header contd. Formal parameter/argument: in the definition Actual parameter/argument: value we put in when we are using the constructor/method public Climber(int x) Climber bill = new Climber(4); public static void takeTheField(Athlete a) takeTheField(bob); Formal argument Actual argument
12
How to get started Copy and paste Lab03, modifying the Display information as needed Let’s look at page 16 in the TJ packet and enter the takeTheField method after the class heading and { (public class Lab04), but before the main method header (public static void main(String[] args) Read the specification to see how many Athletes to make and what they should do
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.