Unit 5: More on Classes/Objects Notes

Size: px
Start display at page:

Download "Unit 5: More on Classes/Objects Notes"

Transcription

1 Unit 5: More on Classes/Objects Notes AP CS A The Difference between Primitive and Object/Reference Data Types First, remember the definition of a variable. A variable is a. So, an obvious question is: what is stored in a particular variable? For primitive data types (e.g. ) the answer is easy. The variable stores the data. int x = 78; int y = x; For object variables, the answer is more complicated. An object variable contains a to an object - not the object itself. Point p1 = new Point( 3, 4 ); Point p2; p2 = p1; 1. How many Point objects does this code create? 2. How many Point objects does this code create? Point x1; Point x2; Point x3 = new Point( 17, -22 ); Point x4 = x3; is a special value that can assigned to any object variable. This indicates that the variable does not contains a reference to an object. Reference variables can be initialized with a value of null. For example: Point p1 = null; To check if a variable has a value of null, use ==. For example: public void dosomething( String x ){ if ( x == null ) x does not contain a reference to a String object Page 1

2 If you attempt to call a method but the object variable does not contain a reference to an object, then you will get this runtime error:. For example: Point pt = null; double d = pt.distance( 0, 5.5 ); // If an object is instantiated but at some later time no variable contains a reference to that object, the JVM will delete that object in a process called. For example: Point man = new Point( -1, 0 ); man = null; // Why public and private? When someone uses an object, they don t usually need to know how an object does something; they only need to know what an object does. For example, we ve been using Scanner objects without ever seeing the actual code inside that class. This is similar to how we use everyday objects. In the process of writing a class, we have (so far) always made the instance variables private and everything else public. Let s consider why making instance variables private is useful. Look over the code below. public class Person1{ private int age = 8; public int get(){ return age; public void set(int n){ age = n; public class Person2{ public int age = 9; // another class Person1 p1 = new Person1(); Person2 p2 = new Person2(); System.out.println( p1.get() ); System.out.println( p2.age ); p1.set( 24 ); p2.age = 25; System.out.println( p1.get() ); System.out.println( p2.age ); The above code compiles and runs. What is displayed? The Person1 and Person2 classes do the same thing. Unfortunately, both have a design flaw How can the flaw be fixed in the Person1 class? How can the flaw be fixed in the Person2 class? This is one reason to make the instance variable private and then write getter and setter methods. Also, if an instance variable is public then you cannot change its name, or data type, without breaking any code that uses this class. Conclusion: Page 2

3 The of an object refers to the current values of all its instance variables. If the value of even one instance variable changes, then the state of the object has changed. Two methods in the same class are if they have the same name but different parameters. Two methods in the same class cannot differ by just their. Default Constructors. If a class does not define a constructor, it automatically has one. All instance variables are given default values. ints and doubles have default values of booleans have a default value of reference (aka object) variables have a default value of public static void ( String [] args ){ public class Panda { private int x; public int get(){ return x; IMPORTANT. If you write even one constructor, then you lose the default constructor. The Scope of a variable refers both to its lifetime and its accessibility. Lifetime means how long does the variable exist and accessibility means who can see/use the variable. Variable Lifetime Accessibility public instance variable Any class that can access the object. private instance variable parameters in a constructor or method local variables in a constructor or method Only the constructors and methods within its class. Only the constructor or method that it is declared in. Only within the block that it is declared in. A block of code is a section of code that is explicitly or implicitly contained with a pair of curly braces. Page 3

4 Heap vs. Stack. Memory to run a program can be divided into the heap and the stack. The heap is for objects (and other things) and the stack is for local variables and parameters. Each call to a method generates a new stack frame for that method public class Practice { public static void ( String [] args ) { Dog a = new Dog( 2 ); int z = 5; z = a.bark( 3 ); System.out.print( z ); public class Dog{ private int w; public Dog(int x){ w = x; public int bark(int y) { int n = y + w; return n; Line currently being executed Stack Heap 1. The method is called. 2. The Dog constructor is called and a new stack frame is generated constructor Completion of line 2. The stack frame for the constructor is removed. 3. Another local variable is declared. 4/5. The bark method is called and a new stack frame is generated. bark 6. A local variable in the bark method is declared. bark 7. The value of n is returned and assigned to z. The stack frame for the bark method is deleted. Page 4

5 this. Within an instance method or a constructor, this is a reference to the current object the object whose method or constructor is being called. You can refer to any member of the current object from within an instance method or a constructor by using this. - In many circumstances, the use of this is optional. Compare these two versions of the same class. The Way We Write a Class The Way Some People Like to Write a Class public class Square{ public class Square{ private int side; private int side; public Square( int s ){ side = s; public Square( int s ){ this.side = s; public int area(){ return side * side; public int area(){ return this.side * this.side; Some people think that the version on the right makes it clearer that we are changing the value of the instance variable of the current object. Here s some sad code where the use of this is required. In this example, every Ape that is created must immediately be added to a Zoo. public class Zoo{ public class Ape { private String names = ""; private String name; public void add( Ape a ){ names += a.get() + " "; public String getnames(){ return names; All of this code compiles and runs. What is displayed when the Runner class is executed? public Ape( String s, Zoo z ){ this.name = s; z.add( ); public String get(){ return name; public static void (String [] args){ Zoo z = new Zoo(); Ape a1 = new Ape( "Bob", z ); Ape a2 = new Ape( "Ann", z ); String s = z.getnames(); System.out.println( s ); Page 5

6 You can access the private instance variables of a parameter IF the parameter is an object of that class. For example: public class Box{ private int width; public static void ( String [] args ){ private int length; Box b1 = new Box( 3, 6 ); Box b2 = new Box( 5, 4 ); System.out.println(b1.biggerThan( b2 )); System.out.println(b2.biggerThan( b1 )); public Box( int wid, int len ){ width = wid; length = len; public boolean biggerthan( Box b ){ int my_area = this.width*this.length; int other_area = b.width * b.length; return my_area > other_area; Constants. Sometimes you want to create a variable that never changes. For example: public class Bill{ private final double TAX_RATE = 0.1; public static void ( String [] args ){ private double total = 0; Bill b = new Bill(); public double add( double item ){ total += item + item*tax_rate; return total; Use the keyword final to make a variable a constant. double x = b.add( 10 ); System.out.println( x ); x = b.add( 20 ); System.out.println( x ); It is customary to use all upper-case characters for a constant s name. Constants improve your code s readability and tainability. Trying to change the value of a constant generates a compiler error. Functional Decomposition is the process of breaking a method into Smaller, more focused methods are The helper methods will almost always be. After function decomposition, a method should be easier to understand and may be shorter than the original code. You should think of using Page 6

7 It can look something like this: First Version public return_type method( parameters ){ section A (some lines of code) section B (some lines of code) section C (some lines of code) After Functional Decomposition public return_type method( parameters ){ methoda( parameters ); methodb( parameters ); methodc( parameters ); private return_type methoda( parameters ){ code private return_type methodb( parameters ){ code private return_type methodc( parameters ){ code Here s a more concrete example. The purpose of the method is to print out a grid similar to the one shown. The size of the grid depends on the parameter. In the figure to the right, size is First Version public void method( int size ){ for ( int k = 0; k < size; k++ ) System.out.print( "+++" ); System.out.println(); int x = 0; for ( int row = 0; row < size; row++ ){ for ( int col = 0; col < size; col++ ){ if ( x == size-1 ) x = 0; System.out.print( " " + x + " " ); x++; System.out.println(); After Functional Decomposition public void method( int size ){ printplus( size ); Notice how printnumbers( size ); we call these printplus( size ); methods. private void printplus( int size ){ private void printnumbers ( int size ){ for ( int k = 0; k < size; k++ ) System.out.print( "+++" ); System.out.println(); Page 7

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

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

AP CS Unit 3: Control Structures Notes

AP CS Unit 3: Control Structures Notes AP CS Unit 3: Control Structures Notes The if and if-else Statements. These statements are called control statements because they control whether a particular block of code is executed or not. Some texts

More information

APCS Semester #1 Final Exam Practice Problems

APCS Semester #1 Final Exam Practice Problems Name: Date: Per: AP Computer Science, Mr. Ferraro APCS Semester #1 Final Exam Practice Problems The problems here are to get you thinking about topics we ve visited thus far in preparation for the semester

More information

Unit 5: More on Classes/Objects Exercises

Unit 5: More on Classes/Objects Exercises Unit 5: More on Classes/Objects Exercises AP CS A 1. Select the TRUE statement. a) This does not compile. b) This does compile.. Select the TRUE statement. a) This does not compile. b) This does compile.

More information

Intro. Classes Beginning Objected Oriented Programming. CIS 15 : Spring 2007

Intro. Classes Beginning Objected Oriented Programming. CIS 15 : Spring 2007 Intro. Classes Beginning Objected Oriented Programming CIS 15 : Spring 2007 Functionalia HW 4 Review. HW Out this week. Today: Linked Lists Overview Unions Introduction to Classes // Create a New Node

More information

Introduction to Programming Using Java (98-388)

Introduction to Programming Using Java (98-388) Introduction to Programming Using Java (98-388) Understand Java fundamentals Describe the use of main in a Java application Signature of main, why it is static; how to consume an instance of your own class;

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

CS 251 Intermediate Programming Methods and More

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

More information

Chapter 6 Introduction to Defining Classes

Chapter 6 Introduction to Defining Classes Introduction to Defining Classes Fundamentals of Java: AP Computer Science Essentials, 4th Edition 1 Objectives Design and implement a simple class from user requirements. Organize a program in terms of

More information

AP CS Unit 7: Arrays Exercises

AP CS Unit 7: Arrays Exercises AP CS Unit 7: Arrays Exercises 1. What is displayed? int [] a = new int[ 3 ]; System.out.println(a.length ); 2. What is displayed? int [] sting = { 34, 23, 67, 89, 12 ; System.out.println( sting[ 1 ] );

More information

CS 231 Data Structures and Algorithms, Fall 2016

CS 231 Data Structures and Algorithms, Fall 2016 CS 231 Data Structures and Algorithms, Fall 2016 Dr. Bruce A. Maxwell Department of Computer Science Colby College Course Description Focuses on the common structures used to store data and the standard

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

3. Convert 2E from hexadecimal to decimal. 4. Convert from binary to hexadecimal

3. Convert 2E from hexadecimal to decimal. 4. Convert from binary to hexadecimal APCS A Midterm Review You will have a copy of the one page Java Quick Reference sheet. This is the same reference that will be available to you when you take the AP Computer Science exam. 1. n bits can

More information

Unit 4: Classes and Objects Notes

Unit 4: Classes and Objects Notes Unit 4: Classes and Objects Notes AP CS A Another Data Type. So far, we have used two types of primitive variables: ints and doubles. Another data type is the boolean data type. Variables of type boolean

More information

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

CISC-124. Passing Parameters. A Java method cannot change the value of any of the arguments passed to its parameters. CISC-124 20180215 These notes are intended to summarize and clarify some of the topics that have been covered recently in class. The posted code samples also have extensive explanations of the material.

More information

CS Week 14. Jim Williams, PhD

CS Week 14. Jim Williams, PhD CS 200 - Week 14 Jim Williams, PhD This Week 1. Final Exam: Conflict Alternatives Emailed 2. Team Lab: Object Oriented Space Game 3. BP2 Milestone 3: Strategy 4. Lecture: More Classes and Additional Topics

More information

9 Working with the Java Class Library

9 Working with the Java Class Library 9 Working with the Java Class Library 1 Objectives At the end of the lesson, the student should be able to: Explain object-oriented programming and some of its concepts Differentiate between classes and

More information

AP Computer Science A Unit 7. Notes on Arrays

AP Computer Science A Unit 7. Notes on Arrays AP Computer Science A Unit 7. Notes on Arrays Arrays. An array is an object that consists of an of similar items. An array has a single name and the items in an array are referred to in terms of their

More information

Array. Prepared By - Rifat Shahriyar

Array. Prepared By - Rifat Shahriyar Java More Details Array 2 Arrays A group of variables containing values that all have the same type Arrays are fixed length entities In Java, arrays are objects, so they are considered reference types

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

Object Oriented Programming: In this course we began an introduction to programming from an object-oriented approach.

Object Oriented Programming: In this course we began an introduction to programming from an object-oriented approach. CMSC 131: Chapter 28 Final Review: What you learned this semester The Big Picture Object Oriented Programming: In this course we began an introduction to programming from an object-oriented approach. Java

More information

Overview CSE 142. Naming Revisited Declarations. Defining Parts of Objects

Overview CSE 142. Naming Revisited Declarations. Defining Parts of Objects ÿþýûú Overview CSE 142 Anatomy of an object: constructors, methods, and instance variables Quick Review Creating, naming, and using objects Creating (empty) classes and instances of them Today Details

More information

Day 4. COMP1006/1406 Summer M. Jason Hinek Carleton University

Day 4. COMP1006/1406 Summer M. Jason Hinek Carleton University Day 4 COMP1006/1406 Summer 2016 M. Jason Hinek Carleton University today s agenda assignments questions about assignment 2 a quick look back constructors signatures and overloading encapsulation / information

More information

3.1 Class Declaration

3.1 Class Declaration Chapter 3 Classes and Objects OBJECTIVES To be able to declare classes To understand object references To understand the mechanism of parameter passing To be able to use static member and instance member

More information

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

Java How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved. Java How to Program, 10/e Education, Inc. All Rights Reserved. Each class you create becomes a new type that can be used to declare variables and create objects. You can declare new classes as needed;

More information

QUIZ How do we implement run-time constants and. compile-time constants inside classes?

QUIZ How do we implement run-time constants and. compile-time constants inside classes? QUIZ How do we implement run-time constants and compile-time constants inside classes? Compile-time constants in classes The static keyword inside a class means there s only one instance, regardless of

More information

Introduction to Java. Handout-1d. cs402 - Spring

Introduction to Java. Handout-1d. cs402 - Spring Introduction to Java Handout-1d cs402 - Spring 2003 1 Methods (i) Method is the OOP name for function Must be declared always within a class optaccessqualifier returntype methodname ( optargumentlist )

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

AP CS Unit 8: Inheritance Exercises

AP CS Unit 8: Inheritance Exercises AP CS Unit 8: Inheritance Exercises public class Animal{ System.out.print("A"); public void m2(){ System.out.print("B"); public class Dog extends Animal{ System.out.print("C"); public void m3(){ System.out.print("D");

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

CS 1302 Chapter 9 (Review) Object & Classes

CS 1302 Chapter 9 (Review) Object & Classes CS 1302 Chapter 9 (Review) Object & Classes Reference Sections 9.2-9.5, 9.7-9.14 9.2 Defining Classes for Objects 1. A class is a blueprint (or template) for creating objects. A class defines the state

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

CSE 142 Su 04 Computer Programming 1 - Java. Objects

CSE 142 Su 04 Computer Programming 1 - Java. Objects Objects Objects have state and behavior. State is maintained in instance variables which live as long as the object does. Behavior is implemented in methods, which can be called by other objects to request

More information

COE318 Lecture Notes Week 10 (Nov 7, 2011)

COE318 Lecture Notes Week 10 (Nov 7, 2011) COE318 Software Systems Lecture Notes: Week 10 1 of 5 COE318 Lecture Notes Week 10 (Nov 7, 2011) Topics More about exceptions References Head First Java: Chapter 11 (Risky Behavior) The Java Tutorial:

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

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

Programming II (CS300)

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

More information

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

Data Structures. Data structures. Data structures. What is a data structure? Simple answer: a collection of data equipped with some operations. Data Structures 1 Data structures What is a data structure? Simple answer: a collection of data equipped with some operations. Examples Lists Strings... 2 Data structures In this course, we will learn

More information

Coding Standards for Java

Coding Standards for Java Why have coding standards? Coding Standards for Java Version 1.3 It is a known fact that 80% of the lifetime cost of a piece of software goes to maintenance; therefore, it makes sense for all programs

More information

Mr. Monroe s Guide to Mastering Java Syntax

Mr. Monroe s Guide to Mastering Java Syntax Mr. Monroe s Guide to Mastering Java Syntax Getting Started with Java 1. Download and install the official JDK (Java Development Kit). 2. Download an IDE (Integrated Development Environment), like BlueJ.

More information

AP CS Unit 7: Interfaces Exercises 1. Select the TRUE statement(s).

AP CS Unit 7: Interfaces Exercises 1. Select the TRUE statement(s). AP CS Unit 7: Interfaces Exercises 1. Select the TRUE statement(s). a) This code will not compile because a method cannot specify an interface as a parameter. public class Testing { public static void

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

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

AP CS Unit 6: Inheritance Exercises

AP CS Unit 6: Inheritance Exercises AP CS Unit 6: Inheritance Exercises 1. Suppose your program contains two classes: a Student class and a Female class. Which of the following is true? a) Making the Student class a subclass of the Female

More information

CSCI 161 Introduction to Computer Science

CSCI 161 Introduction to Computer Science CSCI 161 Introduction to Computer Science Department of Mathematics and Computer Science Lecture 2b A First Look at Class Design Last Time... We saw: How fields (instance variables) are declared How methods

More information

Goal of lecture. Object-oriented Programming. Context of discussion. Message of lecture

Goal of lecture. Object-oriented Programming. Context of discussion. Message of lecture Goal of lecture Object-oriented Programming Understand inadequacies of class languages like Ur- Java Extend Ur-Java so it becomes an object-oriented language Implementation in SaM heap allocation of objects

More information

Math Modeling in Java: An S-I Compartment Model

Math Modeling in Java: An S-I Compartment Model 1 Math Modeling in Java: An S-I Compartment Model Basic Concepts What is a compartment model? A compartment model is one in which a population is modeled by treating its members as if they are separated

More information

Review questions. Review questions, cont d. Class Definition. Methods. Class definition: methods. April 1,

Review questions. Review questions, cont d. Class Definition. Methods. Class definition: methods. April 1, April 1, 2003 1 Previous Lecture: Intro to OOP Class definition: instance variables & methods Today s Lecture: Instance methods Constructors Methods with input parameters Review questions Where do you

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

BM214E Object Oriented Programming Lecture 7

BM214E Object Oriented Programming Lecture 7 BM214E Object Oriented Programming Lecture 7 References References Revisited What happens when we say: int x; double y; char c;??? We create variables x y c Variable: Symbol plus a value Assume that we

More information

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

SPRING 13 CS 0007 FINAL EXAM V2 (Roberts) Your Name: A pt each. B pt each. C pt each. D or 2 pts each Your Name: Your Pitt (mail NOT peoplesoft) ID: Part Question/s Points available Rubric Your Score A 1-6 6 1 pt each B 7-12 6 1 pt each C 13-16 4 1 pt each D 17-19 5 1 or 2 pts each E 20-23 5 1 or 2 pts

More information

CS 102/107 - Introduction to Programming Midterm Exam #2 - Prof. Reed Spring 2011

CS 102/107 - Introduction to Programming Midterm Exam #2 - Prof. Reed Spring 2011 CS 102/107 - Introduction to Programming Midterm Exam #2 - Prof. Reed Spring 2011 What is your name?: This test has the following sections: I. True/False... 60 points; (30 questions, 2 points each) II.

More information

5.6.1 The Special Variable this

5.6.1 The Special Variable this ALTHOUGH THE BASIC IDEAS of object-oriented programming are reasonably simple and clear, they are subtle, and they take time to get used to And unfortunately, beyond the basic ideas there are a lot of

More information

Introduction to Computer Science I Spring 2010 Sample mid-term exam Answer key

Introduction to Computer Science I Spring 2010 Sample mid-term exam Answer key Introduction to Computer Science I Spring 2010 Sample mid-term exam Answer key 1. [Question:] (15 points) Consider the code fragment below. Mark each location where an automatic cast will occur. Also find

More information

CS 1331 Exam 1 ANSWER KEY

CS 1331 Exam 1 ANSWER KEY CS 1331 Exam 1 Fall 2016 ANSWER KEY Failure to properly fill in the information on this page will result in a deduction of up to 5 points from your exam score. Signing signifies you are aware of and in

More information

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

Introduction to Classes and Objects. David Greenstein Monta Vista High School Introduction to Classes and Objects David Greenstein Monta Vista High School Client Class A client class is one that constructs and uses objects of another class. B is a client of A public class A private

More information

BM214E Object Oriented Programming Lecture 8

BM214E Object Oriented Programming Lecture 8 BM214E Object Oriented Programming Lecture 8 Instance vs. Class Declarations Instance vs. Class Declarations Don t be fooled. Just because a variable might be declared as a field within a class that does

More information

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

Java: Classes. An instance of a class is an object based on the class. Creation of an instance from a class is called instantiation. Java: Classes Introduction A class defines the abstract characteristics of a thing (object), including its attributes and what it can do. Every Java program is composed of at least one class. From a programming

More information

AP Computer Science Lists The Array type

AP Computer Science Lists The Array type AP Computer Science Lists There are two types of Lists in Java that are commonly used: Arrays and ArrayLists. Both types of list structures allow a user to store ordered collections of data, but there

More information

Java Fundamentals (II)

Java Fundamentals (II) Chair of Software Engineering Languages in Depth Series: Java Programming Prof. Dr. Bertrand Meyer Java Fundamentals (II) Marco Piccioni static imports Introduced in 5.0 Imported static members of a class

More information

Logic & program control part 2: Simple selection structures

Logic & program control part 2: Simple selection structures Logic & program control part 2: Simple selection structures Summary of logical expressions in Java boolean expression means an expression whose value is true or false An expression is any valid combination

More information

CS32 - Week 4. Umut Oztok. Jul 15, Umut Oztok CS32 - Week 4

CS32 - Week 4. Umut Oztok. Jul 15, Umut Oztok CS32 - Week 4 CS32 - Week 4 Umut Oztok Jul 15, 2016 Inheritance Process of deriving a new class using another class as a base. Base/Parent/Super Class Derived/Child/Sub Class Inheritance class Animal{ Animal(); ~Animal();

More information

Introduction to Computer Science I

Introduction to Computer Science I Introduction to Computer Science I Classes Janyl Jumadinova 5-7 March, 2018 Classes Most of our previous programs all just had a main() method in one file. 2/13 Classes Most of our previous programs all

More information

Introduction to Java https://tinyurl.com/y7bvpa9z

Introduction to Java https://tinyurl.com/y7bvpa9z Introduction to Java https://tinyurl.com/y7bvpa9z Eric Newhall - Laurence Meyers Team 2849 Alumni Java Object-Oriented Compiled Garbage-Collected WORA - Write Once, Run Anywhere IDE Integrated Development

More information

BIT Java Programming. Sem 1 Session 2011/12. Chapter 2 JAVA. basic

BIT Java Programming. Sem 1 Session 2011/12. Chapter 2 JAVA. basic BIT 3383 Java Programming Sem 1 Session 2011/12 Chapter 2 JAVA basic Objective: After this lesson, you should be able to: declare, initialize and use variables according to Java programming language guidelines

More information

AP CS Unit 6: Inheritance Notes

AP CS Unit 6: Inheritance Notes AP CS Unit 6: Inheritance Notes Inheritance is an important feature of object-oriented languages. It allows the designer to create a new class based on another class. The new class inherits everything

More information

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University 9/5/6 CS Introduction to Computing II Wayne Snyder Department Boston University Today: Arrays (D and D) Methods Program structure Fields vs local variables Next time: Program structure continued: Classes

More information

Give one example where you might wish to use a three dimensional array

Give one example where you might wish to use a three dimensional array CS 110: INTRODUCTION TO COMPUTER SCIENCE SAMPLE TEST 3 TIME ALLOWED: 60 MINUTES Student s Name: MAXIMUM MARK 100 NOTE: Unless otherwise stated, the questions are with reference to the Java Programming

More information

CS 1331 Exam 1. Fall Failure to properly fill in the information on this page will result in a deduction of up to 5 points from your exam score.

CS 1331 Exam 1. Fall Failure to properly fill in the information on this page will result in a deduction of up to 5 points from your exam score. CS 1331 Exam 1 Fall 2016 Name (print clearly): GT account (gpburdell1, msmith3, etc): Section (e.g., B1): Signature: Failure to properly fill in the information on this page will result in a deduction

More information

1 Epic Test Review 2 Epic Test Review 3 Epic Test Review 4. Epic Test Review 5 Epic Test Review 6 Epic Test Review 7 Epic Test Review 8

1 Epic Test Review 2 Epic Test Review 3 Epic Test Review 4. Epic Test Review 5 Epic Test Review 6 Epic Test Review 7 Epic Test Review 8 Epic Test Review 1 Epic Test Review 2 Epic Test Review 3 Epic Test Review 4 Write a line of code that outputs the phase Hello World to the console without creating a new line character. System.out.print(

More information

CS-140 Fall Binghamton University. Methods. Sect. 3.3, 8.2. There s a method in my madness.

CS-140 Fall Binghamton University. Methods. Sect. 3.3, 8.2. There s a method in my madness. Methods There s a method in my madness. Sect. 3.3, 8.2 1 Example Class: Car How Cars are Described Make Model Year Color Owner Location Mileage Actions that can be applied to cars Create a new car Transfer

More information

CSE 143 Lecture 20. Circle

CSE 143 Lecture 20. Circle CSE 143 Lecture 20 Abstract classes Circle public class Circle { private double radius; public Circle(double radius) { this.radius = radius; public double area() { return Math.PI * radius * radius; public

More information

Assumptions. History

Assumptions. History Assumptions A Brief Introduction to Java for C++ Programmers: Part 1 ENGI 5895: Software Design Faculty of Engineering & Applied Science Memorial University of Newfoundland You already know C++ You understand

More information

Introduction to Computer Science Unit 2. Notes

Introduction to Computer Science Unit 2. Notes Introduction to Computer Science Unit 2. Notes Name: Objectives: By the completion of this packet, students should be able to describe the difference between.java and.class files and the JVM. create and

More information

CS1150 Principles of Computer Science Objects and Classes

CS1150 Principles of Computer Science Objects and Classes CS1150 Principles of Computer Science Objects and Classes Yanyan Zhuang Department of Computer Science http://www.cs.uccs.edu/~yzhuang CS1150 UC. Colorado Springs Object-Oriented Thinking Chapters 1-8

More information

Java Identifiers, Data Types & Variables

Java Identifiers, Data Types & Variables Java Identifiers, Data Types & Variables 1. Java Identifiers: Identifiers are name given to a class, variable or a method. public class TestingShastra { //TestingShastra is an identifier for class char

More information

Computer Components. Software{ User Programs. Operating System. Hardware

Computer Components. Software{ User Programs. Operating System. Hardware Computer Components Software{ User Programs Operating System Hardware What are Programs? Programs provide instructions for computers Similar to giving directions to a person who is trying to get from point

More information

class objects instances Fields Constructors Methods static

class objects instances Fields Constructors Methods static Class Structure Classes A class describes a set of objects The objects are called instances of the class A class describes: Fields (instance variables)that hold the data for each object Constructors that

More information

Static Methods. Why use methods?

Static Methods. Why use methods? Static Methods A method is just a collection of code. They are also called functions or procedures. It provides a way to break a larger program up into smaller, reusable chunks. This also has the benefit

More information

(12-1) OOP: Polymorphism in C++ D & D Chapter 12. Instructor - Andrew S. O Fallon CptS 122 (April 3, 2019) Washington State University

(12-1) OOP: Polymorphism in C++ D & D Chapter 12. Instructor - Andrew S. O Fallon CptS 122 (April 3, 2019) Washington State University (12-1) OOP: Polymorphism in C++ D & D Chapter 12 Instructor - Andrew S. O Fallon CptS 122 (April 3, 2019) Washington State University Key Concepts Polymorphism virtual functions Virtual function tables

More information

AP CS Unit 4: Classes and Objects Programs

AP CS Unit 4: Classes and Objects Programs AP CS Unit 4: Classes and Objects Programs 1. Copy the Bucket class. Make sure it compiles (but you won t be able to run it because it does not have a main method). public class Bucket { private double

More information

Methods. Methods. Mysteries Revealed

Methods. Methods. Mysteries Revealed Methods Methods and Data (Savitch, Chapter 5) TOPICS Invoking Methods Return Values Local Variables Method Parameters Public versus Private A method (a.k.a. func2on, procedure, rou2ne) is a piece of code

More information

CISC-124. Dog.java looks like this. I have added some explanatory comments in the code, and more explanation after the code listing.

CISC-124. Dog.java looks like this. I have added some explanatory comments in the code, and more explanation after the code listing. CISC-124 20180115 20180116 20180118 We continued our introductory exploration of Java and object-oriented programming by looking at a program that uses two classes. We created a Java file Dog.java and

More information

Outline. Object Oriented Programming. Course goals. Staff. Course resources. Assignments. Course organization Introduction Java overview Autumn 2003

Outline. Object Oriented Programming. Course goals. Staff. Course resources. Assignments. Course organization Introduction Java overview Autumn 2003 Outline Object Oriented Programming Autumn 2003 2 Course goals Software design vs hacking Abstractions vs language (syntax) Java used to illustrate concepts NOT a course about Java Prerequisites knowledge

More information

CS 200 Arrays, Loops and Methods Jim Williams, PhD

CS 200 Arrays, Loops and Methods Jim Williams, PhD CS 200 Arrays, Loops and Methods Jim Williams, PhD This Week 1. Team Lab: Arrays and Hangman a. drawing diagrams (bring paper and pencil) 2. BP1 Milestone 1 due Thursday 3. Lecture: More Arrays and Methods

More information

I. True/False: (2 points each)

I. True/False: (2 points each) CS 102 - Introduction to Programming Midterm Exam #1 - Prof. Reed Fall 2004 What is your name?: (0 points) There are two sections: I. True/False..............52 points; ( 26 questions, 2 points each) II.

More information

C10: Garbage Collection and Constructors

C10: Garbage Collection and Constructors CISC 3120 C10: Garbage Collection and Constructors Hui Chen Department of Computer & Information Science CUNY Brooklyn College 3/5/2018 CUNY Brooklyn College 1 Outline Recap OOP in Java: composition &

More information

University of Cape Town ~ Department of Computer Science Computer Science 1015F ~ Test 2. Question Max Mark Internal External

University of Cape Town ~ Department of Computer Science Computer Science 1015F ~ Test 2. Question Max Mark Internal External Name: Please fill in your Student Number and Name. Student Number : Student Number: University of Cape Town ~ Department of Computer Science Computer Science 1015F ~ 2009 Test 2 Question Max Mark Internal

More information

Chapter 5 Names, Bindings, Type Checking, and Scopes

Chapter 5 Names, Bindings, Type Checking, and Scopes Chapter 5 Names, Bindings, Type Checking, and Scopes Names Variables The Concept of Binding Scope and Lifetime Type Checking Referencing Environments Named Constants Names Used for variables, subprograms

More information

CS18000: Problem Solving And Object-Oriented Programming

CS18000: Problem Solving And Object-Oriented Programming CS18000: Problem Solving And Object-Oriented Programming Class (and Program) Structure 31 January 2011 Prof. Chris Clifton Classes and Objects Set of real or virtual objects Represent Template in Java

More information

More About Classes CS 1025 Computer Science Fundamentals I Stephen M. Watt University of Western Ontario

More About Classes CS 1025 Computer Science Fundamentals I Stephen M. Watt University of Western Ontario More About Classes CS 1025 Computer Science Fundamentals I Stephen M. Watt University of Western Ontario The Story So Far... Classes as collections of fields and methods. Methods can access fields, and

More information

Methods and Data (Savitch, Chapter 5)

Methods and Data (Savitch, Chapter 5) Methods and Data (Savitch, Chapter 5) TOPICS Invoking Methods Return Values Local Variables Method Parameters Public versus Private 2 public class Temperature { public static void main(string[] args) {

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

CS Introduction to Programming Midterm Exam #1 - Prof. Reed Fall 2009

CS Introduction to Programming Midterm Exam #1 - Prof. Reed Fall 2009 CS 102 - Introduction to Programming Midterm Exam #1 - Prof. Reed Fall 2009 What is your name?: There are two sections: I. True/False..................... 60 points; ( 30 questions, 2 points each) II.

More information

Java Review. Fundamentals of Computer Science

Java Review. Fundamentals of Computer Science Java Review Fundamentals of Computer Science Link to Head First pdf File https://zimslifeintcs.files.wordpress.com/2011/12/h ead-first-java-2nd-edition.pdf Outline Data Types Arrays Boolean Expressions

More information

ENGR 2710U Midterm Exam UOIT SOLUTION SHEET

ENGR 2710U Midterm Exam UOIT SOLUTION SHEET SOLUTION SHEET ENGR 2710U: Object Oriented Programming & Design Midterm Exam October 19, 2012, Duration: 80 Minutes (9 Pages, 14 questions, 100 Marks) Instructor: Dr. Kamran Sartipi Name: Student Number:

More information

public void m1(){ int n = 5; m2( n ); System.out.println( n ); }

public void m1(){ int n = 5; m2( n ); System.out.println( n ); } Review The Dog class will be used as needed. public class Dog { private int x; public Dog(int h){ x = h; public void set(int n){ x = n; public int get(){ return x; public String tostring(){ return "woof";

More information

Object-oriented programming. and data-structures CS/ENGRD 2110 SUMMER 2018

Object-oriented programming. and data-structures CS/ENGRD 2110 SUMMER 2018 Object-oriented programming 1 and data-structures CS/ENGRD 2110 SUMMER 2018 Lecture 1: Types and Control Flow http://courses.cs.cornell.edu/cs2110/2018su Lecture 1 Outline 2 Languages Overview Imperative

More information

ESE115 Introduction to Programming with Java. Midterm 2 November 10, 2005 SOLUTION

ESE115 Introduction to Programming with Java. Midterm 2 November 10, 2005 SOLUTION ESE115 Introduction to Programming with Java Midterm 2 November 10, 2005 SOLUTION 0 2D Arrays 1. Problems involving symmetry are common in computations involving art and the natural world. We will write

More information