ITI 1120 Lab #9. Slides by: Diana Inkpen, Alan Williams, Daniel Amyot Some original material by Romelia Plesa

Similar documents
Chapter 1b Classes and Objects

(b) Draw a hash table having 10 buckets. Label the buckets 0 through 9.

(b) Draw a hash table having 10 buckets. Label the buckets 0 through 9.

Chapter 11: Create Your Own Objects

ITI Introduction to Computing II

Object-Based Programming (Deitel chapter 8)

(a) in assignment statements. (b) when invoking methods (with objects as parameters) (d) equality of references versus equality of objects

CSI 1100 / 1500 Fall 2004 Introduction to Computer Science I Final Examination

Classes and Objects: A Deeper Look

LAB 7. Objectives: Navin Sridhar D 8 54

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

Lab #7 Library Classes and JUnit Testing. Daniel Amyot, Diana Inkpen, Alan. Agenda. In this lab, you are going to create your own

Solutions to the 2005 exam

Chapter 15: Object Oriented Programming

Chapter 4 Defining Classes I

Classes and Objects: A Deeper Look

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

Container Vs. Definition Classes. Container Class

Lec 3. Compilers, Debugging, Hello World, and Variables

Object-Based Programming

Topic 7: Algebraic Data Types

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

CS 302 Week 9. Jim Williams

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

TOPIC 12 CREATING CLASSES PART 1

CS 106 Introduction to Computer Science I

Class CSE F. *slides are from CSE S at SKKU & at MIT

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

public static boolean isoutside(int min, int max, int value)

CSIS 10B Lab 2 Bags and Stacks

Data Structures. Data structures. Data structures. What is a data structure? Simple answer: a collection of data equipped with some operations.

Classes and Objects:A Deeper Look

Defining Your Own Classes

Principles of Object Oriented Programming. Lecture 4

CSC 240 Computer Science III Spring 2018 Midterm Exam. Name

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

Object-Oriented Design Lecture 3 CSU 370 Fall 2007 (Pucella) Friday, Sep 14, 2007

Lesson 10A OOP Fundamentals. By John B. Owen All rights reserved 2011, revised 2014

Object Oriented Programming

1. Which of the following is the correct expression of character 4? a. 4 b. "4" c. '\0004' d. '4'

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

CS 101 Fall 2006 Midterm 3 Name: ID:

Software Development With Java CSCI

Objects and Classes: Working with the State and Behavior of Objects

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

Methods (example: Methods1.java)

CONTENTS: While loops Class (static) variables and constants Top Down Programming For loops Nested Loops

Object Oriented Programming

Computer Science II. OO Programming Classes Scott C Johnson Rochester Institute of Technology

Problem Grade Total

CE221 Programming in C++ Part 1 Introduction

CHAPTER 7 OBJECTS AND CLASSES

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

PROGRAMMING FUNDAMENTALS

Lecture 8 Classes and Objects Part 2. MIT AITI June 15th, 2005

CS 251 Intermediate Programming Methods and Classes

CS 251 Intermediate Programming Methods and More

Object-oriented Programming and Software Engineering CITS1001. Multiple-choice Mid-semester Test

The class diagram contains only 2 elements: LabClass and Student. The LabClass class is linked to the Student class.

Java Classes & Primitive Types

1 OBJECT-BASED PROGRAMMING CHAPTER 8

Defining Classes. Chap 1 introduced many concepts informally, in this chapter we will be more formal in defining

CHAPTER 7 OBJECTS AND CLASSES

Java Classes & Primitive Types

Introduction to Computer Science II (CSI 1101)

Exception Handling. Sometimes when the computer tries to execute a statement something goes wrong:

Exception Handling. Run-time Errors. Methods Failure. Sometimes when the computer tries to execute a statement something goes wrong:

Chapter 6 Lab Classes and Objects

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

Lecture 13: Two- Dimensional Arrays

Programming Problems 22nd Annual Computer Science Programming Contest

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

Basic Operations jgrasp debugger Writing Programs & Checkstyle

Lecture 3. Lecture

