Classes and Objects. What is Design? The parts of the software including – what information each part holds – what things each part can do – how the various.

Slides:



Advertisements
Similar presentations
UNIT II DATA TYPES OPERATORS AND CONTROL STATEMENT 1.
Advertisements

AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
7 Copyright © 2005, Oracle. All rights reserved. Creating Classes and Objects.
10 Copyright © 2005, Oracle. All rights reserved. Reusing Code with Inheritance and Polymorphism.
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 4 : Polymorphism King Fahd University of Petroleum & Minerals College of Computer.
1 Chapter 10 - Structures, Unions, Bit Manipulations, and Enumerations Outline 10.1Introduction 10.2Structure Definitions 10.3Initializing Structures 10.4Accessing.
Black Jack in Objective-C
1 CSE 331 Enumerated types ( enum ) slides created by Marty Stepp based on materials by M. Ernst, S. Reges, D. Notkin, R. Mercer, Wikipedia
Case Study: Focus on Structures Math 130 Lecture 21 B Smith: 10/04: Required 35 minutes to complete. 15 minutes was spent returning test 2 and completing.
2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 10 - Structures, Unions, Bit Manipulations, and Enumerations Outline 10.1Introduction 10.2Structure.
2008 Pearson Education, Inc. All rights reserved Pointers and Pointer-Based Strings.
New features in JDK 1.5 Can these new and complex features simplify Java development?
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 7.9Arrays of Pointers Arrays can contain pointers For.
Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view class.
© Glenn Rowe AC Lab 2 A simple card game (twenty- one)
1. Define the concept of assertions. 1 Explain the use of assertions. 2 Create Java program using assertions. 3 Run Java program using assertions. 4 2.
Color Templates Software Engineering Module: Core of Java Topic: Constructors TALENTSPRINT | © Copyright 2012.
1.A computer game is an example of A.system software; B.a compiler; C.application software; D.hardware; E.none of the above. 2.JVM stands for: A.Java Virtual.
1 User-Defined Classes The String class is a pre-defined class that is provided by the Java Designer. Sometimes programmers would like to create their.
Phil Campbell London South Bank University Java 1 First Steps.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Four Defining Your Own Classes.
Copyright 2008 by Pearson Education Building Java Programs Chapter 8 Lecture 8-3: Encapsulation, toString reading: self-checks: #13-18,
Objects, Variables & Methods Java encapsulates data and action modules that access the data in one container, called an object. Object members that.
IMPLEMENTING CLASSES Chapter 3. Black Box  Something that magically does its thing!  You know what it does but not how.  You really don’t care how.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Chapter Three - Implementing Classes.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
CS 206 Introduction to Computer Science II 01 / 21 / 2009 Instructor: Michael Eckmann.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
1 Classes, Encapsulation, Methods and Constructors Class definitions Scope of Data –Instance data –Local data The this Reference Encapsulation and Java.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
© The McGraw-Hill Companies, 2006 Chapter 7 Implementing classes.
Copyright 2010 by Pearson Education Building Java Programs Chapter 8 Lecture 8-2: Object Behavior (Methods) and Constructors reading:
CSS446 Spring 2014 Nan Wang.  To learn how to choose appropriate classes for a given problem  To understand the concept of cohesion  To minimize dependencies.
Programming Languages and Paradigms Object-Oriented Programming.
Writing Classes (Chapter 4)
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals; Apply data abstraction.
CSE 1302 Lecture 7 Object Oriented Programming Review Richard Gesick.
Java Quiz Bowl A fun review of the Java you should know from CMPT 201 If you don’t know the answers - this week is for you to study up!
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Arrays and ArrayLists in Java L. Kedigh. Array Characteristics List of values. A list of values where every member is of the same type. Each member in.
CS 206 Introduction to Computer Science II 09 / 10 / 2009 Instructor: Michael Eckmann.
CSC 142 Computer Science II Zhen Jiang West Chester University
ACM/JETT Workshop - August 4-5, : Defining Classes in Java.
Draw 3 cards without replacement from a standard 52 card deck. What is the probability that: 1.They are all red ? 2.At least one is black ? 3.They are.
SEEM Java – Basic Introduction, Classes and Objects.
Bank Account Example public class BankAccount { private double balance; public static int totalAccounts = 0; public BankAccount() { balance = 0; totalAccounts++;
Wednesday –POD –I have updated grades in powerschool. If you have a zero for a lab grade, it probably means you didn’t DropItToMe. Please do so. –Slides.
Comparing ArrayLists and Arrays. ArrayLists ArrayLists are one type of flexible-size collection classes supported by Java –ArrayLists increase and decrease.
1 Classes, Encapsulation, Methods and Constructors Class definitions Scope of Data –Instance data –Local data The this Reference Encapsulation and Java.
Attribute - CIS 1068 Program Design and Abstraction Zhen Jiang CIS Dept. Temple University SERC 347, Main Campus 12/24/2016.
 2000 Prentice Hall, Inc. All rights reserved Introduction Structures –Collections of related variables (aggregates) under one name Can contain.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Java: Variables and Methods By Joshua Li Created for the allAboutJavaClasses wikispace.
Outline Anatomy of a Class Encapsulation Anatomy of a Method Graphical Objects Graphical User Interfaces Buttons and Text Fields Copyright © 2012 Pearson.
Basic Class Structure. Class vs. Object class - a template for building an object –defines the instance data that the object will hold –defines instance.
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
COMP 14 Introduction to Programming Mr. Joshua Stough March 23, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218.
2-Oct-16 Basic Object-Oriented Concepts. 2 Concept: An object has behaviors In old style programming, you had: data, which was completely passive functions,
More About Objects and Methods
Building Java Programs
Ch 4: Writing Classes Java Software Solutions Foundations of Program Design Sixth Edition by Lewis & Loftus Coming up: Classes and Objects.
Outline Writing Classes Copyright © 2012 Pearson Education, Inc.
Anatomy of a Method.
Building Java Programs
More on Classes and Objects
More About Objects and Methods
Building Java Programs
Object Oriented Programming Review
CSC 205 Java Programming II
Presentation transcript:

Classes and Objects

What is Design? The parts of the software including – what information each part holds – what things each part can do – how the various parts interact

Why do We Care About Design? As system grows larger – helps us understand the software we are about to build – helps teams develop components that will work together – helps us find where we need to work to add new features fix defects

Object Oriented Design Design of the system is structured around the objects in a system Objects are the things the system manages or uses to accomplish its goals Object == Instance

Objects An object is made of two types of things: – What it knows stored in instance variables – What it does coded as methods

How do we Build Objects? A class is template for objects of a particular type So a class must contain: – instance variables variables that are not inside a method each instance of the class gets its own copy of those instance variables to that it can hold the information it needs – methods encoding the operations the objects must be able to do

The Over Used Word Class We use the word class to mean many things: – the java code that is a template for building objects of a particular type – a non-primitive type that we are defining – the set of objects of the same type

What a Class Looks Like public class Account { private double balance; /** * Deposit a certain amount into the account depositAmount the amount to deposit */ public void deposit(double depositAmount) { balance = balance + depositAmount; } /** * Get the current balance of the account the current balance */ public double getBalance() { return balance; } Instance Variables Method to deposit money into the account Method to see how much money is in the account

Creating and Using Objects Show this in Eclipse: public class Runner { public static void main(String[] args) { Account savings; Account ira; savings = new Account(); savings.deposit(32.13); ira = new Account(); ira.deposit( ); ira.deposit(333.22); System.out.println("Savings balance: " + savings.getBalance()); System.out.println("IRA balance: " + ira.getBalance()); }

Classes in Our Lab We want to build the start of a card playing game (up through shuffling the cards) The objects we need: – 52 card objects – 1 deck object

Card Class A card needs to know two things: – its face value and its suit – well store both as ints face value: 0 = Ace, 1 = 2,... 9 = 10, 10 = Jack, 11 = Queen, 12 = King suit: 0 = spades, 1 = hearts, 2 = diamonds, 3 = clubs A card needs three operations – one to retrieve its face value – one to retrieve its suit – one to build a description of this card Find these things in the code in Eclipse

Creating a Card It doesnt make sense to have a card with no face value or suit If we dont initialize them, primitive instance variables default to zero, so every card would start out as the Ace of Spades Wed like a way to initialize the card when we create it

Constructors Special methods that are used only for the creation of objects We know a method is a constructor if – it has no return value AND – its name matches the name of the class

Our Constructor and Its Use /** * Create a new card with a given suit and value * v the face value of the card (0 - 12) s the suit of the card (0 - 3) */ public Card(int v, int s) { faceValue = v; suit = s; } Card c2; c2 = new Card(12,3); What card will this create?

CardRunner – Whats that main method? Look at CardRunner class method with this declaration: – public static void main(String[] args) This is the method that the Java Virtual Machine runs when you run your program Weve seen this method in all of our previous labs – we just didnt explain what it is!

Types of Classes We now have two types of classes – Runnable: has a main() method and can be run by the Java Virtual Machine – Instantiable: is a template for creating objects Has a constructor if we dont declare a constructor, the compiler will give us the default (no parameters) constructor that allocates the space, but doesnt initialize the instance variables – they default to zeros for the primitive types weve studies and nulls (no pointer) for reference types)

toString() We often convert objects to Strings – for example, when we concatenate them into output statements Java gives every class a method to do this – provides default We can make our own to replace the default – must be declared as: public String toString()

Converting our int to a Suit Wed like the description of a card to have Spades instead of 0 Make an array that encodes that conversion: SUIT_DESCRIPTION: String Diamonds String Spades String Clubs String Hearts

In the Code private static final String[] SUIT_DESCRIPTION = { "Spades", "Hearts", "Diamonds", "Clubs" }; SUIT_DESCRIPTION[suit] What will this evaluate to if suit has a value of 0? If suit has a value of 3?

The Deck Class Instance of deck need to know: – what cards they have and what order they are in an array of type Card Instance of deck need to do: – output the cards in order (toString()) – shuffle

Deck Constructor /** * Create the deck by filling it with the appropriate cards */ public Deck() { cards = new Card[NUMBER_OF_CARDS]; int position = 0; for (int suit = 0; suit < NUMBER_OF_SUITS; suit++) { for (int faceValue = 0; faceValue < NUMBER_OF_CARDS/NUMBER_OF_SUITS; faceValue++) { cards[position] = new Card(faceValue, suit); position++; }