Docsity

Java Programming Assignment: Creating a ChangeMaker Class - Prof. Gregory Shaw, Study notes of Computer Science

Toggle sidebar
bg1
Computer Programming I Instructor: Greg Shaw
COP 2210
Programming Assignment #1
(Using a Java IDE)
I. The Assignment
This assignment is to create a Java program consisting of two
classes.
The first is the ChangeMaker class, a hard copy of which
was/will be distributed in class today. You are to copy this
class exactly as it appears, with your name appearing where
indicated, and save it in a file named ChangeMaker.java.
DO NOT TYPE IN THE LINE NUMBERS!
The second class is a “test” class for the ChangeMaker class
called ChangeMakerTester.java. This class is to be downloaded
from the class web page.
Don't worry about what the statements mean. Our goal here is
to familiarize ourselves with the procedure for creating,
compiling, and executing a program, and for printing the
program and its output - before we turn our attention to the
Java language itself.
1. If you have not already done so, create your project in
NetBeans
2. Download the ChangeMakerTester class (make sure to save it
as ChangeMakerTester.java) and store it in the src folder
of your NetBeans project
3. Create a new Java class without a main method called
ChangeMaker.java, and enter the code exactly as it appears
in the copy given in class
4. Compile your ChangeMaker class. If you did not copy it
exactly, you will have syntax errors. Fix the errors and
re-compile until there are no more syntax errors
At this point, the way to fix the errors is just to make
pf3
pf4
pf5

Partial preview of the text

Download Java Programming Assignment: Creating a ChangeMaker Class - Prof. Gregory Shaw and more Study notes Computer Science in PDF only on Docsity!

Computer Programming I Instructor: Greg Shaw COP 2210

Programming Assignment

(Using a Java IDE)

I. The Assignment

This assignment is to create a Java program consisting of two classes. The first is the ChangeMaker class, a hard copy of which was/will be distributed in class today. You are to copy this class exactly as it appears, with your name appearing where indicated, and save it in a file named ChangeMaker.java. DO NOT TYPE IN THE LINE NUMBERS! The second class is a “test” class for the ChangeMaker class called ChangeMakerTester.java. This class is to be downloaded from the class web page. Don't worry about what the statements mean. Our goal here is to familiarize ourselves with the procedure for creating, compiling, and executing a program, and for printing the program and its output - before we turn our attention to the Java language itself.

  1. If you have not already done so, create your project in NetBeans
  2. Download the ChangeMakerTester class (make sure to save it as C hangeMakerTester.java ) and store it in the src folder of your NetBeans project
  3. Create a new Java class without a main method called ChangeMaker. java , and enter the code exactly as it appears in the copy given in class
  4. Compile your ChangeMaker class. If you did not copy it exactly, you will have syntax errors. Fix the errors and re-compile until there are no more syntax errors

 At this point, the way to fix the errors is just to make

sure that each line of your code looks exactly like mine, but do read the error messages and see if you can begin to understand some of them

  1. Finally, when the ChangeMaker class compiles successfully, compile and run the ChangeMakerTester class (it has no errors)
  • @ Name
  • I affirm that this program is entirely my own work and
  • none of it is the work of any other person. / import java.text.NumberFormat; // to format o/p as currency /*
  • Computes change due - using the minimum coinage possible - when
  • the user enters the amount of a purchase and the amount tendered / public class ChangeMaker { // instance var's private double amountDue ; // total cost of purchases private double amountTendered ; // amount paid private int dollarsDue ; // number of dollar coins due in change private int quartersDue ; // number of quarters due private int dimesDue ; // number of dimes due private int nickelsDue ; // number of nickels due private int penniesDue ; // number of pennies due private NumberFormat formatter = NumberFormat.getCurrencyInstance() ; /*
  • Constructs a ChangeMaker object
  • @param totalDue the amount of the purchase
  • @param amountReceived the amount tendered (paid) */ public ChangeMaker(double totalDue, double amountReceived) { // set instance variables to parameters passed amountDue = totalDue ; amountTendered = amountReceived ; // other instance var's automatically initialized to 0 System.out.println("\nAmount Due: " + formatter.format(amountDue)) ; System.out.println("Amount Received: "
  • formatter.format(amountTendered)) ;
  • Computes the number of coins of each denomination to be given as change */ public void computeChange() { int changeDue ; // total change due, in cents if (amountDue == amountTendered) { System.out.println("\nExact change! Thanks and have a nice day!") ; } else if (amountDue > amountTendered) // owes more money! { System.out.println("\nLo siento, you still owe me another "
  • formatter.format(amountDue - amountTendered)) ; } else // has change coming { // compute change due, in cents for easier calculations changeDue = (int) Math.round((amountTendered - amountDue) * 100) ; // compute number of dollars due, and reduce change due accordingly dollarsDue = changeDue / 100 ; changeDue = changeDue % 100 ; // now do the same for all other coins quartersDue = changeDue / 25 ; changeDue = changeDue % 25 ; dimesDue = changeDue / 10 ; changeDue = changeDue % 10 ; nickelsDue = changeDue / 5 ; changeDue = changeDue % 5 ; penniesDue = changeDue ; // change due is <= 4 cents