Classes CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.

Slides:



Advertisements
Similar presentations
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.
Advertisements

Inheritance Polymorphism Briana B. Morrison CSE 1302C Spring 2010.
Our BankAccount class should define three methods  deposit  withdraw  getBalance How a programmer will carry out these operations?  We assume that.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Chapter Three - Implementing Classes.
Chapter 9 – Inheritance Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.
Classes. Object-Oriented Design Method for designing computer programs Consider “objects” interacting in the program –Example: a zoo, a gradebook.
ECE122 L6: Problem Definition and Implementation February 15, 2007 ECE 122 Engineering Problem Solving with Java Lecture 6 Problem Definition and Implementation.
Using Objects Object: an entity in your program that you can manipulate Attributes Methods Method: consists of a sequence of instructions that can access.
ACM/JETT Workshop - August 4-5, Object-Oriented Basics & Design.
Garbage Collection CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Programming Languages and Paradigms Object-Oriented Programming.
Writing Classes (Chapter 4)
Methods CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Locks CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
1 OOP in C#: Object Interaction. Inheritance and Polymorphism OOP in C# - Del 1.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Week 4 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Inheritance CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 2.
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!
Chapter 7 Objects and Classes 1 Fall 2012 CS2302: Programming Principles.
ACO 101: Introduction to Computer Science Anatomy Part 2: Methods.
Classes All Java code must be inside classes Class types – Utility classes Used to store constants, utility methods, etc. – Driver classes – Abstract Data.
Inheritance CSC 171 FALL 2004 LECTURE 18. READING Read Horstmann, Chapter 11.
Multithreading : synchronization. Avanced Programming 2004, Based on LYS Stefanus’s slides slide 4.2 Solving the Race Condition Problem A thread must.
Week 4 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Conditions CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
1 Announcements Note from admins: Edit.cshrc.solaris instead of.tcshrc Note from admins: Do not use delta.ece.
Chapter 4 Introduction to Classes, Objects, Methods and strings
Session 02 and 03: C# OOP 1 OOP in C#: Classes and Objects in C#. Object-Oriented Design. UML Class Diagram. Object Interaction. FEN AK IT:
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Concurrent Computing CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
ACM/JETT Workshop - August 4-5, : Defining Classes in Java.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Getting Started with Java: Object declaration and creation Primitive.
Monitors CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
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.
Semaphores CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Topic 8Classes, Objects and Methods 1 Topic 8 l Class and Method Definitions l Information Hiding and Encapsulation l Objects and Reference Classes, Objects,
Object Oriented Programming I ( ) Dr. Adel hamdan Part 03 (Week 4) Dr. Adel Hamdan Date Created: 7/10/2011.
Topics Instance variables, set and get methods Encapsulation
©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 3: An Introduction to Classes 1 Chapter 3 An Introduction to Classes.
Outline Anatomy of a Class Encapsulation Anatomy of a Method Graphical Objects Graphical User Interfaces Buttons and Text Fields Copyright © 2012 Pearson.
OOP in C# - part 1 OOP in C#: –Object Interaction. –Inheritance and Polymorphism (next module). FEN 20121UCN Technology: Computer Science.
COMPUTER SCIENCE & TECHNOLOGY DEGREE PROGRAMME FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UVA WELLASSA ‏ Properties of Object Oriented Programming.
Comp1004: Building Better Objects II Encapsulation and Constructors.
COMP Information Hiding and Encapsulation Yi Hong June 03, 2015.
Reading Parameters CSCI 201 Principles of Software Development Jeffrey Miller, Ph.D.
Principles of Software Development
Data Structures and Algorithms revision
Java OOP Overview Classes and Objects, Members and Class Definition, Access Modifier, Encapsulation Java OOP Overview SoftUni Team Technical Trainers.
Implementing Classes Yonglei Tao.
Chapter Three - Implementing Classes
Defining Your Own Classes Part 1
Encapsulation and Constructors
More on Classes and Objects
Defining Classes and Methods
CS100J Lecture 7 Previous Lecture This Lecture Java Constructs
JAVA CLASSES.
By Rajanikanth B OOP Concepts By Rajanikanth B
Defining Classes and Methods
Introduction to Object-Oriented Programming
Defining Classes and Methods
Java 1/31/2017 Back to Objects.
Classes and Objects CGS3416 Spring 2019.
Classes.
ITE “A” GROUP 2 ENCAPSULATION.
CSG2H3 Object Oriented Programming
Introduction to Computer Science and Object-Oriented Programming
Presentation transcript:

Classes CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L

Outline USC CSCI 201L2/11 ▪C▪Classes vs Objects ▪P▪Program

Object Analogy ▪To drive a car, you need the accelerator pedal, the brake pedal, the steering wheel, and the ignition (among other things) ▪But what actually happens when you turn the key in the ignition? ›Most drivers do not know, but they do not need to know to be able to drive the car ▪This is the idea behind objects ›The code that uses the object knows what the method will do, but it does not need to know how it does it USC CSCI 201L3/11 Classes vs Objects

Object vs Class ▪An object is a high-level term for the idea behind a class ›We need something to represent a “Car” ▪A class is the actual implementation of an object public class Car {... } ▪Once a class is created, you can create instances of the class (which will be variables) – this is often referred to as an object as well Car myCar = new Car(); USC CSCI 201L4/11 Classes vs Objects

Object Description ▪Objects (and in turn, classes) can be described by two key features ›Data ›Methods ▪The methods of an object operate on the data that is part of the object or data that is passed into the method (and usually on both) ›If a method of a class does not operate on data that is part of the class, why is the method part of the class? USC CSCI 201L5/11 Classes vs Objects

BankAccount Object ▪What is the data that is associated with a bank account? ›Name ›Account Number ›Balance ›PIN ▪What are the functions (methods) that can be performed on a bank account? ›Deposit ›Withdrawal ›Transfer ›Check Balance ›Change PIN USC CSCI 201L6/11 Classes vs Objects

BankAccount Class 1public class BankAccount { 2private int accountNumber; 3private int pin; 4private double balance; 5 6public void setBalance(double bal) { 7balance = bal; 8} 9public double getBalance () { 10return balance; 11} 12public void withdraw(double amount) { 13balance -= amount; 14} 15public static void main(String [] args) { 16BankAccount ba = new BankAccount(); 17ba.setBalance( ); 18System.out.println(“balance = “ + ba.getBalance()); 19ba.withdraw(100.00); 21System.out.println(“balance = “ + ba.getBalance()); 22} 23 } USC CSCI 201L7/11 Classes vs Objects

Access Modifiers ▪There are four access modifiers in Java ›public ›protected › ›private ▪If a variable or method is declared… ›public, any other piece of code can access the variable or method ›private, only the class in which that variable or method is declared can access it › (meaning that there is no identifier), any class in the same package can access it ›protected, any class declared in the same package or who inherits from the class can access it ▪Rule of Thumb – Hide the data and expose the necessary methods USC CSCI 201L8/11 Classes vs Objects

Encapsulation ▪A class is considered encapsulated if all of the data is declared private ▪A class is considered fully encapsulated if all of the data is declared private AND you provide an accessor (getter) and manipulator (setter) method for each piece of data ›Manipulators are also called mutators USC CSCI 201L9/11 Classes vs Objects

Outline USC CSCI 201L10/11 ▪C▪Classes vs Objects ▪P▪Program

Program ▪Write a program that reads data from a user that represents employee names and hourly rates. Calculate the annual salary of the employee assuming 50 weeks at 40 hours/week are worked each year. Use two classes and methods where applicable. Here is a sample execution with user input bolded: USC CSCI 201L11/11 Program c:\>java com.uscInc.Salary How many employees? 2 Please enter name of employee #1: Mary Please enter hourly rate of Mary: Please enter name of employee #2: Tony Please enter hourly rate of Tony: Mary’s annual salary is $ Tony’s annual salary is $ c:\>