1 / 20

Unit 1 Lab03 Notes

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

Download Presentation

Unit 1 Lab03 Notes

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. Unit 1 Lab03 Notes

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

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

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

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

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

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

  8. 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();

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

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

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

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

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

  14. 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???

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

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

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

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

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

  20. Lab03 – Climber Methods climbUpRight climbDownRight */pseudocode: tL, m, m, tR, m */ climbUpLeft climbDownLeft

More Related