CS62 - Fall 2020 - Lab 1

Example code in this lecture

   ClassBasics

Lecture notes

  • classes
       - A class is a template for an object
       - a class defines:
          - the types of things you can do to/with an object of that class (i.e. methods)
          - how you can create new objects
          - what data we need to keep track of for an object
       - Let's say we want to come up with a template (i.e. class) for balls
          - What questions might we ask about a ball? What types of things might we do to a ball?
             - how big is it?
             - what is it filled with?
             - what's it's color?
             - where is it?
             - move it?
             - throw it?
             - ...
          - What information might we need to keep track of about a ball?
             - size
             - color
             - what it's filled with
             - location
          - all this information defines the category of ball
       - we can instantiate the class (i.e. create a new ball object) by setting all of the attributes
          - each ball we create will have it's own individual attributes
          - changing the attributes of one ball (e.g. the location) will NOT affect the other balls
       - The class then describes all the things we can do with a particular instance of a ball (any ball!):
          - throw it
          - ask about it's color
          - ...

  • designing a class in Java
       - three key things when designing a class:
          - what are the methods we want the object to have?
             - what types of things do we want the object to do?
             - what types of questions might we want to ask about the object?
          - what data do we need to keep track of *per* object?
             - what differentiates one object of the class from another?
          - what information do we need to make or create a new object of that class?

  • let's design a card class to represent a playing card
       - what methods do we want the object to have?
          - what suit?
          - what number?
          - maybe:
             - face-up/face-down?
             - change suit/number?
             - location?
       - what data do we need to keep track of per card?
          - number
          - suit
          - maybe: location, face-up/face-down
       - what information do we need to know to create a new card?
          - number
          - suit


  • writing a class in Java
       - first, we tell Java that we're declaring a new class

          public class ClassName{
       
          }

          - this goes in a file that has the same name as the class with the extension .java
          - by convention, class names are capitalized
       - for all of the data that we want to store, we create "instance variables"
          - these are just variables declared inside of the class
          - they are available to methods declared inside the class
       - for each bit of functionality we want, we write methods
       - the "constructor" of an object is a method that has the same name as the class and NO return type
          - it's used to create (or construct) a new object

  • look at the BasicCard class in the ClassBasics code
       - two instance variables: number and suit
          - each card object will have its own copy of these variables
       - 4 methods (including the constructor)
          - all are very simple!
          - BasicCard is the constructor and is called when we're creating a new card
             - it takes two parameters
             - all it does is store these two parameters away
          - getSuit and getNumber
             - just give data back
          - cheat()
             - doesn't take any parameters or give anything back
             - alters the state of the card

  • creating a new object
       - now that we have defined out class, we can create a new object that is an instance of that class
       - creating a new class also creates a new type

       BasicCard card = new BasicCard(10, "hearts")

          - BasicCard is the type
          - card is the name of the variable
             - this first part is just like what we've been doing all along
          - = (assignment)
          - new: keyword that tells Java to create a new object
          - BasicCard(...) is the call to the constructor
             - Java creates a new object
             - that object has its own copies of the instance variables
             - the constructor code is then called

  • main
       - ALL Java programs have to start in a "main" method
       - the syntax for the main method is:

       public static void main(String[] args){
          ...
       }

       - To run a program, you click on the green arrow button
          - Eclipse will look for a main method in the currently open class
          - and the program will start executing there
       

  • look at the main method in the BasicCard class in the ClassBasics code
       - constructs two different card objects
       - prints out the number and suit for the two cards
       - calls the cheat method on card1
       - then prints out the number and suit for the two cards again
       
       - when you run the code:
          - you see each of the cards gets their own copies of the instance variables
             - each object has it's own state
          - the cheat method changes the state of card1, but card2 is unaffected