CS Programming I: Classes

Size: px
Start display at page:

Download "CS Programming I: Classes"

Transcription

1 CS Programming I: Classes Marc Renault Department of Computer Sciences University of Wisconsin Madison Fall 2017 TopHat Sec 3 (PM) Join Code: TopHat Sec 4 (AM) Join Code:

2 Classes

3 1/23 Class Defining Classes Classes provide a way of defining new types. They are data structures that: can group member variables. can group member methods.

4 1/23 Class Defining Classes Classes provide a way of defining new types. They are data structures that: can group member variables. can group member methods. public members are accessible outside the class. private members are only accessible inside the class.

5 1/23 Class Defining Classes Classes provide a way of defining new types. They are data structures that: can group member variables. can group member methods. public members are accessible outside the class. private members are only accessible inside the class. Creating instances of classes MyClass varname = new MyClass(arg1,...) Creating instances of your own classes work just as the predefined Java classes.

6 2/23 TopHat Question 1 What is the output when executed? public class MyClass { public int a; public MyClass () { a = 5; public static void main ( String [] args ) { MyClass b = new MyClass (); b.a = 10; System. out. print (b.a);

7 3/23 TopHat Question 2 What is the output when executed? public class MyClassBis { private int a; public MyClassBis () { a = 5; public static void main ( String [] args ) { MyClassBis b = new MyClassBis (); b.a = 10; System. out. print (b.a);

8 4/23 TopHat Question 3 What is the output when UseMyClass is executed? public class UseMyClass { public static void main ( String [] args ) { MyClass b = new MyClass (); b. a = 10; System. out. print (b.a); public class MyClass { public int a; public MyClass () { a = 5;

9 5/23 TopHat Question 4 What is the output when UseMyClassBis is executed? public class UseMyClassBis { public static void main ( String [] args ) { MyClassBis b = new MyClassBis (); b. a = 10; System. out. print (b.a); public class MyClassBis { private int a; public MyClassBis () { a = 5;

10 6/23 Class Interface Class Interface Public view of the class. public methods and members that can be accessed from outside.

11 6/23 Class Interface Class Interface Public view of the class. public methods and members that can be accessed from outside. Good Practice: Keep Things private Code smell: indecent exposure. private unless reason to make public. Encapsulation is one of the fundamental principals in object-oriented programming (OOP).

12 7/23 Member Variables public class HockeyPlayer { private String name ; private int goals ; private int assists ; private int gamesplayed ; private double toi ; private double penaltymin ; public HockeyPlayer () { name = ""; initmembervars (); Fields Fields are member variables. Non-static member variables are instance variables. public HockeyPlayer ( String name ) { this. name = name ; initmembervars ();

13 7/23 Member Variables public class HockeyPlayer { private String name ; private int goals ; private int assists ; private int gamesplayed ; private double toi ; private double penaltymin ; public HockeyPlayer () { name = ""; initmembervars (); Fields Fields are member variables. Non-static member variables are instance variables. Properties Fields that can be set by those using the class. public HockeyPlayer ( String name ) { this. name = name ; initmembervars ();

14 Member Variables public class HockeyPlayer { private String name ; private int goals ; private int assists ; private int gamesplayed ; private double toi ; private double penaltymin ; public HockeyPlayer () { name = ""; initmembervars (); Fields Fields are member variables. Non-static member variables are instance variables. Properties Fields that can be set by those using the class. Default Values public HockeyPlayer ( String name ) { this. name = name ; Uninitialized member initmembervars (); variables are given default values. 7/23

15 Member Instance Methods private void initmembervars () { goals = 0; assists = 0; gamesplayed = 0; toi = 0.0; penaltymin = 0.0; Instance Methods Non-static methods whose behaviour depends on the particular object instance. public String getname () { return name ; public void setname ( String name ) { this. name = name ; public int getgoals () { return goals ; 8/23

16 Constructors public class HockeyPlayer { private String name ; private int goals ; private int assists ; private int gamesplayed ; private double toi ; private double penaltymin ; Constructor Special method with no return type. Same name as the class. Called when creating an instance. Can be overloaded. public HockeyPlayer () { name = ""; initmembervars (); public HockeyPlayer ( String name ) { this. name = name ; initmembervars (); private void initmembervars () { 9/23

17 Constructors public class HockeyPlayer { private String name ; private int goals ; private int assists ; private int gamesplayed ; private double toi ; private double penaltymin ; public HockeyPlayer () { name = ""; initmembervars (); public HockeyPlayer ( String name default ) { constructor with this. name = name ; no parameters. initmembervars (); private void initmembervars () { Constructor Special method with no return type. Same name as the class. Called when creating an instance. Can be overloaded. Default Constructor If no constructor is defined, Java provides a It is good practice to define a constructor. 9/23

18 10/23 What is this? public HockeyPlayer ( String name ) { this. name = name ; initmembervars (); public void setname ( String name ) { this. name = name ; this keyword this is a reference to the object (or the current instance). The object is an implicit parameter of all instance methods.

19 10/23 What is this? this keyword this is a reference to the object (or the current instance). The object is an implicit parameter of all instance methods. Can also be used to call other constructors: public HockeyPlayer () { name = ""; goals = 0; assists = 0; gamesplayed = 0; toi = 0.0; penaltymin = 0.0; public HockeyPlayer ( String name ) { this (); this. name = name ;

20 11/23 Mutators, Accessors, and Helpers private void initmembervars () { goals = 0; assists = 0; gamesplayed = 0; toi = 0.0; penaltymin = 0.0; public int getpoints () { return goals + assists ; public void addgoal () { goals ++; Mutators Methods that change the member variables.

21 11/23 Mutators, Accessors, and Helpers private void initmembervars () { goals = 0; assists = 0; gamesplayed = 0; toi = 0.0; penaltymin = 0.0; public int getpoints () { return goals + assists ; public void addgoal () { goals ++; Accessors Methods that access the member variables without changing them. Mutators Methods that change the member variables.

22 11/23 Mutators, Accessors, and Helpers private void initmembervars () { goals = 0; assists = 0; gamesplayed = 0; toi = 0.0; penaltymin = 0.0; public int getpoints () { return goals + assists ; public void addgoal () { goals ++; Helpers private method used within the class. Accessors Methods that access the member variables without changing them. Mutators Methods that change the member variables.

23 12/23 Getters and Setters public String getname () { return name ; Getter Accessor method that retrieves a property. public void setname ( String name ) { this. name = name ;

24 12/23 Getters and Setters public String getname () { return name ; Getter Accessor method that retrieves a property. public void setname ( String name ) { this. name = name ; Setter Mutator method to change a property.

25 Getters and Setters public String getname () { return name ; Getter Accessor method that retrieves a property. public void setname ( String name ) { this. name = name ; Setter Mutator method to change a property. Best Practice Good for properties. Violates OOP encapsulation when used for all fields. Why getter and setter methods are evil https: // why-getter-and-setter-methods-are-evil.html 12/23

26 13/23 TopHat Question 5 What is the output? public class UseHockeyPlayer { public static void main ( String [] args ) { HockeyPlayer h = new HockeyPlayer (" Austin Matthews "); System. out. print (h); a. Austin Matthews b. HockeyPlayer@5e265ba4

27 14/23 TopHat Question 6 What is the output when executed? public class SimpleClass { public int x; public SimpleClass ( int x) { this.x = x; public String tostring () { return "" + x; public static void main ( String [] args ) { SimpleClass a = new SimpleClass (10); SimpleClass b = new SimpleClass (10); b. x = 20; System. out. print (a + " " + b);

28 15/23 Static Member Variables Static Member Variables Class variables Stored in a special part of the heap. Stack Heap

29 15/23 Static Member Variables Static Member Variables Class variables Stored in a special part of the heap. Stack Heap class StaticA { public static int x; public int y; public class StaticEx { public static void main ( String [] args ){ y: 0 StaticA s = new StaticA (); s StaticA: x: 0

30 15/23 Static Member Variables Static Member Variables Class variables Stored in a special part of the heap. Stack Heap class StaticA { public static int x; public int y; public class StaticEx { public static void main ( String [] args ){ y: 0 StaticA s = new StaticA (); s. y = 1; s StaticA: x: 0

31 15/23 Static Member Variables Static Member Variables Class variables Stored in a special part of the heap. Stack Heap class StaticA { public static int x; public int y; public class StaticEx { public static void main ( String [] args ){ y: 1 StaticA s = new StaticA (); s. y = 1; s StaticA: x: 0

32 15/23 Static Member Variables Static Member Variables Class variables Stored in a special part of the heap. Stack Heap class StaticA { public static int x; public int y; public class StaticEx { public static void main ( String [] args ){ y: 1 StaticA s = new StaticA (); s. y = 1; s. x = 2; s StaticA: x: 0

33 15/23 Static Member Variables Static Member Variables Class variables Stored in a special part of the heap. Stack Heap class StaticA { public static int x; public int y; public class StaticEx { public static void main ( String [] args ){ y: 1 StaticA s = new StaticA (); s. y = 1; s. x = 2; s StaticA: x: 2

34 15/23 Static Member Variables Static Member Variables Class variables Stored in a special part of the heap. Stack t s Heap class StaticA { public static int x; public int y; public class StaticEx { public static void main ( String [] args ){ y: 1 StaticA s = new StaticA (); s. y = 1; s. x = 2; StaticA t y: 0 StaticA: x: 2 = new StaticA ();

35 15/23 Static Member Variables Static Member Variables Class variables Stored in a special part of the heap. Stack t s Heap class StaticA { public static int x; public int y; public class StaticEx { public static void main ( String [] args ){ y: 1 StaticA s = new StaticA (); s. y = 1; s. x = 2; y: 0 StaticA: x: 2 StaticA t = new StaticA (); t. x = 3;

36 15/23 Static Member Variables Static Member Variables Class variables Stored in a special part of the heap. Stack t s Heap class StaticA { public static int x; public int y; public class StaticEx { public static void main ( String [] args ){ y: 1 StaticA s = new StaticA (); s. y = 1; s. x = 2; y: 0 StaticA: x: 3 StaticA t = new StaticA (); t. x = 3;

37 15/23 Static Member Variables Static Member Variables Class variables Stored in a special part of the heap. Stack t s Heap class StaticA { public static int x; public int y; public class StaticEx { public static void main ( String [] args ){ y: 1 StaticA s = new StaticA (); s. y = 1; s. x = 2; y: 0 StaticA: x: 3 StaticA t = new StaticA (); t. x = 3; t. y = 4;

38 15/23 Static Member Variables Static Member Variables Class variables Stored in a special part of the heap. Stack t s Heap class StaticA { public static int x; public int y; public class StaticEx { public static void main ( String [] args ){ y: 1 StaticA s = new StaticA (); s. y = 1; s. x = 2; y: 4 StaticA: x: 3 StaticA t = new StaticA (); t. x = 3; t. y = 4;

39 16/23 TopHat Question 7 What is the output when executed? public class SimpleStaticClass { public static int x = 10; public String tostring () { return "" + x; public static void main ( String [] args ) { SimpleStaticClass a = new SimpleStaticClass (); SimpleStaticClass b = new SimpleStaticClass (); b. x = 20; System. out. print (a + " " + b);

40 17/23 TopHat Question 8 What is the output? public class StaticEx2 { private static int i = 0; public static int f() { return ++i; public static void main ( String [] args ) { System. out. print (f() + " " + f() + " " + f ());

41

42 18/23 Unified Modeling Language () A modeling language used in software engineering. A visual way to describe a software system.

43 18/23 Unified Modeling Language () A modeling language used in software engineering. A visual way to describe a software system. Some Diagrams Use Case Diagram Class Diagram Instance Diagram

44 18/23 Unified Modeling Language () A modeling language used in software engineering. A visual way to describe a software system. Some Diagrams Use Case Diagram Class Diagram Instance Diagram Resources Online Drawing Tool:

45 19/23 Use Case Diagram BP1 Mine Sweeper BP1 Mine Sweeper Configure size of the map Choose locations to sweep Player Choose to play again Use Case Diagram Simple graphical representation of the system. Describes how the external actors (users) interact with the system.

46 Class Diagram HockeyPlayer name: String goals: int assists: int addgoal() setname(name: String) saves: int shots: int Goalie getsavepct(): double plays for HockeyTeam * plays for 1..* 0..1 belongs to * HockeyLeague Class Diagram Describes the structure of the system at the class level. 20/23

47 Instance Diagram HockeyLeague name = NHL HockeyTeam name = Maple Leafs location = Toronto HockeyTeam wild name = Wild location = Minnesota HockeyTeam hawks name = Blackhawks location = Chicago HockeyPlayer name = Austin Matthews Goals = 13 Assists = 12 Goalie name = Frederik Andersen saves = 681 shots = 741 Object / Instance Diagram Describes the state of the system at the object level at a given instance in time. 21/23

48 22/23 Exercise Draw the use-case and class diagrams for the following Used Car Sale system. Vehicles are identified by a VIN, colour, engine type, gas type, and transmission type. Each vehicle has certificate recording the number of kilometres, date of last inspection, number of accidents. Vehicles are sub-divided into: cars, noting the number of passengers trucks, noting the weight class vans, noting the number of passenger and the weight class Clients are registered, indicating their name, address and phone number. Clients can search for and buy the used vehicles. Clients sign purchase contracts indicating the purchase date, sale price, number of payments.

49 23/23 Further Reading COMP SCI 200: Programming I zybooks.com, zybook code: WISCCOMPSCI200Fall2017 Chapter 12. Creating Classes Chapter 13. More Classes

50 Appendix References Appendix

51 Appendix References References

52 Appendix References 24/23 Image Sources I

CS Programming I: Inheritance

CS Programming I: Inheritance CS 200 - Programming I: Inheritance Marc Renault Department of Computer Sciences University of Wisconsin Madison Fall 2017 TopHat Sec 3 (PM) Join Code: 719946 TopHat Sec 4 (AM) Join Code: 891624 Inheritance

More information

CS Programming I: Arrays

CS Programming I: Arrays CS 200 - Programming I: Arrays Marc Renault Department of Computer Sciences University of Wisconsin Madison Fall 2017 TopHat Sec 3 (PM) Join Code: 719946 TopHat Sec 4 (AM) Join Code: 891624 Array Basics

More information

CS Programming I: Programming Process

CS Programming I: Programming Process CS 200 - Programming I: Programming Process Marc Renault Department of Computer Sciences University of Wisconsin Madison Fall 2017 TopHat Sec 3 (PM) Join Code: 719946 TopHat Sec 4 (AM) Join Code: 891624

More information

CS Programming I: Exceptions

CS Programming I: Exceptions CS 200 - Programming I: Marc Renault Department of Computer Sciences University of Wisconsin Madison Spring 2018 TopHat Sec 3 (AM) Join Code: 427811 TopHat Sec 4 (PM) Join Code: 165455 Command-Line Arguments

More information

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide AP Computer Science Chapter 10 Implementing and Using Classes Study Guide 1. A class that uses a given class X is called a client of X. 2. Private features of a class can be directly accessed only within

More information

CS Programming I: Exceptions

CS Programming I: Exceptions CS 200 - Programming I: Exceptions Marc Renault Department of Computer Sciences University of Wisconsin Madison Fall 2017 TopHat Sec 3 (PM) Join Code: 719946 TopHat Sec 4 (AM) Join Code: 891624 Command-Line

More information

Programming II (CS300)

Programming II (CS300) 1 Programming II (CS300) Chapter 03: Creating Classes MOUNA KACEM mouna@cs.wisc.edu Spring 2019 Creating Classes 2 Constructors and Object Initialization Static versus non-static fields/methods Encapsulation

More information

Principles of Object Oriented Programming. Lecture 4

Principles of Object Oriented Programming. Lecture 4 Principles of Object Oriented Programming Lecture 4 Object-Oriented Programming There are several concepts underlying OOP: Abstract Types (Classes) Encapsulation (or Information Hiding) Polymorphism Inheritance

More information

CS Programming I: ArrayList

CS Programming I: ArrayList CS 200 - Programming I: ArrayList Marc Renault Department of Computer Sciences University of Wisconsin Madison Spring 2018 TopHat Sec 3 (AM) Join Code: 427811 TopHat Sec 4 (PM) Join Code: 165455 ArrayLists

More information

Programming II (CS300)

Programming II (CS300) 1 Programming II (CS300) Chapter 03: Creating Classes MOUNA KACEM mouna@cs.wisc.edu Spring 2018 Creating Classes 2 Constructors and Object Initialization Static versus non-static fields/methods Encapsulation

More information

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

CS111: PROGRAMMING LANGUAGE II. Lecture 1: Introduction to classes CS111: PROGRAMMING LANGUAGE II Lecture 1: Introduction to classes Lecture Contents 2 What is a class? Encapsulation Class basics: Data Methods Objects Defining and using a class In Java 3 Java is an object-oriented

More information

CS Programming I: Branches

CS Programming I: Branches CS 200 - Programming I: Branches Marc Renault Department of Computer Sciences University of Wisconsin Madison Fall 2017 TopHat Sec 3 (PM) Join Code: 719946 TopHat Sec 4 (AM) Join Code: 891624 Boolean Statements

More information

CS Programming I: Programming Process

CS Programming I: Programming Process CS 200 - Programming I: Programming Process Marc Renault Department of Computer Sciences University of Wisconsin Madison Spring 2018 TopHat Sec 3 (AM) Join Code: 427811 TopHat Sec 4 (PM) Join Code: 165455

More information

CS Programming I: Programming Process

CS Programming I: Programming Process CS 200 - Programming I: Programming Process Marc Renault Department of Computer Sciences University of Wisconsin Madison Spring 2019 TopHat Sec 3 (AM) Join Code: 560900 TopHat Sec 4 (PM) Join Code: 751425

More information

CS Programming I: Primitives and Expressions

CS Programming I: Primitives and Expressions CS 200 - Programming I: Primitives and Expressions Marc Renault Department of Computer Sciences University of Wisconsin Madison Spring 2018 TopHat Sec 3 (AM) Join Code: 427811 TopHat Sec 4 (PM) Join Code:

More information

CS1004: Intro to CS in Java, Spring 2005

CS1004: Intro to CS in Java, Spring 2005 CS1004: Intro to CS in Java, Spring 2005 Lecture #13: Java OO cont d. Janak J Parekh janak@cs.columbia.edu Administrivia Homework due next week Problem #2 revisited Constructors, revisited Remember: a

More information

CmSc 150 Fundamentals of Computing I. Lesson 28: Introduction to Classes and Objects in Java. 1. Classes and Objects

CmSc 150 Fundamentals of Computing I. Lesson 28: Introduction to Classes and Objects in Java. 1. Classes and Objects CmSc 150 Fundamentals of Computing I Lesson 28: Introduction to Classes and Objects in Java 1. Classes and Objects True object-oriented programming is based on defining classes that represent objects with

More information

CS Programming I: Branches

CS Programming I: Branches CS 200 - Programming I: Branches Marc Renault Department of Computer Sciences University of Wisconsin Madison Fall 2018 TopHat Sec 3 (AM) Join Code: 925964 TopHat Sec 4 (PM) Join Code: 259495 Boolean Statements

More information

Classes. Classes as Code Libraries. Classes as Data Structures

Classes. Classes as Code Libraries. Classes as Data Structures Classes Classes/Objects/Interfaces (Savitch, Various Chapters) TOPICS Classes Public versus Private Static Data Static Methods Interfaces Classes are the basis of object-oriented (OO) programming. They

More information

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

Classes. Classes as Code Libraries. Classes as Data Structures. Classes/Objects/Interfaces (Savitch, Various Chapters) Classes Classes/Objects/Interfaces (Savitch, Various Chapters) TOPICS Classes Public versus Private Static Data Static Methods Interfaces Classes are the basis of object-oriented (OO) programming. They

More information

Encapsulation in Java

Encapsulation in Java Encapsulation in Java EECS2030: Advanced Object Oriented Programming Fall 2017 CHEN-WEI WANG Encapsulation (1.1) Consider the following problem: A person has a name, a weight, and a height. A person s

More information

CS Programming I: Using Objects

CS Programming I: Using Objects CS 200 - Programming I: Using Objects Marc Renault Department of Computer Sciences University of Wisconsin Madison Fall 2017 TopHat Sec 3 (PM) Join Code: 719946 TopHat Sec 4 (AM) Join Code: 891624 Binary

More information

Programming II (CS300)

Programming II (CS300) 1 Programming II (CS300) Chapter 02: Using Objects MOUNA KACEM mouna@cs.wisc.edu Fall 2018 Using Objects 2 Introduction to Object Oriented Programming Paradigm Objects and References Memory Management

More information

Day 3. COMP 1006/1406A Summer M. Jason Hinek Carleton University

Day 3. COMP 1006/1406A Summer M. Jason Hinek Carleton University Day 3 COMP 1006/1406A Summer 2016 M. Jason Hinek Carleton University today s agenda assignments 1 was due before class 2 is posted (be sure to read early!) a quick look back testing test cases for arrays

More information

Fundamentos de programação

Fundamentos de programação Fundamentos de programação Orientação a Objeto Classes, atributos e métodos Edson Moreno edson.moreno@pucrs.br http://www.inf.pucrs.br/~emoreno Contents Object-Oriented Programming Implementing a Simple

More information

Programming II (CS300)

Programming II (CS300) 1 Programming II (CS300) Chapter 05: Inheritance and Interfaces MOUNA KACEM mouna@cs.wisc.edu Fall 2018 Inheritance and Interfaces 2 Introduction Inheritance and Class Hierarchy Polymorphism Abstract Classes

More information

CSCI 1301: Introduction to Computing and Programming Summer 2018 Lab 07 Classes and Methods

CSCI 1301: Introduction to Computing and Programming Summer 2018 Lab 07 Classes and Methods Introduction This lab introduces you to additional concepts of Object Oriented Programming (OOP), arguably the dominant programming paradigm in use today. In the paradigm, a program consists of component

More information

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

Encapsulation. Administrative Stuff. September 12, Writing Classes. Quick review of last lecture. Classes. Classes and Objects Administrative Stuff September 12, 2007 HW3 is due on Friday No new HW will be out this week Next Tuesday we will have Midterm 1: Sep 18 @ 6:30 7:45pm. Location: Curtiss Hall 127 (classroom) On Monday

More information

CS Programming I: File Input / Output

CS Programming I: File Input / Output CS 200 - Programming I: File Input / Output Marc Renault Department of Computer Sciences University of Wisconsin Madison Fall 2017 TopHat Sec 3 (PM) Join Code: 719946 TopHat Sec 4 (AM) Join Code: 891624

More information

CS Programming I: Using Objects

CS Programming I: Using Objects CS 200 - Programming I: Using Objects Marc Renault Department of Computer Sciences University of Wisconsin Madison Spring 2018 TopHat Sec 3 (AM) Join Code: 427811 TopHat Sec 4 (PM) Join Code: 165455 Binary

More information

CS 302 Week 9. Jim Williams

CS 302 Week 9. Jim Williams CS 302 Week 9 Jim Williams This Week P2 Milestone 3 Lab: Instantiating Classes Lecture: Wrapper Classes More Objects (Instances) and Classes Next Week: Spring Break Will this work? Double d = new Double(10);

More information

CS1083 Week 2: Arrays, ArrayList

CS1083 Week 2: Arrays, ArrayList CS1083 Week 2: Arrays, ArrayList mostly review David Bremner 2018-01-08 Arrays (1D) Declaring and using 2D Arrays 2D Array Example ArrayList and Generics Multiple references to an array d o u b l e prices

More information

Anatomy of a Class Encapsulation Anatomy of a Method

Anatomy of a Class Encapsulation Anatomy of a Method Writing Classes Writing Classes We've been using predefined classes. Now we will learn to write our own classes to define objects Chapter 4 focuses on: class definitions instance data encapsulation and

More information

UML IB Computer Science. Content developed by Dartford Grammar School Computer Science Department

UML IB Computer Science. Content developed by Dartford Grammar School Computer Science Department UML IB Computer Science Content developed by Dartford Grammar School Computer Science Department HL Topics 1-7, D1-4 1: System design 2: Computer Organisation 3: Networks 4: Computational thinking 5: Abstract

More information

OBJECTS AND CLASSES CHAPTER. Final Draft 10/30/2011. Slides by Donald W. Smith TechNeTrain.com

OBJECTS AND CLASSES CHAPTER. Final Draft 10/30/2011. Slides by Donald W. Smith TechNeTrain.com CHAPTER 8 OBJECTS AND CLASSES Slides by Donald W. Smith TechNeTrain.com Final Draft 10/30/2011 Chapter Goals To understand the concepts of classes, objects and encapsulation To implement instance variables,

More information

Software and Programming 1

Software and Programming 1 Software and Programming 1 Week 9 Lab - Use of Classes and Inheritance 8th March 2018 SP1-Lab9-2018.ppt Tobi Brodie (Tobi@dcs.bbk.ac.uk) 1 Lab 9: Objectives Exercise 1 Student & StudentTest classes 1.

More information

Computer Science II (20073) Week 1: Review and Inheritance

Computer Science II (20073) Week 1: Review and Inheritance Computer Science II 4003-232-01 (20073) Week 1: Review and Inheritance Richard Zanibbi Rochester Institute of Technology Review of CS-I Hardware and Software Hardware Physical devices in a computer system

More information

Getter and Setter Methods

Getter and Setter Methods Example 1 namespace ConsoleApplication14 public class Student public int ID; public string Name; public int Passmark = 50; class Program static void Main(string[] args) Student c1 = new Student(); Console.WriteLine("please..enter

More information

Chapter 4 Defining Classes I

Chapter 4 Defining Classes I Chapter 4 Defining Classes I This chapter introduces the idea that students can create their own classes and therefore their own objects. Introduced is the idea of methods and instance variables as the

More information

Chapter 4: Writing Classes

Chapter 4: Writing Classes Chapter 4: Writing Classes Java Software Solutions Foundations of Program Design Sixth Edition by Lewis & Loftus Writing Classes We've been using predefined classes. Now we will learn to write our own

More information

This exam is open book. Each question is worth 3 points.

This exam is open book. Each question is worth 3 points. This exam is open book. Each question is worth 3 points. Page 1 / 15 Page 2 / 15 Page 3 / 12 Page 4 / 18 Page 5 / 15 Page 6 / 9 Page 7 / 12 Page 8 / 6 Total / 100 (maximum is 102) 1. Are you in CS101 or

More information

Lecture 3. COMP1006/1406 (the Java course) Summer M. Jason Hinek Carleton University

Lecture 3. COMP1006/1406 (the Java course) Summer M. Jason Hinek Carleton University Lecture 3 COMP1006/1406 (the Java course) Summer 2014 M. Jason Hinek Carleton University today s agenda assignments 1 (graded) & 2 3 (available now) & 4 (tomorrow) a quick look back primitive data types

More information

Programming II (CS300)

Programming II (CS300) 1 Programming II (CS300) Chapter 05: Inheritance and Interfaces MOUNA KACEM mouna@cs.wisc.edu Spring 2018 Inheritance and Interfaces 2 Introduction Inheritance and Class Hierarchy Polymorphism Abstract

More information

Object Oriented Programming

Object Oriented Programming Object Oriented Programming Objectives To review the concepts and terminology of object-oriented programming To discuss some features of objectoriented design 1-2 Review: Objects In Java and other Object-Oriented

More information

Binghamton University. CS-140 Fall Problem Solving. Creating a class from scratch

Binghamton University. CS-140 Fall Problem Solving. Creating a class from scratch Problem Solving Creating a class from scratch 1 Recipe for Writing a Class 1. Write the class boilerplate stuff 2. Declare Fields 3. Write Creator(s) 4. Write accessor methods 5. Write mutator methods

More information

Anatomy of a Method. HW3 is due Today. September 15, Midterm 1. Quick review of last lecture. Encapsulation. Encapsulation

Anatomy of a Method. HW3 is due Today. September 15, Midterm 1. Quick review of last lecture. Encapsulation. Encapsulation Anatomy of a Method September 15, 2006 HW3 is due Today ComS 207: Programming I (in Java) Iowa State University, FALL 2006 Instructor: Alexander Stoytchev Midterm 1 Next Tuesday Sep 19 @ 6:30 7:45pm. Location:

More information

CS Programming I: File Input / Output

CS Programming I: File Input / Output CS 200 - Programming I: File Input / Output Marc Renault Department of Computer Sciences University of Wisconsin Madison Spring 2018 TopHat Sec 3 (AM) Join Code: 427811 TopHat Sec 4 (PM) Join Code: 165455

More information

CSCI 1301: Introduction to Computing and Programming Spring 2019 Lab 10 Classes and Methods

CSCI 1301: Introduction to Computing and Programming Spring 2019 Lab 10 Classes and Methods Note: No Brainstorm this week. This lab gives fairly detailed instructions on how to complete the assignment. The purpose is to get more practice with OOP. Introduction This lab introduces you to additional

More information

Example: Fibonacci Numbers

Example: Fibonacci Numbers Example: Fibonacci Numbers Write a program which determines F n, the (n + 1)-th Fibonacci number. The first 10 Fibonacci numbers are 0, 1, 1, 2, 3, 5, 8, 13, 21, and 34. The sequence of Fibonacci numbers

More information

Object Oriented Programming

Object Oriented Programming Object Oriented Programming Objectives To review the concepts and terminology of object-oriented programming To discuss some features of objectoriented design 1-2 Review: Objects In Java and other Object-Oriented

More information

Objects as a programming concept

Objects as a programming concept Objects as a programming concept IB Computer Science Content developed by Dartford Grammar School Computer Science Department HL Topics 1-7, D1-4 1: System design 2: Computer Organisation 3: Networks 4:

More information

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

Objects and Classes. 1 Creating Classes and Objects. CSCI-UA 101 Objects and Classes Based on Introduction to Java Programming, Y. Daniel Liang, Brief Version, 10/E 1 Creating Classes and Objects Classes give us a way of defining custom data types and associating data with operations on

More information

CLASSES AND OBJECTS IN JAVA

CLASSES AND OBJECTS IN JAVA Lesson 8 CLASSES AND OBJECTS IN JAVA (1) Which of the following defines attributes and methods? (a) Class (b) Object (c) Function (d) Variable (2) Which of the following keyword is used to declare Class

More information

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

Classes and Objects 3/28/2017. How can multiple methods within a Java class read and write the same variable? Peer Instruction 8 Classes and Objects How can multiple methods within a Java class read and write the same variable? A. Allow one method to reference a local variable of the other B. Declare a variable

More information

Chapter 7. Inheritance

Chapter 7. Inheritance Chapter 7 Inheritance Introduction to Inheritance Inheritance is one of the main techniques of objectoriented programming (OOP) Using this technique, a very general form of a class is first defined and

More information

Chapter 4. Defining Classes I

Chapter 4. Defining Classes I Chapter 4 Defining Classes I Introduction Classes are the most important language feature that make object-oriented programming (OOP) possible Programming in Java consists of defining a number of classes

More information

Static, Final & Memory Management

Static, Final & Memory Management Static, Final & Memory Management The static keyword What if you want to have only one piece of storage regardless of how many objects are created or even no objects are created? What if you need a method

More information

Introduction to OO Concepts

Introduction to OO Concepts Introduction to OO Concepts Written by John Bell for CS 342, Fall 2018 Based on chapters 1, 2, and 10 of The Object-Oriented Thought Process by Matt Weisfeld, with additional material from UML Distilled

More information

class declaration Designing Classes part 2 Getting to know classes so far Review Next: 3/18/13 Driver classes:

class declaration Designing Classes part 2 Getting to know classes so far Review Next: 3/18/13 Driver classes: Designing Classes part 2 CSC 1051 Data Structures and Algorithms I Getting to know classes so far Predefined classes from the Java API. Defining classes of our own: Driver classes: Account Transactions

More information

Notes on Chapter Three

Notes on Chapter Three Notes on Chapter Three Methods 1. A Method is a named block of code that can be executed by using the method name. When the code in the method has completed it will return to the place it was called in

More information

Distributed Systems Recitation 1. Tamim Jabban

Distributed Systems Recitation 1. Tamim Jabban 15-440 Distributed Systems Recitation 1 Tamim Jabban Office Hours Office 1004 Sunday, Tuesday: 9:30-11:59 AM Appointment: send an e-mail Open door policy Java: Object Oriented Programming A programming

More information

Java Object Oriented Design. CSC207 Fall 2014

Java Object Oriented Design. CSC207 Fall 2014 Java Object Oriented Design CSC207 Fall 2014 Design Problem Design an application where the user can draw different shapes Lines Circles Rectangles Just high level design, don t write any detailed code

More information

Software and Programming 1

Software and Programming 1 Software and Programming 1 Lab 7: Construction of a Simulated Cash Register and a Student Class 28 February 2019 SP1-Lab7-2018-19.ppt Tobi Brodie (Tobi@dcs.bbk.ac.uk) 1 Coursework Plagiarism Plagiarism

More information

ITI Introduction to Computing II

ITI Introduction to Computing II ITI 1121. Introduction to Computing II Marcel Turcotte School of Electrical Engineering and Computer Science Inheritance Introduction Generalization/specialization Version of January 20, 2014 Abstract

More information

Comp 248 Introduction to Programming Chapter 4 - Defining Classes Part A

Comp 248 Introduction to Programming Chapter 4 - Defining Classes Part A Comp 248 Introduction to Programming Chapter 4 - Defining Classes Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia University, Montreal, Canada These slides has been

More information

A First Object. We still have another problem. How can we actually make use of the class s data?

A First Object. We still have another problem. How can we actually make use of the class s data? A First Object // a very basic C++ object class Person public: Person(string name, int age); private: string name; int age; We still have another problem. How can we actually make use of the class s data?

More information

Creating Your Own Classes

Creating Your Own Classes Creating Your Own Classes 1 Objectives At the end of the lesson, the student should be able to: Create their own classes Declare properties (fields) and methods for their classes Use the this reference

More information

Lab Exercise#1: In Lab#1 You have written the class called Author which is designed as shown in the class diagram

Lab Exercise#1: In Lab#1 You have written the class called Author which is designed as shown in the class diagram ECOM 2124 Computer Programming II Lab. Instructor: Ruba A. Salamah Lab#2 Objectives: 1. To Practice how to define classes and create objects 2. Practice the class relationships (composition) Lab Exercise#1:

More information

Object Oriented Programming is a programming method that combines: Advantage of Object Oriented Programming

Object Oriented Programming is a programming method that combines: Advantage of Object Oriented Programming Overview of OOP Object Oriented Programming is a programming method that combines: a) Data b) Instructions for processing that data into a self-sufficient object that can be used within a program or in

More information

Objects and State. COMP1400 Week 9. Wednesday, 19 September 12

Objects and State. COMP1400 Week 9. Wednesday, 19 September 12 Objects and State COMP1400 Week 9 Mutator methods The internal state of an object can change. We do this by changing the values contained in its fields. Methods that change an object's state are called

More information

An Introduction to C++

An Introduction to C++ An Introduction to C++ Introduction to C++ C++ classes C++ class details To create a complex type in C In the.h file Define structs to store data Declare function prototypes The.h file serves as the interface

More information

Software and Programming 1

Software and Programming 1 Software and Programming 1 Lab 7: Construction of a Simulated Cash Register and a Student Class 22 February 2018 SP1-Lab7-2018.ppt Tobi Brodie (Tobi@dcs.bbk.ac.uk) 1 Coursework Plagiarism Plagiarism is

More information

Making New instances of Classes

Making New instances of Classes Making New instances of Classes NOTE: revised from previous version of Lecture04 New Operator Classes are user defined datatypes in OOP languages How do we make instances of these new datatypes? Using

More information

Object-Oriented Programming (OOP) Basics. CSCI 161 Introduction to Programming I

Object-Oriented Programming (OOP) Basics. CSCI 161 Introduction to Programming I Object-Oriented Programming (OOP) Basics CSCI 161 Introduction to Programming I Overview Chapter 8 in the textbook Building Java Programs, by Reges & Stepp. Review of OOP History and Terms Discussion of

More information

Recitation 3 Class and Objects

Recitation 3 Class and Objects 1.00/1.001 Introduction to Computers and Engineering Problem Solving Recitation 3 Class and Objects Spring 2012 1 Scope One method cannot see variables in another; Variables created inside a block: { exist

More information

2. [20] Suppose we start declaring a Rectangle class as follows:

2. [20] Suppose we start declaring a Rectangle class as follows: 1. [8] Create declarations for each of the following. You do not need to provide any constructors or method definitions. (a) The instance variables of a class to hold information on a Minesweeper cell:

More information

System.out.print(); Scanner.nextLine(); String.compareTo();

System.out.print(); Scanner.nextLine(); String.compareTo(); System.out.print(); Scanner.nextLine(); String.compareTo(); Starting Out with Java: From Control Structures Through Objects Sixth Edition Chapter 6 A First Look at Classes Chapter Topics 6.1 Objects and

More information

Practice problem on defining and using Class types. Part 4.

Practice problem on defining and using Class types. Part 4. CS180 Programming Fundamentals Practice problem on defining and using Class types. Part 4. Implementing object associations: Applications typically consist of collections of objects of different related

More information

Distributed Systems Recitation 1. Tamim Jabban

Distributed Systems Recitation 1. Tamim Jabban 15-440 Distributed Systems Recitation 1 Tamim Jabban Office Hours Office 1004 Tuesday: 9:30-11:59 AM Thursday: 10:30-11:59 AM Appointment: send an e-mail Open door policy Java: Object Oriented Programming

More information

COMP 250: Java Object Oriented Programming

COMP 250: Java Object Oriented Programming COMP 250: Java Object Oriented Programming January 22-23, 2018 Carlos G. Oliver Slides adapted from M. Blanchette 2 Objects behave according to their kind (Type) Let us turn to Genesis 1:25. "And God made

More information

CREATED BY: Muhammad Bilal Arslan Ahmad Shaad. JAVA Chapter No 5. Instructor: Muhammad Naveed

CREATED BY: Muhammad Bilal Arslan Ahmad Shaad. JAVA Chapter No 5. Instructor: Muhammad Naveed CREATED BY: Muhammad Bilal Arslan Ahmad Shaad JAVA Chapter No 5 Instructor: Muhammad Naveed Muhammad Bilal Arslan Ahmad Shaad Chapter No 5 Object Oriented Programming Q: Explain subclass and inheritance?

More information

INTRODUCTION TO OBJECT ORIENTED PROGRAMMING (OOP)

INTRODUCTION TO OBJECT ORIENTED PROGRAMMING (OOP) INTRODUCTION TO OBJECT ORIENTED PROGRAMMING (OOP) CS302 Introduction to Programming University of Wisconsin Madison Lecture 18 By Matthew Bernstein matthewb@cs.wisc.edu Purpose of Object Oriented Programming

More information

ITI Introduction to Computing II

ITI Introduction to Computing II ITI 1121. Introduction to Computing II Marcel Turcotte School of Electrical Engineering and Computer Science Inheritance Introduction Generalization/specialization Version of January 21, 2013 Abstract

More information

OpenStax-CNX module: m Java3002r Review * R.G. (Dick) Baldwin

OpenStax-CNX module: m Java3002r Review * R.G. (Dick) Baldwin OpenStax-CNX module: m45762 1 Java3002r Review * R.G. (Dick) Baldwin This work is produced by OpenStax-CNX and licensed under the Creative Commons Attribution License 4.0 Abstract This module contains

More information

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

Inheritance. Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L Inheritance Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 9.4 1 Inheritance Inheritance allows a software developer to derive

More information

CSCI 136 Data Structures & Advanced Programming. Lecture 3 Fall 2017 Instructors: Bill & Bill

CSCI 136 Data Structures & Advanced Programming. Lecture 3 Fall 2017 Instructors: Bill & Bill CSCI 136 Data Structures & Advanced Programming Lecture 3 Fall 2017 Instructors: Bill & Bill Administrative Details Lab today in TCL 216 (217a is available, too) Lab is due by 11pm Sunday Copy your folder

More information

Week 3 Classes and Objects

Week 3 Classes and Objects Week 3 Classes and Objects written by Alexandros Evangelidis, adapted from J. Gardiner et al. 13 October 2015 1 Last Week Last week, we looked at some of the different types available in Java, and the

More information

Recap of OO concepts. Objects, classes, methods and more. Mairead Meagher Dr. Siobhán Drohan. Produced by:

Recap of OO concepts. Objects, classes, methods and more. Mairead Meagher Dr. Siobhán Drohan. Produced by: Recap of OO concepts Objects, classes, methods and more. Produced by: Mairead Meagher Dr. Siobhán Drohan Department of Computing and Mathematics http://www.wit.ie/ Classes and Objects A class defines a

More information

CS 251 Intermediate Programming Methods and Classes

CS 251 Intermediate Programming Methods and Classes CS 251 Intermediate Programming Methods and Classes Brooke Chenoweth University of New Mexico Fall 2018 Methods An operation that can be performed on an object Has return type and parameters Method with

More information

ECE 122. Engineering Problem Solving with Java

ECE 122. Engineering Problem Solving with Java ECE 122 Engineering Problem Solving with Java Lecture 6 Problem Definition and Implementation Outline Problem: Create, read in and print out four sets of student grades Setting up the problem Breaking

More information

CSE115 Introduction to Computer Science I Coding Exercise #7 Retrospective Fall 2017

CSE115 Introduction to Computer Science I Coding Exercise #7 Retrospective Fall 2017 This week the main activity was a quiz activity, with a structure similar to our Friday lecture activities. The retrospective for the quiz is in Quiz-07- retrospective.pdf This retrospective explores the

More information

8.1 Inheritance. 8.1 Class Diagram for Words. 8.1 Words.java. 8.1 Book.java 1/24/14

8.1 Inheritance. 8.1 Class Diagram for Words. 8.1 Words.java. 8.1 Book.java 1/24/14 8.1 Inheritance superclass 8.1 Class Diagram for Words! Inheritance is a fundamental technique used to create and organize reusable classes! The child is- a more specific version of parent! The child inherits

More information

CS Week 13. Jim Williams, PhD

CS Week 13. Jim Williams, PhD CS 200 - Week 13 Jim Williams, PhD This Week 1. Team Lab: Instantiable Class 2. BP2 Strategy 3. Lecture: Classes as templates BP2 Strategy 1. M1: 2 of 3 milestone tests didn't require reading a file. 2.

More information

CS 2530 INTERMEDIATE COMPUTING

CS 2530 INTERMEDIATE COMPUTING 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

More information

DM503 Programming B. Peter Schneider-Kamp.

DM503 Programming B. Peter Schneider-Kamp. DM503 Programming B Peter Schneider-Kamp petersk@imada.sdu.dk! http://imada.sdu.dk/~petersk/dm503/! TYPE CASTS & FILES & EXCEPTION HANDLING 2 Type Conversion Java uses type casts for converting values

More information

Ticket Machine Project(s)

Ticket Machine Project(s) Ticket Machine Project(s) Understanding the basic contents of classes Produced by: Dr. Siobhán Drohan (based on Chapter 2, Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes,

More information

Handout 7. Defining Classes part 1. Instance variables and instance methods.

Handout 7. Defining Classes part 1. Instance variables and instance methods. Handout 7 CS180 Programming Fundamentals Spring 15 Page 1 of 8 Handout 7 Defining Classes part 1. Instance variables and instance methods. In Object Oriented programming, applications are comprised from

More information

Object-Oriented Programming in Java

Object-Oriented Programming in Java Software and Programming I Object-Oriented Programming in Java Roman Kontchakov / Carsten Fuhs Birkbeck, University of London Outline Object-Oriented Programming Public Interface of a Class Instance Variables

More information

COMP Information Hiding and Encapsulation. Yi Hong June 03, 2015

COMP Information Hiding and Encapsulation. Yi Hong June 03, 2015 COMP 110-001 Information Hiding and Encapsulation Yi Hong June 03, 2015 Review of Pass-By-Value What is the output? public void swap(student s1, Student s2) Student s3 = s1; s1 = s2; s2 = s3; Student berkeley

More information

CSC 1214: Object-Oriented Programming

CSC 1214: Object-Oriented Programming CSC 1214: Object-Oriented Programming J. Kizito Makerere University e-mail: jkizito@cis.mak.ac.ug www: http://serval.ug/~jona materials: http://serval.ug/~jona/materials/csc1214 e-learning environment:

More information