Course Supervisor: Dr. Humera Tariq Hands on Lab Sessions: Ms. Sanya Yousuf

CS 61B, Spring 1999 MT3 Professor M. Clancy

CS Week 13. Jim Williams, PhD

Final Exam Practice. Partial credit will be awarded.

Guessing Game with Objects

Object Oriented Design: Identifying Objects

CSC-140 Assignment 6

Module Contact: Dr Gavin Cawley, CMP Copyright of the University of East Anglia Version 1

Chapter 4. Defining Classes I

CSC1590 Java Assignment 2-0. User Defined Classes

CS 307 Midterm 1 Spring 2009

Understanding class definitions. Looking inside classes (based on lecture slides by Barnes and Kölling)

Chapter 13: Arrays of Objects

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

if (x == 0); System.out.println( x=0 ); if (x = 0) System.out.println( x=0 );

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

Recitation 3 Class and Objects

COMP19612 exam performance feedback 2014

COMP-202 Unit 4: Programming with Iterations

Introduction to Computing II (ITI 1121) Midterm Examination

COMP 250: Java Programming I. Carlos G. Oliver, Jérôme Waldispühl January 17-18, 2018 Slides adapted from M. Blanchette

Software and Programming 1

Birkbeck (University of London) Software and Programming 1 In-class Test Mar 2018

UNIVERSITY OF THE FREE STATE DEPARTMENT OF COMPUTER SCIENCE AND INFORMATICS CSIS2614 MODULE TEST 1

Computing Science 114 Solutions to Midterm Examination Tuesday October 19, In Questions 1 20, Circle EXACTLY ONE choice as the best answer

Ticket Machine Project(s)

Transcription:

ITI 1120 Lab #9 Slides by: Diana Inkpen, Alan Williams, Daniel Amyot Some original material by Romelia Plesa 1

Objectives Review fundamental concepts Example: the Time class Exercises Modify the Time class The Car class 2

Classes Some Concepts A class contains variables and methods (code) Think of a class as a type of complex variable that provides a template to create objects. The object contains complex data (many variables of different types) and the possible operations on these variables (the methods). Reference to an object The reference variable contains the reference (an address) to an object. The following is a declaration of a reference variable. ClassName refvariablename; Recall that after a reference variable is declared, it contains a null value (does not reference an object). 3

Some Concepts (continued) Creating an object An object MUST BE created to be used: refvariablename = new ClassName( arg list ); The method after new is the class constructor its function is to initialise any variables of the new object. Instance variables: variables defined in the class and created within the object. Instance methods: the code that offers operations with the object variables. 4

A Time class Suppose we want to be able to work with values representing clock times to 1 minute precision. What information to we have to store to represent a time? How do we store that information? What operations might we want to do with a Time value? 5

What to store in Time? Two integers: hour: the hour number (0 hour 23) minute: the minute number (0 minute 59) class Time { public int hour; public int minute; } Alternatives? Only minutes or seconds from midnight! 6

Declaring and creating Time objects // DECLARE VARIABLES / DATA DICTIONARY Time time1; // ALGORITHM BODY // time is null here time1 = new Time( ); // We now have a Time object time1.hour = 17; // but it contains? for values time1.minute = 45; // time1 now represents 17:45 7

What do we have? time1 hour 17 minute 45 8

A method to set the time class Time { public int hour; public int minute; } public void settime( int h, int m ) { this.hour = h; this.minute = m; } 9

Usage // DECLARE VARIABLES / DATA DICTIONARY Time time1; // ALGORITHM BODY time1 = new Time( ); time1.settime( 17, 45 ); // time is null here // time1 now represents 17:45 10

This method is different! Did anyone see what was missing from the method: public void settime( int h, int m ) { this.hour = h; this.minute = m; } The word static does not appear in the method header. 11

Instance methods When the word static does not appear in the method header, this means that the method can be called via a variable declared to be of a type matching the class name. This is called an instance method. An instance method can make use of the variables defined in the class. The result: the method will produce different results for different object instances. 12

