CS201 - Spring 2014 - Class 3
Admin
- should have handed in assignment 1, part 1
- assignment 1, part 2 is due Wednesday before class
problems for the day
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
- 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
improving our card class
- let's say we're trying to model a 52 card deck with 4 suits and 13 cards
- does our current class enforce that the cards created have to be valid?
- No!
- what do we need to check/enforce?
- numbers between 1 and 13
- suits are one of four suits
boolean operators
- to enforce that the card numbers are valid, we want to ask two questions:
- is the number >= 1
- is the number <= 13
- Java has three main boolean operators
- &&: returns true if both arguments are true, false otherwise
- ||: returns true if at least one argument is true, false otherwise
- !: negation, returns true if false and false if true
write a method called isValidNumber that takes a number as a parameter and checks if the number is valid
What are MIN_NUMBER and MAX_NUMBER?
- they're constants!
- a constant is a variable who's value NEVER changes once it's been initialized
- why do we use constants?
- Makes our code easier to read
- We can use a name rather than a random value
- Makes our code easier to update
- If we decide to change the value later on (programmatically), then we can just change it in one place
- If we had used it in many places, we would have to remember to put it in many places
- to indicate that a variable is a constant, we make it all uppercase
final
- python doesn't have any explicit mechanism for declaring constants
- Java allows us to communicate to it that this particular variable is a constant
- the keyword "final" indicates this
- anything that is final CANNOT be changed once it has been initialized
- For example, if I try and write:
MIN_NUMBER = 10;
I'll get an error
look at isValidSuit method in EvenBetterCard class in
ClassBasics code
- notice anything unusual?
look at the new constructor in the BetterCard class in the
ClassBasics code
- we're calling our two isValid methods and changing the behavior of the constructor if they're false
- we can test these out by creating an object that's invalid
BetterCard badCard = new BetterCard(-1, "banana");
System.out.println("Card number: " + badCard.getNumber());
System.out.println("Card suit: " + badCard.getSuit());
information hiding
- in addition to accessing methods with the . (dot) notation, you can also access some variables associated with a class
- accessing instance variables directly is bad coding practice, so don't do it!
- unfortunately, other people besides ourselves might use our code and so we want to make sure that we protect our data
- for example, look at the following code:
BetterCard card = new BetterCard(10, "hearts");
card.suit = "banana";
System.out.println("Card number: " + card.getNumber());
System.out.println("Card suit: " + card.getSuit());
- We went through all that work trying to protect the integrity of our data through the constructor and someone can still mess it up!
public and private
- one of the powers of Java is that you only expose the methods that the user needs to your class and can hide everything else
- Java allows you to control what information
- to do this we can declare methods and variables to be either public or private by listing this at the beginning of their declaration
- public means they are accessible to anyone
- private means they are only accessible inside the class
- some rules of thumb
- all instance variables should be private
- if you need to give someone access to read the variable or set the variable, do it through public methods!
- the constructor(s) is almost always public
- methods define the external functionality of the class/object should be made public
- all other methods should be made private
- what should we make public and what should we make private?
look at the EvenBetterCard class in
ClassBasics code
- I've made all of the instance variables private
- the constructor, getSuit, getNumber and cheat have all been made public
- my two isValid methods are private
- now if I try and modify the suit directly:
EvenBetterCard card = new EvenBetterCard(10, "hearts");
card.suit = "banana";
- Java won't let me!
static
- Everything in Java has to be inside a class
- For instance variables, that means that each time we get a new copy of the object, we get extra copies of the variables
- For constants, that means each copy of the object will have it's own versions of the constants. Does that make sense?
- No!
- if we declare a variable to be static, there's only one version of it PER class, NOT per object
- all constants we will declare as static