CpE 2101L Lab Exercises
CPE 2101L: Object-Oriented Programming | Hands-On Exercises
ELF © 2019 v2.0 Beta | 6 of 23
Exercise 4
Defining Objects and Classes
Target Course Outcome:
CO1: Design object-oriented models using UML diagrams.
CO2: Solve real-world programming problems applying the object-oriented principles and techniques
Implement the following problems into UML Class Diagram and a working program. Create a test program to test
all the methods defined in class.
LE4.11 ) Person Type. Create a class that specifies the members to implement a name. Implement the class data
variables and methods below.
firstName: string Variable to store the first name
lastName: string Variable to store the last name
print() : void Method to output the first name and last name
setName(string first, string last): void Method to set firstName and lastName according
to the parameters.
getFirstName() const: string Method to return the first name.
getLastName() const: string Method to return the last name.
personType() Sets firstName and lastName to null strings.
personType(string first, string last) Sets firstName and lastName according to the
parameters.
LE XXXXXXXXXXRectangle. Following the example of the Circle class, design a class named Rectangle to represent a
ectangle. The class contains:
▪ Two double data fields named width and height that specify the width and height of the rectangle.
The default values are 1 for both width and height.
▪ A no-arg constructor that creates a default rectangle.
▪ A constructor that creates a rectangle with the specified width and height.
▪ A method named getArea() that returns the area of this rectangle.
▪ A method named getPerimeter() that returns the perimeter.
Draw the UML diagram for the class then implement the class. Write a test program that creates two
Rectangle objects—one with width 4 and height 40, and the other with width 3.5 and height 35.9. Display
the width, height, area, and perimeter of each rectangle in this order.
LE4.13 ) Grade Distribution. Create a class that represents a grade distribution for a given course. Write methods
to perform the following tasks:
• Set the number of each of the letter grades A, B, C, D and F.
• Read the number of each of the letter
grades A, B, C, D and F.
• Return the total number of grades.
• Return the percentage of letter grade
as a whole number between 0 and 100,
inclusive.
• Draw a bar graph of the grade
distribution.
CPE 2101L: Object-Oriented Programming | Hands-On Exercises
ELF © 2019 v2.0 Beta | 7 of 23
The graph will have five bars, one per grade. Each bar can be a horizontal row of asterisks, such that the number of
asterisks in a row is proportionate to the percentage of grades in each category. Let one asterisk represent 2 percent
of 50 asterisks co
espond to 100 percent. Mark the horizontal axis at 10 percent increments from 0 to 100 percent
label each line with its letter grade.
For example, if the grades are 1 A, 4 Bs, 6 Cs, 2 Ds and 1 F, the total number of grades is 14, the percentage of As is
7, the percentage of Bs is 29, the percentage of Cs is 43, the percentage of Ds is 14 and the percentage of Fs is 7. The
A row would contain 4 asterisks (7 percent of 50 rounded to the nearest integer), the B row 14, C row 21, the D row
7 and the F row is 4. The graph would look like above.