Instance methods For example, Time time1; Time time2; time1 = new Time( ); time2 = new Time( ) time1.settime( 17, 45 ); // time1 is 17:45 time2.settime( 14, 30 ); // time2 is 14:30 13

What do we have? time1 hour 17 minute 45 time2 hour 14 minute 30 14

this Objects time1 and time2 use the same code in class Time to set their own copy of hour and minute. When we want to refer to the object on which I was called, we use this. 15

this time1.settime( 17, 45 ); time1 hour minute this 17 45 time2 hour 14 minute 30 16

Information Hiding If we want to ensure that: hour: must be in range 0 hour 23 minute: must be in range 0 minute 59 then direct access to these variables should not be permitted. class Time { private int hour; private int minute; } Instead, access should be provided through settime, and we can adjust the values if needed. 17

Revised version of settime public void settime( int h, int m ) { // If minutes value is too large, adjust it // by mod 60, and add to hours value. } if ( m > 59 ) { h = h + m / 60; // determine hours to add m = m % 60; // puts minutes in range } else { ; // do nothing } this.hour = h % 24; // puts hours in range this.minute = m; 18

Accessors With the variables now declared to be private, we need to provide a way for other classes to ask for the values. public int gethours() { return hour; } public int getminute( ) { return minute; } 19

Compare times for equality Suppose we want a method that checks whether one time object is equal to another. One approach: a static method public static boolean isequal( Time t1, Time t2 ) This would be called as Time.isEqual( t1, t2 ) Alternative: an instance method public boolean isequal( Time t2 ) This would be called as t1.isequal( t2 ) 20

The static method public static boolean isequal( Time t1, Time t2 ) { return (t1.hour == t2.hour) && (t1.minute == t2.minute); } If the method is inside the class Time, it can access the private variables inside the class. Why are there 2 parameters in the above case? 21

The instance method public boolean isequal( Time t2 ) { return (this.hour == t2.hour) && (this.minute == t2.minute); } In this case, we are comparing ourself to another Time value. Why is only one parameter sufficient in this case? 22

Exercise 1 Add the following methods to the Time class: public boolean isbefore( Time t2 ) Returns true if the time represented by this is before the time in t2, and false otherwise public Time duration( Time t2 ) Returns a new Time object with the number of hours and minutes between this and t2. Write a main method in the class TestTime to test your new methods (or create JUnit tests). If you have time, add a display method: public String tostring() which allows the display of the Time object according to the format hour:minute in a print/println (for example System.out.println(time1); ) 23

A Line class Design a Java class Line that will store information for a line, where the line is in an (x,y) coordinate space, and provide operations to work with lines. Each line object starts at a point (xstart, ystart ) and ends at a point (xend, yend), where xstart, ystart, xend, and yend are real-valued numbers that could be positive or negative. y (xend, yend) x (xstart, ystart) 24

UML diagram for Line 25

Method descriptions Set the start and end points of the line. Name of method: setpoints(...) Parameters to the method: xs, ys, xe, ye Results: (none) Modified: the line object Return the length of the line The length is Name of method: length( ) Parameters to the method: none. Result: length of the line (a real value) 26

Method descriptions Translate the line by (tx, ty), where tx and ty are any positive or negative real values. A translation of a line represents "sliding" the entire line. The value tx is added to the x coordinates of the start and end of the line, and the value ty is added to the y coordinates of the start and end of the line. Name of method: translate(...) Parameters to the method: tx, ty Results: (none) Modified: the line object 27

Method descriptions Return the slope of the line. The slope is (ye ys) / (xe xs) (watch out for vertical lines their slope is infinite!) Name of method: slope( ) Parameters to the method: none. Result: the slope of the line (a real value) 28

Method descriptions Returns a String with information about the line. The string that is returned for a line with (for example) start point (0.0,1.0) and end point (3.5,- 1.2) should be: Line from (0.0, 1.0) to (3.5, -1.2) Formatting of the values to a specific number of decimal places is not required. Name of method: tostring ( ) Parameters to the method: (none) Results: a String in the above format 29

Exercise Implement the class Line - implement the tostring method - add a printlineinfo method Test the class using the main method in LineTest.java 30