User Defined Classes. CS 180 Sunil Prabhakar Department of Computer Science Purdue University

Similar documents
User Defined Classes Part 2. CS 180 Sunil Prabhakar Department of Computer Science Purdue University

Chapter 4. Defining Your Own Classes Part 1. CS 180 Sunil Prabhakar Department of Computer Science Purdue University

CSCI 1370 APRIL 26, 2017

Chapter 4 Defining Classes I

Recitation 09/12/2007. CS 180 Department of Computer Science, Purdue University

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

Chapter 4. Defining Classes I

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

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

Chapter 6 Introduction to Defining Classes

CS 103 Unit 10 Slides

Programming II (CS300)

EECS168 Exam 3 Review

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

COMP-202: Foundations of Programming. Lecture 9: Classes and Objects Sandeep Manjanna, Summer 2015

Methods. Methods. Mysteries Revealed

Tutorial 6 CSC 201. Java Programming Concepts مبادئ الربجمة باستخدام اجلافا

CIS3023: Programming Fundamentals for CIS Majors II Summer 2010

CS 103 Unit 10 Slides

Java: Classes. An instance of a class is an object based on the class. Creation of an instance from a class is called instantiation.

Methods and Data (Savitch, Chapter 5)

ECOM 2324 COMPUTER PROGRAMMING II

CS101: Fundamentals of Computer Programming. Dr. Tejada www-bcf.usc.edu/~stejada Week 8: Abstract Data Types

COMSC-051 Java Programming Part 1. Part-Time Instructor: Joenil Mistal

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

CS162 Computer Science I Fall 2018 Practice Exam 1 DRAFT (9 Oct.)

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

Announcements. Final exam. Course evaluations. Saturday Dec 15 10:20 am -- 12:20 pm Room: TBA

Static, Final & Memory Management

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

CS 231 Data Structures and Algorithms, Fall 2016

Programming II (CS300)

Objects as a programming concept

Week 11: Class Design

Programming II (CS300)

CS/ENGRD 2110 FALL Lecture 5: Local vars; Inside-out rule; constructors

OO Programming Concepts. Classes. Objects. Chapter 8 User-Defined Classes and ADTs

Lecture 5: Methods CS2301

Designing Classes. Appendix D. Slides by Steve Armstrong LeTourneau University Longview, TX 2007, Prentice Hall

Chapter 13. Inheritance and Polymorphism. CS 180 Sunil Prabhakar Department of Computer Science Purdue University

CS 161: Object Oriented Problem Solving

MIDTERM REVIEW. midterminformation.htm

11/19/2014. Objects. Chapter 4: Writing Classes. Classes. Writing Classes. Java Software Solutions for AP* Computer Science A 2nd Edition

Reviewing for the Midterm Covers chapters 1 to 5, 7 to 9. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

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

Topic 10: Introduction to OO analysis and design

CS 161: Object Oriented Problem Solving

Getter and Setter Methods

Defining Classes and Methods

CS121: Computer Programming I

This Week s Agenda (Part A) CS121: Computer Programming I. Changing Between Loops. Things to do in-between Classes. Answer. Combining Statements

12/22/11. Java How to Program, 9/e. public must be stored in a file that has the same name as the class and ends with the.java file-name extension.

Introduction to Programming Using Java (98-388)

CS111: PROGRAMMING LANGUAGE II

Chapter 9 Objects and Classes. OO Programming Concepts. Classes. Objects. Motivations. Objectives. CS1: Java Programming Colorado State University

CMSC 132: Object-Oriented Programming II. Inheritance

Functions. Lab 4. Introduction: A function : is a collection of statements that are grouped together to perform an operation.

Announcements. PS 3 is due Thursday, 10/6. Midterm Exam 1: 10/14 (Fri), 9:00am-10:53am

CS 177 Spring 2010 Exam I

Survey #2. Variable Scope. University of British Columbia CPSC 111, Intro to Computation Alan J. Hu. Readings. Scope Static.

Introduction to Java

