Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

CPSC 1110 LAB 6 Interfaces (Chapter 10) In this lab we will implement a simple interface type Sequence. This is problem P10.2 from the book.

CPSC 1110 LAB 6

Interfaces (Chapter 10)

In this lab we will implement a simple interface type Sequence. This is problem P10.2 from the book. Your primary task in this lab will be to implement a PrimeSequence class that must implement the Sequence interface. Worked example 10.1 has been uploaded to UTC Learn as a starting point for your code. Note that there are 5 .java files (StarterCode.zip) as well as a pdf document Worked Example 10.1.pdf. You may use BlueJ or Eclipse to complete the lab. PLEASE COMMENT YOUR CODE. You will have points taken off if you do not comment your code. You can see sample comments in my starter code for how you should comment your code. Keep your code neat.

You should end up with one project folder that you will turn into a .zip file and submit to UTC Learn. You should use today's lab time to work on the lab, and ask any questions that you may have about the assignment. I will take your submitted program and run it to make sure that it works to assign you a grade. You will also need to submit a pdf document that has screen-shots of your program's output. The directions below will tell you when to take screen shots. This can be easily done in windows 7 using the Snipping Tool. If you are unsure how to use this feature, please ask for help (or Google how to use the snipping tool). You can use Microsoft Word/Open Office/Google Docs to create your document. I want you to ZIP your entire project folder. See the end of this document for more details.

Some helpful tips:

1)Compile often do it.

2)You are only responsible for creating a single class, . However, your understanding of how to do this will depend on your understanding of the provided starter code.

3)It may be helpful to use the Debugger or print statements to check your work. (always).

4)We will have a demo/tester class for this lab that tests the and classes (provided in the starter code). You will need to modify the tester to additionally test your class. The tester class is named

Tasks: Follow the directions below to complete your lab assignment

For today's lab we will be completing Exercise P10.2 from the book.

P10.2 Write a class PrimeSequence that implements the Sequence interface of Worked Example 10.1, and produces the sequence of prime numbers. (There is a PDF file on UTC Learn that has Worked example 10.1).

To start this lab you will need to take a look at the starter code, as well as the Worked Example 10.1 document that explains the project.

Next, you will need to create a new project, and run the starter code from UTC Learn (StarterCode.zip). (If you use eclipse you can simply make a new project, and copy/paste the starter code files into the src directory of your new project. Then, in eclipse, you will need to highlight the new project, and press F5 to refresh. This should populate your project explorer with the new files you just placed in the src directory).

At this point you should be able to run the starter code, and you should see output similar to the output generated in the Worked Example 10.1 document.

Next you will create a PrimeSequence class that implements the Sequence interface.

To implement your next() method, you will need to come up with a plan to generate prime numbers. Your PrimeSequence class method next() will return 2 on the first call, 3 on the second call, 5 on the third call, etc ... Also recall that a whole number greater than 1 is prime IFF (if and only if) it is divisible by only 1 and itself. So, in order to generate prime numbers, you will simply need to test each value above the previous prime number for divisibility by 2, by 3, by 4, etc

For example, the first prime is 2. Our next prime candidate is 3. We check 3 for divisibility by 2, and see that 3 is not divisible by 2, so it is our next prime number.

Now, to generate our next prime number, we will start with 4, and check divisibility by 2. This test fails, and we move on to 5.

We test 5 for divisibility by 2, then 3, then 4. 5 is not evenly divisible by any of those values, therefore 5 is our next prime number.

You will need to implement this algorithm in a loop. You test divisibility by using the mod operator. A number n is divisible by a number x, if n % x = 0.

Note that this algorithm provided is very inefficient. It is a brute force algorithm. Therefore, you try to generate more than about 100,000 prime numbers it will take your algorithm a LONG time to finish. The demo class uses a value of 1000, which should run rather quickly. (Currently my computer is generating primes using this algorithm, working on generating 1,000,000 primes. It is not done after 5 minutes of computing. By contrast, generating 100,000 primes took maybe 5 seconds).

IMPORTANT!! To make it easier for me to test/grade your program, I want all of your method/class names to be the same. This will allow me to make a single set of test code that I can run on everyone's methods. Follow the name conventions shown in the lab documentation. (For this lab you simply need to create a PrimeSequence class that implements the Sequence interface in order to adhere to my method names).

Given:

/** This class analyzes the distribution of the last digit of values from a sequence. */ public class LastDigitDistribution { private int[] counters; /** Constructs a distribution whose counters are set to zero. */ public LastDigitDistribution() { counters = new int[10]; } /** Processes values from this sequence. @param seq the sequence from which to obtain the values @param valuesToProcess the number of values to process */ public void process(Sequence seq, int valuesToProcess) { for (int i = 1; i <= valuesToProcess; i++) { int value = seq.next(); int lastDigit = value % 10; counters[lastDigit]++; } } /** Displays the counter values of this distribution. */ public void display() { for (int i = 0; i < counters.length; i++) { System.out.println(i + ": " + counters[i]); } } }

public interface Sequence { int next(); }

public class SquareSequence implements Sequence { private int n; public int next() { n++; return n * n; } }

public class RandomSequence implements Sequence { public int next() { return (int) (Integer.MAX_VALUE * Math.random()); } }

public class SequenceDemo { public static void main(String[] args) { LastDigitDistribution dist1 = new LastDigitDistribution(); dist1.process(new SquareSequence(), 1000); dist1.display(); System.out.println(); LastDigitDistribution dist2 = new LastDigitDistribution(); dist2.process(new RandomSequence(), 1000); dist2.display(); } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Administrator Limited Edition

Authors: Martif Way

1st Edition

B0CGG89N8Z

More Books

Students also viewed these Databases questions

Question

3. The group answers the questions.

Answered: 1 week ago

Question

What is meant by decentralisation of authority ?

Answered: 1 week ago

Question

Briefly explain the qualities of an able supervisor

Answered: 1 week ago

Question

Define policy making?

Answered: 1 week ago

Question

Define co-ordination?

Answered: 1 week ago

Question

What are the role of supervisors ?

Answered: 1 week ago

Question

What does Processing of an OLAP Cube accomplish?

Answered: 1 week ago