Homework #3: Classes and Constructors By J. H. Wang Apr. 24, 2015.

Slides:



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

Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 4 Modular Programming with Functions.
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 4 : Polymorphism King Fahd University of Petroleum & Minerals College of Computer.
Container Classes A container class is a data type that is capable of holding a collection of items. In C++, container classes can be implemented as.
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.
CSCI 160 Midterm Review Rasanjalee DM.
EC-241 Object-Oriented Programming
Homework #2: Functions and Arrays
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide Overview 10.1 Structures 10.2 Classes 10.3 Abstract Data Types.
Wednesday, 10/2/02, Slide #1 CS 106 Intro to CS 1 Wednesday, 10/2/02  QUESTIONS (on HW02 – due at 5 pm)??  Today:  Review of parameters  Introduction.
Tutorial 6 February 4th/5th, 2015
Display a 12-Month Calendar CS-2301 D-term Programming Assignment #2 12-Month Calendar CS-2301 System Programming C-term 2009 (Slides include materials.
Wednesday, 12/11/02, Slide #1 CS 106 Intro to Comp. Sci. 1 Wednesday, 12/11/02  QUESTIONS??  Today: CLOSING CEREMONIES!  HW #5 – Back Monday (12/16)
COMP 110 Introduction to Programming Mr. Joshua Stough October 8, 2007.
C++ data types. Structs vs. Classes C++ Classes.
Class template Describing a generic class Instantiating classes that are type- specific version of this generic class Also are called parameterized types.
Classes with multiple methods Part 1. Review of classes & objects Early on, we learned that objects are the basic working units in object-oriented programs.
CS 117 Spring 2002 April 5, Exam 3 April 10 –files, arrays, strings, classes –practice exams are up –review on Monday.
Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Slide 1 Overview – Chapter Section 4 Arrays and Classes (10.4)
Practice for Midterm 1. Practice problems These slides have six programming problems for in-class practice There are an additional seven programming problems.
Assignment #2, 12- month Calendar CS-2301, B-Term Programming Assignment #2 12-Month Calendar CS-2301, System Programming for Non-Majors (Slides.
CS 241 – Computer Programming II Lab Kalpa Gunaratna –
CPS 2231 Computer Organization and Programming Instructor: Tian (Tina) Tian.
Introduction to Object Oriented Design. Topics Designing Your Own Classes Attributes and Behaviors Class Diagrams.
Introduction to Classes and Objects (Through Ch 5) Dr. John P. Abraham Professor UTPA.
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals; Apply data abstraction.
Classes and Objects. Topics The Class Definition Declaring Instance Member Variables Writing Instance Member Methods Creating Objects Sending Messages.
Homework #3: Classes and Constructors
BUILDING JAVA PROGRAMS CHAPTER 7 Arrays. Exam #2: Chapters 1-6 Thursday Dec. 4th.
Homework #4: Operator Overloading and Strings By J. H. Wang May 8, 2012.
Utilities (Part 2) Implementing static features 1.
Homework #5: Pointers, Dynamic Arrays and Inheritance
DATA STRUCTURES LAB 1 TA: Nouf Al-harbi
OOP in Java : © W. Milner 2005 : Slide 1 Java and OOP Part 2 – Classes and objects.
Chapter 3 Part I. 3.1 Introduction Programs written in C ◦ All statements were located in function main Programs written in C++ ◦ Programs will consist.
1 CSC241: Object Oriented Programming Lecture No 02.
Homework #2: Functions and Arrays By J. H. Wang Mar. 20, 2012.
Homework #1: C++ Basics, Flow of Control, and Function Basics By J. H. Wang Mar. 13, 2012.
Chapter 3 Introduction to Classes and Objects Definitions Examples.
Static. 2 Objectives Introduce static keyword –examine syntax –describe common uses.
Review TEST 2 Chapters 4,5,7. QUESTION For which type of operands does the == operator always work correctly: (a) int, (b) double, or (c) String?
Homework #4: Operator Overloading and Strings By J. H. Wang Apr. 17, 2009.
Homework #2: Functions and Arrays By J. H. Wang Mar. 24, 2014.
Homework #1: C++ Basics, Flow of Control, and Function Basics
Homework #3: Classes and Constructors By J. H. Wang Apr. 14, 2014.
Homework #4: Operator Overloading and Strings By J. H. Wang May 12, 2014.
Homework #5: Pointers, Dynamic Arrays and Inheritance By J. H. Wang Jun. 5, 2009.
1 Mr. Muhammad Hanif Lecturer Information Technology MBBS Campus Dadu University of SIndh.
The for loop.
Homework #5: Pointers, Dynamic Arrays and Inheritance By J. H. Wang May 31, 2011.
Week 12 - Monday.  What did we talk about last time?  Defining classes  Class practice  Lab 11.
Struct s (7.4) Used as data aggregates for an entity can be different types of data e.g. for student id, name, GPA, address,... Similar to classes, but.
Extra Recitations Wednesday 19:40-22:30 FENS L055 (tomorrow!) Friday 13:40-16:30 FENS L063 Friday 17: :30 FENS L045 Friday 19:40-22:30 FENS G032.
Classes - Intermediate
1 Introduction to Computer Science for Majors II CPSC 233, Winter 2013 CPSC 233, winter 2013 Tutorial 7, Feb 6/7, 2013.
Homework #3: Classes and Constructors
College Board Topics – A.P. Computer Science A Program Design - Read and understand a problem's description, purpose, and goals; Apply data abstraction.
Homework #4: Operator Overloading and Strings By J. H. Wang May 22, 2015.
100 學年度碩士班新生暑期課程 程式設計 Object-Oriented Programming: Part I The Basics of Classes.
Class Definitions: The Fundamentals Chapter 6 3/30/15 & 4/2/15 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education.
Classes and Objects.
Examples of Classes & Objects
HKCT Java OOP Unit 02 Object Oriented Programming in Java Unit 02 Methods, Classes, and Objects 1.
Classes & Objects: Examples
Implementing Classes Chapter 3.
Chap 1 Chap 2 Chap 3 Chap 5 Surprise Me
CSC 111 Exam 3 Review Fall 2005.
Review of Previous Lesson
Agenda Types and identifiers Practice Assignment Keywords in Java
Separating Interface from Implementation
Presentation transcript:

Homework #3: Classes and Constructors By J. H. Wang Apr. 24, 2015

Part I: Hand-Written Exercises 1. Suppose your program contains the following class, class SmartPhone { public: void setPrice(double newPrice); void setProfit(double newProfit); double getPrice(); private: double price; double profit; double getProfit(); }; (To be continued on the next slide…)

(…continued from the previous slide) And suppose the main function of your program contains the following declaration and that the program somehow sets the values of all the member variables to some values: SmartPhone iphone6, samsung_s6, htc1_m9; double price, profit; (To be continued on the next slide…)

(…continued from the previous slide) Which of the following statements are then allowed in the main function of your program? For illegal ones, please explain the reasons why they are illegal. (a) iphone6.price = ; (b) samsung_s6.setPrice(199.97); (c) htc1_m9 = samsung_s6; (d) price = htc1_m9.getPrice(); (e) profit = iphone6.profit; (f) iphon6.setProfit(samsung_s6.getProfit());

2. (a) Explain what public: and private: mean in a class definition. (b) What should be made public and what should be private? Please describe the reasons. (c) When you define a C++ class, what items are considered part of the interface ? What items are considered part of the implementation ? 3. What are static variables and static functions ? When do we need them? Please give an example.

Part II: Programming Exercises 4.Define a class for a type called CounterType. An object of this type is used to count things, so it records a count that is a nonnegative integer. Include the following member functions: (a) a function that sets the counter to a count given as an argument, (b) functions that increase the count by one and decrease the count by one. Be sure that no member function allows the value of the counter to become negative or out of range for nonnegative whole numbers, (c) a function that returns the current count value, and (d) a function that outputs the count. Embed your class definition in a C++ test program.

5.Define a class called Day that is an abstract data type for a day. Your class will have one member variable of type int to represent a day (1 for Monday, 2 for Tuesday, and so forth). Include all the following member functions: (a) a constructor to set the day using the first three letters in the name of the day as one string argument (“MON” for Monday, “TUE” for Tuesday, and so forth), (b) a constructor to set the day using an integer as an argument (1 for Monday, 2 for Tuesday, and so forth), (c) a default constructor, (To be continued on the next slides…)

(… continued from the previous slides) (d) an input function that reads the day as an integer, (e) an input function that reads the day as the first three letters in the name of the day, (f) an output function that displays the day as an integer, (g) an output function that displays the day as the first three letters in the name of the day, (h) and a member function that returns the next day as a value of type Day. Embed your class definition in a C++ test program.

Homework Submission Due: 2 weeks (May 8, 2015) Hand-written exercises –Please write your name and answers on papers Programs –Please submit to – File names : Please name your file according to our homework. For example, _HW1.zip or _quiz2.rar

Any Questions or Comments?