ITI 1120 Lab #10 Objects and Classes Slides by: Romelia Plesa, Sylvia Boyd, Alan Williams, Diana InkPen, Daniel Amyot, Gilbert Arbez, Mohamad Eid.

Slides:



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

CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
CSCI 3328 Object Oriented Programming in C# Chapter 9: Classes and Objects: A Deeper Look 1 Xiang Lian The University of Texas – Pan American Edinburg,
TOPIC 12 CREATING CLASSES PART 1 1 Notes adapted from Introduction to Computing and Programming with Java: A Multimedia Approach by M. Guzdial and B. Ericson,
Animation Mrs. C. Furman. Animation  We can animate our crab by switching the image between two pictures.  crab.png and crab2.png.
CS 106 Introduction to Computer Science I 03 / 24 / 2008 Instructor: Michael Eckmann.
School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 9: 1 CMT1000: Introduction to Programming Ed Currie Lecture 11: Objects.
CS 106 Introduction to Computer Science I 03 / 21 / 2008 Instructor: Michael Eckmann.
COMP 110 Introduction to Programming Mr. Joshua Stough October 8, 2007.
Enhancing classes Visibility modifiers and encapsulation revisited
CS 106 Introduction to Computer Science I 11 / 15 / 2006 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 03 / 23 / 2007 Instructor: Michael Eckmann.
C++ Classes in Depth. Topics Designing Your Own Classes Attributes and Behaviors Writing Classes in C++ Creating and Using Objects.
Understanding class definitions Looking inside classes.
Week 4-5 Java Programming. Loops What is a loop? Loop is code that repeats itself a certain number of times There are two types of loops: For loop Used.
ITP © Ron Poet Lecture 13 1 Helper Objects. ITP © Ron Poet Lecture 13 2 Console Helper Object  We have used Console objects for a while, let’s see what.
Chapter 7 More Lists. Chapter 7: More Lists 7.1 – Circular Linked Lists 7.2 – Doubly Linked Lists 7.3 – Linked Lists with Headers and Trailers 7.4 – A.
Java Class Syntax CSIS 3701: Advanced Object Oriented Programming.
1 Romelia Plesa, Alan Williams, Sylvia Boyd, Daniel Amyot, Diana Inkpen, Gilbert Arbez, Mohamad Eid ITI 1120 Lab #3.
 A Collection class is a data type that is capable of holding a group of items.  In Java, Collection classes can be implemented as a class, along with.
