| 6.170 | Laboratory in Software Engineering Fall 2002 Problem Set 1: Getting Started with Java Due: Tuesday, September 10, 2002 at 4:00 pm |
Quick Link: Problem Set Submission
Contents:
Make a new class called MyFinancialCalc that calculates the following: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);
}
}
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
Implement the method findPrimesFaster in class Primes by copying the code from the findPrimes method and modifying it to have the following features: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
}
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.
"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 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 "
"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).
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.package ps1;
class Point {
double x;
double y;
// Create a point from coordinates
Point(double xVal, double yVal) {
x = xVal;
y = yVal;
}
}
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.
Now define the subclass Automobile, that extends the class Vehicle. Include private member variables (strings) for the make, model, and year of the automobile.package ps1;
public class Vehicle {
private int numberOfWheels;
public Vehicle(int numOfWheels) {
numberOfWheels = numOfWheels;
}
public String toString() {
return "This vehicle has " + numberOfWheels + " wheels.";
}
}
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: 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: Can we use other classes in the Java API (aside from Math and String) to solve this problem set?