18.10.2014 Views

COSC 1P03 Assignment 3

COSC 1P03 Assignment 3

COSC 1P03 Assignment 3

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

<strong>COSC</strong> <strong>1P03</strong> <strong>Assignment</strong> 3<br />

Due: Mar. 6, 2013 @ 12:00 pm noon (late date Mar. 9)<br />

In preparation for this assignment, create a folder called Assign3 with three subfolders: Numbers,<br />

Test and RunBigInt to contain, respectively, your Numbers library package, a project to test your<br />

implementation and the project to run for submission. The files for this assignment can be downloaded<br />

from URL: http://www.cosc.brocku.ca/Offerings/<strong>1P03</strong>/A3Files.zip. Unzip A3Files.zip<br />

and move BigInt.java into the Numbers folder and RunBigInt.java into the RunBigInt<br />

folder. In DrJava, create a project for the BigInt ADT in the Numbers folder and projects for testing<br />

and submission in the Test and RunBigInt folders, respectively.<br />

Abstract Data Type BigInt<br />

Sometimes, for example in Number Theory, it is necessary to work with very large integers—often 100s<br />

of digits long. In order to provide such a facility, a new library package called Numbers is to be<br />

developed with a new type called BigInt defined by the following interface declaration (found as file<br />

BigInt.java in A3Files.zip):<br />

package Numbers;<br />

public interface BigInt {<br />

public int getNumDigits ( );<br />

public int getDigit ( int i );<br />

public boolean equals ( BigInt val );<br />

public int compareTo ( BigInt val );<br />

public BigInt add ( BigInt val );<br />

public BigInt sub ( BigInt val );<br />

public BigInt mul ( BigInt val );<br />

public BigInt div ( BigInt val );<br />

} // BigInt<br />

Negative BigInt values are not defined and there might be a limit to the number of digits supported.<br />

Negative values and values with too many digits would cause an InvalidBigIntException to be<br />

thrown.<br />

One possible representation of BigInt is as an array of digits. The digits can be stored, from low order<br />

(ones—rightmost digit) to high order as the elements of an array of int. A fixed array size (say 100<br />

digits) can be used and the number of digits can be stored in a second variable (i.e. this as an<br />

implementation using “variable-sized” arrays). For example, the number 123456 would be stored as the<br />

array {6,5,4,3,2,1} with a digit count of 6.<br />

As part of the Numbers package, write the implementation class ArrayBigInt, to implement the<br />

BigInt interface using the representation above. Your implementation class should include a default<br />

constructor creating the BigInt value 0 and a constructor to create a BigInt value from a long<br />

value. It should also include an implementation of toString which returns the BigInt as a sequence<br />

of digits from high order to low order position.<br />

Suggestions<br />

Addition<br />

As in manual arithmetic, addition is performed by adding corresponding digits starting at the ones (10 0 )<br />

position, the sum (it if is no more than 9) being the corresponding digit in the result. If the sum is greater


than 9, the ones digit of the sum is used for the corresponding digit in the result and a carry (of 1) is<br />

carried to the next digit position. Thus, at any digit position, the sum is of the corresponding digits plus<br />

the carry from the previous position (which is either 0 or 1). If there is a carry in the leftmost position,<br />

the result has an additional digit, being the carry.<br />

Subtraction<br />

Subtraction works much like addition. Starting at the ones position, the corresponding digits are<br />

subtracted. If the difference is non-negative, this is the digit of the result. If the difference is negative, a<br />

borrow (of 1) is borrowed from the next position and the digit of the result is the difference increased by<br />

10. Again, at any position, the difference is the difference in the corresponding digits minus the borrow<br />

from the previous position (which is either 0 or 1). Since negative BigInt values are not supported, the<br />

second value should not be greater than the first and thus there cannot be a borrow in the leftmost digit<br />

position.<br />

Comparison<br />

Equality occurs if each digit in the corresponding digit positions are equal. Values with differing<br />

numbers of digits are unequal with the longer being the greater. With equal number of digits, the digits<br />

can be compared from the highest order position until unequal digits are encountered at which point the<br />

value with the smaller digit is the smaller.<br />

Multiplication and Division<br />

Multiplication and division can be implemented (not very efficiently) as repeated addition or subtraction,<br />

respectively.<br />

Testing<br />

To test your implementation, you must first create a .jar file from the Numbers package<br />

(Project/Create Jar File from Project) into the Numbers folder. Create a project in the<br />

Test folder with a class for testing ArrayBigInt. Import Numbers into this class, define variables<br />

using the BigInt type and create BigInt objects using ArrayBigInt constructors. Results of<br />

testing should be written to an ASCIIDisplayer. In your test project, add the .jar file in<br />

Numbers as an Extra Classpath (Project/Properties). Compile and run your test class to<br />

test your implementation. Marks will be awarded for quality of testing.<br />

Submission:<br />

Details regarding preparation and submission of assignments in <strong>COSC</strong> <strong>1P03</strong> are found on the <strong>COSC</strong><br />

<strong>1P03</strong> website at URL: http://www.cosc.brocku.ca/Offerings/<strong>1P03</strong>/AssignGuide.pdf. This<br />

document includes a discussion of assignment preparation, programming standards, evaluation criteria<br />

and academic conduct (including styles for citation) in addition to the detailed assignment submission<br />

process copied below. Part of the marks for the assignment will be awarded for programming<br />

standards.<br />

To prepare and submit the assignment electronically from the lab, follow the procedure below:<br />

1. Ensure your folder (say Assign3) for the assignment is stored on your Z: drive.<br />

2. Print (to PDFCreator) the .java file of your ArrayBigInt class for the assignment<br />

using the name ArrayBigInt.pdf and save the .pdf file at the top level of the project<br />

folder (i.e. directly within Assign3).


3. Print (to PDFCreator) the .java file of your test class (see Testing above) for the<br />

assignment using the name Test.pdf and save the .pdf file at the top level of the project<br />

folder (i.e. directly within Assign3).<br />

4. Run your test program and Save the image of the ASCIIDisplayer<br />

(File/Save Image of Window…) as TestOut.pdf at the top level (i.e. in the<br />

Assign3 folder).<br />

5. In the RunBigInt project add the .jar file in Numbers as an Extra Classpath<br />

(Project/Properties).<br />

6. Compile and Run the RunBigInt project. Save the image of the ASCIIDisplayer<br />

(File/Save Image of Window…) as Output.pdf at the top level (i.e. in the<br />

Assign3 folder).<br />

7. Run PuTTY by selecting PuTTY under All Programs in the Start menu.<br />

8. Double-click sandcastle in the Load, save … entry.<br />

9. Enter your Brock userid and press the Enter key.<br />

10. Once you have the sandcastle% prompt, navigate to your project directory for your<br />

assignment (say Assign3).<br />

Here are a few useful commands (press Enter after typing the command):<br />

ls –l<br />

cd <br />

- list files in current directory<br />

- changes to the specified subdirectory (note, do not<br />

include the )<br />

e.g. cd Assign3<br />

cd ..<br />

- go up 1 directory level<br />

Note: If your file or folder names include spaces or special characters, you have to enclose<br />

the name in quotes, e.g. cd ″<strong>COSC</strong> <strong>1P03</strong>″.<br />

11. Once you have confirmed you are in the correct project directory, type the command<br />

submit1p03 and follow the instructions. It is important to note that the script will copy<br />

everything from the current directory and its subfolders to the <strong>1P03</strong> electronic drop box. It is<br />

important you are in the correct directory when you run the script. The script will confirm<br />

what you have submitted.<br />

12. Log off sandcastle by typing logout.<br />

For help in submitting an assignment from home see the <strong>COSC</strong> Help Center at URL:<br />

http://www.cosc.brocku.ca/help/esubmit.<br />

DrJava<br />

The folder from which you do the electronic submission should contain the project folder, including all<br />

files (including those supplied) relevant to the project—the .java and .class files for the assignment<br />

and .pdf files for program listings and output.<br />

Other platforms<br />

If you are using an IDE other than DrJava to prepare your assignment at home, you must copy your code<br />

into DrJava to create new project(s) and then compile and run and prepare the submission as above. Your<br />

electronic submission must only include DrJava project folders and the .pdf files as described.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!