6.170 Laboratory in Software Engineering
Fall 2002
Problem Set 1: Getting Started with Java
Due: Tuesday, September 10, 2002 at 4:00 pm

Handout P1

Quick Link: Problem Set Submission

Contents:


Getting started



Problems

Problem 1: Language Basics [10 points]
Readings: Language Basics
Look at the code below which calculates the value of investing an initial sum of money at a specified interest rate and for a specified number of years.  Download source FinancialCalc.java.
package ps1;

public class FinancialCalc {

        public static void main(String[] args) {
                double principal = 1000.00;    // $1000 initial investment
                double interestRate = 0.035;   // 3.5% interest rate
                int numOfYears = 7;            // investment length is 7 years
                
                double value = 0.0;
                value = principal * Math.pow((1 + interestRate), numOfYears);
                System.out.println("Investing $" + principal + 
                                   " at an interest rate of " + (interestRate*100) + "%" +
                                   " for " + numOfYears + " years" +
                                   " will have a final worth of $" + value);
        }

}
Make a new class called MyFinancialCalc that calculates the following:
  1. The amount of money you should invest today (at an interest rate of 5%) to have a total amount of $1000.00 at the end of 4 years.
  2. The interest rate you need to turn an initial investment of $500.00 into $525.00 at the end of 3 years (hint:  be careful when carrying out integer arithmetic--don't forget to cast variables if necessary).
  3. The number of years you need to invest an initial sum of $100.00 at an interest rate of 4.4% to have a final value of $150.00 (hint:  the number of years is not necessarily an integer).
We suggest using the methods in the class java.lang.Math to aid your calculations.  Remember that the value (V) of an investment (principal P) compounded yearly for Y years at interest rate I is given by the formula:
V = P * (1 + I)^Y
Run the code you have written, and turn in both your code and the output of the program (place the output into a separate text file called myfinancialcalc.txt).
 
 
 
 
Problem 2: Language Basics [10 points]
Readings: Language Basics Look at the method below which finds prime numbers.  Download source Primes.java.
package ps1;

public class Primes {

        private static void findPrimes(int nValues, boolean printPrimes) {
                boolean isPrime = true;

                for (int i = 2; i <= nValues; i++) {
                        isPrime = true;
      
                        for (int j = 2; j < i; j++) {
                                if (i % j == 0) {
                                        isPrime = false;
                                        break;
                                }
                        }

                        if (printPrimes && isPrime) {
                                System.out.println(i);
                        }
                }
        }
        
        // REMAINING METHODS NOT SHOWN
}
Implement the method findPrimesFaster in class Primes by copying the code from the findPrimes method and modifying it to have the following features: Run the program, and turn in both your modified version of Primes.java and the output of the program (place the output into a separate text file called primes.txt). Note that you will be making further changes to Primes.java in the next problem.
 
 
 
 
Problem 3: Object Basics [15 points]
Readings: Object Basics and Simple Data Objects In this problem you will implement the method findPrimesEvenFaster in class Primes.  Your implementation should take the following into account: So, for example, if the method has already discovered the prime numbers 2, 3, 5, 7, 11, and 13, and we are testing whether the integer 17 is prime, it is sufficient to try and divide the integer 17 by the prime numbers 2 and 3.

Run the program, and turn in both your modified version of Primes.java and the output of the program (place the output into a separate text file called primes2.txt).  Note that for problems 2 and 3 you will be turning in one version of Primes.java with the modifications required by both problems.

There are (at least) two ways to write findPrimesEvenFaster: one using an array, and one using a Vector.  What are the tradeoffs of each approach?  Briefly list the advantages and disadvantages of the approach you took, compared to the alternative.  Include your explanation as a comment in Primes.java.



Problem 4: Object Basics [25 points]
Readings: Object Basics and Simple Data Objects Write a new class called StringScrambler that takes a String as input and reverses the order of all words found in the String.  So for example:
"To be or not to be, that is the question." becomes "question. the is that be, to not or be To"
"Stressed spelled backwards is Desserts" becomes "Desserts is backwards spelled Stressed"
Assume for this problem that a word consists of consecutive characters (letters, numbers, and punctuation) not separated by whitespace.  Also assume that all words in the input string have exactly one space between them, and that the input string has no initial or trailing spaces.  So for example, the following sentence violates all three assumptions:  "  This sentence starts with two initial spaces, has more than one space     in between words, and ends with a trailing space "

Download a skeleton implementation of the class StringScrambler.java.  Feel free to define as many helper methods as you need.  Run the program, and turn in both your modified version of StringScrambler.java and the output of the program (place the output into a separate text file called stringscrambler.txt).
 
 
  


Problem 5: Classes and Inheritance [25 points]
Readings: Classes and Inheritance We define a basic class for point objects.  Download source Point.java.
package ps1;

class Point {

        double x;
        double y;


        // Create a point from coordinates
        Point(double xVal, double yVal) {
                x = xVal;
                y = yVal;
        }
       
}
Use the Point objects to define a class Line. Include a constructor to create a line from two points, a method length to calculate the length of a line, and a toString method to print out a description of the line.

Finally, write a method intersects, called from a Line object, which returns a point as the intersection of two lines (view resource for intersection point of two lines).

Turn in the code that you have written. Make sure the code compiles.
 
 
 
 

Problem 6: Classes and Inheritance [15 points]
Readings: Classes and Inheritance Suppose we have defined a class to represent a vehicle as follows (download source Vehicle.java):
package ps1;

public class Vehicle {

        private int numberOfWheels;

        
        public Vehicle(int numOfWheels) {
                numberOfWheels = numOfWheels;
        }

        
        public String toString() {
                return "This vehicle has " + numberOfWheels + " wheels.";
        }

}
Now define the subclass Automobile, that extends the class Vehicle. Include private member variables (strings) for the make, model, and year of the automobile.

Define two constructors for the subclass Automobile--one that accepts the make, model, and year, and a second that accepts the make, model, year, and number of wheels.  Make sure that the private member variable, numberOfWheels, of the superclass Vehicle is properly initialized.

Finally, define the toString method for the subclass Automobile that overrides Vehicle's toString method. It should print the make, model, and year of the Automobile, as well as, the number of wheels.

Turn in the code you have written.
 

End of Problem Set 1. Please see Problem Set Submission instructions.


Q & A

This section will list clarifications and answers to common questions about problem sets. We'll try to keep it as up-to-date as possible, so this should be the first place to look (after carefully rereading the problem set handout and the specifications) when you have a problem.

Question: In Problem 2, is it really wise to use a labelled continue  statement?  Isn't this like the evil goto statement in other languages, which leads to "spaghetti code"?
Answer:

Question: What Java coding conventions should we follow?
Answer: Follow the Code Conventions for the Java Programming Language. But, no points will be deducted for other reasonable coding conventions.

Question: Can we use other classes in the Java API (aside from Math and String) to solve this problem set?
Answer: Yes, but it isn't necessary.


Back to the 6.170 home page.
For problems or questions regarding this page, contact: 6.170-webmaster@mit.edu.