Sie sind auf Seite 1von 4

ISE 208 Homework 2: Practice with Arrays and Classes

Due: Tuesday, March 15, at 11:59 PM

Submission Information

When you are finished with the assignment, upload your completed Java source code
(the .java file(s), not the compiled .class file(s)) via the submission link on Blackboard.
Be sure to put your name in a comment at the top of your source code.

Instructions

For this assignment, you will write Java code to perform two separate tasks. The first
task uses arrays to identify prime numbers. The second task asks you to define a
simple class.

For simplicity, we will put everything into the same driver. A source code program
skeleton is provided at the end of this assignment; fill in the remaining code as directed
in the steps below. You may test your code directly using BlueJ or Eclipse, or you can
use the test driver that will be provided on the “Assignments” Web page.

Both problems are described on the pages that follow.


Part 1: Linear Equations

A 2 x 2 system of linear equations can be solved via Cramer's rule, as shown below:

ax + by = e ed − bf af − ec
x= y=
cx + dy = f ad − bc ad − bc
Implement a class named LinearEquation that contains everything in the list below.
Include a test program that prompts the user to enter values for a, b, c, d, e, and f and
displays the result (x and y). If ad - bc is 0, report that "The equation has no solution."

a. Private double instance variables for a, b, c, d, e, and f


b. A constructor that takes arguments for a, b, c, d, e, and f
c. Six get() methods for a, b, c, d, e, and f
d. A boolean method named isSolvable(). This method returns true if ad - bc
is not 0.
e. Methods getX() and getY() that return the solution for the equation.

NOTE: Your solution should use double variables to represent a, b, c, d, e, f, x,


and y!

Part 2: The Sieve of Eratosthenes

A prime number is an integer that is greater than 1 that is evenly divisible only by itself
and 1. For example, 2, 3, 5, and 11 are prime numbers, but 4, 9, and 22 are not.

The Sieve of Eratosthenes is an ancient and extremely simple way to identify all of the
prime numbers less than a given value n. The sieve works as follows:

1. Start by listing all of the integers from 2 through n.


2. Begin with 2 and cross out (eliminate, or mark) all integers that are greater than 2 that
are multiples of 2 (for example, eliminate 4, 6, 8, 10, etc.).
3. Find the next unmarked integer and remove all of its multiples from the list (except for
the number itself). For example, the first unmarked integer after 2 is 3, so we remove
or mark all of the unmarked multiples of 3 that are greater than 3 (6, 9, 12, etc.).
4. Repeat this multiple-marking process for each next unmarked number in turn, until we
reach the end of the list.
5. At the end of this process, all of the remaining unmarked numbers are prime; the
marked numbers are composite (non-prime) values.

For example, here is an example using the sieve algorithm to find all prime integers less
than 30 (marked numbers are replaced by an X):
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30

2 3 X 5 X 7 X 9 X 11 X 13 X 15 X 17 X 19 X 21 X 23 X 25 X 27 X 29 X (mark 2s)

2 3 X 5 X 7 X X X 11 X 13 X X X 17 X 19 X X X 23 X 25 X X X 29 X (mark 3s)

2 3 X 5 X 7 X X X 11 X 13 X X X 17 X 19 X X X 23 X X X X X 29 X (mark 5s)

2 3 X 5 X 7 X X X 11 X 13 X X X 17 X 19 X X X 23 X X X X X 29 X (mark 7s)

2 3 X 5 X 7 X X X 11 X 13 X X X 17 X 19 X X X 23 X X X X X 29 X (mark 11s)

2 3 X 5 X 7 X X X 11 X 13 X X X 17 X 19 X X X 23 X X X X X 29 X (mark 13s)

etc.

(Note that, as a shortcut, you can stop marking once you have passed n/2)

Complete the sieve() method. This method takes an integer value n as its argument,
where 2 ≤ n ≤ 500), and returns an ArrayList containing all of the prime numbers
between 2 and n, inclusive, in ascending order.
Sample Program Skeleton

// Name:
// SBU ID #:
// E-mail address:
// ISE 208 Homework 2

import java.util.*; // for ArrayList and other classes

public class Homework2


{
// You may add any helper methods that you wish to your
// implementation.

public static void main (String [] args)


{
// Fill in code here to call the method and class that
// you wrote for parts 1 and 2
}

public static ArrayList<Integer> sieve (int n)


{
ArrayList<Integer> primes = new ArrayList<Integer> ();

// Fill in your solution to part 2 here

return primes;
}

class LinearEquation
{
// Fill in your code for Part 1 here