200 likes | 214 Views
Unit 1 Lab03 Notes. Data Types. So far we have talked about data types only in terms of classes/objects. A class can explain what type of data an object is Example: Robot karel - we are specifying that karel is a data type of Robot
E N D
Data Types • So far we have talked about data types only in terms of classes/objects. • A class can explain what type of data an object is • Example: Robot karel - we are specifying that karel is a data type of Robot • Because objects are linked to classes they are complex (they have data fields and methods) • Sometimes we may just need to use a number or other simple value
Primitive Data • There are pre-defined primitive data types in JAVA • These can be used for numbers, letters, etc. • Primitive data is more simplistic than an object. • When you create a primitive data, it is not connected to any data fields or methods.
Primitive Data • Primitive data is stored in memory spaces called variables • NO new keyword OR constructor is used when creating. • Declaring primitive data creates a name and a fixed space in memory • Initialization assigns an initial value.
Creating Objects vs Creating Primitive Data • Format for instantiating an object: • Format for declaring and initializing primitive data: Object type object name (you pick) = new keyword Constructor Primitive data type Variable name (you pick) = Value
Examples • In Java, declaring integers uses the keyword int and declaring decimal numbers uses the keyword double. int myGrade = 95; double myGPA = 4.2; Intializing Declaring Primitive data type Variable Name Assigns Integer value Primitive data type Assigns decimal value Variable Name
Example • In Java, using keyword boolean means that the variable can only have a value of true or false. boolean answer = true; Declaring Intializing variable type Name (you pick) Assigns boolean value
Using Primitive Data • Once you have declared a primitive variable, you can use it throughout the program, just like objects. • You do not need to repeat the type after declaration. int myGrade = 95; myGrade = 95 + 2;//extra credit pts • The new value of myGrade is 97. • Remember, myGrade is not an object and not linked to any methods. You will never say myGrade.something();
Constructors • What is the point? • Always have the same name as the class. • Are not inherited like methods and data fields. • Each class must have its own constructors defined. • If you don’t define a constructor for your class, JAVA will automatically assign a default constructor. Specify how to create objects
Constructors • We have looked at how they are used (along with new keyword) in application classes. • Example: Robot karel = new Robot(3, 5,Display.NORTH,25); • What happens when the previous statement is executed? Arguments/Parameters Robot constructor
Well… • If jordan is an Athlete, what happens when we execute: jordan.turnRight(); • Java searches through the Athlete class until it finds the turnRight method. • Then it looks inside the { }, at the definition, to see what to do.
Constructors work the same way Robot karel = new Robot(3, 5,Display.NORTH,25); • Java searches through the Robot class until it finds the Robot constructor that requires 4 arguments. • Then it looks inside the { } to see what to do. • There, the attributes, or data fields are set: • 3 becomes the x-coord, the 5 becomes the y-coord, direction becomes north and the beepers become 25.
What About Athletes? Athlete jordan=new Athlete(1,2,Display.EAST, 20); • Java searches through the Athlete class until it finds the Athlete constructor that accepts 4 arguments. • Then it looks inside the { } to see what to do.
Athlete(1, 2, Display.EAST, 20); It finds… public Athlete(int x,int y,int dir, int beep) { super(x,y,dir,beep); } • The word super can theoretically be replaced with Robot. This would then read: Robot(1,2,Display.EAST,20); • Does this look familiar???
Java still searches… • Java now takes those values and sends them on to the Robot 4-arg constructor • Then it looks inside the { } to see what to do. • There, the attributes, or data fields are set: • 1 becomes the x, the 2 becomes the y, the direction becomes east and the beepers become 20.
Why super? • Since subclasses can’t directly inherit constructors from their superclass, the keyword super allows subclasses to at least use the superclass’s constructor
Lab03 - Climber • A Climber isa Athlete. What does this mean? • Climbers will be able to do an additional 4 methods that you define Climber can do everything an Athlete (and Robot) can do
Lab03 – Climber Constructor public Climber(int x) {super(x, 1, Display.NORTH, 1); } • There is only one constructor that allows you to put in one argument.
Lab03 – Starting the Lab • Open the Climber.java file • Use the pseudocode (non-java description of what should happen) given for climbUpRight • Add an additional 3 methods • Then create Lab03, using Lab02 and the TJ packet as a guide
Lab03 – Climber Methods climbUpRight climbDownRight */pseudocode: tL, m, m, tR, m */ climbUpLeft climbDownLeft