Last Update: Nov. 9, 2025

Assignment 5 (PS5): Methods with Return, Input, Formatting, Conditionals

This assignment focuses on methods with parameters and returns. It will use methods in the StdDraw class, the Math class, and the printf method.

Due: 11:55 PM, Sunday, Nov. 16, 2025.

IMPORTANT: The best advice we can give you about PS5 is to carefully test, debug, and re-test your code as you write it. Do not attempt to write a whole program at once—if you do, then you may have no idea where to find the error if the program doesn't work. Proactive testing will save you enormous amounts of time in the long run. Trust us!

Part 0: Text Input/Output (Scanner and Printf) and Conditionals.

Please watch this video on how to use Scanner to get user input, how to use printf to format output, and how to use conditionals. StdDraw. (pptx, pdf)

Part 1. Cashier [input/output, arithmetic operators; data range and precision; Math class]

The program reads in unit price, quantity, sales tax, and paid amount. It then determines the amount of change, if any, that would be received; and prints the numbers of 50 dollars, 20 dollars, 10 dollars, 5 dollars, 1 dollars, quarters, dimes, nickels, and pennies that should be returned. Maximize the dollars/coins with the highest value. Follow the format below:

        Welcome to Cashier!
        
        Enter the unit price: 1.28
        Enter the quantity: 1
        Enter the sales tax rate: 6.0
        The total owed amount is $1.36 ($1.28 plus 6.00% tax)
        Enter the paid amount: 5.00
        Your change of $3.64 is given as:
        0 50 dollars
        0 20 dollars
        0 10 dollars
        0 5 dollars
        3 1 dollars
        2 quarters
        1 dime
        0 nickels
        4 pennies
        

Implementation Details

Please put the solution into a file named Cashier.java. You should also use Cashier as the name of the class.

Discussions

The strategy of maximizing the dollars/coins with the highest value is also referred to as a greedy strategy. The greedy strategy produces a solution that uses the minimum number of dollars/coins in dispensing change for the American currency system. However, there are currency systems that use different denominations for which this property is not valid. One example is the old British coin system consisting of a halfpenny, a penny, threepence, sixpence, a shilling (12 pence), a florin (24 pence), a half-crown (30 pence), a crown (60 pence), a pound (240 pence), and a guinea (252 pence).

  1. Can you give an amount of change so that the greedy strategy does not produce the minimum number of coins in the old British system?
Your report should include Cashier.doc or Cashier.txt to answer the first question.

Part 2: Monte Carlo Simulation (Conditional, Accumulative Loop)

Monte Carlo methods are widely used in many fields, in particular physical and mathematical fields when it is impossible to obtain a closed-form expression or infeasible to apply a deterministic algorithm. According to this link: "The modern version of the Monte Carlo method was invented in the late 1940s by Stanislaw Ulam, while he was working on nuclear weapon projects at the Los Alamos National Laboratory. It was named, by Nicholas Metropolis, after the Monte Carlo Casino, where Ulam's uncle often gambled."

In this part, we will implement the classical Monte Carlo example: computing the value of Pi. The setup of the problem is as follows. There is a 1 by 1 square. There is a circle with radius 0.5 in the square. A picture of the setup is shown below.
image

The basic idea of Monte Carlo in this setting is simple. As your program "drops" points randomly into the square, some of the points will drop into the circle, and some will not. Suppose the program drops N points and H are in the circle. Then H divided by N (called hit ratio) should be proportional to the ratio of the area of the circle and that of the square. Please derive pi from this expression.

We provide a skeleton to get you started. The details of requirements on your program consist of the following:

Please name your class MonteCarloPi and save the class in a file named MonteCarloPi.java.

Part 3: Submit Your Assignment

Please submit electronically for assignment #5 and make sure you submit (1) Cashier.java, Cashier.doc (or Cashier.txt), and (2) MonteCarloPi.java.java. Please be sure that you choose the .java file, NOT the .class file when uploading. The .class file is the compiled version of your code which we cannot examine and grade. So, please make sure you submit *.java files. Also, remember that you always need to include the header.

    //*******************************************************************
    //
    //   File: FileName.java          Assignment No.: 5
    //
    //   Author: <your name>      Email: <your email>
    //
    //   Class: ClassName
    // 
    //   Time spent on this problem: 
    //   --------------------
    //      Please give a description about your design. 
    //
    //*******************************************************************

The submission repository for ps5 is https://gitee.com/simmonsong/ct-xmuf25-ps5.

Please follow the instructions in Assignments Submission to submit your assignments.

Git introduction is a help document for git utilization..

Enjoy!


Some part of the problem set derived from Building Java Programs.