Top Banner
CSC172 Intro Pepper
23

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.

Dec 29, 2015

Download

Documents

Ruth Chandler
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: 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.

CSC172 Intro

Pepper

Page 2: 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.

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

Page 3: 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.

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/

Page 6: 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.

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

Page 7: 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.

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

Page 8: 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.

Scanner reminder

• Bring a Scanner blueprint into your program with an import statement:

import java.util.Scanner;

Page 9: 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.

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

Page 10: 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.

Your program talks to user

• System.out.println talks to user, but cannot get responseSystem.out.println(“enter something”);

Page 11: 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.

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

Page 12: 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.

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

Page 13: 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.

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

Page 14: 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.

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

Page 15: 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.

Game Answer

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

Page 16: 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.

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)

Page 17: 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.

Card Exception

• Add exception when Card is sent a bad rank or suit.

• Handle it in the Game

Page 18: 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.

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

Page 19: 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.

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 >

Page 20: 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.

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;

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

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.

Page 22: 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.

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.

Page 23: 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.

Summary

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

parms