CSC172 Intro Pepper
Dec 29, 2015
Goals
• Reminder of what a class is• How to create and use classes• How to design classes• How to think in terms of objects• How to comment – javadoc
What is a Object Oriented Programming? OOP
• Focus on nouns, things– Rather than verbs, action
• Class is a blueprint for creating an object– Filled with instance variables
• Record State
– Non-Static Methods • Instructions for behavior• Take in parameters and "this" instance (implicit parm)• Special method – Constructor• toString
Picture from: http://javabymanish.blogspot.com/
A Blue Print and an Object
Bike code
• See code: – http://docs.oracle.com/javase/tutorial/java/conce
pts/class.html
• Encapsulation– Mutators & Accessors– Invariants • Exception enforcement
Picture from: http://www.nnwj.de/encapsulation.html
You Try• Create a Card class with toString and
getCardScore method. (Getscore just returns the rank, so A= 1; J=11; Q = 12; K=13 )
• Use 171 Java Tools
Card Class Answer• Link for Card class code
– Notice• Constructor does not create new values• Constructor has no return type and same name• Notice a bad card value will just be created• See use of this• Properties become instance variables• Behaviors become methods• No static
– Documentation! – You now have to do this.• See javadoc toggle view• Class comments• Method comments• Each parameter in and out comments
Scanner reminder
• Bring a Scanner blueprint into your program with an import statement:
import java.util.Scanner;
Our Scanner Guy
• We need to create a Scanner guy (object instance) using the Scanner blueprint (class) and hold him in a Scanner box (variable) Scanner keyb = new Scanner(System.in);
KEYB
Your program talks to user
• System.out.println talks to user, but cannot get responseSystem.out.println(“enter something”);
Reading from the keyboard
• Once we create a Scanner guy (object instance), we can ask our Scanner variable (we called it keyb) to read an integer from the terminal window:variable = keyb.nextInt();
Ask before you read• Scanner takes in typing, but does not ask for
it. You do that:System.out.println ("What is the first value\t?");int value1 = keyb.nextInt();System.out.println ("What is the second value\t?");int value2 = keyb.nextInt();
Now, your variables are inside your program just as if you had set them to a specific number
value 2value1
Other things scanner can read
• You can read:– nextInt()– nextLong()– nextByte()– nextDouble()– next() – up to next whitespace (delimiter) – nextLine() – up to “\n” (end of line)– useDelimiter()
• Reading pointer – nextLine() moves your reading pointer down a line
You Try
• Write a main method– creates 2 cards – prints the cards – prints the sum of the score of the cards. – (Extra – arrays)• fill an array of 5 cards instead using a loop
Game Answer
• Link to Code• Notice: – Created using new and calling constructor– Call methods from the object, not the class– No "this" access
Class Invariants - Exceptions• IllegalArgumentException• How to throw inside class method– throw new IllegalArgumentException("detailed message")
• How to catch inside client method– try/catch block
• try { do the action } • catch (illegalArgumentException ex) { what to do if the try failed;
can use ex variable}
– Or ignore it• throws IllegalArgumentException ( in method header)
Card Exception Answer• Link for card; Link for game• Notice– Rank test uses an Or , not 1 <= rank <= 13– Suit test uses .equals("CLUBS"), not suit = "CLUBS"– Test input, not instance variable– IllegalArgumentException is an existing exception class in
java.lang• No import
– See why private instance variables are needed?– If there were a setter for these variables, move the code into
a setter and call it
Export and Run on Windows
• Export in BlueJ– Project / Create Jar file– Choose main method
• Run in windows command menu– Start Command– Cd to jar– java –jar < name of jar >
Packages
• Basically a folder that contains folders and classes– package is a namespace that organizes a set of related
classes and interfaces • http://docs.oracle.com/javase/tutorial/java/concepts/
package.html
• Edit / new package• Move classes into the package – Put "package <package name> ; " statement: – Ex: package Pepper;
Packages and Import Statements
• A package is Java’s way of forming a library of classes.
• If you wish to use a package of classes that are not the directory in which you are working, write:import java.util.Scanner; // to include Scanner
Import java.util.*; // to include all of
// java.util’s classes.
Package Names and Directories
• A package can be created by grouping all the classes together in one directory and placing the linePackage PackageName;At the beginning before any Java code.
• Now these classes can be used by adding an import statement to the classes that use it.