Homework #1 CMSC 131
Due Saturday Sept 18, 6:00 pm Object-Oriented Programming I
Type of homework: Open Fall 2004


This homework will allow you to practice variables, strings, input/output facilities, conditionals, loops, and the Eclipse IDE.


For this assignment you will write a credit card application. The program you will write allows users to input a credit card number and an specified amount to be charged. If the credit card is valid and the amount does not exceed the credit card limit, the appropriate amount will be charged to the card. More details about the program are provided in the Specifications section. You may want to take a look at the Sample Run section before you read the detailed description.

In addition to your program, you will keep a log of how much time you are spending on various parts of the homework. The format for the log is described in the Time Log section below.

This homework will be graded as follows:

This homework is considered an open homework. Please visit the course web page for information regarding the policy associated with homeworks of this course.


Credit Cards

In the real world, we would like to write a program that can handle a large number of credit cards, however for simplicity, we will assume only three credit cards can be processed by your program. The credit cards and the credit limit associated with each one are:

Credit Card Number

Credit Limit

1234 1234 1234


1212 3434 4545


1111 2222 3333


Notice that in a credit card number there is a blank character between each group of four digits. In the description that follows when we refer to a valid credit card number we mean one of the three numbers in the above table.


The processing associated with your program can be divided in two subparts: identifying whether we have a valid credit card and charging the credit card (if any valid card was provided).

Identifying a Credit Card

Your program will start by prompting the user for a credit card number. Your program must display the message "Input credit card number." in order to read the number. If the user provides a valid credit card number, your program will proceed to the Charging the Credit Card component of your program, below. If the user provides an invalid credit card number, your program will notify the user about the invalid credit card number by displaying the message "Invalid credit card number." After recognizing an invalid credit card number, your program must ask the user whether he/she wants to try another credit card number by displaying the message "Do you want to try another card?". The program will keep repeating the previously described process as long as the user does not provide a valid credit card number and wants to try another card. If the user decides not to provide a valid credit card at all, your program will terminate and no further processing will take place.

Charging the Credit Card

When your program reaches this point, you know that it has identified one of the three valid cards. Next, your program read the amount to charge, ask the user for confirmation that the amount is correct, and then verify whether the amount can be charged to the specified card.

Your program will read the amount to charge using the message "Enter amount to charge:". After reading this amount your program will ask the user to confirm the amount by using the message "Is $⟨providedAmount⟩ the correct amount?". The field ⟨providedAmount⟩ corresponds to the value entered by the user. If the user acknowledges the amount is correct, your program will convert the input string into variable of type float or double and then proceed to the final stage of verifying that the amount can be charged to the card. (This is described below.) Otherwise, your program will continue asking the user for an amount to charge until an amount is confirmed. Notice that at this point, the program does not provide an option to quit. The user must eventually provide a valid amount and confirm it.

After your program has confirmed the amount to be charged, it will verify that the amount can be charged to the card. A card can only be charged if the amount does not exceed the credit card limit. If the card can be charged then the message "Payment approved." will be generated. Otherwise, the message "Payment denied; credit limit exceeded." will be displayed. The program will end after the appropriate message is generated.

Getting Started

In order to help you get started, we have defined a CreditCard class with a main( ) method in a file named CreditCard.java. In addition, we have provided a time log file for you to complete. Both of these files can be retrieved by checking out the files from the CVS repository. Remember that you must have set up your repository in order to check out and submit homeworks. (The check-out process is all set up now. The submission process is still under construction. We will provide instructions in class sometime during the week of Sep 13.) Information on how to do this can be found in the Managing Homeworks section of the Eclipse tutorial. Follow the instructions given there to check out project p1. Then when you switch back to the Dr. Java, you will see the above files, and you will be able to start modifying them.


Sample Run

The following provides an sample run of the program you are expected to write. Keep in mind this is just an example and not the only scenario your program is expected to handle.

After starting the program and entering an invalid credit card number

After selecting OK

After selecting OK

After selecting Yes and entering valid credit card number

After selecting OK and entering amount

After selecting OK

After selecting No and entering 200

After selecting OK

After selecting Yes

After pressing OK programs ends

Time Log

You must turn in a time log that you will maintain during the week as you work on this class. You will not be graded on the content of this log, only by its existence. It will help us to understand how the class is going and where there are trouble spots. It is important that you fill it out accurately as you work (and not after the fact). We do read these logs, and therefore we ask you to please complete it as accurate as possible. By the way, in the Documents section of the class web page you can find a sample time log.

Student ID
Homework #1 Time Log

Date, task, # hours
Date, task, # hours
Date, task, # hours
Date, task, # hours
⟨comments here⟩


For each session of work outside of class, put one entry in the log. For the task, put one of:

  • reading
  • homework
  • other

Time Log


Submit your project using the submit project option associated with Eclipse. Remember to complete your time log before submitting your project.

About the Due Date

Note that the due date is on Saturday. (Originally we had planned to have the due date on Thursday, Sept 16. But this is a Jewish holiday, and so we decided to push the due date back by 48 hours.) You should try your best to finish Thursday at 6:00pm. In this way, you will have access to TAs during their office hours, and if you need a little extra time, you will still have Friday office hours to seek help.

Challenge Problem

Keep in mind that you are not required to implement the following problem. Please visit the course web page for information regarding challenge problems.

In this homework the credit card numbers must be entered using the format specified above. Modify your program so that it can recognize valid credit card numbers regardless of the number (and position) of blank characters in the credit card number. For example, the following should be recognized as valid credit card numbers:

1675      2222      8911


1 6   75 2  222 8   91  1

One possible approach to solve this problem is to use Java's StringTokenizer class.

Web Accessibility