1. Homepage
  2. Programming
  3. CS 0445 Algorithms and Data Structures I Project 1: ReallyLongInt

CS 0445 Algorithms and Data Structures I Project 1: ReallyLongInt

Engage in a Conversation
PITTCS 0445Algorithms and Data Structures IReallyLongIntArrayDSJava

Algorithms and Data Structures I CourseNana.COM

Project 1: ReallyLongInt[1] CourseNana.COM

https://xkcd.com/2319 CourseNana.COM

Background

Note: All starter files will be shared in a link at the end of this document CourseNana.COM

  CourseNana.COM

In this project, we will use what we’ve reviewed about object-oriented programming in Java in combination with the ADT bag from lecture to create and utilize a 2D array based data structure. This project is broken into two tasks: CourseNana.COM

  CourseNana.COM

-       Task 1A: Design and implement a generic class (ArrayDS<T>) that will act as a data structure for accessing sequences of Java Objects. Your ArrayDS<T> class will primarily implement 2 interfaces – SequenceInterface<T> and ReorderInterface. The details of these interfaces are explained in the files SequenceInterface.java and ReorderInterface.java. Read these files over very carefully before implementing your ArrayDS<T> class. CourseNana.COM

-       Task 1B: Utilize your ArrayDS<T> class to store and manipulate arbitrary length integers. We can think of an integer as a sequence of decimal digits. For example, the number 1234 could be stored as the digit '1' followed by the digit '2' followed by the digit '3' followed by the digit '4'. We will store these digits in an ArrayDS object. Clearly, to perform operations on a number that is stored in this fashion, we must access the digits one at a time in some systematic way. More specific details follow below. CourseNana.COM

  CourseNana.COM

Task 1A: ArrayDS

For the details on the functionality of your ArrayDS<T> class, carefully read over the files SequenceInterface.java, ReorderInterface.java and Project1A.java. You must use these files as specified and cannot remove/alter any of the code that is already written in them. There are different ways of implementing the SequenceInterface<T> and ReorderInterface interface methods, some of which are more efficient than others. Try to think of the best way of implementing these methods in this assignment, but the most important thing at this point is getting them to work correctly. A lot of pencil and paper work is recommended before actually starting to write your code. Later we will discuss the relative merits of different implementations. Your ArrayDS<T> class header should be: CourseNana.COM

  CourseNana.COM

     public class ArrayDS<T> implements SequenceInterface<T>, ReorderInterface { CourseNana.COM

  CourseNana.COM

Important Note: The primary data within your ArrayDS<T> class must be a two-dimensional array. You may not use any predefined Java collection class (e.g., ArrayList) for your ArrayDS<T> data fields. You may not declare and/or use any one-dimensional arrays except in the toArray() , reverse(), and hasSubsequence() methods. Any methods besides these three will only be eligible for 50% credit if they are found to use one-dimensional arrays. This will be applied after the project is due! CourseNana.COM

  CourseNana.COM

You must use the following instance variables inside the ArrayDS<T> class: CourseNana.COM

  CourseNana.COM

private final BagInterface<Integer>[][] array; //the underlying 2-D array CourseNana.COM

private int size; //the number of items in the sequence CourseNana.COM

private T[] alphabet; //the possible item values (e.g., the decimal digits) CourseNana.COM

private T firstItem; //the first item in the sequence CourseNana.COM

private T lastItem; //the last item in the sequence CourseNana.COM

  CourseNana.COM

You should add other variables and named constants to follow the secure programming practices we mentioned in class. CourseNana.COM

  CourseNana.COM

To illustrate how to store a sequence of objects as a two-dimensional array, let's have an example. CourseNana.COM

  CourseNana.COM

Let's take the sequence 9875732732 as an example. This is a sequence of decimal digits. The sequence alphabet is the set of possible values from which the sequence items are drawn. So, in this example the alphabet is the set of decimal digits {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}. This sequence can be visualized like this: CourseNana.COM

  CourseNana.COM

CourseNana.COM

  CourseNana.COM

Each of the ten digits of the alphabet is represented by a circle. Every pair of consecutive digits in the sequence results in an arrow in the diagram, and the arrow's label includes the position of the second digit in the pair. For example, because 9 is followed by 8 in the sequence, there is an arrow from 9 to 8 in the diagram. Because the position of 8 in the sequence is 1, the arrow from 9 to 8 has 1 in its label. Because 3 is followed by 2 twice in the sequence, the arrow from 3 to 2 is labeled by 6 and 9, the positions of 2 in the sequence in both occurrences of 32 in the sequence. CourseNana.COM

  CourseNana.COM

To construct the sequence from the diagram, we follow the arrows starting from the firstItem (9), then 8 then 7. With each step, we keep track of our position in the sequence. At 7, we are at position 2 and we have to decide whether to go to 3 or to 5. We go to 5 because of the label {3} of the arrow from 7 to 5, indicating that 5 is at the next position (3). We keep following the arrows until we reach the last item. The circles that we will go through represent the sequence. CourseNana.COM

  CourseNana.COM

Here are some properties of the sequence in the example above. Please match these with the methods in SequenceInterface.java: CourseNana.COM

  CourseNana.COM

  CourseNana.COM

size() == 10 CourseNana.COM

first() == 9 CourseNana.COM

last() == 2 CourseNana.COM

predecessor(8, 7) == true CourseNana.COM

predecessor(9, 2) == false CourseNana.COM

getFrequencyOf(3) == 2 CourseNana.COM

itemAt(3) == 5 CourseNana.COM

firstOccurenceOf(7) == 2 CourseNana.COM

indexInAlphabet(3) == 3 CourseNana.COM

nextIndex(7, 2) == 5 CourseNana.COM

nextIndex(7, 7) == 3 CourseNana.COM

prevIndex(2, 6) == 3 CourseNana.COM

prevIndex(2, 9) == 3 CourseNana.COM

hasSubsequence(732) == true CourseNana.COM

hasSubsequence( 983 ) == false CourseNana.COM

  CourseNana.COM

The diagram above (which, by the way, is called a graph) is represented as an NxN two-dimensional array, where N is the size of the alphabet, which is 10 in the example. The array representation of the diagram above is: CourseNana.COM

CourseNana.COM

Each cell at row i and column j in the array contains the label of the arrow going from item i in the alphabet to item j in the alphabet. If there is no arrow between i and j, the cell is empty (null). Each cell is a BagInterface of Integers. CourseNana.COM

  CourseNana.COM

The sequence after append(7) will look like this: CourseNana.COM

CourseNana.COM

Its array representation is: CourseNana.COM

CourseNana.COM

After prefix(0): CourseNana.COM

CourseNana.COM

Besides the methods of SequenceInterface<T> and ReorderInterface, you will also need to write the following constructors: CourseNana.COM

  CourseNana.COM

public ArrayDS(T[] alphabet) CourseNana.COM

public ArrayDS(ArrayDS<T> other) CourseNana.COM

  CourseNana.COM

The first constructor initializes the underlying array to a 2D array of size alphabet.length x alphabet.length. The entry type is ResizableArrayBag<Integer>. The BagInterface and the ResizableArrayBag classes are located in BagInterface.java and ResizableArrayBag.java, respectively. The second constructor (called a “copy constructor”) initializes the ArrayDS object as a copy of the argument (inserting all of the items inside the arrays in the argument object to the corresponding arrays in the new object). CourseNana.COM

  CourseNana.COM

Finally, you will need to override the following method: CourseNana.COM

  CourseNana.COM

public String toString(); CourseNana.COM

  CourseNana.COM

This method will return a String that is the concatenation of all of the items in the Sequence implemented by the ArrayDS object appended together without spaces. For example, if an ArrayDS object contains the numbers 1, 2, 3, 4, 5, 6, toString() should output 123456. CourseNana.COM

  CourseNana.COM

To check your implementation of ArrayDS<T>, the Project1A.java file should compile, run, and give output identical to P1AOut.txt. Please note that this statement doesn't suggest that you delay testing until you are done with all the methods of ArrayDS. Instead, you should use stubs (comment out what you haven’t implemented yet) and incrementally test your code using Project1A.java as you complete each of the methods. CourseNana.COM

Task 1B: ReallyLongInt

Now that we have a working ArrayDS we are going to put it to use by adding a new subclass: ReallyLongInt. ReallyLongInt represents a non-negative integer with an arbitrary size that has no leading zeros. Arbitrarily long integers are useful in many applications, like cryptography. CourseNana.COM

  CourseNana.COM

Inheritance: ReallyLongInt must be a subclass of ArrayDS. However, since ArrayDS is generic while ReallyLongInt isn’t, you should use the following header: CourseNana.COM

  CourseNana.COM

public class ReallyLongInt extends ArrayDS<Integer> implements Comparable<ReallyLongInt> { CourseNana.COM

  CourseNana.COM

Note that rather than T, the underlying element data type in ArrayDS is now `Integer`. This means that the individual digits of your ReallyLongInt will be Integer objects. ArrayDS was designed using generics, but for this particular subclass we will only use it for Integer objects. CourseNana.COM

  CourseNana.COM

Data: The data for this class is inherited and you may not add any additional instance variables. You will certainly need local (method) variables for the various operations but the only instance variables that you need are those inherited from ArrayDS. Note that you cannot access the ArrayDS instance variables directly from inside ReallyLongInt code. You may only use the public methods of ArrayDS, for example: you can’t access size directly, but you can call size() to retrieve the number of items currently in the sequence. CourseNana.COM

  CourseNana.COM

Operations: Your ReallyLongInt class must implement the methods shown below. Note that the compareTo() method is necessary to implement the Comparable interface. CourseNana.COM

  CourseNana.COM

private ReallyLongInt(int size) CourseNana.COM

  CourseNana.COM

This constructor will create a "zeroed out" ReallyLongInt with the given size. Note that this constructor leaves the number in an inconsistent state (having leading zeros), so it should only be used within the class itself as a utility (for example, you might need it in your add() and subtract() methods). For this reason, it is declared as private. CourseNana.COM

  CourseNana.COM

public ReallyLongInt(String s) CourseNana.COM

  CourseNana.COM

The string s consists of a valid sequence of digits with no leading zeros (except for the number 0 itself – a special case). Insert the digits as Integer objects, such that the most significant digit is at the beginning (head) of the sequence. CourseNana.COM

  CourseNana.COM

public ReallyLongInt(ReallyLongInt other) CourseNana.COM

  CourseNana.COM

This just requires a call to super. However, it is dependent upon a correct implementation of the copy constructor for the ArrayDS class that you implemented in task 1A. CourseNana.COM

  CourseNana.COM

To help you out with the assignment, the above methods are already implemented in ReallyLongInt.java! CourseNana.COM

  CourseNana.COM

These next methods will be implemented by you as part of the project: CourseNana.COM

  CourseNana.COM

public ReallyLongInt add(ReallyLongInt rightOp) CourseNana.COM

  CourseNana.COM

Return a NEW ReallyLongInt that is the sum of the current ReallyLongInt and the parameter ReallyLongInt, without altering the original values. For example: CourseNana.COM

  CourseNana.COM

ReallyLongInt X = new ReallyLongInt("123456789"); CourseNana.COM

ReallyLongInt Y = new ReallyLongInt("987654321"); CourseNana.COM

ReallyLongInt Z; CourseNana.COM

Z = X.add(Y); CourseNana.COM

System.out.println(X + " + " + Y + " = " + Z); CourseNana.COM

  CourseNana.COM

should produce the output: 123456789 + 987654321 = 1111111110 CourseNana.COM

Be careful to handle the addition carry correctly and to process the digits in the correct order. Also, be careful to handle numbers with differing numbers of digits! CourseNana.COM

  CourseNana.COM

public ReallyLongInt subtract(ReallyLongInt rightOp) CourseNana.COM

Return a NEW ReallyLongInt that is the difference of the current ReallyLongInt and the parameter rightOp. Since all ReallyLongInts are non-negative, if rightOp is greater than the current ReallyLongInt, you should throw an ArithmeticException. Otherwise, subtract digit by digit (borrowing if necessary) as expected. This method is tricky because it can result in leading zeros, which we don't want. Be careful to handle this case (and consider the methods provided by ArrayDS that will allow you to handle it). For example:

ReallyLongInt X = new ReallyLongInt("123456"); CourseNana.COM

ReallyLongInt Y = new ReallyLongInt("123455"); CourseNana.COM

ReallyLongInt Z; CourseNana.COM

Z = X.subtract(Y); CourseNana.COM

System.out.println(X + " - " + Y + " = " + Z); CourseNana.COM

  CourseNana.COM

should produce the output: 123456 - 123455 = 1 CourseNana.COM

As with the add() method, be careful to handle numbers with differing numbers of digits. Also note that borrowing may extend over several digits. See Project1B.java for some example cases.

  CourseNana.COM

Important Note: Both the add() and subtract() methods are tricky and have different cases to consider. For these methods especially I strongly recommend working out some examples on paper to see what needs to be considered before actually coding them. CourseNana.COM

  CourseNana.COM

public int compareTo (ReallyLongInt rightOp) CourseNana.COM

  CourseNana.COM

Defined the way we expect compareTo to be. Remember that if one number has more digits than the other, then clearly it is bigger (since there are no leading 0s). Otherwise, the numbers must be compared digit by digit. CourseNana.COM

  CourseNana.COM

public boolean equals(Object rightOp) CourseNana.COM

  CourseNana.COM

Defined the way we expect equals to be defined for objects – comparing the data and not the reference. Don't forget to cast rightOp to ReallyLongInt so that its array can be accessed (note: the argument is an Object rather than ReallyLongInt because we are overriding equals() from the version defined in class Object). Note: This method can easily be implemented once compareTo() has been completed. CourseNana.COM

  CourseNana.COM

public ReallyLongInt multTenToThe(int num) CourseNana.COM

  CourseNana.COM

Return the result of multiplying the current ReallyLongInt by 10num (10^num). Note that this can be done very simply through adding 0's. CourseNana.COM

  CourseNana.COM

public ReallyLongInt divTenToThe(int num) CourseNana.COM

  CourseNana.COM

Return the result of dividing the current ReallyLongInt by 10num (10^num). Note that this can be done very simply through shifting. Since 10num and our ReallyLongInt are both integers, make sure to use integer division! CourseNana.COM

  CourseNana.COM

To verify that your ReallyLongInt class works correctly, you will use it with the program Project1B.java. Your output should match that shown in P1BOut.txt. Just like with Task 1A, you should use stubs and incrementally test your code–one or two methods at a time–using Project1B.java as you implement each of the methods. CourseNana.COM

Deliverables

You are responsible for submitting: CourseNana.COM

-       ArrayDS.java CourseNana.COM

-       ReallyLongInt.java CourseNana.COM

  CourseNana.COM

All other files will be automatically supplied to Gradescope. I would suggest avoiding making code changes to any of the other files (besides stubbing out Project1A.java and Project1B.java to test parts of your implementations at a time as you work). CourseNana.COM

  CourseNana.COM

If you use Eclipse or any other Java IDEs to work on this project, remember to test on a command line before submitting. Sometimes editors add package lines to the top of .java files that will break the autograder. CourseNana.COM

Hints

-       See file P1AOut.txt to see how your output for task A should look. As noted, your output when running Project1A.java should be identical to this. CourseNana.COM

-       See file P1BOut.txt to see how your output for task B should look. As noted, your output when running Project1B.java should be identical to this. CourseNana.COM

-       In order for the Project1A.java output to show correctly, you must override the toString() method in the ArrayDS class. CourseNana.COM

-       Think carefully about the order in which you implement ArrayDS methods as some methods can make others considerably easier to implement! CourseNana.COM



[1] Adapted from Dr. Sherif Khattab and Dr. John Ramirez’s coursework CourseNana.COM

Get in Touch with Our Experts

WeChat (微信) WeChat (微信)
Whatsapp WhatsApp
PITT代写,CS 0445代写,Algorithms and Data Structures I代写,ReallyLongInt代写,ArrayDS代写,Java代写,PITT代编,CS 0445代编,Algorithms and Data Structures I代编,ReallyLongInt代编,ArrayDS代编,Java代编,PITT代考,CS 0445代考,Algorithms and Data Structures I代考,ReallyLongInt代考,ArrayDS代考,Java代考,PITThelp,CS 0445help,Algorithms and Data Structures Ihelp,ReallyLongInthelp,ArrayDShelp,Javahelp,PITT作业代写,CS 0445作业代写,Algorithms and Data Structures I作业代写,ReallyLongInt作业代写,ArrayDS作业代写,Java作业代写,PITT编程代写,CS 0445编程代写,Algorithms and Data Structures I编程代写,ReallyLongInt编程代写,ArrayDS编程代写,Java编程代写,PITTprogramming help,CS 0445programming help,Algorithms and Data Structures Iprogramming help,ReallyLongIntprogramming help,ArrayDSprogramming help,Javaprogramming help,PITTassignment help,CS 0445assignment help,Algorithms and Data Structures Iassignment help,ReallyLongIntassignment help,ArrayDSassignment help,Javaassignment help,PITTsolution,CS 0445solution,Algorithms and Data Structures Isolution,ReallyLongIntsolution,ArrayDSsolution,Javasolution,