CS 2530 INTERMEDIATE COMPUTING

Similar documents
CS111: PROGRAMMING LANGUAGE II. Lecture 1: Introduction to classes

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

CS1004: Intro to CS in Java, Spring 2005

CMPT 117: Tutorial 1. Craig Thompson. 12 January 2009

Assignment 1 due Monday at 11:59pm

CS 106 Introduction to Computer Science I

Objects as a programming concept

Introduction to Java. Handout-1d. cs402 - Spring

Classes. Classes as Code Libraries. Classes as Data Structures

Chapter 9 Objects and Classes. Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved.

CS 251 Intermediate Programming Methods and More

Distributed Systems Recitation 1. Tamim Jabban

Chapter 4. Defining Classes I

FINAL TERM EXAMINATION SPRING 2010 CS304- OBJECT ORIENTED PROGRAMMING

Darshan Institute of Engineering & Technology for Diploma Studies Unit 3

CS 170 Java Programming 1. Week 13: Classes, Testing, Debugging

Static Members. OOC 4 th Sem, B Div Prof. Mouna M. Naravani

Chapter 5: Classes and Objects in Depth. Introduction to methods

Programming II (CS300)

Programming II (CS300)

CS 251 Intermediate Programming Methods and Classes

Classes and Objects 3/28/2017. How can multiple methods within a Java class read and write the same variable?

CLASSES AND OBJECTS IN JAVA

Anatomy of a Class Encapsulation Anatomy of a Method


Introduction to Classes and Objects. David Greenstein Monta Vista High School

Programming II (CS300)

Object Oriented Design

Downloaded S. from Kiran, PGT (CS) KV, Malleswaram STRUCTURES. Downloaded from

Microsoft Visual Basic 2005: Reloaded

PIC 10A. Lecture 15: User Defined Classes

Chapter 4 Defining Classes I

OBJECT ORIENTED PROGRAMMING USING C++ CSCI Object Oriented Analysis and Design By Manali Torpe

An Introduction to C++

Lecture 7. Log into Linux New documents posted to course webpage

Introduction to Programming Using Java (98-388)

Classes. Classes as Code Libraries. Classes as Data Structures. Classes/Objects/Interfaces (Savitch, Various Chapters)

Programming II (CS300)

Distributed Systems Recitation 1. Tamim Jabban

Chapter 4: Writing Classes

CS 106 Introduction to Computer Science I

STUDENT LESSON A5 Designing and Using Classes

Chapter 3 Objects and Classes

Review: C++ Basic Concepts. Dr. Yingwu Zhu

Object Oriented Programming SCJ2153. Class and Object. Associate Prof. Dr. Norazah Yusof

Object Class. EX: LightSwitch Class. Basic Class Concepts: Parts. CS257 Computer Science II Kevin Sahr, PhD. Lecture 5: Writing Object Classes

Objec,ves. Review: Object-Oriented Programming. Object-oriented programming in Java. What is OO programming? Benefits?

What does it mean by information hiding? What are the advantages of it? {5 Marks}

Encapsulation. Administrative Stuff. September 12, Writing Classes. Quick review of last lecture. Classes. Classes and Objects

Abstract Data Types (ADTs) 1. Legal Values. Client Code for Rational ADT. ADT Design. CS 247: Software Engineering Principles

Thursday, February 16, More C++ and root

Introduction to Classes

3.Constructors and Destructors. Develop cpp program to implement constructor and destructor.

Lecture 02, Fall 2018 Friday September 7

Abstraction in Software Development

Object Oriented Design

CS 247: Software Engineering Principles. ADT Design

Darshan Institute of Engineering & Technology for Diploma Studies

CS250 Final Review Questions

Programming II (CS300)

Midterms Save the Dates!

Class 15. Object-Oriented Development from Structs to Classes. Laura Marik Spring 2012 C++ Course Notes (Provided by Jason Minski)

Topics. Class Basics and Benefits Creating Objects.NET Architecture and Base Class Libraries 3-2

Template. SSE2034: System Software Experiment 3, Fall 2018, Jinkyu Jeong

EECS168 Exam 3 Review

Constructor - example

Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub

Review for Midterm. Instructor: Scott Kristjanson CMPT 135 SFU Surrey, Spring 2016

Programs as Models. Procedural Paradigm. Class Methods. CS256 Computer Science I Kevin Sahr, PhD. Lecture 11: Objects

Dot and Scope Resolution Operator

More About Objects and Methods

More About Objects and Methods. Objectives. Outline. Harald Gall, Prof. Dr. Institut für Informatik Universität Zürich.

Part 3. Why do we need both of them? The object-oriented programming paradigm (OOP) Two kinds of object. Important Special Kinds of Member Function

