Homework #3: Classes and Constructors

Slides:



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

Programming and Data Structure
C Structures Basics of structures Typedef. Data Hierarchy Byte –8 bits (ASCII character ‘A’ = ) Field –Group of characters (character string “Fred”)
C++ Basics March 10th. A C++ program //if necessary include headers //#include void main() { //variable declaration //read values input from user //computation.
Homework #2: Functions and Arrays
Eiffel examples. Example 1 class C creation default_create feature {ANY} -- the public class data count: INTEGER element: DOUBLE end; features - the data.
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.
Introduction to Application Programming IST 256 Application Programming for Information Systems Xiaozhong Liu
Programming with Collections Collections in Java Using Arrays Week 9.
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)
1 Lab Session-3 CSIT221 Spring 2003 b Group Worksheet 3 Exercise (Demo Required) b No new lab demo will be assigned to allow you to focus on HW#1.
1 11/8/06CS150 Introduction to Computer Science 1 Arrays Chapter 8 page 477 November 13, 2006.
Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Slide 1 Overview – Chapter Section 4 Arrays and Classes (10.4)
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.
Array Processing: Exercises School of Business Eastern Illinois University © Abdou Illia, Spring 2002 (Week 10, Friday 3/28/2003)
Crypto Project Sample Encrypted Data: –Turing: CS Public\CryptoProjectData Crypto_Short_Keys_P_U.out Crypto_Long_Keys_O_R.out Bonus +10 points on.
Arrays and ArrayLists in Java L. Kedigh. Array Characteristics List of values. A list of values where every member is of the same type. Each member in.
Array Cs212: DataStructures Lab 2. Array Group of contiguous memory locations Each memory location has same name Each memory location has same type a.
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.
Homework #5: Pointers, Dynamic Arrays and Inheritance
DATA STRUCTURES LAB 1 TA: Nouf Al-harbi
CHAPTER 7 DATA INPUT OUTPUT Prepared by: Lec. Ghader R. Kurdi.
OOP in Java : © W. Milner 2005 : Slide 1 Java and OOP Part 2 – Classes and objects.
19&20-2 Know how to declare pointer variables. Understand the & (address) and *(indirection) operators. Dynamic Memory Allocation Related Chapter: ABC.
CS161 Topic #16 1 Today in CS161 Lecture #16 Prepare for the Final Reviewing all Topics this term Variables If Statements Loops (do while, while, for)
Arrays II (Strings). Data types in C Integer : int i; Double: double x; Float: float y; Character: char ch; char cha[10], chb[]={‘h’,’e’,’l’,’l’,’o’};
Introduction to C Programming Lecture 6. Functions – Call by value – Call by reference Arrays Today's Lecture Includes.
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.
Lab 1 Logbook ADT. OVERVIEW A monthly logbook consists of a set of entries, one for each day of the month.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 8 Arrays.
Arithmetic, Class Variables and Class Methods Week 11
C Programming Lecture 16 Pointers. Pointers b A pointer is simply a variable that, like other variables, provides a name for a location (address) in memory.
Homework #4: Operator Overloading and Strings By J. H. Wang Apr. 17, 2009.
1 CSC103: Introduction to Computer and Programming Lecture No 24.
Copyright © 2002 W. A. Tucker1 Chapter 9 Lecture Notes Bill Tucker Austin Community College COSC 1315.
Homework #2: Functions and Arrays By J. H. Wang Mar. 24, 2014.
Homework #1: C++ Basics, Flow of Control, and Function Basics
Structuring Data: Arrays ANSI-C. Representing multiple homogenous data Problem: Input: Desired output:
Programming Fundamentals I Java Programming Spring 2009 Instructor: Xuan Tung Hoang TA: Tran Minh Trung Lab 03.
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.
Homework #5: Pointers, Dynamic Arrays and Inheritance By J. H. Wang May 31, 2011.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 11P. 1Winter Quarter Arrays Lecture 11.
FUNCTIONS. Midterm questions (1-10) review 1. Every line in a C program should end with a semicolon. 2. In C language lowercase letters are significant.
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
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.
13/10/2016CS150 Introduction to Computer Science 1 Multidimensional Arrays  Arrays can have more than one column  Two dimensional arrays have two columns.
1 CSC103: Introduction to Computer and Programming Lecture No 17.
Homework #4: Operator Overloading and Strings By J. H. Wang May 22, 2015.
100 學年度碩士班新生暑期課程 程式設計 Object-Oriented Programming: Part I The Basics of Classes.
Lecture 2 Arrays. Topics 1 Arrays hold Multiple Values 2 Accessing Array Elements 3 Inputting and Displaying Array Contents 4 Array Initialization 5 Using.
Introduction to programming in java Lecture 21 Arrays – Part 1.
Object-Oriented Design Chapter 7 1. Objectives You will be able to Use the this reference in a Java program. Use the static modifier for member variables.
CCSA 221 Programming in C CHAPTER 3 COMPILING AND RUNNING YOUR FIRST PROGRAM 1 ALHANOUF ALAMR.
Classes and Objects.
C++ Arrays.
HKCT Java OOP Unit 02 Object Oriented Programming in Java Unit 02 Methods, Classes, and Objects 1.
Classes & Objects: Examples
Implementing Classes Chapter 3.
Chapter 11 Classes.
Presentation transcript:

Homework #3: Classes and Constructors By J. H. Wang Apr. 17, 2012

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

(…continued from the previous slide) And suppose the main function of your program contains the following declarations and that the program somehow sets the values of all the member variables to some values: Automobile hyundai, jaguar, tata; double aPrice, aProfit; int cnt; (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) hyundai.price = 4999.99; (b) jaguar.setPrice(30000.97); (c) tata.setPrice(25000.99); (d) tata.setCount(500); (e) aPrice = jaguar.getPrice(); (f) aProfit = jaguar.getProfit(); (g) hyundai =jaguar; (h) tata.count = 500;

2. (a) Explain what public: and private: mean in a class definition. (b) When you define a C++ class, should you make the member variables public or private? Should you make the member functions public or private? Please explain the reasons why. 3. What is a default constructor? Does every class have a default constructor?

Part II: Programming Exercises Define a class IntSet that can store a set of integers. Write the following member functions: (a) a default constructor and a constructor that can put an array of integers into the set, (b) a member function that puts a single integer into the set, (c) a member function that returns the current number of elements in the set, (d) two member functions that computes the sum and average of the current set of integers respectively, (e) a member function that lists all the integers in the current set. Embed the class in a C++ test program.

5. Define a class Month that is an abstract data type for a month 5. Define a class Month that is an abstract data type for a month. Your class will have one member variable of type int to represent a month (1 for January, 2 for February, and so on). Include the following member functions: (a) a constructor to set the month using the first three letters in the English name of the month as one string argument (“JAN” for January, “FEB” for February, and so forth), (b) a constructor to set the month using an integer as an argument (1 for January, 2 for February, 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 month as an integer, (e) an input function that reads the month as the first three letters in the name of the month, (f) an output function that displays the month as an integer, (g) an output function that displays the month as the first three letters in the name of the month, (h) and a member function that returns the next month as a value of type Month. Embed your class in a C++ test program.

Homework Submission Due: 2 weeks (May 1, 2012) Hand-written exercises Please write your name and answers on papers Programs Please submit to http://140.124.183.39/oop/ File names: Please name your file according to our homework. For example, HW1.zip or quiz2.rar

Any Questions?