Fall CS 101: Test 2 Name UVA ID. Grading. Page 1 / 4. Page3 / 20. Page 4 / 13. Page 5 / 10. Page 6 / 26. Page 7 / 17.

CS 302 Week 9. Jim Williams

Survey #2. Teen Talk Barbie TM Reloaded. University of British Columbia CPSC 111, Intro to Computation Alan J. Hu Partially Filled Arrays ArrayLists

CS/ENGRD 2110 SPRING Lecture 5: Local vars; Inside-out rule; constructors

Scope. Scope is such an important thing that we ll review what we know about scope now:

CS1004: Intro to CS in Java, Spring 2005

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

CS112 Lecture: Working with Numbers

Programming overview

Outline. Overview. Control statements. Classes and methods. history and advantage how to: program, compile and execute 8 data types 3 types of errors

COMP 202. More on OO. CONTENTS: static revisited this reference class dependencies method parameters variable scope method overloading

SPRING 13 CS 0007 FINAL EXAM V2 (Roberts) Your Name: A pt each. B pt each. C pt each. D or 2 pts each

Programming II (CS300)

ECE 122. Engineering Problem Solving with Java

CS112 Lecture: Variables, Expressions, Computation, Constants, Numeric Input-Output

CS 161: Object Oriented Problem Solving

Chapter 9 Objects and Classes. Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All rights reserved.

CISC-124. Passing Parameters. A Java method cannot change the value of any of the arguments passed to its parameters.

Defining Classes and Methods

EXAMINATION FOR THE DIPLOMA IN INFORMATION TECHNOLOGY; YEAR 2

CHAPTER 7 OBJECTS AND CLASSES

M e t h o d s a n d P a r a m e t e r s

Administration. Classes. Objects Part II. Agenda. Review: Object References. Object Aliases. CS 99 Summer 2000 Michael Clarkson Lecture 7

JAVA PROGRAMMING LAB. ABSTRACT In this Lab you will learn to define and invoke void and return java methods

Table of Contents Date(s) Title/Topic Page #s. Chapter 4: Writing Classes 4.1 Objects Revisited

Encapsulation. You can take one of two views of an object: internal - the structure of its data, the algorithms used by its methods

Chapter 8. Exception Handling. CS 180 Sunil Prabhakar Department of Computer Science Purdue University

OOP++ CSE219, Computer Science III Stony Brook University

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

UNDERSTANDING CLASS DEFINITIONS CITS1001

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

CS 101 Exam 2 Spring Id Name

CS 251 Intermediate Programming Methods and Classes

CS 251 Intermediate Programming Methods and More

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

1B1b Classes in Java Part I

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

Java Bytecode (binary file)

Anatomy of a Class Encapsulation Anatomy of a Method

Transcription:

User Defined Classes CS 180 Sunil Prabhakar Department of Computer Science Purdue University

Announcements Register for Piazza. Deleted post accidentally -- sorry. Direct Project questions to responsible TA if sending by email. better to use Piazza. If you are not seeing the right attendance on Blackboard send me email with your iclicker ID and name. 2

Objectives This week we will study how to create your own classes including: Create objects from user defined classes Add data members and methods to classes Argument passing for methods in Java Scope of variables

Problem Create a program that reads in details about your course staff and then prints them out in a neat table. Instructor (me), Course Coordinator, and TA. For each individual, we need to record the following (String) values: First Name, Last Name Email Office 4

Sample output Instructor: Sunil Prabhakar Office: LWSN 2142C Email: sunil@cs.purdue.edu Course Coordinator: Lorenzo Martino Office: HAAS 144 Email: lmartino@purdue.edu Teaching Assistant: Daniel Thornburg Office: LWSN B159 Email: dthorb@purdue.edu 5

Solution Since we are creating multiple versions of the same data and performing the same operations, it would be very helpful to have a class for saving each person's data and printing it out neatly. No such standard class exists. We will create one: CS180Staff Our program will also be a separate class that will use this CS180Staff class. this will be our controller class: CourseStaff 6

The CS180Staff class We want each object of this class to store the data for a single person: these are data members of each object firstname, lastname, email, office input the values for each person this is a behavior for which we define a method readdetails(); print out the details of the person neatly this is a behavior for which we define a method printneatly(); 7

CS180Staff class import javax.swing.*; public class CS180Staff { private String firstname, lastname, email, office; void getdetails(){ firstname = JOptionPane.showInputDialog(null, "Enter First Name:"); lastname = JOptionPane.showInputDialog(null, "Enter Last Name:"); email = JOptionPane.showInputDialog(null, "Enter email:"); office = JOptionPane.showInputDialog(null, "Enter office:"); void printneatly(){ System.out.println(" System.out.println(" System.out.println(" " + firstname + " " + lastname); Email: " + email); Office: " + office); 8

Controller class: CourseStaff public class CourseStaff { public static void main(string[] args){ CS180Staff instructor, coordinator,ta; instructor = new CS180Staff(); instructor.getdetails(); coordinator = new CS180Staff(); coordinator.getdetails(); ta = new CS180Staff(); ta.getdetails(); System.out.println("Instructor:"); instructor.printneatly(); System.out.println("Coordinator:"); coordinator.printneatly(); System.out.println("Teaching Assistant:"); ta.printneatly(); 9

CS180Staff class import javax.swing.*; public class CS180Staff { Data Members declared outside any method Methods. private String firstname, lastname, email, office; void getdetails(){ firstname = JOptionPane.showInputDialog(null, "Enter First Name:"); lastname = JOptionPane.showInputDialog(null, "Enter Last Name:"); email = JOptionPane.showInputDialog(null, "Enter email:"); office = JOptionPane.showInputDialog(null, "Enter office:"); void printneatly(){ System.out.println(" System.out.println(" System.out.println(" " + firstname + " " + lastname); Email: " + email); Office: " + office); 10

Files and Classes public class CourseStaff { public static void main(string [] args){... File: CourseStaff.java public class CS180Staff {... File: CS180Staff.java There are two source files. Each class definition is stored in a separate file. javac CourseStaff.java javac CS180Staff.java To run, both classes need to be compiled. 0101110010101... main File: CourseStaff.class 11100101111010... File: CS180Staff.class java CourseStaff CS180Staff.class needs to be found. 11

Adding Data Members Data members are declared outside any method. Typically before any method. private is a modifier. We will understand it later. It is an alternative to public which we have seen before. private String firstname; this is optional -- if left out, it is assumed to be public Each object of this class will get its own copy of each data member. 12

Methods Note that we have created two methods for the CS180Staff class. Each corresponds to a well-defined piece of work. Makes it easy to use methods. Similarly, the Math class methods all performed a well-defined operation. Any number of methods can be defined for a class. Method names should give clues to their function. Typically verbs (e.g, printdetails()). 13

Adding methods Methods for a class are defined as follows: public void dosomething(int code){... Return type. The name of the method is preceded by a type indicating the type of value returned by this method when it is done. May be: a primitive type, a Class, or void -- indicating that nothing is returned. A method may take arguments (e.g., main) 14

Execution flow Our programs begin execution at the first statement in the main method. Statement are executed in order. When a method is called, the execution moves to the first line of that method each statement is executed in order, until we get a return statement, or the end of the method then control returns back to the caller. 15

Example flow class Test { public static void main (String[] args) { println( inside main ); methoda(); println( back from methoda ); public void methoda () { println( inside methoda ); methodb(); println( back from methodb ); public void methodb () { println( inside methodb ); 16

Sample Execution Flow class Test { public static void main (String[] args) { println( inside main ); methoda(); println( back from methoda ); public void methoda () { println( inside methoda ); methodb(); println( back from methodb ); public void methodb () { println( inside methodb ); 17

What is happening? CS180Staff instructor, coordinator; instructor coordinator instructor = new CS180Staff(); instructor.getdetails(); : CS180Staff firstname lastname email office 18

What is happening? CS180Staff instructor, coordinator; instructor coordinator instructor = new CS180Staff(); instructor.getdetails(); : CS180Staff firstname lastname email office Sunil Prabhakar sunil@cs LWSN 2142C void getdetails(){ firstname = JOptionPane.showInputDialog (null, "Enter First Name:"); lastname = JOptionPane.showInputDialog (null, "Enter Last Name:"); email = JOptionPane.showInputDialog (null, "Enter Email:"); office = JOptionPane.showInputDialog (null, "Enter Office:"); 19

What is happening? CS180Staff instructor, coordinator; instructor firstname : CS180Staff coordinator Sunil instructor = new CS180Staff(); instructor.getdetails(); coordinator = new CS180Staff(); coordinator.getdetails(); : CS180Staff lastname Prabhakar firstname Lorenzo email sunil@cs lastname Martino office LWSN 2142C email lmartino@cs office HAAS 144 20

Important point Calling new CS180Staff creates a new object of this class with its own copy of all data members. When a method is called on such an object it modifies only that object's copies of the data members (e.g., firstname). Thus instructor.getdetails() causes only the data members of the object referenced by instructor to be affected, not other CS180Staff objects. 21

Problem: Course Participants Create a program to input course participants info for Staff (as before), and 2 students, then print it out neatly. Each student has a name, gpa (double), and is identified by her ID (String) a student object must always have a valid ID. We should be able to read and change the name and gpa of the student at any time, but not the ID. 22

The Student class class Student { private String name, id; private double gpa; public void getdetails(){ name = JOptionPane.showInputDialog(null, "Enter Name:"); id = JOptionPane.showInputDialog(null, "Enter ID:"); gpa = 0.0; public void printneatly(){ System.out.println(" " + name); System.out.println(" ID: " + id); System.out.println(" GPA: " + gpa); public void setname(string studentname){ name = studentname; public String getname(){ return name; // CONTINUED... //... public String getid(){ return id; public double getgpa(){ return gpa; public void setgpa(double g){ gpa = g; 23

Controller class: CourseParticipants public class CourseParticipants { public static void main(string[] args){ CS180Staff instructor, coordinator,ta; Student student1, student2; instructor = new CS180Staff(); instructor.getdetails();... student1 = new Student(); student1.getdetails(); student2 = new Student(); student2.getdetails();... student1.printneatly(); student2.printneatly(); Note: same method names but different behavior for CS180Staff and Student objects. 24

Files and Classes public class CourseParitcipants { public static void main(string [] args){... There are three source files. Each class definition is stored in a separate file. File: CourseParticipants.java javac public class CS180Staff {... File: CS180Staff.java public class Student {... File: Student.java javac 11100101111010... File: Student.class javac To run, all classes need to be compiled. 0101110010101... main File: CourseStaff.class 11100101111010... File: CS180Staff.class java CourseParticipants 25

Initialization class Student { private String name = "Unknown", id = "?"; private double gpa= 0.0; Works for name and gpa, but is misleading. Can set it later. What about ID -- we won't be able to change it. Need an id at object creation time. For this we use a special method called a Constructor -- this is called when an object is created using the new keyword. 26

A Constructor public Student ( ) {... A constructor is a method defined within the class (like any other method). it may or may not take any arguments However, it should always be a public method it has no return type its name is the same as the class name It is not necessary to define a constructor -- in this case the compiler creates a default constructor. 27

StudentV2 (with a Constructor) class StudentV2 { private String name, id; private double gpa; public StudentV2(String studentid){ id = studentid; name = "Unknown"; gpa = 0.0;... 28

Using StudentV2 Objects public class TestClass { public static void main(string[] args){ StudentV2 student; student = new StudentV2("3478734"); student.printneatly(); Since the constructor for Student expects a String argument, we can no longer create student objects without providing this argument. Now a student object will have the given ID when created. 29

Constructors Each class must have a constructor if we are to create objects of the class. A constructor without arguments is called a default constructor. If a class does not define any constructors then a default constructor is automatically provided by the compiler. If any constructors are defined in the class, no default constructor will be provided. 30

Multiple Constructors What if we want to optionally allow objects to have an ID and a name at initialization? We can define two versions of constructors. class StudentV3 {... public StudentV3(String studentid){ id = studentid; name = "Unknown"; gpa = 0.0; public StudentV3(String studentid, String sname){ id = studentid; name = sname; gpa = 0.0;... 31

Multiple Constructors Which one is executed to create a new object? Depends upon how many (and what types of) arguments are passed to it. More later. class Test { public static void main(string[] args){ StudentV3 student; student = new StudentV3(); This version not available now! student = new StudentV3("677632"); student = new StudentV3("7658478", "John Doe"); 32

Return values If the return type of a method is void, it returns nothing. Otherwise, it must end with a return statement: return <expression>; The expression may be an identifier, an expression, or a literal. The type of the expression must be compatible with the return type of the method. A return causes the method to return to the caller. 33

Problem Extend the student class so that it recomputes the gpa when grades are reported. The class will not keep track of grades -- only the gpa. recordgrade(); will be called to ask the Student object to record a grade. It should prompt for the number of credits for the course, and the grade expressed as an integer (4 for A, 3 for B, etc ) and then recompute the GPA. 34

The StudentV4 Class class StudentV4 {... private int totalcredits; public StudentV4(String studentid, String sname){... totalcredits = 0; public void recordgrade(){ int grade, credit; credit = Integer.parseInt(JOptionPane.showInputDialog(null, "Enter Number of Credits"); grade = Integer.parseInt(JOptionPane.showInputDialog(null, "Enter Grade (as integer)"); recomputegpa(credit, grade); public void recomputegpa(int newcredit, int newgrade){ double totalgradecredits; totalgradecredits = gpa * totalcredits; totalcredits += newcredit; totalgradecredits += newcredit * newgrade; gpa = totalgradecredits / totalcredits;... 35

Using StudentV4 class TestStudentV4 { public static void main (String[] args) { StudentV4 jane; jane = new StudentV4("2342342", "Jane Doe"); jane.recordgrade(); jane.printneatly(); jane.recordgrade(); jane.printneatly(); 36

Method Arguments If a method takes arguments, every call to the method must provide values for these. The number and type of arguments expected by a method are declared by the method. Each time a method is called New storage is created for its arguments These arguments are initialized with copies of the values from the method call. The program begins execution at the beginning of the method. 37

Initializing arguments public void recordgrade(){ int grade, credit;... recomputegpa(credit, grade); grade credit 4 3 public void recomputegpa(int newcredit, int newgrade){ double totalgradecredits; totalgradecredits = gpa * totalcredits; totalcredits += newcredit; totalgradecredits += newcredit * newgrade; gpa = totalgradecredits / totalcredits;... newcredit newgrade totalgradecredits 3 4 38

Matching arguments public void methoda(){ int i, j; double x, y;... methodb(i, x); methodb(x, i); methodb(i); methodc(i, x); public void methodb(int a, double b){... public void methodc(double a, double b) {... methodc(x, i); methodd(i, x); methodd(i, x, 5); public void methodd(int a, double b, int c){... 39

Pass by value public void methoda(){ int i; i = 1; doubleup(i); System.out.println("i = " + i); i = 1 public void doubleup(int a){ a = 2 * a; System.out.println("a = " + a); a = 2 Only a copy of the value of i is passed. Changes to a do not affect the value of i. This is called Pass by value 40

Objects as arguments Methods can take object arguments as well. The class of the object in the call must be compatible with the class of the object in the method declaration. for now this means the same class As with assignment of reference types, only the reference is copied over. the object being referenced is not copied! 41

Passing Objects public void methoda(){ Student student; student student = new Student("2343", "Jane"); changegpa(student); System.out.println("Gpa = " + student.getgpa ()); Gpa = 4.0 name id : Student Jane 2343 gpa 0.0 4.0 public void changegpa(student s){ s.setgpa(4.0); s 42

Returning Objects As with arguments passed to a method, when a method returns a primitive data type, the value is returned. when a method returns a class data type, the reference to an object is returned. For example, A constructor returns the reference to the newly created object. 43

Protecting Object Data class Test { public static void main (String[] arg){ Student s = new Student("4343"); s.id = "234"; s.name = "Jane"; s.setid("234"); s.setname("jack"); class Student { public String id; public String name; public void setid(string newid){... public void setname(string newname){... System.out.println(s.getName() + s.getid()); ID can be changed from outside! public String getid(){... public String getname(){... 44

Encapsulation One of the key benefits of OOP Limit who can view/modify what data members and how avoids accidental or intentional errors Improves program reliability and reuse Achieved by hiding data members from outside the class limiting which methods can be called directly from outside the class using public and private modifiers 45

Visibility modifiers A data member or method that is declared public can be accessed by the code in any class. A private data member can only be accessed code that is part of the same class. A private method can only be called from code that is part of the same class. 46

Protecting Object Data class Test { public static void main (String[] arg){ Student s = new Student("4343"); s.id = "234"; s.name = "Jane"; s.setid("234"); s.setname("jack"); System.out.println(s.getName() + s.getid()); id and setid() are inaccessible. class Student { private String id; public String name; private void setid(string newid){... public void setname(string newname){... public String getid(){... public String getname(){... 47

Guidelines Implementation details (data members) should be private Use accessor/mutator methods Internal methods should be private Constructors are usually public Constants may be made public if useful (e.g. Math.PI) Default value is public. 48

Accessor and Mutator Methods Since most data members are usually defined to be private, it is common practice to provide methods to read and modify the values of data members. Accessor methods are methods that retrieve the value of private data members. E.g., getname(), getid() Mutator methods are methods that modify the value of private data members. E.g., setname (). 49

Identifier types Identifiers can be declared almost anywhere in a program. There are three main types of declarations: Data members of a class Declared outside any method Usually at the beginning of the class definition Formal parameters of a method Local variables inside a method 50

Identifier extent and scope Each identifier refers to a piece of memory. That piece is reserved upon declaration. The lifetime of this reservation is called the extent of the identifier. The ability to access this location from a given line of code is called scope. Important to understand both. Extent and scope depend upon the type of variable and its declaration. 51

Extent Object data members created when an object is created (by new) destroyed when the object is garbage collected (no more references to it) must be unique within each class Formal parameters created each time the method is called destroyed when the method finishes execution must be unique for each method Local variables created upon declaration destroyed at end of block must be unique for each block, Limiting extent allows compilers to reuse space 52

Which one do we mean? It is legal to reuse the name of a data member as a formal parameter, or a local variable. Each use of the identifier in a method is matched with exactly one of these as follows: A local variable, or parameter, if it exists. A data member, otherwise. Thus, a data member can be masked! Can lead to subtle errors. 53

Identifiers class Student { Data Members declared outside any method private String private String name; id; Formal parameters in method header. public void setname( String newname ){ String temp; name = newname; Local variables defined within method.... 54

Masked Data Member class Student { private String private String name; id; public setname(string name) { String temp; name = name;... Refer to formal parameter, not data member. 55

Masked Data Member 2 class Student { private String private String name; id; Refers to local variable, not data member. public setname(string newname) {... String name; name = newname; 56

Remember,. A local variable can be declared just about anywhere! Its scope (the area of code from where it is visible) is limited to the enclosing braces. Statements within a pair of braces are called a block. Local variables are destroyed when the block finishes execution. Data members of a class are declared outside any method. Their scope is determined by public and private modifiers. 57

Calling Object Methods Methods defined without the static keyword are called Object Methods. These methods can only be called on an object of the given class. class Test { public static void main() { Student s; String n; s.getid(); s = new Student("776", "Jianyu"); s.getid(); n = getname(); s does not reference a valid Student object. getname() must be called on a Student object. 58

Calling Methods of Same Class class Student { private String id; private String name; public void setname(string newname){... private void setid(string newid){ setname("id changed");... public String getid(){... No object specified. setname() will use the same object as the one on which setid() was called. 59