Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

CSE1322L Assignment 1, Summaries of Object Oriented Programming

US currency is made up of notes and coins. The most common notes are $1, $5, $10, ... Print out how much money you have in quarters ($0.25 coins).

Typology: Summaries

2021/2022

Uploaded on 09/12/2022

shashwat_pr43
shashwat_pr43 ๐Ÿ‡บ๐Ÿ‡ธ

4.5

(15)

233 documents

1 / 14

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CSE1322L Assignment 1
Introduction:
US currency is made up of notes and coins. The most common notes are $1, $5, $10,
$20, $50 and $100. The most common coins are 1c (pennies), 5c (nickels), 10c
(dimes), and 25c (quarters).
In this assignment, you will model US currency with objects, creating objects for each
note and coin.
Tasks:
First write a class called Coins that has:
โ— A private integer called quantityOnHand
โ— A private float called denomination
โ— A private float called weight
โ— A constructor which takes two floats (denomination and weight) and sets the
object attributes.
โ— A method called getTotalWeight() which returns a float representing the total
weight of that coin (weight * quantityOnHand)
โ— A method called getTotalValue() which returns a float representing the total value
of this coin (denomination * quantityOnHand)
โ— A method called increaseQuantity which takes in a quantity (int) and increases
the quantityOnHand by that amount.
โ— A method called decreaseQuantity which takes in a quantity (int) and decreases
the quantityOnHand by that amount. Note, you can never have less than 0 of
any coin. If you are asked to decreaseQuantity below 0, just set it to 0.
โ— A method called getQuantityOnHand which takes in no parameters, and returns
the value of the quantityOnHand attribute (an int).
โ— A method called printPretty which takes in a float called amount, and returns a
string. The body of the method will be:
Java:
return(โ€œ$โ€+String.format("%4.2f",amount));
C#:
return(โ€œ$โ€+amount.ToString("F2"));
โ— An override of toString (java) or ToString (C#) which returns a string like โ€œ$3.25 in
$0.25 coinsโ€. The first value should be the total value of the coin, the second
value should be the denomination of the coin. Hint printPretty() may come in
handy here.
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe

Partial preview of the text

Download CSE1322L Assignment 1 and more Summaries Object Oriented Programming in PDF only on Docsity!

CSE1322L Assignment 1

Introduction:

US currency is made up of notes and coins. The most common notes are $1, $5, $10,

$20, $50 and $100. The most common coins are 1c (pennies), 5c (nickels), 10c

(dimes), and 25c (quarters).

In this assignment, you will model US currency with objects, creating objects for each

note and coin.

Tasks:

First write a class called Coins that has:

โ— A private integer called quantityOnHand

โ— A private float called denomination

โ— A private float called weight

โ— A constructor which takes two floats (denomination and weight) and sets the

object attributes.

โ— A method called getTotalWeight() which returns a float representing the total

weight of that coin (weight * quantityOnHand)

โ— A method called getTotalValue() which returns a float representing the total value

of this coin (denomination * quantityOnHand)

โ— A method called increaseQuantity which takes in a quantity (int) and increases

the quantityOnHand by that amount.

โ— A method called decreaseQuantity which takes in a quantity (int) and decreases

the quantityOnHand by that amount. Note, you can never have less than 0 of

any coin. If you are asked to decreaseQuantity below 0, just set it to 0.

โ— A method called getQuantityOnHand which takes in no parameters, and returns

the value of the quantityOnHand attribute (an int).

โ— A method called printPretty which takes in a float called amount, and returns a

string. The body of the method will be:

Java:

return(โ€œ$โ€+String.format("%4.2f",amount));

C#:

return(โ€œ$โ€+amount.ToString("F2"));

โ— An override of toString (java) or ToString (C#) which returns a string like โ€œ$3.25 in

$0.25 coinsโ€. The first value should be the total value of the coin, the second

value should be the denomination of the coin. Hint printPretty() may come in

handy here.

Next write a class called Notes which has: โ— A private integer called quantityOnHand โ— A private integer called denomination โ— A constructor which takes one integer called denomination and sets the object attributes. โ— A method called getTotalValue() which returns a int representing the total value of this note (denomination * quantityOnHand) โ— A method called increaseQuantity which takes in a quantity (int) and increases the quantityOnHand by that amount. โ— A method called decreaseQuantity which takes in a quantity (int) and decreases the quantityOnHand by that amount. Note, you can never have less than 0 of any note. If you are asked to decreaseQuantity below 0, just set it to 0. โ— A method called getQuantityOnHand which takes in no parameters, and returns the value of the quantityOnHand attribute (an int). โ— A method called printPretty which takes in a float called amount, and returns a string. The body of the method will be: Java:

return(โ€œ$โ€+String.format("%4.2f",amount));

C#:

return(โ€œ$โ€+amount.ToString("F2"));

โ— An override of toString (java) or ToString (C#) which returns a string like โ€œ$32. in $1.00 notesโ€. The first value should be the total value of the note, the second value should be the denomination of this note. Finally, write a main method. Start it with the following code: Notes twenties=new Notes(20); Notes tens=new Notes(10); Notes fives=new Notes(5); Notes ones=new Notes(1); Coins quarters=new Coins(0.25f,0.2f); Coins dimes=new Coins(0.10f,0.08f); Coins nickels=new Coins(0.05f,0.176f); Coins pennies=new Coins(0.01f,0.088f); dimes.increaseQuantity(41); nickels.increaseQuantity(17); pennies.increaseQuantity(132); ones.increaseQuantity(33); fives.increaseQuantity(12); tens.increaseQuantity(2); twenties.increaseQuantity(5);

โ—‹ If at the end you donโ€™t have enough money (i.e. you still owe them more

than 0.001), you should tell them that, and let them know how much you

still owe them. (see examples below).

โ—‹ Remember as you give them each note/coin, you should decrease the

quantity of that note/coin that you have on hand.

โ— Finally, print out how much money you have left, and how much the coins weigh.

Be aware, floating point numbers are not a good way to store currency in general. This

is because computers cannot accurately store numbers like 0.01. They store a

representation of the number. This is why above, it tells you to check if you still owe

more than 0.001 rather than checking if you owe more than 0.0 because itโ€™s likely a

fraction of a penny will still be owed at the end. Youโ€™ll find that sometimes when you run

your code itโ€™ll be off by a penny, which is ok for this assignment. If you are curious,

check out this wikipedia page:

https://en.wikipedia.org/wiki/Floating-point_arithmetic#Accuracy_problems

Example Runs: [User input in red]

$100.00 in $20.00 notes. $20.00 in $10.00 notes. $60.00 in $5.00 notes. $33.00 in $1.00 notes. $0.00 in $0.25 coins. $4.10 in $0.10 coins. $0.85 in $0.05 coins. $1.32 in $0.01 coins. Total Money is $219.27 total weight is 17.888oz How much do you need?

Give them a $20 note Give them a $20 note Give them a $1 note Give them a $1 note Give them a dime Give them a dime Give them a dime Give them a dime

Give them a nickel Give them a penny Give them a penny I have $176.80 left, it's total weight is 17.216oz

Run #2:

$100.00 in $20.00 notes. $20.00 in $10.00 notes. $60.00 in $5.00 notes. $33.00 in $1.00 notes. $0.00 in $0.25 coins. $4.10 in $0.10 coins. $0.85 in $0.05 coins. $1.32 in $0.01 coins. Total Money is $219.27 total weight is 17.888oz How much do you need?

Give them a $10 note Give them a $5 note Give them a $1 note Give them a $1 note Give them a $1 note Give them a $1 note Give them a dime Give them a dime Give them a nickel Give them a penny Give them a penny I have $200.00 left, it's total weight is 17.376oz

Run #3:

$100.00 in $20.00 notes. $20.00 in $10.00 notes. $60.00 in $5.00 notes. $33.00 in $1.00 notes. $0.00 in $0.25 coins. $4.10 in $0.10 coins.

Give them a $1 note Give them a $1 note Give them a $1 note Give them a $1 note Give them a $1 note Give them a $1 note Give them a $1 note Give them a $1 note Give them a $1 note Give them a $1 note Give them a $1 note Give them a $1 note Give them a $1 note Give them a $1 note Give them a $1 note Give them a dime Give them a dime Give them a dime Give them a dime Give them a dime Give them a dime Give them a dime Give them a dime Give them a dime Give them a dime Give them a dime Give them a dime Give them a dime Give them a dime Give them a dime Give them a dime Give them a dime Give them a dime Give them a dime Give them a dime Give them a dime Give them a dime Give them a dime Give them a dime Give them a dime Give them a dime Give them a dime

Give them a dime Give them a dime Give them a dime Give them a dime Give them a dime Give them a dime Give them a dime Give them a dime Give them a dime Give them a dime Give them a dime Give them a dime Give them a dime Give them a dime Give them a nickel Give them a nickel Give them a nickel Give them a nickel Give them a nickel Give them a nickel Give them a nickel Give them a nickel Give them a nickel Give them a nickel Give them a nickel Give them a nickel Give them a nickel Give them a nickel Give them a nickel Give them a nickel Give them a nickel Give them a penny Give them a penny Give them a penny Give them a penny Give them a penny Give them a penny Give them a penny Give them a penny Give them a penny Give them a penny Give them a penny

Give them a penny Give them a penny Give them a penny Give them a penny Give them a penny Give them a penny Give them a penny Give them a penny Give them a penny Give them a penny Give them a penny Give them a penny Give them a penny Give them a penny Give them a penny Give them a penny Give them a penny Give them a penny Give them a penny Give them a penny Give them a penny Give them a penny Give them a penny Give them a penny Give them a penny Give them a penny Give them a penny Give them a penny Give them a penny Give them a penny Give them a penny Give them a penny Give them a penny Give them a penny Give them a penny Give them a penny Give them a penny Give them a penny Give them a penny Give them a penny Give them a penny Give them a penny

Give them a penny Give them a penny Give them a penny Give them a penny Give them a penny Give them a penny Give them a penny Give them a penny Give them a penny Give them a penny Give them a penny Give them a penny Give them a penny Give them a penny Give them a penny Give them a penny Give them a penny Give them a penny Give them a penny Give them a penny Give them a penny Give them a penny Give them a penny Give them a penny Give them a penny Give them a penny Give them a penny Give them a penny Give them a penny Give them a penny Give them a penny Give them a penny Give them a penny Give them a penny Give them a penny Give them a penny Give them a penny I don't have enough money. I still owe you $0. I have $0.00 left, it's total weight is 0oz

โ—‹ Correctly calculates total value(1 point) โ— Method increaseQuantity (3 points total) โ—‹ Takes one parameter of type int (1 point) โ—‹ Correctly increases the quantityOnHand by the parameter (2 points) โ— Method decreaseQuantity (3 points total) โ—‹ Takes one parameter of type int (1 point) โ—‹ Correctly decreases the quantityOnHand by the parameter (2 points) โ— Method getQuantityOnHand (3 points total) โ—‹ Takes in no parameter (1 point) โ—‹ Returns an integer (1 point) โ—‹ Correctly returns the quantityOnHand. โ— Override of toString/ToString (5 points total) โ—‹ Correct override method header (2 points) โ—‹ Correctly returns the appropriate string (2 points) โ—‹ Uses prettyPrint() correctly (1 point) Notes Class (23 points total): โ— 2 attributes (4 points total) โ—‹ 1 point for attribute defined private, one for correct type used. โ— Constructor (2 points total) โ—‹ Takes in 1 parameters of correct type (1 points) โ—‹ Correctly sets object variables (1 points) โ— Method getTotalValue (3 points total) โ—‹ Takes no parameters (1 point) โ—‹ Returns an int (1 point) โ—‹ Correctly calculates total value(1 point) โ— Method increaseQuantity (3 points total) โ—‹ Takes one parameter of type int (1 point) โ—‹ Correctly increases the quantityOnHand by the parameter (2 points) โ— Method decreaseQuantity (3 points total) โ—‹ Takes one parameter of type int (1 point) โ—‹ Correctly decreases the quantityOnHand by the parameter (2 points) โ— Method getQuantityOnHand (3 points total) โ—‹ Takes in no parameter (1 point) โ—‹ Returns an integer (1 point) โ—‹ Correctly returns the quantityOnHand. โ— Override of toString/ToString (5 points total) โ—‹ Correct override method header (2 points) โ—‹ Correctly returns the appropriate string (2 points) โ—‹ Uses prettyPrint() correctly (1 point)

Driver class/program (37 points total): โ— Correctly copied/pasted object instantiation & increaseQuantity code (2 points total) โ— Correctly uses toString/ToString in each object to print out value of each note/coin (There should be 8 lines - 1 point each) โ— Correctly calculates the total value of money they have (5 points) โ— Correctly calculates the total weight of the coins (4 points) โ— Correctly prints out the total value of money as well as total weight of coins ( point) โ— Correctly calculates how many $20โ€™s to give. Prints out the right number of โ€œGive them a $20 noteโ€ lines, and correctly keeps track of how many $20โ€™s are left. ( points) โ— Correctly calculates how many $10โ€™s to giveโ€ฆ(2 points) โ— Correctly calculates how many $5โ€™s to giveโ€ฆ(2 points) โ— Correctly calculates how many $1โ€™s to giveโ€ฆ(2 points) โ— Correctly calculates how many quarters to giveโ€ฆ(2 points) โ— Correctly calculates how many dimes to giveโ€ฆ(2 points) โ— Correctly calculates how many nickels to giveโ€ฆ(2 points) โ— Correctly calculates how many pennies to giveโ€ฆ(2 points) โ— Correctly calculates the final amount of money left, and itโ€™s weight and prints it ( point).