Remember not to create packages. Use the default package
16A
Write a class, TextCondensor
, whose constructor takes an ArrayList<String>
as a parameter.
Provide methods:
public Set<String> condenseText
() which removes any duplicates and returns an Set<String> containing words without duplicates and in alphabetical order. Think Setpublic int condensedSize()
which gets the number of unique elements in the text. Do this efficiently without duplicating code already writtenpublic void setList(ArrayList<String> newText)
which sets a new list of textHere are the files you will need:
humpty.txt: Humpty Dumpty sat on a wall, Humpty Dumpty had a great fall. All the king's horses and all the king's men Couldn't put Humpty together again. mary.txt: Mary had a little lamb, little lamb, little lamb, Mary had a little lamb whose fleece was white as snow. And everywhere that Mary went Mary went, Mary went, everywhere that Mary went The lamb was sure to go. He followed her to school one day, school one day, school one day, He followed her to school one day, Which was against the rules, It made the children laugh and play, laugh and play, laugh and play, It made the children laugh and play, To see a lamb at school. And so the teacher turned it out, turned it out, turned it out, And so the teacher turned it out, But still it lingered near, He waited patiently about, ly about, ly about, He waited patiently about, Till Mary did appear. "Why does the lamb love Mary so?" love Mary so?" love Mary so?" "Why does the lamb love Mary so?" The eager children cried. "Why, Mary loves the lamb, you know," lamb, you know," lamb, you know," "Why, Mary loves the lamb, you know," The teacher did reply
16B
The sieve of Earthosthenes is an algorithm for finding all the prime numbers less that or equal to a given integer. With a piece of paper the process would be:
Write down all the numbers less than or equal to n. Cross out all the multiples of 2 (except 2) Cross out all the multiples of 3 ... up to the square root of n.
This URL http://www.javawithus.com/programs/sieve-of-eratosthenes explains the algorithm. There are lots of websites(including this one) that implement this algorithm using arrays. Don't just copy this code. You are to use a set in your implementation.
You are to write a class Sieve
.
It has a constructor that takes as a parameter the upper limit for the primes
public Sieve(int upperLimit)
It has methods
public Set<Integer> getPrimes()
which returns a set containing all the primes less than or equal to the upper limit. The numbers should be in increasing order. Implement the algorithm using a set.public void setUpperLimit(int newLimit)
which sets a new upper limit for this sievepublic int primeCount()
which returns the number of primes that are less than the upper limit. Do this efficiently without duplicating code already written. (That is, this method should call the getPrimes
method rather than finding the primes itself)