Lab11

Document Sample
Lab11 Powered By Docstoc
					 AP Computer Science I                         Java Keyword Lab Assignment # 11A

 The Input Protection Program                         80 & 100 Point Versions

 Assignment Purpose:
 The purpose of this assignment is to practice compound conditions with control
 structures. This lab assignment also has the practical value of working with a variety
 of “data input protection” situations. Finally, this lab assignment gives practice with
 interactive “text-style” keyboard input.


Write a program that requests the program user for a password, age, gpa and gender input. Each
one of the four data entries needs to be protected against erroneous input. After correct data is
entered, the program needs to display the entered data values.




Use the following correct data-range criteria

password:          SPOCK                 (case-sensitive; requires all upper-case letters)

age:               0 – 125

gpa:               0.0 – 4.0

gender:            M or F                (lower case ‘m’ and ‘f’ is not required)



You are expected to enter data from the keyboard using “text-style” input. This involves using
various features that were not used in previous programs. The java.io.* package needs to be
imported and use the following statements to make text-style input possible.



 public static void main (String args[]) throws IOException
 {
   BufferedReader input = new BufferedReader(new InputStreamReader(System.in));

                   Remember that keyboard input is stored as a string value.


                         Exposure Java Chapter XI Lab Assignments Page 1 07-01-03
80 Point Version
The 80-point version does not protect against repeated password entries. The program user can re-
enter a new password as many times as is required. The only job of this lab version is to check for
correct data values. You need to provide a loop that repeats as long as the data is incorrect. Make
sure that the input protection loop uses the boolean data type.



Lab11a 80 Point Version                     Single Required Output
Enter password ===>> qwerty
Incorrect; please re-enter
Enter password ===>> SPOCK
Enter Age ===>> 130
Incorrect; please re-enter
Enter Age ===>> -5
Incorrect; please re-enter
Enter Age ===>> 57
Enter GPA ===>> -1
Incorrect; please re-enter
Enter GPA ===>> 4.01
Incorrect; please re-enter
Enter GPA ===>> 3.785
Enter gender ===>> Q
Incorrect; please re-enter
Enter gender ===>> 1
Incorrect; please re-enter
Enter gender ===>> F

Age:      57
GPA:      3.785
Gender:   F




100 Point Version
The 100-point version makes a small, but very significant improvement. The program user can no
longer enter an unlimited number of passwords at will. You will need to use a compound condition
that checks for both the correct password and the number of entry attempts. A total of four attempts
are allowed. If the fourth attempt still is an incorrect password, the program aborts.




                         Exposure Java Chapter XI Lab Assignments Page 2 07-01-03
Lab11a 100 Point Version                 Two Separate Required Outputs

Enter password ===>> QWERTY
Incorrect; please re-enter
Enter password ===>> spock
Incorrect; please re-enter
Enter password ===>> Spock
Incorrect; please re-enter
Enter password ===>> Password
Incorrect; please re-enter

Excessive password entries
Program aborted



Enter password ===>> SPOCK
Enter Age ===>> -5
Incorrect; please re-enter
Enter Age ===>> 130
Incorrect; please re-enter
Enter Age ===>> 57
Enter GPA ===>> -1
Incorrect; please re-enter
Enter GPA ===>> 4.01
Incorrect; please re-enter
Enter GPA ===>> 3.785
Enter gender ===>> Q
Incorrect; please re-enter
Enter gender ===>> 1
Incorrect; please re-enter
Enter gender ===>> F

Age:      57
GPA:      3.785
Gender:   F




                      Exposure Java Chapter XI Lab Assignments Page 3 07-01-03
 AP Computer Science I                          Java Keyword Lab Assignment # 11B

 The “Watch What Your Borrow” Program I                              80 & 100 Point Versions

 Assignment Purpose:
 This assignment is the first of a two-part lab assignment that gives students practice
 with Java control structures. Lab assignment 11B is a subset of Lab 11C, which
 uses a greater variety of control structures. The “Watch What You Borrow”
 assignment also has the intended purpose to show students the reality of borrowing
 money and the manner in which payments fluctuate with different rates and the
 number of different payback times.



Write a program that enters the mortgage amount or loan amount, yearly interest, payback years
from the keyboard, and then computes the monthly payment amount of the loan.



A monthly mortgage payment is the same as a monthly loan payment. Normally, real estate loans
are called mortgage loans. In this case you know the amount of the loan (called the principal), the
annual interest paid, and the length of time to pay back the loan. The program needs to take that
information and compute the monthly payment. You will not need to do any exotic calculations to
compute this amount. There exists a formula to compute this very requirement.

The letters in the formula below represent the following values:

      P - - Principal amount borrowed, or loan amount
      R - - Rate of interest computed for each month
      N - - Number of months to pay back the loan or mortgage




                           R x (1 + R )N 
                PAYMENT =          N     xP
                           (1 + R ) - 1 


Watch out for a couple of potential snags as you use this formula. The interest that the program user
enters is the yearly interest, which is expressed as a percent. The formula uses the monthly rate.
                          Exposure Java Chapter XI Lab Assignments Page 4 07-01-03
This means that you need to convert percent to a rate and convert the yearly rate to a monthly rate.
You will get some very strange errors, and you will likely lock up the system, if you fail to make the
conversion. A second, more minor snag, is the number of months, which is required by the formula.
The program user enters the number of years to pay back the loan, not the number of months. This will
require another small conversion.

The program requires that you create an Interest class. The Interest class has attributes and data.
You will be provided with these required items for this assignment, but you need to complete the
methods.


class Interest
{
  private double principal;                  //   Loan or mortgage amount
  private double percent;                    //   yearly interest entered in percents
  private int years;                         //   Number of years to pay back the loan
  private double monthlyPmt;                 //   Monthly loan payment computed by provided formula
  private int months;                        //   Number of months to pay back the loan
  private double monthlyRate;                //   Monthly interest rate

    public Interest(double la, double yi, int py)
    // Constructor
    // Assigns values for Loan Amount, Yearly Interest and Payback Years
    // Constructor should also compute the MonthlyRate and number of Months

    {
    }

    public void computePayment()
    // void methods which computes the monthly loan payment with the provided formula.
    {
    }

    public void displayResults()
    // void method, which displays the attribute values of the Interest object.
    {
    }

}




The required output intentionally use the same mortgage amount with three different interest rates.
The three rates reflect realistic low-end, high-end and average rates that have been used in the
United States during the last fifty years. Note the tremendous difference in the monthly payment
amounts between the low-end and high-end interest rates. There are actually higher interest rates
for people with bad credit. Car loans and other loans are available for individuals with bad credit, but
rates may go as high as 29%.




                                  Exposure Java Chapter XI Lab Assignments Page 5 07-01-03
Lab11b 80 Point Version                      Three Separate Required Outputs
Enter principle amount ===>>        100000
Enter annual percent    ===>>       6
Enter years to pay back ===>>       30

Loan Amount:    100000.0
Yearly Interest: 6.0
Payback Years: 30
Monthly Payment: 599.5505251527569



Enter principle amount ===>>        100000
Enter annual percent    ===>>       12
Enter years to pay back ===>>       30

Loan Amount:       100000.0
Yearly Interest:   12.0
Payback Years:     30
Monthly Payment:   1028.6125969255045



Enter principle amount ===>>        100000
Enter annual percent    ===>>       18
Enter years to pay back ===>>       30

Loan Amount:       100000.0
Yearly Interest:   18.0
Payback Years:     30
Monthly Payment:   1507.0853718043256




100-Point Version
You saw in the output requirements for the 80-point version that no attempt was made to round off
the monthly payments. The 100-point version adds the requirement of computing the total amount of
payments made and the total amount of interest paid. It will be important to round off the monthly
payment to the nearest penny. Payments over a 30-year period can compound round-off errors
when there are 360 payments that are all off by some fraction of a penny. Rounding-off to the
nearest cent is a requirement for the 100-point version and you are expected to create a method to
perform that action. The method has the following appearance.


   private double round(double x)
   // method which returns x rounded off to the nearest 1/100th
   {

   }

Note that this is a private method of the Interest class. It is not necessary to call round outside the
scope of an Interest object. This method exists strictly for the purpose of methods that are members
of the Interest class.


                          Exposure Java Chapter XI Lab Assignments Page 6 07-01-03
Lab11b 100 Point Version                   Three Separate Required Outputs
Enter annual percent    ===>>     6
Enter years to pay back ===>>     30


Loan Amount:       100000.0
Yearly Interest:   6.0
Payback Years:     30
Monthly Payment:   599.55
Total Payments:    215838.0
Total Interest:    115838.0


Enter principle amount ===>>      100000
Enter annual percent    ===>>     12
Enter years to pay back ===>>     30


Loan Amount:       100000.0
Yearly Interest:   12.0
Payback Years:     30
Monthly Payment:   1028.61
Total Payments:    370299.6
Total Interest:    270299.6



Enter principle amount ===>>      100000
Enter annual percent    ===>>     18
Enter years to pay back ===>>     30


Loan Amount:       100000.0
Yearly Interest:   18.0
Payback Years:     30
Monthly Payment:   1507.09
Total Payments:    542552.4
Total Interest:    442552.4




                        Exposure Java Chapter XI Lab Assignments Page 7 07-01-03
 AP Computer Science I                         Java Unit Lab Assignment # 11C

 The “Watch What Your Borrow” Program II 80 & 100 Point Versions

 Assignment Purpose:
 This assignment is the second stage of the two-part lab assignment that gives
 students practice with a variety of control structures and compute interest problems.
 This assignment continues where Lab11B finished.



Write a program that helps you make informed decisions about loans, mortgages, and credit cards.
Millions of people borrow money with loans, mortgages and credit cards, and millions of people are
not completely aware of the tremendous impact that interest has on their lives. This assignment
hopes to give you both a challenging computer science assignment, as well as an eye opener that
can help you in the future with your money management.



Inside the main method you need to provide the program user the option to select one of three
interest computations. The first selection, Monthly Loan Payment, is in fact what you have already
done for Lab11B. It is now one of three different computations. The program segment shown below
is the statements needed for the selection menu. The remainder of the main method is composed of
a switch statement with different options and different method calls.




   System.out.println("Select one of the following options");
   System.out.println();
   System.out.println("[1] Monthly Loan Payment Computation");
   System.out.println("[2] Amortization Schedule");
   System.out.println("[3] Credit Card Pay-Off Schedule");
   System.out.println();
   System.out.print("Enter your choice ===>> ");
   Choice = Integer.parseInt(Input.readLine());




Each one of the three interest computations will be explained in turn. For convenience, the first
monthly payment computation will be explained again. For the 80-point version you will only do the
first two selections. For the 100-point version you will do every type of computation.


                         Exposure Java Chapter XI Lab Assignments Page 8 07-01-03
[A]       Compute monthly mortgage payment (required for 80 and 100 points)

A monthly mortgage payment is the same as a monthly loan payment. Normally, real estate loans
are called mortgage loans. In this case you know the amount of the loan (called the principal), the
annual interest paid, and the length of time to pay back the loan. The program needs to take that
information and compute the monthly payment. You will not need to do any exotic calculations to
compute this amount. There exists a formula to compute this very requirement.

The letters in the formula below represent the following values:

          P - - Principal amount borrowed, or loan amount
          R - - Rate of interest computed for each month
          N - - Number of months to pay back the loan or mortgage



                             R x (1 + R )N 
                  PAYMENT =          N     xP
                             (1 + R ) - 1 


[B]       Display amortization schedule (required for 80 and 100 points)
An amortization schedule is a “loan pay-off” schedule. Understanding the amortization schedule
requires understanding the nature of loan interest. Each month when you make a payment on a loan
or a credit card, the bank or lender first wants its cut. This is known as the Interest Payment. The
bank subtracts the interest payment from the Monthly Payment made by you to determine how much
is left to reduce your balance. The amount left after the monthly interest is subtracted is called the
Principal Payment. The principal payment is now subtracted from the amount you still owe, which is
called the Principal Balance.

Below is a short amortization schedule that shows the different amounts, just mentioned, in different
columns. This schedule involves a loan of $1000.00 with an annual interest of 13.5% and monthly
payments of $250.00. This loan schedule is very short for the sake of a quick demonstration. Typical
loan pay-off schedules are much longer and require much higher interest payments.


PAYMENT               MONTHLY              INTEREST                PRINCIPAL           PRINCIPAL
 NUMBER               PAYMENT               PAYMENT                  PAYMENT             BALANCE

      1                250.00                   11.25                   238.75           761.25
      2                250.00                    8.56                   241.44           519.81
      3                250.00                    5.85                   244.15           275.66
      4                250.00                    3.10                   246.90            28.76
      5                 29.08                    0.32                    28.76             0.00


                            Exposure Java Chapter XI Lab Assignments Page 9 07-01-03
The majority of loans have a fixed monthly payment. Usually, only the last payment is different. When
you write your program are careful with this last payment. It is easy to end up with a negative balance.
There are no special formulas for this option. All the computations are done based on regular
percentage and interest arithmetic.

Your program actually is not meant to display the monthly progress of the amortization schedule. Only
a summary is required of the total number of months required to pay off the loan along with the total
amount of the payments and the interest. The computation still need to be performed in a loop as is
shown by the amortization schedule.




[C]    Credit card pay-off schedule (100-point version only)

The credit card pay-off schedule uses the same logic as the amortization schedule. A credit card debt
is a loan and the interest is handled in a similar manner. We are not speaking of the situation where a
person charges with a credit card and pays off the balance at the end of the month. There is little
computation required for such a situation. The big difference between loans and credit cards is that
credit cards do not carry a fixed monthly payment. They have a fixed monthly minimum rate, which
usually is around 3% and you need to use 3% for this assignment. This means that the monthly
payment gets smaller, as the balance becomes smaller. This is a major problem with credit cards. The
reality of life’s financial pressures forces many people to make the minimum payment, and that system
keeps people in debt forever.

The credit card pay-off schedule is computed in the same manner as the amortization schedule.
However, you first must compute the monthly payment, which is 3% of the credit card balance. Make
sure that your monthly payment does not fall below $10.00. Most credit card companies are not
interested in handling payments less than $10.00.

The credit card pay-off schedule that follows shows the start and the end of the schedule to save paper.
For the sample schedule assume that the credit card has a $2500.00 balance, the annual interest is
18.9% and the minimum monthly payment is 3% of the balance or $10.00, whichever is more.

PAYMENT             MONTHLY               INTEREST                PRINCIPAL           PRINCIPAL
 NUMBER             PAYMENT                PAYMENT                  PAYMENT             BALANCE

   1                  75.00                  39.38                    35.62              2464.38
   2                  73.93                  38.81                    35.12              2429.26
   3                  72.88                  38.26                    34.62              2394.64
   4                  71.84                  37.72                    34.12              2360.52
.......             .......                .......                 ........             ........
  186                 10.00                   0.46                     9.54                19.66
  187                 10.00                   0.31                     9.69                 9.97
  188                 10.00                   0.16                     9.84                 0.13
  189                  0.13                   0.00                     0.13                 0.00

TOTAL PAYMENTS:            5037.71
TOTAL INTEREST:            2537.71


                          Exposure Java Chapter XI Lab Assignments Page 10 07-01-03
Here is a little lesson about credit card interest reality. The credit card bank charges 18.9% interest per
year but by the time the card is paid off, you have paid more than 100% total interest. Few credit cards
advertise that bit of information. Consider a second point. This credit card pay-off schedule assumed
that the credit card is never again used after its initial charge of $2500.00, which does not reflect the
way most people use credit cards. When you finish with this program, check out different loan and
credit card situations. In particular, observe how much interest is saved on loans and credit cards when
you pay more than the minimum amount required. You will find this especially true for home mortgages
that are set up for 30-year payments. Tremendous amounts of interest can be saved on long-term
loans and mortgages when they are paid off quicker.

You also need to be aware that credit card interest is much higher than car loan and house mortgage
interest. This is especially true for new credit card holders who have no established credit card history.
Another popular procedure is to give people new credit cards with low initial interest rates. Few people
read the small print, which indicates that this low rate is only good for the first couple of years.

Like the amortization schedule your program actually is not meant to display the monthly progress of
the credit card pay-off schedule. Only a summary is required of the total number of months required to
pay off the loan along with the total amount of the payments and the interest. The computation still
needs to be performed in a loop as is shown by the amortization schedule.


You will need to use the Interest class, which was started with the Lab11b assignment. Additional
attributes and methods are needed to satisfy to additional computations of the Lab11c assignment.
The Interest attributes and the stubs of the required methods are shown below.


class Interest
{
  private double principal;               //   Loan or mortgage amount
  private double percent;                 //   yearly interest entered in percents
  private int years;                      //   Number of years to pay back the loan
  private double monthlyPmt;              //   Monthly loan payment computed by provided formula
  private int months;                     //   Number of months to pay back the loan
  private double monthlyRate;             //   Monthly interest rate
  private int pmtNr;                      //   Number of monthly payments required for pay-off
  private double interestPmt;             //   Monthly interest paid for loan or credit card
  private double principalPmt;            //   Monthly amount used to reduced principal balance
  private double totalPmt;                //   Total amount of payment made
  private double totalInt;                //   Total amount of interest paid

 public Interest(double la, double yi, int py)
 // Constructor
 // Assigns values for Loan Amount, Yearly Interest and Payback Years
 // Constructor should also compute the MonthlyRate and number of Months
 {
 }

 public void computePayment()
 // void methods which computes the monthly loan payment with the provided formula.
 {
 }




                                 Exposure Java Chapter XI Lab Assignments Page 11 07-01-03
    private double round(double x)
    // method which returns x rounded off to the nearest 1/100th
    {
    }

    public void amortization()
    // method which computes loan amortization and displays total number of payments,
    // total payment amount and total interest amount
    {
    }

    public void creditCard()
    // method which computes credit card pay-off and displays total number of payments,
    // total payment amount and total interest amount
    {
    }
}




80-Point Version
The 80-point version of Lab11c only requires computing monthly loan payments and the amortization
schedule. Keep in mind that the amortization schedule is not actually shown on a monthly basis.
You will only show the total number of payments made, the total amount of the payment and the total
amount of the interest paid.



Lab11c 80 Point Version                              Three Separate Required Outputs
Select one of the following options

[1]       Monthly Loan Payment Computation
[2]       Amortization Schedule

Enter your choice          ===>>     1

Enter loan amount       ===>>              30000
Enter annual percent    ===>>              12.5
Enter years to pay back ===>>              5

Monthly Loan Payment Computation

Loan Amount:             30000.0
Yearly Interest:         12.5
Payback Years:           5
Monthly Payment:         674.94
Total Payments:          40496.4
Total Interest:          10496.4



Select one of the following options

[1]   Monthly Loan Payment Computation
[2]   Amortization Schedule
Enter your choice ===>> 2

                                Exposure Java Chapter XI Lab Assignments Page 12 07-01-03
Enter principle balance ===>> 30000
Enter annual percent    ===>> 12.5
Enter monthly payment   ===>> 674.94

Amortization Schedule

Loan Amount:        30000.0
Yearly Interest:    12.5
Monthly Payment:    674.94
Payback Months:     60
Total Payments:     40496.3
Total Interest:     10496.3




100-Point Version
The 100-point version of Lab11c requires computing monthly loan payments, amortization schedules
and credit card pay-offs. Keep in mind that the amortization schedule and the credit card pay-off are
not actually shown on a monthly basis. You will only show the total number of payments made, the
total amount of the payment and the total amount of the interest paid.




Lab11c 100 Point Version                     Three Separate Required Outputs
Select one of the following options

[1]   Monthly Loan Payment Computation
[2]   Amortization Schedule
[3]   Credit Card Pay-Off Schedule

Enter your choice     ===>>   1

Enter loan amount       ===>>      30000
Enter annual percent    ===>>      12.5
Enter years to pay back ===>>      5

Monthly Loan Payment Computation


Loan Amount:        30000.0
Yearly Interest:    12.5
Payback Years:      5
Monthly Payment:    674.94
Total Payments:     40496.4
Total Interest:     10496.4




                         Exposure Java Chapter XI Lab Assignments Page 13 07-01-03
Select one of the following options

[1]   Monthly Loan Payment Computation
[2]   Amortization Schedule
[3]   Credit Card Pay-Off Schedule

Enter your choice     ===>>     2

Enter principle balance        ===>>        30000
Enter annual percent           ===>>        12.5
Enter monthly payment          ===>>        674.94

Amortization Schedule

Loan Amount:        30000.0
Yearly Interest:    12.5
Monthly Payment:    674.94
Payback Months:     60
Total Payments:     40496.3
Total Interest:     10496.3



Select one of the following options

[1]   Monthly Loan Payment Computation
[2]   Amortization Schedule
[3]   Credit Card Pay-Off Schedule

Enter your choice     ===>>     3

Enter credit card balance           ===>>     5000
Enter annual percent                ===>>     18.9

Credit Card Pay-Off Schedule

Loan Amount:        5000.0
Yearly Interest:    18.9
Payback Months:     237
Total Payments:     10300.78
Total Interest:     5300.78




                         Exposure Java Chapter XI Lab Assignments Page 14 07-01-03

				
DOCUMENT INFO
Shared By:
Categories:
Tags:
Stats:
views:0
posted:5/18/2012
language:
pages:14
fanzhongqing fanzhongqing http://
About