Lecture Notes 8, csc211
Prepared by: Anthony Larrain
These notes serve as an outline to the lecture, they are not intended to be complete. You should be reading the assigned sections. During class I may include or omit certain topics.
Loan Calculator Program
Write a loan calculator program that computes both monthly and total payments for a given loan amount, annual interest rate, and loan period. Allow the user to enter multiple computations. Use the formula.
In the formula P is the amount borrowed, i is the monthly rate and n is the number of payments.
Overall Plan
- Get the three values from the user.
- Amount of Loan.
- Yearly Interest Rate.
- Duration of the loan in years.
- Compute monthly and total payments. Use functions to compute monthly and total payments.
- Output the results.
- Repeat
Arrays In Java
Consider the problem, suppose we wanted to find the average of a collection of scores and print out a table showing each score and its distance from the mean.
- A data structure used to group a collection of values of the same type using a single name.
- Each member can be accessed by an index(Starts at 0).
- In Java an Array is an Object (Special).
- Allocated Dynamically.
- Arrays have a public instance variable called length which specifies the capacity.
- The Capacity of an array object cannot change after it is created.
- Runtime Bounds Checking.
- Examples:
More On Java Strings
String name = "LipTon"; String lower = name.toLowerCase(); System.out.println(name); // outputs LipTon System.out.println(lower); // outputs lipton String upper = name.toUpperCase(); System.out.println(name); // outputs LipTon System.out.println(upper); // outputs LIPTON System.out.println(name.length()); // outputs 6 System.out.println(name.charAt(0)); // outputs 'L' System.out.println(name.charAt(name.length() - 1)); // outputs 'n' String sub = name.substring(0,3); System.out.println(sub); // outputs Lip sub = name.substring(3,6); System.out.println(sub); // outputs Ton String answer = " yes "; String response = answer.trim(); System.out.println(answer); // outputs ---yes-- where - is the space character System.out.println(response); // outputs yes String doAgain = "yes"; String another = new String("yes"); if(doAgain == another){ System.out.println("They are equal"); }else{ System.out.println("Not equal"); // output Not equal } if(doAgain.equals(another)){ System.out.println("They are equal"); // output They are equal } else{ System.out.println("Not equal"); }
== vs. equals() method
To ignore the difference between lower and upper case use equalsIgnoreCase
Remember when you want to see the services (method) available to String(Or any java class) go to the java API.
The Switch Statement
See MortgageRates.java and DiceStats.java