Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSC172 Intro Pepper. 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.

Similar presentations


Presentation on theme: "CSC172 Intro Pepper. 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."— Presentation transcript:

1 CSC172 Intro Pepper

2 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

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

4 A Blue Print and an Object

5 Bike code See code: – http://docs.oracle.com/javase/tutorial/java/conce pts/class.html 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

6 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

7 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

8 Scanner reminder Bring a Scanner blueprint into your program with an import statement: import java.util.Scanner;

9 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

10 Your program talks to user System.out.println talks to user, but cannot get response System.out.println(“enter something”);

11 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();

12 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 2 value1

13 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

14 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

15 Game Answer Link to Code Notice: – Created using new and calling constructor – Call methods from the object, not the class – No "this" access

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

17 Card Exception Add exception when Card is sent a bad rank or suit. Handle it in the Game

18 Card Exception Answer Link for card; Link for game Link for cardLink 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

19 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

20 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/p ackage.html http://docs.oracle.com/javase/tutorial/java/concepts/p ackage.html Edit / new package Move classes into the package – Put "package ; " statement: – Ex: package Pepper;

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

22 Package Names and Directories A package can be created by grouping all the classes together in one directory and placing the line Package 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.

23 Summary Design Classes Create Classes Create Objects from those classes Use Scanner Exceptions Javadoc – class header, method header with parms


Download ppt "CSC172 Intro Pepper. 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."

Similar presentations


Ads by Google