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


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.

int [] data // declares a reference to an array of integers data = new int [3] // creates an array object with capacity 3. // assign each cell a value data[0] = 2; data[1] = 4; data[2] = 6; System.out.println(data[0]); // outputs 2 if(data[1] == 4) { System.out.println("Yes it is 4 "); } data[0] = data[2]; System.out.println(data[0]); // outputs 6 System.out.println(data[2]); // outputs 6; System.out.println( data.length ) // outputs 3 data[3] = 1000; // ERROR: not in bounds, arrays start at 0 // can declare, construct and initialize in one step int [] data = {2,4,6}; // can create arrays of other types char [] vowels = {'a','e','i','o','u'}; String [] days = {"sunday", "monday", "tuesday"};

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

String str1 = new String("tony"); // Never create a String object this way. String str2 = new String('tony"); str1 == str2 returns false str1.equals(str2) returns true

To ignore the difference between lower and upper case use equalsIgnoreCase

str1.equalsIgnoreCase(str2); Conclusion: When we want to see if two strings have the exact same characters use the equals method defined in the String class.

Remember when you want to see the services (method) available to String(Or any java class) go to the java API.

API for Java Strings


The Switch Statement

See MortgageRates.java and DiceStats.java

@Anthony Larrain 2005 - 2008

Send Questions or Comments to javadepaul@yahoo.com