Programming Practices and Object-Oriented Concepts

Programming Practices and Object-Oriented Concepts
Slide Note
Embed
Share

In this lecture, we delve into good programming practices concerning methods, classes, and inheritance. We explore the use of methods that return values, subclasses, and the significance of comments in code. The session touches on variable declaration, setup, and drawing, emphasizing the importance of structuring code and utilizing auto-formatting tools. Additionally, the lecture covers the basics of methods, classes, and objects, highlighting the role of functions and procedures, method declarations, and parameter usage. Furthermore, the discussion extends to understanding classes as blueprints and objects as a means to organize related data with control methods.

  • Programming practices
  • Methods
  • Classes
  • Inheritance
  • Object-oriented concepts

Uploaded on Mar 08, 2025 | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. IAT 265 Lecture 6 Methods, Classes, Inheritance May 17, 2016 IAT 265

  2. This Lecture A few good programming practices Methods Classes Rockets, asteroids Methods that return values Subclasses May 17, 2016 IAT 265

  3. Good Practices Comments are your friend! Put a block of commenting at the top of each program that explains what it does Comment in code what a section will do Comment when you appropriate code!! Auto-format is also your friend!! Your programs will have at least three sections Variable declaration setup() draw() May 17, 2016 IAT 265

  4. Methods Methods, also known as Functions (methods which return something) Procedures (methods which don t return stuff) Method declaration void vendingMachine( int coinCents ){ println("You inserted "+coinCents+" cents."); } Method call int quarter = 25; vendingMachine(quarter); May 17, 2016 IAT 265

  5. Methods Method declaration parameter void vendingMachine( int coinCents ){ println("You inserted "+coinCents+" cents."); } void means doesn't return anything The method declaration is like a blueprint Doesn't matter if declaration is before or after call! Parameter name (e.g. coinCents) is just the name given to data passed into the method May 17, 2016 IAT 265

  6. Methods Method call argument int quarter = 25; vendingMachine(quarter); vendingMachine(quarter) == vendingMachine(25) Method call must occur in either setup(), draw(), or another method You can call it as many times as you like! vendingMachine()s for everyone!! May 17, 2016 IAT 265

  7. Classes Types Primitives: int, float, char, boolean Objects: array, string, class May 17, 2016 IAT 265

  8. Objects We ve worked with some objects before, like Arrays. We can make our own objects, to keep related data together, with methods to control that data. May 17, 2016 IAT 265

  9. Classes Classes are the blueprints for our new objects. To declare a new Class (a new type of object): class MyToy { // fields (class variables) // methods (class functions) } May 17, 2016 IAT 265

  10. Fields and Methods class MySquare { int xPos, yPos; MySquare(x, y) { xPos = x; yPos = y; } fields x y constructor drawMe() (one kind of method) void drawMe() { rect(xPos, yPos, 50, 50); } } methods May 17, 2016 IAT 265

  11. Fields and Methods x y class MySquare { int xPos, yPos; drawMe() MySquare(x, y) { } xPos = x; yPos = y; 10 10 20 90 void drawMe() { } } rect(xPos, yPos, 50, 50); drawMe() drawMe() square1 square2 MySquare square1 = new MySquare(10, 10); MySquare square2 = new MySquare(20, 90); May 17, 2016 IAT 265

  12. Fields and Methods x y class MySquare { int xPos, yPos; drawMe() MySquare(int x, int y) { xPos = x; yPos = y; } 10 10 20 90 drawMe() drawMe() void drawMe() { } } rect(xPos, yPos, 50, 50); square2 square1 MySquare square1 = new MySquare(10, 10); MySquare square2 = new MySquare(20, 90); square1.drawMe(); square2.drawMe(); May 17, 2016 IAT 265

  13. Arrays of Objects? Let s make a bunch of squares! MySquare[] squares = new MySquare [10] ; // initialize all of our squares. for (int i = 0; i < 10; i ++) { squares[i] = new MySquare(i*10, i*10); } squares[4].drawMe(); // draw the 4th square. May 17, 2016 IAT 265

  14. Asteroids Let s adapt this to make an array of Asteroids for our rocket from lecture 4 class Asteroid { //fields float rotation = 0; float xPos, yPos; float velocityX, velocityY; long lastDrawMillis = 0; } May 17, 2016 IAT 265

  15. Asteroids When we create an asteroid, let s have it start in a random position, and move in a random direction. class Asteroid { // constructor Asteroid() { } xPos = random(0, 400); yPos = random(0, 400); rotation = random(0, TWO_PI); velocityX = sin(rotation)*10; velocityY = -cos(rotation)*10; May 17, 2016 IAT 265

  16. Asteroids class Asteroid { // draw method void drawMe() { May 17, 2016 IAT 265

  17. Revisit our example So far we have a rocket that flies around in a field of asteroids What if we want our rocket to be able to fire But we don t want to get rid of our non-firing rocket Create a subclass! May 17, 2016 IAT 265

  18. Inheritance Subclasses inherit fields and methods from parent class ArmedRocket extends Rocket { } May 17, 2016 IAT 265

  19. Our subclass needs a constructor Our empty ArmedRocket example creates an error Processing doesn t know how to construct an ArmedRocket We want the ArmedRocket constructor to do the same work as the Rocket constructor ArmedRocket(int initialX, int initialY, float initialRot) { super(initialX, initialY, initialRot); } The keyword super means to refer to the parent class. In this case, to call the Parent Class Constructor May 17, 2016 IAT 265

  20. Now we have ArmedRocket We can use an ArmedRocket now in our example But, it s basically just a copy of Rocket The only reason to define an ArmedRocket is to add new capabilities or to override old ones May 17, 2016 IAT 265

  21. Add a fire() method We want our fire method to draw a missile that shoots out of the rocket We could have the fire method draw the missile Is there a problem with this? May 17, 2016 IAT 265

  22. Missiles should also be objects The object oriented solution is to make the missile an object as well All the different types of things in our domain should have a corresponding class Like asteroids and rockets, the missile class should know how to draw itself A Missile is similar to a rocket (position, rotation, draw method, etc.) Now our ArmedRocket.fire() method can just create and return a missile May 17, 2016 IAT 265

  23. The fire() method Missile fire() { Missile m = new Missile(xPos, yPos, rotation); return m; } Now add code in loop to draw missiles May 17, 2016 IAT 265

  24. Using the keyPressed() method keyPressed() is a built in Processing method that is called when a key is pressed May 17, 2016 IAT 265

More Related Content