CS 1063  Week 9:  Fundamental Data Types

The Assignment Statement


Objectives

Assignments



Activity 1  Enhancing the Purse Class


1.  Enhance the Purse class by adding methods addPennies and addDollars.
2.  Add a private method toPennies to the Purse class.  toPennies should calculate and return the total value of all of the coins as an integer.  For example if the total value of the coins is $2.14 then this method should return 214. 
3.  Add methods getNumDollars and getNumCents to the Purse class.  The getNumDollars method should return the number of whole dollars in the purse, as an integer.  The getNumCents method should return the number of cents, as an integer.  For example, if the total value of the coins in the purse is $2.14, getNumDollars returns 2 and getNumCents returns 14.   Refer back to common uses of the mod operator.  Use your private method you wrote in #2 and the following constant to do these calculations.

  final int PENNIES_PER_DOLLAR = 100;

Strings

A string is a sequence of characters.  Strings are object of the String class.

Given the following string:    String message = "Hello, World!"
1.  You can compute the length of a string with the length method.
       int n = message.length();     n = 13
2.  Strings can be concatenated, that is, put end to end to yield a new longer string.  String concatencation is denoted by the + operator.
        String name = "Dave";
        String message = "Hello, " + name;   message = Hello, Dave
3.  Whenever one of the arguments of the + operator is a string, the other argument is converted to a string.
       String a = "Agent";
       int n = 7;
       String bond = a + n;    bond = "Agent7"

        double total = 3.70;
        System.out.println("The total is " + total);   outputs "The total is 3.70"

4.  Use the substring method to extract a part of a string.
        String greeting = "Hello, World!";
        String sub = greeting.substring(0,5);     sub = "Hello"
        sub = greeting.substring(7,12);  sub = "World"
        sub = greeting.substring(7);    sub = "World!";

Reading Console Input and testing your enhancements       


Use the Scanner class to read keyboard input in a console window. 

Example:

import java.util.Scanner;

    Scanner in = new Scanner(System.in);
    System.out.print("How many nickels do you have? ");

    int quantity = in.nextInt();

Some methods in the Scanner class that we will use often.

     

Activity 2

1.  Instantiate a new purse object thePurse.
2.  Ask the user how many pennies, nickels, dimes, quarters and dollars they want to add to the purse.  Add these amounts to the purse.
3.  Get the total value in the purse and print this value with a label.

Activity 3  The Cashier Class


Giving change.  Implement a program that directs a cashier how to give change.  Assume that the cashier has had no transactions.  The program has two inputs: the amount due and the amount received from the customer.  compute the difference, and compute the dollars, quarters, dimes, nickels, and pennies that the customer should receive in return.

For example, to compute the whole dollar amount to return
1.  Transform the difference into an integer balance, denominated in pennies
2.  Compute the whole dollar amount by dividing by 100
3.  Compute the remaining amount by moding by 100

Repeat for quarters, dimes and nickels.  Display the remaining pennies.

To transform the difference into an integer balance easily
(int)( (myAmountReceived - myAmountDue) * 100)

Define a class Cashier with methods


1.  Determine the instance variables(data) for your class.  Determine what  information an object needs to store to do its job.  List beside each method from step 1 the information that is necessary to complete the task. 

2.  Creating your project and implementing the class using methods without the code and comments describing what the method will do.

                   Setup:
3.  Implementing the constructors and methods using the comments you wrote in the shell.  Run your project to check for any errors.

4.  Write a second class to execute test instructions using the code below


Scanner in = new Scanner(System.in);
Cashier harry = new Cashier();

// Enter the amount due
System.out.print("Enter the amount due. ");
double due = in.nextDouble();
harry.setAmountDue(due);

//Enter the amount received
System.out.print("Enter the amount received. ");
double received = in.nextDouble();

harry.receive(received);

//  Testing getAmountDue, getAmountReceived and toString
System.out.println("Due = " + harry.getAmountDue() );
System.out.println("Received = " + harry.getAmountReceived() );
System.out.println(harry);

//  Convert change to number of dollars, quarters, dimes, nickels and pennies to give back
int dollars = harry.returnDollars();
int quarters = harry.returnQuarters();
int  dimes = harry.returnDimes();
int nickels = harry.returnNickels();
int pennies = harry.returnPennies();

// Print the change
System.out.println("Dollars = " + dollars);
System.out.println("Quarters = " + quarters);
System.out.println("Dimes = " + dimes);
System.out.println("Nickels = " + nickels);
System.out.println("Pennies = " + pennies);



Last  Modified:  September 29, 2005 at 5:01pm