| 6.170 | Laboratory in Software Engineering
Spring 2002 Problem Set 1: Getting Started with Java Due: Tuesday, February 12, 2002 at 4:00 pm |
Quick Link: Problem Set Submission
Contents:
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:
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).V = P * (1 + I)^Y
Problem 2: Language Basics
[10 points]
Readings: Language
Basics
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:
Problem 3: Object Basics
[15 points]
Readings: Object
Basics and Simple Data Objects
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.
Did you notice anything strange when you ran the program? Run it a couple of times, paying attention to how fast each method completes its task. Write a brief paragraph explaining what you see (append this explanation to the end of primes2.txt).
(Addendum: It has come to the
TA's attention that the machine on which you run the Primes program
has a bigger effect on the output than we had expected. When
answering the above question, base it on the sample program output
found here.)
Problem 4: Object Basics
[25 points]
Readings: Object
Basics and Simple Data Objects
"To be or not to be, that is the question." becomes "question. the is that be, to not or be To"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 at most 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 "
"Stressed spelled backwards is Desserts" becomes "Desserts is backwards spelled Stressed"
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
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
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.
Question: Problem 2, Anonymous Comment concerning the
wiseness of introducing "spaghetti code" in terms of a labeled continue
(goto). Thank you very much for bringing this up the the staff so that
we may address this issue.
Answer: