Homework #2

Due: By 5pm, Thurs. Jan 25

Worth: 30 points, Late papers will be docked 5 points per 24 hour period.

Collaboration: Feel free to work together on this assignment, although each person should turn in his/her own copy.

Turn In: A printout of your C++ program, a printout of your Java program, and a few paragraphs answering the questions in Step 5. Pledge your paper.

Objective: Students will compare programs written in C++ and Java.

1. Create an algorithm for computing all of the primes from 1 to n. (The efficiency of the algorithm does not really matter.)

2. Implement your algorithm in both Java and C++. Describe the algorithm you are using in your comments. If you do not know Java, either work with a classmate or borrow someone’s book. There are also many resources online. Most of the syntax you will use in this program is the same in Java and in C++.

Your program should NOT display all of the computed primes. It should simply say, for example:

There are primes between 1 and 10.

DONE

3. Run both of these programs on eagle. When running the C++ program, use a.out and g++ as before. When translating the Java program, use javac as usual. However, when you are running the Java program type:

java -Djava.compiler=NONE classname

This turns the JIT (Just In Time) compiler off. Note: For larger, more complex programs, the results you will see will occur (although less noticeably) even with the JIT compiler on.

 

4. Choose three values for n. My algorithm was O(N2), and I choose n=500, n=1000, and n=5000. If your algorithm is more efficient, you may have to choose larger numbers to see any results.

Compare the time each of the three values of n takes to run with each program. (You should have six results.) When measuring the time, you don’t have to be extremely precise; using a watch with a second hand will suffice.

5. Write a few paragraphs answering each of the following questions:

What happened? Describe your results quantitatively here.

Why do you think this happened? Do some reading about Java vs. C++ and explain how the two differ. Form a hypothesis and support it.

What implications does this have? Describe at least one scenario where you think C++ should be used instead of Java and at least one scenario where you think Java should be used instead of C++.

Note: I am not looking for Shakespeare here, but I am expecting good, clear, college-level writing.

Extra Challenge: Try to create the most efficient algorithm in Step 1 that you can.