CSC 120

Week 2


Reading Assignment:

Read Chapter 2
Here are the programs we looked at in class: Interest.java HelloWorld2.java InputString.java InputDouble.java InputChars.java Adder.java

Lab/Homework Assignment:

  1. Here is the interest program Interest.java we looked at in the class.

     public class Interest {
    
                /*
                    This  program  computes the value of an investment of  
                   $1000.0 invested at an interest
                   rate of 0.02 for one year.  
                */
             
                public static void main(String[] args) {
                
                    /* Declare the variables. */
                
                    double principal;     // The value of the investment.
                    double rate;          // The annual interest rate.
                    double interest;      // Interest earned in one year.
                    
                    /* Do the computations. */
                    
                    principal = 1000.0;
                    rate = 0.02;
                    interest = principal * rate;   // Compute the interest.
                    
                    principal = principal + interest;
                          // Compute value of investment after one year, with interest.
                          // (Note: The new value replaces the old value of principal.)
                          
                    /* Output the results. */
                          
                    System.out.print("The interest earned is $");
                    System.out.println(interest);
                    System.out.print("The value of the investment after one year is $");
                    System.out.println(principal);
                                   
                } // end of main()
                   
             } // end of class Interest
    
    
    

    Add to this program so that it first asks the user to enter the principal and the interest rate. Then your program should compute and print out the investment after the first and second years.

  2. Write a program that asks the user's first name and last name, and then greets the user by name and tells the user how many letters his/her name contains. For example, if the user's name is John Smith, then the program should respond:
    Hello, John Smith, nice to meet you!
    There are 4 letters in your first name
    and 5 letters in your last name.
    Have a nice day.