Question

This lab will exercise your understanding of some of the concepts covered in Chapter 10: classes,...

This lab will exercise your understanding of some of the concepts covered in Chapter 10:
classes, default constructors, overloaded constructors, arrays of classes
  
A Roman numeral represents an integer using letters. Examples are XVII to represent 17,
MCMLIII for 1953, and MMMCCCIII for 3303. By contrast, ordinary numbers such as 17 or 1953
are called Arabic numerals. The following table shows the Arabic equivalent of all
the single-letter Roman numerals:

M 1000 X 10
D 500 V 5
C 100 I 1
L 50


When letters are strung together, the values of the letters are just added,
with the following exception. When a letter of smaller value is followed by
a letter of larger value, the smaller value is subtracted from the larger value.
For example, IV represents 5 - 1, or 4.

MCMXCV is interpreted as M + CM + XC + V, or 1000 + (1000 - 100) + (100 - 10) + 5,
which is 1995. In standard Roman numerals, no more than thee consecutive copies of
the same letter are used. Following these rules, every number between 1 and 3999
can be represented as a Roman numeral made up of the following one- and two-letter combinations:

M 1000 X 10
CM 900 IX 9
D 500 V 5
CD 400 IV 4
C 100 I 1
XC 90
L 50
XL 40

1. Write a class to represent Roman numerals.

a. The class should have two constructors.:
  
(1) A default constructor
  
(2) A constructor that takes a string as a paramter (i.e., string representing a roman numeral)
  
b. This class should have the following member functions, in addition to the above constructors:

(1) set - sets the member variable to the values in the function parameter
  
(2) print - print the converted integer value
  
(3) convert the roman number string to a positive integer
  
(4) print the roman number string
  
c. This class should have two PRIVATE member variables:
  
(1) string to hold the roman numeral string
  
(2) integer to hold the converted value

3. Create:

a. a class definition file (*.h)
  
b. a class implementation file (*.cpp)
  
c. a client program to test your class (*.cpp)
  
d. a project for these code files
  

4. The client program should:

a. create a roman numeral object using the default constructor
  
b. convert the roman number to the integer equivalent
  
c. output (cout) the roman number entered and the equivalent integer value

d. create a roman numeral object using the overloaded constructor
  
e. convert the roman number to the integer equivalent
  
f. output (cout) the roman number entered and the equivalent integer value
  
g. create an array of the roman class of size 3
  
h. read, from the console, three roman numerals into the array, in a loop
  
i. convert each array component into the equivalent integer value, in a separate loop
  
j. output (cout) the roman number entered and the equivalent integer value for each component of the array.
May be in the conversion loop.
  
For this program, enter only valid roman numerals that convert to integers less than or equal to 3999

Test with the values MCXIV, CCCLIX, MDCLXVI
  

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Program Files:

roman.hpp

/*

Roman Declarations

*/

#ifndef ROMAN_HPP

#define ROMAN_HPP

#include <string>

class Roman{

private:

std::string numeral;

unsigned int digit;

unsigned int getNumValue(char c);

unsigned int convertNumeral(std::string n);

public:

Roman();

Roman(std::string n);

std::string getNumeral();

unsigned int getDigit();

};

#endif

roman.cpp

/*
Roman Definitions
*/

#ifndef ROMAN_CPP
#define ROMAN_CPP

#include "roman.hpp"

Roman::Roman(){
numeral = "";
digit = 0;
}

Roman::Roman(std::string n){
numeral = n;
digit = convertNumeral(n);
}

unsigned int Roman::getNumValue(char c){
switch(toupper(c)){
case 'I':
return 1;
case 'V':
return 5;
case 'X':
return 10;
case 'L':
return 50;
case 'C':
return 100;
case 'D':
return 500;
case 'M':
return 1000;
default:
return -1;
}
}

unsigned int Roman::convertNumeral(std::string n){
unsigned int result = 0;
for(unsigned int i = 0; i < n.length(); i++){
unsigned int char1 = getNumValue(n[i]); // get current character value
if(i+1 < n.length()){ // avoid out of bounds
unsigned int char2 = getNumValue(n[i+1]); // get next characters value
if(char1 >= char2){ // next value is smaller
result += char1; // append value to results
} else { // next value smaller
result += (char2-char1); // subtract first value from second
i++; // skip next char. case: IV == 4
}
} else { // last character reached
result += char1;
i++;
}
}
return result;
}

std::string Roman::getNumeral(){
return numeral;
}

unsigned int Roman::getDigit(){
return digit;
}

#endif

main.cpp


#include <iostream>
#include "roman.hpp"

int main(void){

// create roman numberal objects
Roman num1("MCXIV");
Roman num2("CCCLIX");
Roman num3("MDCLXVI");

// print object values
std::cout << num1.getNumeral() << " : " << num1.getDigit() << std::endl;
std::cout << num2.getNumeral() << " : " << num2.getDigit() << std::endl;
std::cout << num3.getNumeral() << " : " << num3.getDigit() << std::endl;

return 0;
}

output:

MCXIV : 1114
CCCLIX : 359
MDCLXVI : 1666

Hope this helps!

Please let me know if any changes needed

Add a comment
Know the answer?
Add Answer to:
This lab will exercise your understanding of some of the concepts covered in Chapter 10: classes,...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • This lab will exercise your understanding of some of the concepts covered in Chapter 12: virtual...

    This lab will exercise your understanding of some of the concepts covered in Chapter 12: virtual functions (think about compile-time and run-time binding) 1. We will be treating the PersonType object as a base class that may be inherited by multiple objects. We will treat the personType getAddress and setAddress as pure virtual because we wish to have all inherited objects to code these functions but do not need them in the base class. Using the code for person type...

  • Lab Goal : The lab was designed to teach you more about objects and classes. Lab...

    Lab Goal : The lab was designed to teach you more about objects and classes. Lab Description : In this program, you are to create a Roman Numeral class to handle roman numeral operations. How to convert a Roman Numeral to a standard base ten number : : Locate the first individual roman number in the roman number string. Sum up the numeric value of the individual number. Chop of the individual roman numeral from the string and continue the...

  • Assignment Draw a flowchart, using Microsoft Visio or LucidChart, to document the logic required to complete...

    Assignment Draw a flowchart, using Microsoft Visio or LucidChart, to document the logic required to complete this program Save the flowchart as a PDF file Develop a C++ program, following the logic in your flowchart, to convert a series of whole number decimal values to their Roman equivalent. Develop a function that will accept a whole number value in the range 1-9999 and return a string containing the long-form Roman Numeral equivalent of the input value, consisting only of upper-case...

  • Create a function in python called “num_roman” that converts integers between 1 and 3999 into Roman...

    Create a function in python called “num_roman” that converts integers between 1 and 3999 into Roman numerals. The input will be a scalar number and it should output a string that is the Roman numeral representation of the input number. The function should give a meaningful error message if the input is less than one. The function should truncate a fractional value to an integer. Different meaningful error messages should occur if you enter a value greater than 3999 or...

  • Can someone code this asap? Use any language that you want. 2. Ancestral Names Given a...

    Can someone code this asap? Use any language that you want. 2. Ancestral Names Given a list of strings comprised of a name and a Roman numeral, sort the list first by name, then by decimal value of the Roman numeral. In Roman numerals, a value is not repeated more than three times. At that point, a smaller value precedes a larger value to indicate subtraction. For example, the letter I represents the number 1, and Vrepresents 5. Reason through...

  • c++ Error after oveloading, please help? LAB #8- CLASSES REVISITED &&    Lab #8.5 …      Jayasinghe...

    c++ Error after oveloading, please help? LAB #8- CLASSES REVISITED &&    Lab #8.5 …      Jayasinghe De Silva Design and Implement a Program that will allow all aspects of the Class BookType to work properly as defined below: class bookType { public:    void setBookTitle(string s);            //sets the bookTitle to s    void setBookISBN(string ISBN);            //sets the private member bookISBN to the parameter    void setBookPrice(double cost);            //sets the private member bookPrice to cost    void setCopiesInStock(int noOfCopies);            //sets the private member copiesInStock to...

  • Create class Date with the following capabilities: a) Output the date in multiple formats, such as...

    Create class Date with the following capabilities: a) Output the date in multiple formats, such as MM/DD/YYYY June 14, 1992 DDD YYYY b) Use overloaded constructors to create Date objects initialized with dates of the formats in part (a). In the first case the constructor should receive three integer values. In the second case it should receive a String and two integer values. In the third case it should receive two integer values, the first of which represents the day...

  • In C++: Implement a class Polynomial that uses a dynamic array of doubles to store the...

    In C++: Implement a class Polynomial that uses a dynamic array of doubles to store the coefficients for a polynomial. This class does not need the method the overloaded += operator. Your class should have the following methods: Write one constructor that takes an integer n for the degree of a term and a double coefficient c for the coefficient of the term and creates the polynomial c x^n. (This constructor can be given default arguments easily to have a...

  • Implement the following in c++ (use "iostream" and "nsmespace std" please.)

    Implement the following: a. A template class named MyArray. 1) MyArray is a dynamic partially filled array for primitive types. 2) data members: - a pointer for the array - any associated variables needed to manage the array. 3) Constructor must insure that specified capacity is possible. Exit the program if an illegal value is specified. 4) “The Big Three” are required to insure deep copy. 5) Private grow function is used to automatically increase the size of the array...

  • In Java IntegerStream: An integer stream produces a continuous sequence of integers, starting at some initial...

    In Java IntegerStream: An integer stream produces a continuous sequence of integers, starting at some initial integer. Each subsequence integer produced should be one more than the previous integer. The class will need the following methods and constructors: The constructor takes an integer that represents the first value returned by the stream. next(): Takes no input and returns the next value of the stream starting at the initial value. Each time next is called, the number returned should be one...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT