Question

In: Computer Science

C++ Class class Billfold { private: int twenties{}, tens{}, fives{}, dollars{}; public: /* Step 1. (5...

C++ Class

class Billfold
{
private:
int twenties{}, tens{}, fives{}, dollars{};
public:
/*
Step 1. (5 points)
In class Billfold, write two Billfold constructors:
default: set all bill counts to zero; do not allow the bill counts to be uninitialed.
2nd: set all bill counts to non-negative initial values; use parameters for:
twenty, ten, five, dollar.
IMPORTANT: prohibit negative values - if negative bill value given,
use 0 for that bill instead. Accept other bills if >= 0.
Tip: you can combine both of these into one constructor using default parameters
*/

Billfold();
Billfold(int, int, int, int);
/*
Step 9. (5 points)
To avoid redundant code and insure proper validation, modify constructor(s)
as needed to call set() or modify().
*/
  

/*
Step 3. (5 points)
In class Billfold, write the definition for a new public member function: total_value.
total_value returns the total value of all twenties, tens, fives and dollars
in a Billfold object. For example: a billfold with 4 twenties, 3 tens,
2 fives and 1 dollar has a total value of (4*20)+(3*10)+(2*5)+(1*1)=121 (dollars)
This is a getter / accessor, and should be marked const.
*/
int total_value() const;
  
/*
Step 5. (5 points)
In class Billfold, implement a mutator/setter called set() which sets the
bill counts of all bills. Pre-validate any new bill counts; do not allow any
bill count to be less than zero. If the ANY of the bill counts are invalid
(negative), do not modify ANY bill counts. If the set() succeeds, return true;
if the set() fails, return false.
*/
bool set(int, int, int, int);
  

/*
Step 6. (5 points)
In class Billfold, write a show() method that outputs the number of each
bill in the billfold in one string, like this: "($20=4 $10=3 $5=2 $1=1)"
Do NOT output a newline. No endl or \n. Option: return a string so the
caller can cout the string. Now class Billfold is not dependent on iostream.
*/
void show() const;

/*
Step 8. (5 points)
In class Billfold, write a modify() method that modifies bill counts
using a positive (increment), negative (decrement) or zero (keep the same)
value. For ie:
billfold.modify(0, 0, 2, -3) This will: keep twenties, tens the same;
increase fives by 2, and decrease dollars by 3.
IMPORTANT! Do not allow bill counts to become less than zero! In modify,
if ANY bill will become negative, DO NOT MAKE ANY CHANGES.
If modify() succeeds, return true; if modify() fails, return false
*/
bool modify(int, int, int, int);
};


template<typename T>
T get_input(const string &prompt, const string &err_msg=" invalid\n") {
while (true) {
cout<<prompt; T item{};
if ( cin>>item) return item;
cout<<err_msg; cin.clear(); cin.ignore(INT_MAX, '\n');
}
}

int main() {
cout << "COSC 1337 Billfold" << endl;

/*
Step 2. (5 points) (test Step 1)
In main, in one statement, declare a new Billfold object called empty.
Use the default constructor.

In main, in one statement, declare a new Billfold object colled billfold.
Initialize billfold with: 4 twenties, 3 tens, 2 fives, 1 dollar.
*/
  
/*
Step 4. (5 points) (test Step 3)
In main, write code to call the total_value method on the billfold object.
Display the returned value, which is the total value of the dollars in billfold.
Format the output so it displays in dollars and cents. Example: $121.00
Hint: setprecision, fixed; cents will always be .00
*/
  
/*
Step 7. (5 points) (test Step 5 and Step 6)
In main, call set to change the values in billfold to: 8 twenties, 7 tens, 6 fives, 5 dollars.
In main, call show to display the contents of billfold (number of each bill).
*/

/*
Step 10 (10 points) (Test Step 8)
In main, write a menu driven loop that allows the user the add or remove bills
from the billfold object.
In a loop, display the total value, value and options. Use q to quit. For example:
$265.00 ($20=8 $10=7 $5=6 $1=5) Modify d)ollar f)ive t)en t(w)enty q)uit: d
+/-dollar: -3
$262.00 ($20=8 $10=7 $5=6 $1=2) Modify d)ollar f)ive t)en t(w)enty q)uit: f
+/-five: 2
$272.00 ($20=8 $10=7 $5=8 $1=2) Modify d)ollar f)ive t)en t(w)enty q)uit: t
+/-ten: 4
$312.00 ($20=8 $10=11 $5=8 $1=2) Modify d)ollar f)ive t)en t(w)enty q)uit: w
+/-twenty: -5
$212.00 ($20=3 $10=11 $5=8 $1=2) Modify d)ollar f)ive t)en t(w)enty q)uit: w
+/-twenty: -5
$212.00 ($20=3 $10=11 $5=8 $1=2) Modify d)ollar f)ive t)en t(w)enty q)uit: x
Invalid option, try again!
$212.00 ($20=3 $10=11 $5=8 $1=2) Modify d)ollar f)ive t)en t(w)enty q)uit: q

Use the provided get_input function to get the option letter and the bill count.
Your user interface should look similar to example above.

Important: For user input, use get_input (provided). This insures invalid numeric input
does not cause crash or infinite loop. If you cannot use get_input as provided,
you can modify it; or use cin>>value. Avoid crash or infinite loop if invalid input.
*/
  
  
/*
Step 11 (5 points, EXTRA CREDIT, only if you have time)
Create an array (or a vector) of 3 billfolds. No user input necessary.
Construct billfold instances with these bill amounts:
billfold 1: 1 twenties; 1 tens; 1 fives; 1 dollars (value $36.00)
billfold 2: 2 twenties; 4 tens; 6 fives; 8 dollars (value $108.00)
billfold 3: 10 twenties; 10 tens; 10 fives; 10 dollars (value $360.00)

Write a loop that displays the billfold number and amount;
and the total of all billfolds, to appear as follows:
billfold 1 $36.00
billfold 2 $108.00
billfold 3 $360.00
total: $504.00
*/
  
cout<<endl<<"Goodbye!"<<endl; // this should appear as the last line of your output
return EXIT_SUCCESS; // Use return 0 if EXIT_SUCCESS is undefined
}


/*
Step 12 (5 points) (Tests all Steps. NOT extra credit, this is expected.)
Test your code as described above and paste the output at the bottom.
*/

Solutions

Expert Solution

//C++ CODE TO COPY//

#include<iostream>
#include<iomanip>
using namespace std;

class Billfold
{
private:
   int twenties{}, tens{}, fives{}, dollars{};
public:
   /*
   Step 1. (5 points)
   In class Billfold, write two Billfold constructors:
   default: set all bill counts to zero; do not allow the bill counts to be uninitialed.*/
   /*
   2nd: set all bill counts to non-negative initial values; use parameters for:
   twenty, ten, five, dollar.
   IMPORTANT: prohibit negative values - if negative bill value given,
   use 0 for that bill instead. Accept other bills if >= 0.
   Tip: you can combine both of these into one constructor using default parameters
   */
   Billfold(int twen=0, int tens=0, int five=0, int dollars=0)
   {
       this->twenties = twen;
       this->tens = tens;
       this->fives = five;
       this->dollars = dollars;
   }
   /*
   Step 9. (5 points)
   To avoid redundant code and insure proper validation, modify constructor(s)
   as needed to call set() or modify().
   */


   /*
   Step 3. (5 points)
   In class Billfold, write the definition for a new public member function: total_value.
   total_value returns the total value of all twenties, tens, fives and dollars
   in a Billfold object. For example: a billfold with 4 twenties, 3 tens,
   2 fives and 1 dollar has a total value of (4*20)+(3*10)+(2*5)+(1*1)=121 (dollars)
   This is a getter / accessor, and should be marked const.
   */
   const int total_value()
   {
       int total = (this->twenties * 20) + (this->tens * 10) +
           (this->fives * 5) + (dollars * 1);

       return total;
   }

   /*
   Step 5. (5 points)
   In class Billfold, implement a mutator/setter called set() which sets the
   bill counts of all bills. Pre-validate any new bill counts; do not allow any
   bill count to be less than zero. If the ANY of the bill counts are invalid
   (negative), do not modify ANY bill counts. If the set() succeeds, return true;
   if the set() fails, return false.
   */
   bool set(int twen, int tens, int five, int dollars)
   {
       if (twen < 0 || tens < 0 || five < 0 || dollars < 0)
           return false;
       this->twenties = twen;
       this->tens = tens;
       this->fives = five;
       this->dollars = dollars;

       return true;
   }


   /*
   Step 6. (5 points)
   In class Billfold, write a show() method that outputs the number of each
   bill in the billfold in one string, like this: "($20=4 $10=3 $5=2 $1=1)"
   Do NOT output a newline. No endl or \n. Option: return a string so the
   caller can cout the string. Now class Billfold is not dependent on iostream.
   */
   const void show()
   {
       cout << "( $20 = " << this->twenties << " $10 = " << this->tens <<
           " $5 = " << this->fives << " $1 = " << this->dollars << " )";
   }

   /*
   Step 8. (5 points)
   In class Billfold, write a modify() method that modifies bill counts
   using a positive (increment), negative (decrement) or zero (keep the same)
   value. For ie:
   billfold.modify(0, 0, 2, -3) This will: keep twenties, tens the same;
   increase fives by 2, and decrease dollars by 3.
   IMPORTANT! Do not allow bill counts to become less than zero! In modify,
   if ANY bill will become negative, DO NOT MAKE ANY CHANGES.
   If modify() succeeds, return true; if modify() fails, return false
   */
   bool modify(int twen, int tens, int five, int dollars)
   {
       if (twen < 0)
       if (this->twenties - twen < 0)
           return false;
       if (tens < 0)
       if (this->tens - tens < 0)
           return false;
       if (five < 0)
       if (this->fives - five < 0)
           return false;
       if (dollars < 0)
       if (this->dollars - dollars < 0)
           return false;
      
       this->twenties = this->twenties + twen;
       this->tens = this->tens + tens;
       this->fives = this->fives + five;
       this->dollars = this->dollars + dollars;

       return true;
   }
};


template<typename T>
T get_input(const string &prompt, const string &err_msg = " invalid\n") {
   while (true) {
       cout << prompt; T item{};
       if (cin >> item) return item;
       cout << err_msg; cin.clear(); cin.ignore(INT_MAX, '\n');
   }
}

int main() {
   cout << "COSC 1337 Billfold" << endl;

   /*
   Step 2. (5 points) (test Step 1)
   In main, in one statement, declare a new Billfold object called empty.
   Use the default constructor.*/
   Billfold billfold();

   /*
   In main, in one statement, declare a new Billfold object colled billfold.
   Initialize billfold with: 4 twenties, 3 tens, 2 fives, 1 dollar.
   */
   Billfold *billfold2 = new Billfold(4, 3, 2, 1);

   /*
   Step 4. (5 points) (test Step 3)
   In main, write code to call the total_value method on the billfold object.
   Display the returned value, which is the total value of the dollars in billfold.
   Format the output so it displays in dollars and cents. Example: $121.00
   Hint: setprecision, fixed; cents will always be .00
   */
   int total = billfold2->total_value();
   cout << std::fixed;
   cout << std::setprecision(2);
   cout << "$" << total;
   /*
   Step 7. (5 points) (test Step 5 and Step 6)
   In main, call set to change the values in billfold to: 8 twenties, 7 tens, 6 fives, 5 dollars.
   */
   billfold2->set(8, 7, 6, 5);
   /*
   In main, call show to display the contents of billfold (number of each bill).
   */
   billfold2->show();
   /*
   Step 10 (10 points) (Test Step 8)
   In main, write a menu driven loop that allows the user the add or remove bills
   from the billfold object.
   In a loop, display the total value, value and options. Use q to quit. For example:
   $265.00 ($20=8 $10=7 $5=6 $1=5) Modify d)ollar f)ive t)en t(w)enty q)uit: d
   +/-dollar: -3
   $262.00 ($20=8 $10=7 $5=6 $1=2) Modify d)ollar f)ive t)en t(w)enty q)uit: f
   +/-five: 2
   $272.00 ($20=8 $10=7 $5=8 $1=2) Modify d)ollar f)ive t)en t(w)enty q)uit: t
   +/-ten: 4
   $312.00 ($20=8 $10=11 $5=8 $1=2) Modify d)ollar f)ive t)en t(w)enty q)uit: w
   +/-twenty: -5
   $212.00 ($20=3 $10=11 $5=8 $1=2) Modify d)ollar f)ive t)en t(w)enty q)uit: w
   +/-twenty: -5
   $212.00 ($20=3 $10=11 $5=8 $1=2) Modify d)ollar f)ive t)en t(w)enty q)uit: x
   Invalid option, try again!
   $212.00 ($20=3 $10=11 $5=8 $1=2) Modify d)ollar f)ive t)en t(w)enty q)uit: q

   Use the provided get_input function to get the option letter and the bill count.
   Your user interface should look similar to example above.

   Important: For user input, use get_input (provided). This insures invalid numeric input
   does not cause crash or infinite loop. If you cannot use get_input as provided,
   you can modify it; or use cin>>value. Avoid crash or infinite loop if invalid input.
   */
   char choice;
   int bill;
   while (true)
   {
       cout << "\n" << billfold2->total_value() << "$ ";
       billfold2->show();
       cout << " Modify d)ollar f)ive t)en t(w)enty q)uit: ";
       cin >> choice;
       switch (choice)
       {
       case 'd':
           cout << "\n+/-dollar: ";
           cin >> bill;
           billfold2->modify(0, 0, 0, bill);
           break;
       case 'f':
           cout << "\n+/-dollar: ";
           cin >> bill;
           billfold2->modify(0, 0, bill, 0);
           break;
       case 't':
           cout << "\n+/-dollar: ";
           cin >> bill;
           billfold2->modify(0, bill, 0, 0);
           break;
       case 'w':
           cout << "\n+/-dollar: ";
           cin >> bill;
           billfold2->modify(bill, 0, 0, 0);
           break;
       case 'q':
           return 0;
           break;
       default:
           cout << "\nInvalid option, try again!";
           break;
       }
   }
}

//PROGRAM OUTPUT//

//COMMENT DOWN FOR ANY QUERIES...
//HIT A THUMBS UP IF YOU DO LIKE IT!!!


Related Solutions

public class Clock { private int hr; private int min; private int sec; public Clock() {...
public class Clock { private int hr; private int min; private int sec; public Clock() { setTime(0, 0, 0); } public Clock(int hours, int minutes, int seconds) { setTime(hours, minutes, seconds); } public void setTime(int hours, int minutes, int seconds) { if (0 <= hours && hours < 24) hr = hours; else hr = 0; if (0 <= minutes && minutes < 60) min = minutes; else min = 0; if(0 <= seconds && seconds < 60) sec =...
public class SinglyLikedList {    private class Node{        public int item;        public...
public class SinglyLikedList {    private class Node{        public int item;        public Node next;        public Node(int item, Node next) {            this.item = item;            this.next = next;        }    }       private Node first;    public void addFirst(int a) {        first = new Node(a, first);    } } 1. Write the method add(int item, int position), which takes an item and a position, and...
public class IntNode               {            private int data;            pri
public class IntNode               {            private int data;            private IntNode link;            public IntNode(int data, IntNode link){.. }            public int     getData( )          {.. }            public IntNode getLink( )           {.. }            public void    setData(int data)     {.. }            public void    setLink(IntNode link) {.. }         } All questions are based on the above class, and the following declaration.   // Declare an empty, singly linked list with a head and a tail reference. // you need to make sure that head always points to...
public class P2 { public static int F(int x[], int c) { if (c < 3)...
public class P2 { public static int F(int x[], int c) { if (c < 3) return 0; return x[c - 1] + F(x, c - 1); } public static int G(int a, int b) { b = b - a; a = b + a; return a; } public static void main(String args[]) { int a = 4, b = 1; int x[] = { 3, 1, 4, 1, 5 }; String s = "Problem Number 2"; System.out.println(x[2 +...
Consider the following java class: class Student {    private int student_Number;    private String student_Name;    public Student(int...
Consider the following java class: class Student {    private int student_Number;    private String student_Name;    public Student(int stNo,String name) {         student_Number=stNo;         student_Name=name;      }     public String getName() {       return student_Name;     }      public int getNumber() {       return student_Number;      }     public void setName(String st_name) {       student_Name = st_name;     } } Write a Tester class named StudentTester which contains the following instruction: Use the contractor to create a student object where student_Number =12567, student_Name = “Ali”. Use the setName method to change the name of...
public class Date { private int dMonth; //variable to store the month private int dDay; //variable...
public class Date { private int dMonth; //variable to store the month private int dDay; //variable to store the day private int dYear; //variable to store the year //Default constructor //Data members dMonth, dDay, and dYear are set to //the default values //Postcondition: dMonth = 1; dDay = 1; dYear = 1900; public Date() { dMonth = 1; dDay = 1; dYear = 1900; } //Constructor to set the date //Data members dMonth, dDay, and dYear are set //according to...
package applications; public class Matrix { private int[][] m; public Matrix(int x, int y) { m...
package applications; public class Matrix { private int[][] m; public Matrix(int x, int y) { m = new int[x][y]; } public Matrix(int x, int y, int z) { m = new int[x][y]; for(int i = 0; i < x; i++) { for(int j = 0; j < y; j++) { m[i][j] = z; } } } public int rowsum(int i) throws IndexOutOfBoundsException { if (i < 0 || i > m.length-1) { throw new IndexOutOfBoundsException("Invalid Row"); } int sum =...
class A { public: //constructors // other members private: int a; int b; }; Give declatations...
class A { public: //constructors // other members private: int a; int b; }; Give declatations of operator functions for each of the following ways to overload operator + You must state where the declatation goes, whether within the class in the public or private section or outside the class. The operator + may be overloaded. a) as friend function b) as member function c) as non-friend, non-member function
needed asap !!! 1. import java.utility.Random; 2.   3. public class Sequen 4.   5. private int stand  ...
needed asap !!! 1. import java.utility.Random; 2.   3. public class Sequen 4.   5. private int stand   6. private int calc;   7.   8. public Sequen(int numStand) 9. { 10.         stand = numStand; 11.         skip; 12.         } 13.           14.         public void skip() 15.         { 16.         Random sit = new Random(); 17.         calc = sit.nextInt(stand) + 1; 18.         } 19.           20.         public getStand() 21.         { 22.         return stand; 23.         } 24.           25.         int getCalc() 26.         { 27.         return calc; 28.         } 29.         } One error is already identified for you as shown below: Line Number: 5 Error description: Missing semicolon...
class Counter{   private int count = 0;   public void inc(){    count++;     }   public int get(){     return...
class Counter{   private int count = 0;   public void inc(){    count++;     }   public int get(){     return count;   } } class Worker extends Thread{ Counter count;   Worker(Counter count){     this.count = count;   }   public void run(){     for (int i = 0; i < 1000;i++){       synchronized(this){         count.inc();       }}   } } public class Test {     public static void main(String args[]) throws InterruptedException   {     Counter c = new Counter();     Worker w1 = new Worker(c);     Worker w2 = new Worker(c);     w1.start();     w2.start();     w1.join();     w2.join();     System.out.println(c.get());      ...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT