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.

Slides:



Advertisements
Similar presentations
Introduction to Java 2 Programming Lecture 3 Writing Java Applications, Java Development Tools.
Advertisements

11-Jun-14 The assert statement. 2 About the assert statement The purpose of the assert statement is to give you a way to catch program errors early The.
1 Todays Objectives Announcements Homework #1 is due next week Return Quiz 1 – answers are posted on the Yahoo discussion page site Basic Java Programming.
Here’s what we see when we start a new BlueJ “Project”. BlueJ automatically creates a small “readme.txt” file to hold any directions we wish to write about.
Building Java Programs Interactive Programs w/ Scanner What is a class? What is an object? What is the difference between primitive and object variables?
Exceptions & exception handling Use sparingly. Things you can do with exceptions: 1. Define a new exception class. 2. Create an exception instance. 3.
CS102--Object Oriented Programming
Slides prepared by Rose Williams, Binghamton University ICS201 Exception Handling University of Hail College of Computer Science and Engineering Department.
 2005 Pearson Education, Inc. All rights reserved Introduction.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
Fundamentals of Software Development 1Slide 1 Review: Why start with WordGames? You wrote your first lines of code in this course in WordGames.You wrote.
Scanner Pepper With credits to Dr. Siegfried. The Scanner Class Most programs will need some form of input. At the beginning, all of our input will come.
Begin Java having used Alice Pepper - Some slides from Alice in Action with Java.
Java Variables and Expressions CSC160 Professor Pepper (presentation adapted from Dr. Siegfried)
© 2006 Pearson Addison-Wesley. All rights reserved2-1 Console Input Using the Scanner Class Starting with version 5.0, Java includes a class for doing.
Introduction to Computer Programming CSC171 – 001 Getting Started – Chapters 1 & 2 Professor Pepper (presentation adapted from Dr. Siegfried)
Applying OO Concepts Using Java. In this class, we will cover: Overriding a method Overloading a method Constructors Mutator and accessor methods The.
Scanner & Stepwise Refinement Pepper With credit to Dr. Siegfried.
***** SWTJC STEM ***** Chapter 3-1 cg 36 Java Class Libraries & API’s A class library is a set of classes that supports the development of programs. Java.
Introduction to Java Java SE 8 for Programmers Paul Deitel Harvey Deitel Deitel Developer Series 2014.
Chapter 2 Section 2.2 Console Input Using The Scanner CLASS Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska.
Introduction to Computer Programming Error Handling.
Writing Classes (Chapter 4)
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Summary and Exam COMP 102.
Fundamentals of Software Development 1Slide 1 Recap: “From scratch” projects Today’s software engineering is almost NEVER “from scratch”Today’s software.
Simple Programs from Chapter 2 Putting the Building Blocks All Together (corresponds with Chapter 2)
More on Classes Pepper With help from rs.html.
The Java Programming Language
Exceptions. Exception Abnormal event occurring during program execution Examples –Manipulate nonexistent files FileReader in = new FileReader("mumbers.txt“);
Computer Programming 2 Lab(1) I.Fatimah Alzahrani.
Arrays Pepper. What is an Array A box that holds many of the exact same type in mini-boxes A number points to the mini-box The number starts at 0 String.
CS 106 Introduction to Computer Science I 01 / 31 / 2007 Instructor: Michael Eckmann.
Can we talk?. In Hello World we already saw how to do Standard Output. You simply use the command line System.out.println(“text”); There are different.
10-Nov-15 Java Object Oriented Programming What is it?
Textbook: Data Structures and the Java Collections Framework 3rd Edition by William Collins William Collins.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
Strings and Text File I/O (and Exception Handling) Corresponds with Chapters 8 and 17.
CS0007: Introduction to Computer Programming Classes: Documentation, Method Overloading, Scope, Packages, and “Finding the Classes”
FUNDAMENTALS 2 CHAPTER 2. OPERATORS  Operators are special symbols used for:  mathematical functions  assignment statements  logical comparisons 
CHAPTER 5 GC 101 Input & Output 1. INTERACTIVE PROGRAMS  We have written programs that print console output, but it is also possible to read input from.
Javadoc. Purpose of javadoc javadoc is a program that reads your Java program and produces great-looking documentation in HTML format Without any help,
Pengantar OOP Class-Java. 2 Software Development Tools Using Sun Java SDK alone Source File(s) (.java) Programmer Compiler (javac) Class File(s) (.class)
Exceptions Chapter 16 This chapter explains: What as exception is Why they are useful Java exception facilities.
Review Creating Objects Pepper many references from rial/java/objects/object.html
1.Reading from Keyboard 2.Main programs 3.Responsibilities 1 CS12230 Introduction to Programming Lecture 2or3-Other things.
Reading input from the console input. Java's console input The console is the terminal window that is running the Java program I.e., that's the terminal.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Summary and Exam COMP 102.
Martin T. Press.  Main Method and Class Name  Printing To Screen  Scanner.
CSC Java Programming, Fall, 2008 Week 3: Objects, Classes, Strings, Text I/O, September 11.
CSC Java Programming, Spring, 2010 Week 2: Java Data Types, Control Constructs, and their C++ counterparts.
In this class, we will cover: Overriding a method Overloading a method Constructors Mutator and accessor methods The import statement and using prewritten.
College Board Topics – A.P. Computer Science A Program Design - Read and understand a problem's description, purpose, and goals; Apply data abstraction.
Chapter 2 Console Input and Output Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Slides prepared by Rose Williams, Binghamton University Console Input and Output.
Today Javadoc. Packages and static import. Viewing API source code. Upcoming Topics: –protected access modifier –Using the debugger in Eclipse –JUnit testing.
Exceptions and Error Handling. Exceptions Errors that occur during program execution We should try to ‘gracefully’ deal with the error Not like this.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
More Sophisticated Behavior
CSC1401 Input and Output (and we’ll do a bit more on class creation)
Yanal Alahmad Java Workshop Yanal Alahmad
Testing and Debugging.
Something about Java Introduction to Problem Solving and Programming 1.
INPUT STATEMENTS GC 201.
Introduction to javadoc
A+ Computer Science INPUT.
Applying OO Concepts Using Java
Tonga Institute of Higher Education
Introduction to javadoc
Presentation transcript:

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 – 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:

A Blue Print and an Object

Bike code See code: – pts/class.html pts/class.html Encapsulation – Mutators & Accessors – Invariants Exception enforcement Picture from:

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 response System.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 2 value1

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 Add exception when Card is sent a bad rank or suit. Handle it in the Game

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

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

Packages Basically a folder that contains folders and classes – package is a namespace that organizes a set of related classes and interfaces ackage.html ackage.html Edit / new package Move classes into the package – Put "package ; " 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 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.

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