Agenda Object Oriented Programming Reading: Chapter 14.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures for Java William H. Ford William R. Topp Chapter 5 Generic.
SE-1010 Dr. Mark L. Hornick 1 Java Programming Basics.
(c) University of Washington15-1 CSC 143 Java List Implementation via Arrays Reading: 13.
Chapter 10 Defining Classes. The Internal Structure of Classes and Objects Object – collection of data and operations, in which the data can be accessed.
HashCode() 1  if you override equals() you must override hashCode()  otherwise, the hashed containers won't work properly  recall that we did not override.
Question 1a) What is printed by the following Java program? int s; int r; int i; int [] x = {4, 8, 2, -9, 6}; s = 1; r = 0; i = x.length - 1; while (i.
ITI 1120 Lab #5 Contributors: S. Boyd, R. Plesa, A. Felty, D. Inkpen, A. Williams, D. Amyot.
Classes. Student class We are tasked with creating a class for objects that store data about students. We first want to consider what is needed for the.
ITI 1120 Lab #11 Contributors: Diana Inkpen, Daniel Amyot, Sylvia Boyd, Amy Felty, Romelia Plesa, Alan Williams.
Chapter 5 Classes and Methods II Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas, E.
Chapter 3 Object Interaction.  To construct interesting applications it is not enough to build individual objects  Objects must be combined so they.
Newport Robotics Group 1 Tuesdays, 6:30 – 8:30 PM Newport High School Week 4 10/23/2014.
CIS 270—Application Development II Chapter 8—Classes and Objects: A Deeper Look.
Lab 4 - Variables. Information Hiding General Principle: – Restrict the access to variables and methods as much as possible Can label instance variables.
Classes and Objects CS177 Rec 10. Announcements Project 4 is posted ◦ Milestone due on Nov. 12. ◦ Final submission due on Nov. 19. Exam 2 on Nov. 4 ◦
ITI 1120 Lab #3 Tracing and Branching Contributors: G. Arbez, M. Eid, D. Inkpen, A. Williams, D. Amyot.
1 Romelia Plesa, Alan Williams, Sylvia Boyd, Daniel Amyot, Diana Inkpen, Gilbert Arbez, Mohamad Eid ITI 1120 Lab #3.
CS305j Introduction to Computing Classes II 1 Topic 24 Classes Part II "Object-oriented programming as it emerged in Simula 67 allows software structure.
Chapter 6 Object-Oriented Design Part 2. © 2004 Pearson Addison-Wesley. All rights reserved2/20 The this Reference The this reference allows an object.
1.Reading from Keyboard 2.Main programs 3.Responsibilities 1 CS12230 Introduction to Programming Lecture 2or3-Other things.
Chapter 5 Classes and Methods II Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas, E.
Topic 8Classes, Objects and Methods 1 Topic 8 l Class and Method Definitions l Information Hiding and Encapsulation l Objects and Reference Classes, Objects,
Object Oriented Programming and Data Abstraction Rowan University Earl Huff.
CO320 Catchup David Barnes with material from Dermot Shinners-Kennedy.
Spring 2008 Mark Fontenot CSE Honors Principles of Computer Science I Note Set 12.
Midterm Review Tami Meredith. Primitive Data Types byte, short, int, long Values without a decimal point,..., -1, 0, 1, 2,... float, double Values with.
XuanTung Hoang 1 Something to discuss Feedbacks on Midterm Exam Final exam and term project  Final exam requires solid knowledge/skills in Java  Be more.
ITI 1120 Lab #8 Contributors: Diana Inkpen, Daniel Amyot, Sylvia Boyd, Amy Felty, Romelia Plesa, Alan Williams.
CS100Lecture 61 Announcements Homework P1 due on Thursday Homework P2 handed out.
CompSci 230 S Programming Techniques
Topic: Classes and Objects
Java Programming: Guided Learning with Early Objects
CompSci 230 Software Construction
The University of Texas Rio Grande Valley
Subroutines Idea: useful code can be saved and re-used, with different data values Example: Our function to find the largest element of an array might.
בניית מחלקות.
בניית מחלקות.
Logical assertions assertion: A statement that is either true or false. Examples: Java was created in The sky is purple. 23 is a prime number. 10.
Building Java Programs
Object Oriented Programming
Today’s topics UML Diagramming review of terms
Lecture 13: Two-Dimensional Arrays
CSCI 3328 Object Oriented Programming in C# Chapter 9: Classes and Objects: A Deeper Look UTPA – Fall 2012 This set of slides is revised from lecture.
CS360 Client/Server Programming Using Java
Special instance methods: toString
Question 1a) What is printed by the following Java program? int s;
CS 1054: Lecture 2, Chapter 1 Objects and Classes.
Presentation transcript:

ITI 1120 Lab #10 Objects and Classes Slides by: Romelia Plesa, Sylvia Boyd, Alan Williams, Diana InkPen, Daniel Amyot, Gilbert Arbez, Mohamad Eid

Example 1: 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?

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?

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

What do we have? time hour minute

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; }

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

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.

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.

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

What do we have? time hour minute time hour minute

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.

this time hour minute time hour minute this time1.setTime( 17, 45 );

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.

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; }

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; }

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 )

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.

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.

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. (xStart, yStart) (xEnd, yEnd) y x

UML diagram for Line

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)

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

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)

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

Exercise 2 Implement the class Line - implement the toString method - add a printLineInfo method Test the class using the main method in LineTest.java

Extra Exercise We want to realize a program for the administration of a parking lot formed by a collection of parking places. For each parking place the following information has to be stored: –Whether it is free of occupied; –Licence plate of the car, when occupied (a string); –Time since when it is occupied (two integers, for hours and minutes). Write a class ParkingPlace, for handling a parking place, that implements the following methods: –ParkingPlace(): constructor without parameters that constructs a parking place that is initially free; –String toString(): that returns " " if the parking place is free, and the licence plate of the car, if the parking place is occupied; –void carArrives(String plate, int hour, int minutes: modifies the state of the parking place by setting it to occupied, sets the plate of the car that occupies the place to plate, and sets the time since when it is occupied to hour and minutes; if the parking place is already occupied, the method does nothing; –void carLeaves(): modifies the state of the parking place by setting it to free; –boolean free(): returns true if the parking place is free, false otherwise; –String getCar(): returns the plate of the car that occupies the parking place, if the place is occupied, null otherwise; –int getHour(): returns the hour since when the parking place is occupied, if the place is occupied, -1 otherwise; –int getMinutes(): returns the minutes since when the parking place is occupied, if the place is occupied, -1 otherwise;

Exercise 3 Use the example program TestParkingPlace.Java to test the class you have developed.TestParkingPlace.Java