Programming Tools & Computer Science I

CSC 185 & 201, Spring 2013, Northern Virginia Community College

Homework 3: Classes and methods

2013-02-03

Due:

Feb 13, 11:59PM

To submit:

Send an email to me at jal2016@email.vccs.edu with subject CSC 201 HW3 with your answers attached in a zip file.

Alternately, if you follow the directions for 185 HW3, you can complete the project started in repository folder homework/201/hw3, and email me when you're done.

Directions:

For each class below, include a main method that demonstrates how it is used. The course materials have a project started in homework/201/hw3 that you can start with.
  1. Create a Circle class that represents a circle in the plane. Ensure that instances of the Circle class are immutable. Fill in definitions for the methods shown below:
    
    package csc201.hw3;
    
    /** Represent a circle with an arbitrary radius and position in the plane.
     */
    public class Circle {
        
        // Fill in constructor, instance members, and any other methods needed.
        
        /** Determine if this circle overlaps with another.
         */
        public boolean overlaps(Circle c) {
            return false; // finish this method.
        }
        
        /** Determine if this circle is the same (geometrically) as another.
         */
        @Override
        public boolean equals(Object o) {
            return false; // finish this method.
        }
        
        public static void main(String[] args) {
        }
    }
    
        
  2. Create a CashRegister class that represents a cash register, tracking its balance and computing change for transactions. Fill in the methods shown below:
    
    package csc201.hw3;
    
    /** Represent a cash register, tracking balance and determining change
     * required for transactions.
     */
    public class CashRegister {
        // Fill in constructor, instance members, and supporting methods or 
        // other class definitions.
        
        /** Record purchase of given cost, paid with given amount of cash.  Return
         * representation of change given (including number of each denomination
         * dispensed).
         * 
         * If the register cannot make change for the given transaction, signal an
         * error (with a method of your choice).
         */
        public ??? pay(??? cost, ??? cash) {
        }
    
        /** Return current total register balance, as a single value.
         */
        public ??? balance() {
        }
        
        /** Add given currency to register.  If you want, you can alter the method
         * signature to better support your data representation, or define separate
         * methods for each denomination.
         */
        public void addCash(??? cash) {
        }
        
        public static void main(String[] args) {
        }
    }
    
        

    You'll need to determine how to represent the contents of the register and the data type to return that represents the change received. Once you've done that, fill in the ??? in the template.

    Note that the pay method should return the number of each coin or bill needed. For example, if a cost of $3.45 is paid with a $10 bill, you should report change as

    • 1 $5 bill
    • 1 $1 bill
    • 2 quarters
    • 1 nickel

    Make sure to outline your assumptions about the currency denominations supported. If you're having trouble, simplify the problem, perhaps by allowing only whole-dollar prices.