0% found this document useful (0 votes)
51 views

Exercise Lecture One OOP

1. Create a Rectangle class with length and width attributes. Include methods to calculate the perimeter and area. 2. Create an Invoice class to represent hardware store invoices. Include part number, description, quantity, and price attributes. A getInvoiceAmount method calculates the total invoice amount. 3. Create an Account class to represent bank accounts. Include a balance attribute. Constructor validates an initial balance. Methods to credit, debit, and get the balance are included, with debit preventing overdrafts.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
51 views

Exercise Lecture One OOP

1. Create a Rectangle class with length and width attributes. Include methods to calculate the perimeter and area. 2. Create an Invoice class to represent hardware store invoices. Include part number, description, quantity, and price attributes. A getInvoiceAmount method calculates the total invoice amount. 3. Create an Account class to represent bank accounts. Include a balance attribute. Constructor validates an initial balance. Methods to credit, debit, and get the balance are included, with debit preventing overdrafts.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Due Date 08th December, 2016 08:00 AM

1. (Rectangle Class) Create a class Rectangle with attributes length and width. Provide

member functions that calculate the perimeter and the area of the rectangle.
2. (Invoice Class) Create a class called Invoice that a hardware store might use to represent
an invoice for an item sold at the store. An Invoice should include four pieces of
information as data members a part number (type string), a part description (type string),
a quantity of the item being purchased (type int) and a price per item (type int). Your class
should have a constructor that initializes the four data members. In addition, provide a
member function named getInvoiceAmount that calculates the invoice amount (i.e.,
multiplies the quantity by the price per item), then returns the amount as an int value. If
the quantity is not positive, it should be set to 0. If the price per item is not positive, it
should be set to 0. Write a test program that demonstrates class Invoice's capabilities.
3. (Account Class) Create a class called Account that a bank might use to represent
customers' bank accounts. Your class should include one data member of type int to
represent the account balance. Your class should provide a constructor that receives an
initial balance and uses it to initialize the data member. The constructor should validate
the initial balance to ensure that it is greater than or equal to 0. If not, the balance should
be set to 0 and the constructor should display an error message, indicating that the initial
balance was invalid. The class should provide three member functions. Member function
credit should add an amount to the current balance. Member function debit should
withdraw money from the Account and should ensure that the debit amount does not
exceed the Account's balance. If it does, the balance should be left unchanged and the
function should print a message indicating "Debit amount exceeded account balance."
Member function getBalance should return the current balance. Create a program that
creates two Account objects and tests the member functions of class Account.
4. (Employee Class) Create a class called Employee that includes three pieces of information
as data members a first name (type string), a last name (type string) and a monthly salary
(type int). Your class should have a constructor that initializes the three data members. If
the monthly salary is not positive, set it to 0. Write a test program that demonstrates class
Employee's capabilities. Create two Employee objects and display each object's yearly
salary. Then give each Employee a 10 percent raise and display each Employee's yearly
salary again.
5. (Date Class) Create a class called Date that includes three pieces of information as data
members a month (type int), a day (type int) and a year (type int). Your class should have
a constructor with three parameters that uses the parameters to initialize the three data
members. For the purpose of this exercise, assume that the values provided for the year

and day are correct, but ensure that the month value is in the range 1-12; if it is not, set
the month to 1. Provide a member function displayDate that displays the month, day and
year separated by forward slashes (/). Write a test program that demonstrates class
Date's capabilities.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy