Please submit your problems using turnin.
You will have the following files to turn in when you are done:
MyFinancialCalc.java, Primes.java,
StringScrambler.java, and Line.java
Contents:
First, we're going to see how to compile and run Java programs directly
from If you plan to use Emacs (or JEdit) to edit
your code, this is how you'll be running your code in general.
If you plan to use JGrasp (or another IDE) then things will be
slightly different in the future.
Copy Simple.java.
Simple.java is a Java source file a text file of Java code with a .java
extension. To run Simple.java, you must compile it first.
From the command line, you compile a Java class with the javac
command. To compile Simple, run:
javac Simple.java
This may take a while. It will produce a file called
Simple.class which is
the compiled binary form of the Simple class.
To run Simple, type:
java Simple
You should see a welcome message.
If not, then there is something wrong with your installation of
Java, and you need to work that out.
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. Open source FinancialCalc.java.
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:
-
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.
-
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).
-
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
Readings: Language
Basics
Look at the method below which finds prime numbers. Open source
Primes.java.
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:
-
Uses labeled continue instead of break.
-
Does not require the isPrime variable.
- Only tries to divide by integers up to the square root of the number being tested.
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 in to account:
-
When testing whether an integer is prime, it is sufficient to try and
divide by the list of prime numbers up to the square root of the
number being tested.
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.
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.
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 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 "
Open a skeleton implementation of the class StringScrambler.java.
Feel free to define as many helper methods as you need.
,
Readings: Classes
and Inheritance
We defined a basic class for 2D point objects in our first lecture.
Open source Point2d.java.
Use the Point2D 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).