Department of Computer Science

Slides:



Advertisements
Similar presentations
The Line Class Suppose you are involved in the development of a large mathematical application, and this application needs an object to represent a Line.
Advertisements

Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
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.
Introduction to Object-Oriented Programming CS 21a: Introduction to Computing I First Semester,
Chapter 3 – Implementing Classes. Chapter Goals To become familiar with the process of implementing classes To be able to implement simple methods To.
Classes. What is a class? Data and the functions (methods) that operate on that data – collectively called members –Example: bank account class Provide.
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.
Classes and Objects: Recap A typical Java program creates many objects which interact with one another by sending messages. Through the objects interactions,
The Java Programming Language  Simple – but abstract  Safe  Platform-independent ("write once, run anywhere")  Has a Rich growing library  Designed.
ECE122 Feb Introduction to Class and Object Java is an object-oriented language. Java’s view of the world is a collection of objects and their.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
1 Classes Overview l Classes as Types l Declaring Instance Variables l Implementing Methods l Constructors l Accessor and Mutator Methods.
Unit 4II 1 More about classes H Defining classes revisited H Constructors H Defining methods and passing parameters H Visibility modifiers and encapsulation.
CHAPTER 2 OBJECTS AND CLASSES Goals: To understand the concepts of classes and objects To realize the difference between objects and object references.
Enhancing classes Visibility modifiers and encapsulation revisited
1 Chapter 7 Inheritance, Polymorphism, and Scope.
Classes. What is a class? Data and the functions (methods) that operate on that data – collectively called members –Example: bank account class Provide.
Datalogi A 2: 15/9. Java Slides based on Horstmann chapter 2&3 Objects and classes Import, methods, references Implementing a class.
Classes and Objects  A typical Java program creates many objects which interact with one another by sending messages. Through the objects interactions,
Chapter 3 Implementing Classes. Chapter Goals To become familiar with the process of implementing classes To be able to implement simple methods To understand.
ACM/JETT Workshop - August 4-5, Object-Oriented Basics & Design.
COMP Classes Yi Hong May 22, Announcement  Lab 2 & 3 due today.
COM S 207 While-Loop Statement Instructor: Ying Cai Department of Computer Science Iowa State University
Programming Languages and Paradigms Object-Oriented Programming.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
Introduction to Objective-C and Xcode (Part 2) FA 175 Intro to Mobile App Development.
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals; Apply data abstraction.
Introduction to Object-Oriented Programming
Week 4 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Introduction to Java and Object-Oriented Programming AJSS Computer Camp Department of Information Systems and Computer Science Ateneo de Manila University.
Classes CS 21a: Introduction to Computing I First Semester,
Chapter 7 Objects and Classes 1 Fall 2012 CS2302: Programming Principles.
ACO 101: Introduction to Computer Science Anatomy Part 2: Methods.
Chapter 3 Implementing Classes. Assignment Read 3.1 – 3.5 and take notes complete Self Check Exercises 1-10; Due September 24 th Read 3.6 – 3.8 and take.
Methods in Java. Program Modules in Java  Java programs are written by combining new methods and classes with predefined methods in the Java Application.
Week 4 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Classes One class usually represents one type of object –May contain its own member variables –May contain its own methods to operate on the member variables.
CMSC 150 CLASSES CS 150: Mon 6 Feb 2012 Images: Library of Congress.
Classes. Student class We are tasked with creating a class for objects that store data about students. We first want to consider what is needed for the.
OOP in C++ CS 124. Program Structure C++ Program: collection of files Source (.cpp) files be compiled separately to be linked into an executable Files.
ACM/JETT Workshop - August 4-5, : Defining Classes in Java.
Chapter 3: Developing Class Methods Object-Oriented Program Development Using Java: A Class-Centered Approach.
L EC. 06: C LASS D ETAILS S PRING C ONTENT  Class method [review]  Access control  Passing arguments  Method overloading  Variable-length.
Sahar Mosleh California State University San MarcosPage 1 Classes and Objects and Methods.
Lecture 08. Since all Java program activity occurs within a class, we have been using classes since the start of this lecture series. A class is a template.
COM S 228 Introduction to Data Structures Instructor: Ying Cai Department of Computer Science Iowa State University Office: Atanasoff.
COM S 207 ArrayList Instructor: Ying Cai Department of Computer Science Iowa State University
1 Class and Object Lecture 7. 2 Classes Classes are constructs that define objects of the same type. A Java class uses instance variables to define data.
Methods.
Computer Science A 1. Course plan Introduction to programming Basic concepts of typical programming languages. Tools: compiler, editor, integrated editor,
Exam 2 EXAM 2 Thursday!!! 25% of Final Grade Know: loops, switch/case Files Input failure (e.g. scan.hasNextInt())
Chapter 3 Implementing Classes
Basic Class Structure. Class vs. Object class - a template for building an object –defines the instance data that the object will hold –defines instance.
2015 Spring Content Class method [review] Access control
Chapter 12 – Object-Oriented Design
Lecture 3 John Woodward.
Department of Computer Science
CompSci 230 Software Construction
Chapter Three - Implementing Classes
Chapter 3 Introduction to Classes, Objects Methods and Strings
Object-Oriented Programming
JAVA CLASSES.
Classes One class usually represents one type of object
Object Oriented Programming in java
AN INTRODUCTION TO OBJECTS AND CLASSES
Classes CS 21a: Introduction to Computing I
Introduction to Object-Oriented Programming
Previous Lecture: Today’s Lecture: Reading (JV):
Presentation transcript:

