Unit 1 Lab 03 Notes Data Types n

Unit 1 Lab 03 Notes

Data Types n n n 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 n Example: Robot karel - we are specifying that karel is a data type of Robot n 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 n n 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 n n 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 n Format for instantiating an object: Object type n object name (you pick) = new keyword Constructor Format for declaring and initializing primitive data: Primitive data type Variable name = Value (you pick)

Examples n In Java, declaring integers uses the keyword int and declaring decimal numbers uses the keyword double. Declaring Intializing int my. Grade = 95; Primitive data type Variable Name Assigns Integer value double my. GPA = 4. 2; Primitive data type Variable Name Assigns decimal value

Example n In Java, using keyword boolean means that the variable can only have a value of true or false. Declaring Intializing boolean answer = true; 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. n You do not need to repeat the type after declaration. int my. Grade = 95; my. Grade = 95 + 2; //extra credit pts n n n The new value of my. Grade is 97. Remember, my. Grade is not an object and not linked to any methods. You will never say my. Grade. something();

Constructors n n n What is the point? Specify how to create objects Always have the same name as the class. Are not inherited like methods and data fields. n n 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.

Constructors n n 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); Robot constructor n Arguments/Parameters What happens when the previous statement is executed?

Well… If jordan is an Athlete, what happens when we execute: jordan. turn. Right(); n n n Java searches through the Athlete class until it finds the turn. Right 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); n n n 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: n 3 becomes the x-coord, the 5 becomes the ycoord, direction becomes north and the beepers become 25.

What About Athletes? Athlete jordan=new Athlete(1, 2, Display. EAST, 20); n n 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); } n The word super can theoretically be replaced with Robot. This would then read: Robot(1, 2, Display. EAST, 20); n Does this look familiar? ? ?

Java still searches… n n n 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: n 1 becomes the x, the 2 becomes the y, the direction becomes east and the beepers become 20.

Why super? n Since subclasses can’t directly inherit constructors from their superclass, the keyword super allows subclasses to at least use the superclass’s constructor

Lab 03 - Climber n A Climber isa Athlete. What does this mean? Climber can do everything an Athlete (and Robot) can do n Climbers will be able to do an additional 4 methods that you define

Lab 03 – Climber Constructor public Climber(int x) { super(x, 1, Display. NORTH, 1); } n There is only one constructor that allows you to put in one argument.

Lab 03 – Starting the Lab n Open the Climber. java file n n n Use the pseudocode (non-java description of what should happen) given for climb. Up. Right Add an additional 3 methods Then create Lab 03, using Lab 02 and the TJ packet as a guide

Lab 03 – Climber Methods climb. Up. Right climb. Down. Right */pseudocode: t. L, m, m, t. R, m */ climb. Up. Left climb. Down. Left
- Slides: 20