Chapter 12: How to Create and Use Classes

An Introduction To Writing Your Own Classes CSC 123 Fall 2018 Howard Rosenthal

CS 302: Introduction to Programming in Java. Lecture 15

CS304 Object Oriented Programming Final Term

CS313D: ADVANCED PROGRAMMING LANGUAGE

Constants, References

Implementing Abstract Data Types (ADT) using Classes

More C++ : Vectors, Classes, Inheritance, Templates

Inheritance. Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L

Topic 10: Introduction to OO analysis and design

OOPs Concepts. 1. Data Hiding 2. Encapsulation 3. Abstraction 4. Is-A Relationship 5. Method Signature 6. Polymorphism 7. Constructors 8.

Objects and Classes. 1 Creating Classes and Objects. CSCI-UA 101 Objects and Classes

Operations. I Forgot 9/4/2016 COMPUTER SCIENCE DEPARTMENT PICNIC. If you forgot your IClicker, or your batteries fail during the exam

Chapter 5. Feb 08, Chapter 5 1

Kapil Sehgal PGT Computer. Science Ankleshwar Gujarat

Lecture 13 & 14. Single Dimensional Arrays. Dr. Martin O Connor CA166

Java How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved.

Friends and Overloaded Operators

Chapter 10 Introduction to Classes

More C++ : Vectors, Classes, Inheritance, Templates. with content from cplusplus.com, codeguru.com

Friends and Overloaded Operators

Classes and Objects. CGS 3416 Spring 2018

CS313D: ADVANCED PROGRAMMING LANGUAGE

CS 251 Intermediate Programming Inheritance

Defining Classes and Methods

CS360 Lecture 5 Object-Oriented Concepts

CS250 Final Review Questions

Transcription:

CS 2530 INTERMEDIATE COMPUTING Spring 2018 1-24-2018 Michael J. Holmes University of Northern Iowa Today s topic: Writing classes. 1

Die Objects A die has physical attributes: a specific Number of Sides colors (surface and pips) Material Dimensions etc I can complete certain actions with the die: Count the sides Pick it up Roll it Read the value on top etc Image Source: http://rsachi.blogspot.com/2012/11/carnatic-snakes-and-ladders.html How to define a class? There are four key parts to defining a class. Name I have to give it a name, so I know what to call the type of objects I create. Instance Variables the data/attributes the object will maintain. Constructors specialized methods that define how the object is initialized when instantiated. Class Methods the actions or behaviors that this object can complete. 2

Giving it a Name public class ClassName public class Die public class DiePanel Declaring Instance Variables accesslevel datatype variablename; private int age; private String name; All instance variables are declared private to support encapsulation. Weisfeld (page 65-66) refers to these as object attributes, these values are unique to a specific instance of a class. 3

Declaring Class Constants accesslevel static datatype variablename; private static float PI; private static int PIP_SIZE; Weisfeld (page 66-67) refers to these as class attributes, these values are shared by all instances of an class. Constructors When an new instance of a class is created, the constructor is called and executed. The constructor creates a new instance and allocates the required memory. Then the actual constructor code is executed to complete any initialization that is required. Typically the constructor is where the programmer sets any initial values of the instance variables. 4

Declaring Constructors Constructors are specialized class methods that have the same name as the class and initialize the instance variables. accesslevel classname( inputparameters ) public Human() public Human(String aname) Typically, constructors are part of the public interface of the class, so we declare them public. Default Constructor If the class does not have a constructor defined, a default constructor is provided by the compiler. Therefore, all classes have at least one constructor. It is a good idea to always write a constructor to explicitly define the initialization. It is also a good idea to always initialize all of the instance variables as well. 5

Declaring Class Methods accesslevel returntype methodname( inputparameters ) public void add(int value1, int value2) public String getname() Any method that will be available to user of your class must be declared public. The class may also contain private helper methods, to allow you to simplify your implementation. Calling Methods To access an object s method uses dot notation. objectname.methodname() Parameters must match type and order of available methods. 6

Types of Methods Accessor methods return values based on the current state of the object (read only). Die.getNumSides(); Mutator method update instance variable values. Die.setNumSides(6); Types of Methods Some methods may change the state of the object as a side effect. Die.roll(); Some methods return values, some do not. The method declaration tells you. (void OR String, Die, etc.) 7

Local Variables Implementing a class method often requires keeping track of additional values. Weisfield (page 64) refers to these as local attributes. These variables are initialized and used during the execution of the method, but are not retained or available to other parts of code. Method Overloading You can use a method name more than once (including constructors). The overloaded method, must have different method signatures to differentiate them. public int add(int value1, int value2) public float add(float value1, float value2) 8