Homework 5. Yuji Shimojo CMSC 330. Instructor: Prof. Reginald Y. Haseltine

Similar documents
Homework 6. Yuji Shimojo CMSC 330. Instructor: Prof. Reginald Y. Haseltine

Common Misunderstandings from Exam 1 Material

CSI33 Data Structures

Linked List using a Sentinel

CS2141 Software Development using C/C++ C++ Basics

Lecture Notes CPSC 224 (Spring 2012) Today... Java basics. S. Bowers 1 of 8

Topics. bool and string types input/output library functions comments memory allocation templates classes

CSCE 110 PROGRAMMING FUNDAMENTALS

CE221 Programming in C++ Part 1 Introduction

ECE 462 Exam 1. 6:30-7:30PM, September 22, 2010

Agenda. The main body and cout. Fundamental data types. Declarations and definitions. Control structures

Short Notes of CS201

CPSC 427: Object-Oriented Programming

CS201 - Introduction to Programming Glossary By

1. The term STL stands for?

Program template-smart-pointers-again.cc

Assumptions. History

Program template-smart-pointers.cc

Function Overloading

CS 376b Computer Vision

pointers & references

Introduction to Programming Using Java (98-388)

2 2

Use the dot operator to access a member of a specific object.

Vectors of Pointers to Objects. Vectors of Objects. Vectors of unique ptrs C++11. Arrays of Objects

Due Date: See Blackboard

Chapter 2. Procedural Programming

STL components. STL: C++ Standard Library Standard Template Library (STL) Main Ideas. Components. Encapsulates complex data structures and algorithms

Programming in C++: Assignment Week 8

W3101: Programming Languages C++ Ramana Isukapalli

COSC 320 Exam 2 Key Spring Part 1: Hash Functions

CSCE 110 PROGRAMMING FUNDAMENTALS

Lecture 8. Xiaoguang Wang. February 13th, 2014 STAT 598W. (STAT 598W) Lecture 8 1 / 47

Interview Questions of C++

COMP 2355 Introduction to Systems Programming

Due Date: See Blackboard

CE221 Programming in C++ Part 2 References and Pointers, Arrays and Strings

Bruce Merry. IOI Training Dec 2013

University of Maryland Baltimore County. CMSC 202 Computer Science II. Fall Mid-Term Exam. Sections

1 Short Answer (5 Points Each)

Lab 1: First Steps in C++ - Eclipse

Financial computing with C++

Variables. Data Types.

Introduction Of Classes ( OOPS )

CSC1322 Object-Oriented Programming Concepts

More About Classes. Gaddis Ch. 14, CS 2308 :: Fall 2015 Molly O'Neil

การทดลองท 8_2 Editor Buffer Array Implementation

Introduction to Programming

CPSC 427: Object-Oriented Programming

C:\Temp\Templates. Download This PDF From The Web Site

Jordan University of Science & Technology Department of Computer Science CS 211 Exam #1 (23/10/2010) -- Form A

CA341 - Comparative Programming Languages

The University of Nottingham

Semantics of C++ Hauptseminar im Wintersemester 2009/10 Templates

a data type is Types

#include <iostream> #include <algorithm> #include <cmath> using namespace std; int f1(int x, int y) { return (double)(x/y); }

CMSC 202 Midterm Exam 1 Fall 2015

Object oriented programming

Pointers and Arrays CS 201. This slide set covers pointers and arrays in C++. You should read Chapter 8 from your Deitel & Deitel book.

CMSC 202 Section 010x Spring Justin Martineau, Tuesday 11:30am

COEN244: Class & function templates

CAAM 420 Fall 2012 Lecture 29. Duncan Eddy

(A) 99 ** (B) 100 (C) 101 (D) 100 initial integers plus any additional integers required during program execution

ECE 462 Midterm Exam 1. 10:30-11:20AM, September 21, 2007

CS 251 INTERMEDIATE SOFTWARE DESIGN SPRING C ++ Basics Review part 2 Auto pointer, templates, STL algorithms

Object oriented programming

Your first C++ program

More Advanced Class Concepts

Exam 3 Chapters 7 & 9

Intermediate Programming, Spring 2017*

IS0020 Program Design and Software Tools Midterm, Fall, 2004

UEE1303(1070) S12: Object-Oriented Programming Advanced Topics of Class

CSC Java Programming, Fall Java Data Types and Control Constructs

Classes: Member functions // classes example #include <iostream> using namespace std; Objects : Reminder. Member functions: Methods.

Programming C++ Lecture 5. Howest, Fall 2013 Instructor: Dr. Jennifer B. Sartor

1/29/2011 AUTO POINTER (AUTO_PTR) INTERMEDIATE SOFTWARE DESIGN SPRING delete ptr might not happen memory leak!

Intermediate Programming, Spring 2017*

Unified Modeling Language a case study

CS250 Final Review Questions

C++_ MARKS 40 MIN

EECE.3220: Data Structures Spring 2017

Non-numeric types, boolean types, arithmetic. operators. Comp Sci 1570 Introduction to C++ Non-numeric types. const. Reserved words.

Due Date: See Blackboard

Friend Functions, Inheritance

CS

ECE 2400 Computer Systems Programming Fall 2017 Topic 15: C++ Templates

Distributed Real-Time Control Systems. Lecture 17 C++ Programming Intro to C++ Objects and Classes

Lecture 2, September 4

Announcements. CSCI 334: Principles of Programming Languages. Lecture 18: C/C++ Announcements. Announcements. Instructor: Dan Barowy

Inheritance and Polymorphism

There are, of course, many other possible solutions and, if done correctly, those received full credit.

OBJECT ORIENTED PROGRAMMING USING C++

Templates and Vectors

CS201- Introduction to Programming Current Quizzes

CSc 328, Spring 2004 Final Examination May 12, 2004

Programming Abstractions

typedef Labeling<unsigned char,short> LabelingBS; typedef Labeling<unsigned char,short>::regioninfo RegionInfoBS;

1- Write a single C++ statement that: A. Calculates the sum of the two integrates 11 and 12 and outputs the sum to the consol.

Introduction to C++ Systems Programming

array Indexed same type

Transcription:

Homework 5 Yuji Shimojo CMSC 330 Instructor: Prof. Reginald Y. Haseltine July 13, 2013

Question 1 Consider the following Java definition of a mutable string class. class MutableString private char[] chars = new char[200]; private int size = 0; public boolean set(char achar, int index) if (index < 0 index >= chars.length) return false; chars[index] = achar; return true; public String tostring() String result = ""; for (int i = 0; i < size; i++) result += chars[i]; return result; Suppose this class was rewritten in C++ in two ways, the first with the array that represents the list as a stack-dynamic variable, and then with the list as a heap dynamic variable.

Explain when constructors and destructors would be needed. Explain why no constructors or destructors are needed in the Java implementation. Answer 1 First, I ran the following Java class, which I added a main method. MutableString Program in Java class MutableString public static void main(string [] args) MutableString mutablestring = new MutableString(); System.out.println(mutableString.set('A', 0)); System.out.println(mutableString.toString()); private char[] chars = new char[200]; private int size = 1; public boolean set(char achar, int index) if (index < 0 index >= chars.length) return false; chars[index] = achar; return true; public String tostring()

String result = ""; for (int i = 0; i < size; i++) result += chars[i]; return result; Output true A I rewrote the above program in C++ by two approaches as below. The first is using stackdynamic variables, and the second is using heap dynamic variables, so that it allocates heap memory dynamically when the class objects is instantiated. MutableString Program in C++ #1 #include <iostream> #include <string> using namespace std; char chars[200]; int size = 1; bool set(char achar, int index)

if (index < 0 index >= sizeof(chars)) return false; chars[index] = achar; return true; string tostring() string result = ""; for (int i = 0; i < size; i++) result += chars[i]; return result; int main (int argc, const char * argv[]) cout << set('a',0) << endl; cout << tostring() << endl; return 0; Output 1 A MutableString Program in C++ #2 #include <iostream>

#include <string> using namespace std; class MutableString private: char chars[200]; int size; public: // constructor MutableString() size = 1; cout << "constructor is called!" << endl; // destructor ~MutableString() cout << "destructor is called!" << endl; bool set(char achar, int index) if (index < 0 index >= sizeof(chars)) return false; chars[index] = achar; return true;

string tostring() string result = ""; for (int i = 0; i < size; i++) result += chars[i]; return result; ; int main (int argc, const char * argv[]) MutableString mutablestring; cout << boolalpha << mutablestring.set('a',0) << endl; cout << mutablestring.tostring() << endl; return 0; Output constructor is called! true A destructor is called! The output of the second program shows when the constructor and the destructor are called. Constructors are needed when the object is instantiated, and destructors are needed when the scope of the object is terminated.

Question 2 Consider the following C++ template class. template <typename T, int length> class Vector public: Vector(T values[length]) for (int i = 0; i < length; i++) list[i] = values[i]; friend bool operator<(const Vector<T, length>& left, const Vector<T, length>& right) bool result = true; for (int i = 0; i < length; i++) result &= left.list[i] < right.list[i]; return result; private: T list[length]; ; int main()

int first[] = 1, 2, second[] = 2, 3; Vector<int, 2> vector1(first), vector2(second); cout << (vector1 < vector2) << endl; return 0; The class Vector cannot be instantiated for any arbitrary type. For example, consider the following instantiation for a wrapper integer class. class Int public: Int(int i = 0) this->i = i; private: int i; ; int main() Int first[] = Int(1), Int(2), second[] = Int(2), Int(3); Vector<Int, 2> vector1(first), vector2(second); cout << (vector1 < vector2) << endl; return 0;

Explain why the second implementation fails. What must be added to that class so this program will compile? Suppose this program were written in Java. Explain how Java allows the constraints on a generic type parameter to be specified and how they would be specified in this case Java does have one limitation, however. Although wrapper classes can be used to instantiate generic type parameters, primitive types cannot. Explain why. Output of the First Implementation true Answer 2 The second implementation fails because the operands of friend bool operator are Vector objects. When the operator is used in main method, the type of arguments is indicated as integer. I created binomial comparison operating program in Java by using Vector class, generics, and compareto method as below. GenericTypeParameterTest class import java.util.vector; public class GenericTypeParameterTest public static void main(string[] args)

int resultint=0; // Instantiates a generic type of Vector object Vector<String> strings = new Vector<String>(); strings.add("abc"); strings.add("def"); resultint = strings.elementat(0).compareto(strings.elementat(1)); System.out.println(isLeftSmall(resultInt)); // Instantiates a generic type of Vector object Vector<Integer> integers = new Vector<Integer>(); integers.add(1); integers.add(2); resultint = integers.elementat(0).compareto(integers.elementat(1)); System.out.println(isLeftSmall(resultInt)); // compareto method which returns a boolean value static boolean isleftsmall(int resultint) boolean result = true; if (resultint < 0) result = true; else if (resultint > 0) result = false; return result;

Output true true