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 ◦

Slides:



Advertisements
Similar presentations
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Advertisements

Core Java Lecture 4-5. What We Will Cover Today What Are Methods Scope and Life Time of Variables Command Line Arguments Use of static keyword in Java.
Lecture 9: More on objects, classes, strings discuss hw3 assign hw4 default values for variables scope of variables and shadowing null reference and NullPointerException.
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
Static Methods Static methods are those methods that are not called on objects. In other words, they don’t have an implicit parameter. Random number generation.
Defining classes and methods Recitation – 09/(25,26)/2008 CS 180 Department of Computer Science, Purdue University.
6/10/2015C++ for Java Programmers1 Pointers and References Timothy Budd.
School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 9: 1 CMT1000: Introduction to Programming Ed Currie Lecture 11: Objects.
Java Syntax Primitive data types Operators Control statements.
What is a class? a class definition is a blueprint to build objects its like you use the blueprint for a house to build many houses in the same way you.
CS180 Chapter 4 2/1/08. Announcements Project 3 is out –2 weeks due to the exam Exam –Thursday, February 7, 8:30 – 9:30 pm, WTHR 104 –Covers chapters.
CS31: Introduction to Computer Science I Discussion 1A 5/28/2010 Sungwon Yang
Chapter 8: User-Defined Classes and ADTs J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
1 Class Constructors a class constructor is a member function whose purpose is to initialize the private data members of a class object the name of a constructor.
The Clock Example. 1. The Clock Example 2 3 public class Clock { // instance variables private int hr; private int min; private int sec; // constructors:
OOP Languages: Java vs C++
Principles of Computer Programming (using Java) Review Haidong Xue Summer 2011, at GSU.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Java Methods. Topics  Declaring fields vs. local variables  Primitive data types  Strings  Compound Assignment  Conversions from one value to another.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 2.
1 Objects and Classes. 2 OO Programming Concepts Object-oriented programming (OOP) involves programming using objects. An object represents an entity.
Java Quiz Bowl A fun review of the Java you should know from CMPT 201 If you don’t know the answers - this week is for you to study up!
Lecture # 8 Constructors Overloading. Topics We will discuss the following main topics: – Static Class Members – Overloaded Methods – Overloaded Constructors.
Problem Solving using the Java Programming Language May 2010 Mok Heng Ngee Day 5: Arrays.
Arrays Chapter 8. What if we need to store test scores for all students in our class. We could store each test score as a unique variable: int score1.
Java – Methods Part II Lecture Notes 6. Objects, Classes and Computer Memory When a Java program is executing, the memory must hold: 1. Templates for.
Chapter 8: User-Defined Classes and ADTs
Questions? Suggestions?. References References Revisited What happens when we say: int x; double y; char c; ???
ECE122 Feb. 22, Any question on Vehicle sample code?
OOP in Java : © W. Milner 2005 : Slide 1 Java and OOP Part 2 – Classes and objects.
Introduction to Java Lecture Notes 3. Variables l A variable is a name for a location in memory used to hold a value. In Java data declaration is identical.
RECITATION 4. Classes public class Student { } Data Members public class Student { private String name; public String id; }
Chapter 10 Defining Classes. The Internal Structure of Classes and Objects Object – collection of data and operations, in which the data can be accessed.
1 CS 177 Week 12 Recitation Slides Running Time and Performance.
Objects and Classes Mostafa Abdallah
COP3502 Programming Fundamentals for CIS Majors 1 Instructor: Parisa Rashidi.
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
CSC 212 Object-Oriented Programming and Java Part 2.
IT108 Objects and Classes Part I George Mason University Revised 4/3/2012.
CIS 270—Application Development II Chapter 8—Classes and Objects: A Deeper Look.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Objects and Classes.
An Introduction to Java – Part 1 Erin Hamalainen CS 265 Sec 001 October 20, 2010.
Introduction To Objects Oriented Programming Instructor: Mohammed Faisal.
CS 116 Lecture 1 John Korah Contains content provided by George Koutsogiannakis & Matt Bauer.
Grouping Data Together Often we want to group together a number of values or objects to be treated in the same way e.g. names of students in a tutorial.
Structured Programming Dr. Atif Alhejali Lecture 4 Modifiers Parameters passing 1Structured Programming.
Lecture 9: Object and Classes Michael Hsu CSULA. 2 OO Programming Concepts Object-oriented programming (OOP) involves programming using objects. An object.
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Lecture 3: Introduction to Object and Classes Michael Hsu CSULA.
Lecture 3: Introduction to Object and Classes Michael Hsu CSULA.
Java: Base Types All information has a type or class designation
Defining Your Own Classes II
Topic: Classes and Objects
Values vs. References Lecture 13.
Java: Base Types All information has a type or class designation
Java Programming: Guided Learning with Early Objects
Chapter 3: Using Methods, Classes, and Objects
CS 302 Week 11 Jim Williams, PhD.
User-Defined Classes and ADTs
CSC 113 Tutorial QUIZ I.
null, true, and false are also reserved.
Chapter 8: User-Defined Classes and ADTs
CS 302 Week 9 Jim Williams.
Recap Week 2 and 3.
Chapter 9 Objects and Classes Part 01
Review for Midterm 3.
Announcements Lab 5 due Wednesday at noon.
Presentation transcript:

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 ◦ Next Wednesday. 6:30pm

Questions?

Data Types in Java Primitive Types ◦ int ◦ double ◦ boolean ◦ char Object Types ◦ String ◦ Arrays, ex int[] ◦ You can create them!

Reference vs. Primitive variables Variables of primitive types hold values ◦ int x = 25; Variables of object types hold references ◦ int [] array = new int[3]; 25 x array

Sample class myClock public class myClock{ int hour; int min; int sec; } Declare an object of type myClock myClock clock; Here, clock holds null. It points to nothing. Create the object clock = new myClock(); clock 0 0 hour 0 0 min 0 0 sec class object class Create memory space and assign to clock X

Example myClock clock1 = new myClock(); myClock clock2 = clock1; The address is copied. clock1.hour and clock2.hour all refer to the same variable. clock1.hour = 3; System.out.println(clock2.hour); //prints out 3! clock1 0 0 hour 0 0 min 0 0 sec clock2

“==“ and Comparison “==“ compares whatever is in the variables. int x = 25; int y = 25; System.out.println(x == y);//prints out “true” myClock clock1 = new myClock(); myClock clock2 = clock1; System.out.println(clock1 == clock2); //The two reference variables point to // the same address. Prints out “true” 25 x y clock1 0 0 hour 0 0 min 0 0 sec clock2

“==“ and Comparison cont. “==“ compares whatever is in the variables. myClock clock1 = new myClock(); myClock clock2 = new myClock(); System.out.println(clock1 == clock2); // The two reference variables point to different addresses. // Prints out “false” although the two objects have the same content! clock1 0 0 hour 0 0 min 0 0 sec clock2 0 0 hour 0 0 min 0 0 sec

“==“ and Comparison cont. Define a method to compare the contents of two objects. public class myClock{ int hour; int min; int sec; boolean equals(myClock c2){ if ( hour == c2.hour && min == c2.min && sec == c2.sec ) return true; else return false; }

“==“ and Comparison cont. Now you can compare object contents using that method. myClock clock1 = new myClock(); myClock clock2 = new myClock(); System.out.println(clock1.equals(clock2)); // Prints out “true” because the values are the same! clock1 0 0 hour 0 0 min 0 0 sec clock2 0 0 hour 0 0 min 0 0 sec

Constructors A Constructor is invoked when you create an object using the keyword “new” You can use it to initialize member variables. public class myClock{ public myClock(int h, int m, int s){ hour = h; min = m; sec = s; } int hour; int min; int sec; } Now you need to specify the values when you use “new” myClock clock = new myClock(3, 30, 10); clock 3 3 hour 30 min 10 sec

public class myClock{ public myClock(){ } public myClock(int h, int m, int s){ hour = h; min = m; sec = s; } int hour; int min; int sec; } Constructors cont. A default constructor is a constructor that takes zero input arguments. It assigns default values for the variables or simply does nothing If you do not specify your own constructor, Java creates a default constructor for you. default constructor clock 0 0 hour 0 0 min 0 0 sec myClock clock = new myClock();

public vs. private Public members are accessible to the outside world. Private members are only accessible inside the class itself. Usually we have public methods and private variables

public vs. private cont. public class myClock{ private int hour; private int min; private int sec; public myClock(int h, int m, int s){ hour = h; min = m; sec = s;} public myClock(){} public boolean equals(myClock c2){ if ( hour == c2.hour && min == c2.min && sec == c2.sec ) return true; else return false; } public class rec10{ public static void main(String [] strs){ myClock clock1 = new myClock(); myClock clock2 = new myClock(3, 30, 2); System.out.println(clock1.equals(clock2)); System.out.println(clock1.hour); } Compile error!

Accessor methods – get values public class myClock{ private int hour; private int min; private int sec; public myClock(int h, int m, int s){ hour = h; min = m; sec = s;} public myClock(){} public boolean equals(myClock c2){ if ( hour == c2.hour && min == c2.min && sec == c2.sec ) return true; else return false; } public int getHour(){ return hour; } public int getMin(){ return min; } public int getSec(){ return sec; } }

Mutator methods – set values public class myClock{ private int hour; private int min; private int sec; public myClock(int h, int m, int s){ hour = h; min = m; sec = s;} public myClock(){} public void print(){ System.out.println(hour + " " + min + " " + sec); } public boolean equals(myClock c2){ if ( hour == c2.hour && min == c2.min && sec == c2.sec ) return true; else return false; } public int getHour(){ return hour; } public int getMin(){ return min; } public int getSec(){ return sec; } public void setHour(int h){ hour = h; } public void setMin(int m){ if ( m >= 0 && m < 60 ) min = m; } public void setSec(int s){ if ( s >= 0 && s < 60 ) sec = s; } public void setHour(int h){ hour = h; } public void setMin(int m){ if ( m >= 0 && m < 60 ) min = m; } public void setSec(int s){ if ( s >= 0 && s < 60 ) sec = s; } Validity checking is done to ensure that the change is allowable.

public class myClock{ private int hour; private int min; private int sec; public myClock(int h, int m, int s){ setHour(h); setMin(m); setSec(s); } public myClock(){} public void print(){ System.out.println(hour + " " + min + " " + sec); } public boolean equals(myClock c2){ if ( hour == c2.hour && min == c2.min && sec == c2.sec ) return true; else return false; } public int getHour(){ return hour; } public int getMin(){ return min; } public int getSec(){ return sec; } public void setHour(int h){ hour = h; } public void setMin(int m){ if ( m >= 0 && m < 60 ) min = m; } public void setSec(int s){ if ( s >= 0 && s < 60 ) sec = s; } public myClock(int h, int m, int s){ setHour(h); setMin(m); setSec(s); } Constructors set up the values, too. You can take advantage of the mutator methods to do that.

Example of How to Use myClock public class rec10{ public static void main(String [] strs){ myClock clock1 = new myClock(); myClock clock2 = new myClock(3, 30, 2); System.out.println(clock1.equals(clock2)); clock1.setHour(3); clock1.setMin(30); clock1.setSec(2); System.out.println(clock1.equals(clock2)); System.out.println("clock1 shows: " + clock1.getHour() + ":" + clock1.getMin() + ":" + clock1.getSec()); } The program prints out: false true clock1 shows: 3:30:2

Questions?