Department of Computer Science COM S 207 Classes and Objects Instructor: Ying Cai Department of Computer Science Iowa State University yingcai@iastate.edu

So far we have covered Memory allocation and data manipulation Primitive data types Array of primitive data types Execution control if, while-loop, for-loop, do-while-loop Program structuring methods These are essential, but not enough Experience shows that it is difficult to understand and update a program that consists of a large collection of methods

Object-Oriented Programming A programming style in which tasks are solved by collaborating objects Each object has its own set of data, together with a set of methods that act upon the data We have experienced some, e.g., String, Scanner Scanner in = new Scanner(System.in); if (in.hasNextInt()) { … } object construct a Scanner object class invoke a method of Scanner

Classes and Objects className obj = new className(…); class className { // declare instance variables type var1; type var2; ::: type varn; //declare constructor className(parameters) {} //declare methods type method1(parameters) {} type method2(parameters) {} type methodm(paramters) {} } //end className className obj = new className(…);

Definition of Class Vehicle (version1) int passengers; int fuelcap; int mpg; } public static void main(String[] args){ Vehicle minivan = new Vehicle(); Vehicel spartscar = new Vehicle(); minivan.passengers = 7; //use dot (.) to link minivan.fuelcap = 16; //the name of an object minivan.mpg = 21; //with the name of a member sportscar.passengers = 2; sportscar.fuelcap = 14; sportscar.mpg = 12; int range1 = minivan.fuelcap * minivan.mpg; int range2 = sportscar.fuelcap * sportscar.mpg; System.out.println(…);

Memory allocation minivan passenger 7 fuelcap 16 mpg 21 sportscar class Vehicle { int passengers; int fuelcap; int mpg; } public static void main(String[] args){ Vehicle minivan = new Vehicle(); Vehicel spartscar = new Vehicle(); minivan.passengers = 7; minivan.fuelcap = 16; minivan.mpg = 21; sportscar.passengers = 2; sportscar.fuelcap = 14; sportscar.mpg = 12; ::: minivan passenger 7 fuelcap 16 mpg 21 sportscar passenger 2 fuelcap 14 mpg 12 Each object has its own copy of the instant variables of the class

Adding Constructor class Vehicle { int passengers; int fuelcap; int mpg; Vehicle(int p, int f, int m) { passengers = p; fuelcap = f; mpg = m; } you can have multiple constructors, each having its own parameters

Adding Methods class Vehicle { int passengers; int fuelcap; int mpg; int range() {…}; // without parameter double fuelNeeded(int miles); // with a parameter } int range1 = minivan.range(); int range2 = sportscar.range(); double gas1 = minivan.fuelNeeded(400); double gas2 = sportscar.fuelNeeded(400);

Diagram of program structure File Class Variables Constructors Variables Statements Methods A program consists of one or more classes Typically, each class is in a separate .java file

Example 1: Counter Concept: private variables

Example 2: BankAccount Concept: static variables public class BankAccount { private int accountNumber; private String SSN; private String name; private double balance; private static lastAccountNumber; // multiple constructors BankAccount(String SSN, Name) // methods: deposit, withdraw, etc. }

Example 3: Address Implement a class Address. An address has a house number, a street, an optional apartment number, a city, a state, and a postal code. Supply two constructors: one with an apartment number, one without. Supply a print method that prints the address with the street on one line and the city, state, and zip code on the next line.

Example 4: Student Implement a class Student. A student has a name and a total quiz score. Supply an appropriate constructor. Supply the following methods: addQuiz(int score), getTotalScore(), and getAverageScore(). To compute the latter, you need to store the number of quizzes that the student took.

Example 5: Bug Implement a class Bug that models a bug moving along a horizontal line. The bug moves either to the right or left. Initially, the bug moves to the right, but it can turn to change its direction. In each move, its position changes by one unit in the current direction. Supply a constructor that gives a bug’s initial position. Supply the following methods: move(), turn(), getPosition();

Summary A class contains Code (methods) + Data (instance variables) Members of a class are accessed through the DOT (.) operator Each object has its own copy of the instance variables of the class Constructors Methods Other notions: static, public, private