1 Introduction to Computer Science for Majors II CPSC 233, Winter 2013 CPSC 233, winter 2013 Tutorial 7, Feb 6/7, 2013.

Slides:



Advertisements
Similar presentations
Classes & Objects INTRODUCTION : This chapter introduces classes ; explains data hiding, abstraction & encapsulation and shows how a class implements these.
Advertisements

Introduction to classes Sangeetha Parthasarathy 06/11/2001.
Continuation of chapter 6…. Nested while loop A while loop used within another while loop is called nested while loop. Q. An illustration to generate.
Introduction to Computer Science Robert Sedgewick and Kevin Wayne Recursive Factorial Demo pubic class Factorial {
Copyright 2008 by Pearson Education Building Java Programs Chapter 8 Lecture 8-3: Encapsulation, toString reading: self-checks: #13-18,
Problem Solving 5 Using Java API for Searching and Sorting Applications ICS-201 Introduction to Computing II Semester 071.
Using Classes to Store Data Computer Science 2 Gerb.
METHOD OVERRIDING Sub class can override the methods defined by the super class. Overridden Methods in the sub classes should have same name, same signature.
METHOD OVERRIDING 1.Sub class can override the methods defined by the super class. 2.Overridden Methods in the sub classes should have same name, same.
Introduction to C# Erick Pranata © Sekolah Tinggi Teknik Surabaya 1.
Syntax & terminology review While the following slides are not exactly what we did on the board (object diagrams are not shown here) they cover most of.
CS 106 Introduction to Computer Science I 03 / 21 / 2008 Instructor: Michael Eckmann.
Class template Describing a generic class Instantiating classes that are type- specific version of this generic class Also are called parameterized types.
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.
Understanding class definitions – Part II –. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Main.
More C++ Bryce Boe 2013/07/18 CS24, Summer 2013 C.
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals; Apply data abstraction.
Creating Simple Classes. Outline of Class Account Class Account Account # Balance Holder name phone# Overdrawn (true/false) Data Members Open Credit Debit.
An Introduction to Java Chapter 11 Object-Oriented Application Development: Part I.
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!
ACO 101: Introduction to Computer Science Anatomy Part 2: Methods.
Objects, Classes and Syntax Dr. Andrew Wallace PhD BEng(hons) EurIng
© 2006 Pearson Addison-Wesley. All rights reserved How to Design a Supplier 1) Determine the public methods. (Don’t forget the constructor(s).) 2)
JAVA Classes Review. Definitions Class – a description of the attributes and behavior of a set of computational objects Constructor – a method that is.
OOP in Java : © W. Milner 2005 : Slide 1 Java and OOP Part 2 – Classes and objects.
RECITATION 4. Classes public class Student { } Data Members public class Student { private String name; public String id; }
CSC 142 Computer Science II Zhen Jiang West Chester University
Java - Classes JPatterson. What is a class? public class _Alpha { public static void main(String [] args) { } You have been using classes all year – you.
1 Introduction to Computer Science for Majors II CPSC 233, Winter 2013 CPSC 233, winter 2013 Tutorial 14, Mar 27/28, 2013.
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
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 Introduction to Classes and Objects Definitions Examples.
JAVA PROGRAMMING PART III. METHOD STATEMENT Form of method statement [ ] [static] ( [ ]) { } Example public static void main(String args[])
CreatingClasses-SlideShow-part31 Creating Classes part 3 Barb Ericson Georgia Institute of Technology Dec 2009.
1 2/21/05CS250 Introduction to Computer Science II Destructors, Get and Set, and Default Memberwise Assignment.
CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Classes and Objects (contd.) Course Lecture Slides 19 May 2010.
Methods.
Homework #3: Classes and Constructors By J. H. Wang Apr. 24, 2015.
CPSC 233 Tutorial 5 February 9 th /10 th, Java Classes Each Java class contains a set of instance variables and methods Instance Variables: Type.
Computer Science A 1. Course plan Introduction to programming Basic concepts of typical programming languages. Tools: compiler, editor, integrated editor,
CPSC 233 Tutorial Xin Liu Feb 14, Tips on keyboard input How to detect that the user just hit enter when prompted for a string import java.util.Scanner;
1 Introduction to Computer Science for Majors II CPSC 233, Winter 2013 CPSC 233, winter 2013 Tutorial 14, Mar 27/28, 2013.
Basic Class Structure. Class vs. Object class - a template for building an object –defines the instance data that the object will hold –defines instance.
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner Class Design II Lecture 7, Thu Jan
CPSC 233 Tutorial 12 March 4/5 th, TopHat Quiz int[] a = {0}; int[] b = {1}; a = b; What is the value of a[0] i) 0 ii) 1.
Re-Intro to Object Oriented Programming
Examples of Classes & Objects
Department of Computer Science
How to Design Supplier Classes
Class Definitions and Writing Methods
Structures Revisited what is an aggregate construct? What aggregate constructs have we studied? what is a structure? what is the keyword to define a structure?
Anatomy of a class Part I
CS 302 Week 11 Jim Williams, PhD.
CSC240 Computer Science III
Dr Shahriar Bijani Winter 2017
Initializing Arrays char [] cArray3 = {'a', 'b', 'c'};
An Introduction to Java – Part II
Classes & Objects: Examples
Implementing Classes Chapter 3.
CS2011 Introduction to Programming I Methods (II)
Scope of variables class scopeofvars {
Object-Oriented Programming
Instance Method – CSC142 Computer Science II
Properties.
ITE “A” GROUP 2 ENCAPSULATION.
Anatomy of a class Part I
Classes and Objects Systems Programming.
CSG2H3 Object Oriented Programming
CPSC 233 Tutorial 13 March 11/12th, 2015.
Presentation transcript:

1 Introduction to Computer Science for Majors II CPSC 233, Winter 2013 CPSC 233, winter 2013 Tutorial 7, Feb 6/7, 2013

Quiz 2 Definitions OO exercises Quiz 2 CPSC 233, winter

Definitions Access modifiers: public vs. private OO: declaration vs. instantiation OO: reference vs. object Variables: scope vs. lifetime Class members: attributes vs. methods Information hiding: Mutators vs. accessors CPSC 233, winter

Quiz 2 Definitions OO exercises Quiz 2 CPSC 233, winter

Exercise Identify the class definition, body of methods, attributes, local variables. CPSC 233, winter public class Inventory 2. { 3. private String productName; 4. private int productID; 5. public int stockLevel; 6. public void setProductInfo (String name, int id, int level) 7. { 8. productName = name; 9. productID = id; 10. stockLevel = level; 11. } 12. public void getProductInfo () 13. { 14. String info = ""; 15. info += productName + "," + productID + "," + stockLevel; 16. return info; 17. } 18. }

Exercise Identify the scope, in terms of line numbers, of each attribute and local variable CPSC 233, winter public class Inventory 2. { 3. private String productName; 4. private int productID; 5. public int stockLevel; 6. public void setProductInfo (String name, int id, int level) 7. { 8. productName = name; 9. productID = id; 10. stockLevel = level; 11. } 12. public void getProductInfo () 13. { 14. String info = ""; 15. info += productName + "," + productID + "," + stockLevel; 16. return info; 17. } 18. }

Exercise Given the Inventory class, identify the invalid statements in the following Driver class. CPSC 233, winter public class Driver 2. { 3. static void main (String[] args) 4. { 5. Inventory product; 6. product.setProductInfo("bread", 3456, 100); 7. product = new Inventory(); 8. product.setProductInfo ("milk", 1234, 50); 9. System.out.println(product.productName); 10. product.productID = 3425; 11. product.stockLevel --; 12. product = null; 13. System.out.println(product.getProductInfo()); 14. } 15. }

Exercise Given the Driver class, implement the missing methods in the Inventory class. CPSC 233, winter public class Driver 2. { 3. static void main (String[] args) 4. { 5. Inventory product = new Inventory(); 6. product.setProductName ("bread"); 7. product.setProductID (3456); 8. product.increaseStock (); // increase stock level by 1 9. System.out.println(product.getProductInfo()); 10. product.stockLevel --; 11. System.out.println(product.getProductInfo()); 12. } 13. }

Exercise Given the Inventory class, complete the main() method in the Driver class to do the following. Instantiates two objects of the Inventory class Update the product info for the 1 st object to “coke” with product ID as 1239 and stock level 40 Update the produce info for the 2 nd object to “pepsi” with produce ID as 1237 and stock level 39 Increase the stock level for both product using the new method from the previous slide Print the information of both products CPSC 233, winter public class Driver { static void main (String[] args) { }

Quiz 2 Definitions OO exercises Quiz 2 CPSC 233, winter