Assignment
0 (5%): Due 11:59PM Friday September 30,
2011 |
With respect to the development environment:
With respect to Java programming, if you feel rusty we recommend walking through Oracle's Java Tutorial. We also provide three optional Java programming problems below that you can solve if you want more practice.
You are free to get
help from anyone, on any portion of this problem set; that is, the standard
collaboration policy does not apply to this problem set. You will need to
know this material for the remainder of the quarter, however, so make sure you
understand the tools and the concepts. This assignment's primary focus for
grading is correctness, not style.
Note for later:
This assignment does not include any information on using version control
(svn
) or unit testing (JUnit
). These will come soon
and will require learning more about Eclipse, version control and unit
testing. The only warning for now is that you should be very careful to save
your files often and in a place that is backed-up and stable (most likely, your
account on attu
).
All turn-in for this assignment is through the Catalyst webq (your Java code will be copied-and-pasted there). It is set to let you backtrack, edit, etc. so you can work on it in bits and pieces, as needed.
Complete the questionnaire in the Catalyst turn-in form (see bottom of page).
Do the inital
setup for Eclipse. Then do the following to create and run Hello World!
Java perspective
in Eclipse:
Window >> Open Perspective >> Java (default)
. An
Eclipse perspective helps you organize, see and edit your programs through
a specific view; take a peek at a couple of other perspectives, in
particular noting that there are differences between the Java perspective,
the Java Browsing
perspective and the Java Type
Hierarchy
perspective! Assignments331
(or such)
using File >> New >> Java Project
. Java projects
are an Eclipse structure that contains source code and a Java builder that
compiles the program incrementally as the source code is modified. Using
the defaults, click through the following screens to create the project.
a0
, again using File >> New...
. You've
seen Java packages before -- they defined as part of Java as a way to
organize the namespaces of Java programs. a0
should be
created under the src
directory of the project.File >> New
again,
create a Java class named HelloWorld
in package
a0
. You can select the defaults, which will create the class
and some basic template information. Now visit HelloWorld.java and, using copy-and-paste,
replace the created template with that code. (We'll see better ways to load
existing programs later on.) You should now be able to run HelloWorld
(using Ctrl-F11
or Run >> Run As >> Java
application
), seeing the output in the console. Hello World! Best wishes from <uwid>!
" where
<uwid>
is the name that comes before your
@u.washington.edu
identifier. For example, my @u
identifier is notkin
so my greeting would be: "Hello
World! Best wishes from notkin!
" (Run it to check, of course.)Different people have different learning styles. So you can pick among the
following approaches to get the basics of Eclipse. You can mix-and-match any of the following
Eclipse tutorials and guides:
Help >> Welcome
and select the
Tutorials icon. The only pertinent tutorial found here (for now) is
Create a Hello World Application
.Workbench Basic
is the pertinent piece for
now. Within that, read the Eclipse platform overview
and the
Basic Tutorial
(under Getting started
). You can
skim (or skip) the parts on Working with other editors
,
Tasks and markers
, Bookmarks
,
Comparing
, Local history
, and Responsive
UI
—and you needn't look at Team CVS
tutorial or
Ant and external tools
tutorial. You should read/skim the
Concepts
piece, skimming or skipping the topics listed in the
previous sentence.Using the Eclipse Workbench
and another on Eclipse and
Java for Total Beginners
.Create another class, HolaWorld
, and
again using copy-and-paste, replace the template with HolaWorld.java. When you try to compile this
code, you should see compilation errors in the file. In particular the
lines
System.out.println(world.) ... return spanishGree;
are problematic. Eclipse marks these errors with red squiggly lines in
HolaWorld.java
and the Package Explorer
marks
HolaWorld.java
with a red crossmark. (In some cases, you may only
see the second error after you've fixed the first one. And if you get errors
for other files, ignore them for now.) Fix these errors and run
HolaWorld
. Then modify the greeting to be: "Hola Mundo! Los
mejores deseos de <uwid>!
" Run it to check, of course.
What is output when you invoke HolaWorld
through its
main
method, and why?
Define your first Java class with a main
method that will
randomly choose, and then print to the console, one of five possible greetings
that you define. Including the main
method makes the class
runnable, just like HelloWorld
and HolaWorld
. So,
create a Java class named RandomHello
—select the obvious
checkbox on the initial class creation screen to generate main
.
This class will be in package a0
.
A code skeleton for the RandomHello
class is shown below, some
of it generated by Eclipse when you create the class. This skeleton is a
starting point; you are free to organize it as you see fit.
package a0;
/**
* RandomHello selects a random greeting to display to the user.
*/
public class RandomHello {
/**
* Uses a RandomHello object to print
* a random greeting to the console.
*/
public static void main(String[] argv) {
RandomHello randomHello = new RandomHello();
System.out.println(randomHello.getGreeting());
}
/**
* @return a random greeting from a list of five different greetings.
*/
public String getGreeting() {
// YOUR CODE GOES HERE
}
}
Use Java's Random
class to select a greeting; most likely, you've used this in 142/143. To
create the random number generator itself use something like:
Random randomGenerator = new Random();
To create a random number between 0 and 4, use a method invocation like
randomGenerator.nextInt
You must import the Random
class by entering the import
java.util.Random
directly or by using Eclipse's
CTRL-SHIFT-O
command to Organize your imports. This
command adds import statements to your code for missing packages and removes
them for unneeded packages. (If the class that needs to be imported is
ambiguous — for example, there is a java.util.List
as well as a java.awt.List
—Eclipse
will give you a choice.) Until you include the proper import statements,
Eclipse will use a red underline to mark your code as an error.
One way to store a random greeting is using an array. This approach might look
something like:
String[] greetings = new String[] { "Hello World", "Hola Mundo", "Bonjour Monde", "Hallo Welt", "Ciao Mondo"}
When you are finished writing your code and it compiles, run it several times to ensure that all five greetings can be displayed.
This quiz asks a few Java as well as Eclipse questions.
This is yet another exercise intended to get you a little more Eclipse
experience and to brush up on a little bit of Java as well. If you have
difficulties with the Java, you will want to read up on Java control flow
statements. Most of you have learned about computing primes using the
Sieve of Eratosthenes, and many of you have even written a program in
142/143 to implement this. Open Primes.java
with your browser and use it to
create a new runnable class Primes
. Implement the new method
findPrimesFaster
in class Primes
by copying the code
from the findPrimes
method and modifying it to have the following
features:
labeled continue
instead of break
. (A
review of break
and a description of continue
is
here.)isPrime
variable.Part VIII: Prime numbers again (10 points)
This is the final exercise intended to get you a little more Eclipse experience and to brush up on a little bit of Java as well. If you have difficulties with the Java, you will want to read up on Java objects and arrays. In this problem you will implement the methodfindPrimesEvenFaster
in the class
Primes
. You may want to look at the Java Array
or
ArrayList
structures. Your implementation should take the
following into account:
findPrimesEvenFaster
. This technique is called
memoization.Observe how fast each
method takes to perform the task. Did you notice anything strange when you ran
the program?
These are three more programs you can write if you feel you need or want additional practice writing Java code. They will not be graded, but feel free to bring them to the staff for comments, suggestions, help if you run into trouble, etc.
Optional #1: This is an exercise intended to get you a little more
Eclipse experience and to brush up on a little bit of Java as well. If you
have difficulties with the Java, you will want to read up on Java variables,
operators, expressions, statements and blocks, and numbers. (One place to read
up is the Java tutorial.)
Open FinancialCalc.java
with your browser and look at the (undocumented) code, which calculates the
value of investing an initial sum of money at a specified interest rate and for
a specified number of years. Using Eclipse, create a new runnable class
MyFinancialCalc
that calculates:
You should consider 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
(If you were doing real financial calculations, the floating-point
arithmetic used by the double
and float
types is in
practice a poor choice for financial calculations because it is not exact.
What would be a good alternative?)
StringScrambler
that takes a String
as input and reverses the order of
all words found in the String
. So for example:
Assume
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:
split()
and the classes
and Arrays
.
Feel free to define as many helper methods as you need. For extra optional
practice, implement your own version of the split()
method. (One
TA was asked to do this last part at an internship interview.)
Optional #3: Using the following class that supports
Point
objects, 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 method toString
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.
class Point { double x; double y;
// Create a point from coordinates Point(double xVal, double yVal) { x = xVal; y = yVal; }
All turn-in for this assignment is through the Catalyst webq (your Java code will be copied-and-pasted there). It is set to let you backtrack, edit, etc. so you can work on it in bits and pieces, as needed.