Starting from:
$35

$29

Project 3 LinkedList and ArrayList Solution

Instructions




Please read and understand these expectations thoroughly. Failure to follow these instructions could negatively impact your grade. Rules detailed in the course syllabus also apply but will not necessarily be repeated here.




Due: The project is due on Friday, November 8th by 11:55 PM.




Identi cation: Place you and your partner’s x500 in a comment in all les you submit. For example, //Written by shino012 and hoang159.




Submission: Submit a zip or tar archive on Canvas containing all your java les. You are allowed to change or modify your submission, so submit early and often, and verify that all your les are in the submission.




Failure to submit the correct les will result in a score of zero for all missing parts. Late submissions and submissions in an abnormal format (such as .rar or .java) will be penalized. Only submissions made via Canvas are acceptable.




Partners: You may work alone or with one partner. Failure to tell us who is your partner is indistinguishable from cheating and you will both receive a zero. Ensure all code shared with your partner is private.




Code: You must use the exact class and method signatures we ask for. This is because we use a program to evaluate your code. Code that doesn’t compile will receive a signi cant penalty. Code should be compatible with Java 8, which is installed on the CSE Labs computers.




Questions: Questions related to the project can be discussed on Canvas in abstract. This relates to programming in Java, understanding the writeup, and topics covered in lecture and labs. Do not post any code or solutions on the forum. Do not e-mail the TAs your questions when they can be asked on Canvas.




Grading: Grading will be done by the TAs, so please address grading problems to them privately.










IMPORTANT: You are NOT permitted to use ANY built-in libraries, classes, etc.




Double check that you have NO import statements in your code.































1
CSCI 1933 PROJECT 3 CODE STYLE










Code Style




Part of your grade will be decided based on the \code style" demonstrated by your programming. In general, all projects will involve a style component. This should not be intimidating, but it is fundamentally important. The following items represent \good" coding style:




Use e ective comments to document what important variables, functions, and sections of the code are for. In general, the TA should be able to understand your logic through the comments left in the code.




Try to leave comments as you program, rather than adding them all in at the end. Comments should not feel like arbitrary busy work - they should be written assuming the reader is uent in Java, yet has no idea how your program works or why you chose certain solutions.




Use e ective and standard indentation.




Use descriptive names for variables. Use standard Java style for your names: ClassName, functionName, variableName for structures in your code, and ClassName.java for the le names.




Try to avoid the following stylistic problems:




Missing or highly redundant, useless comments. int a = 5; //Set a to be 5 is not help-ful.




Disorganized and messy les. Poor indentation of braces ({ and }).




Incoherent variable names. Names such as m and numberOfIndicesToCount are not use-ful. The former is too short to be descriptive, while the latter is much too descriptive and redundant.




Slow functions. While some algorithms are more e cient than others, functions that are aggressively ine cient could be penalized even if they are otherwise correct. In general, functions ought to terminate in under 5 seconds for any reasonable input.




The programming exercises detailed in the following pages will both be evaluated for code style. This will not be strict { for example, one bad indent or one subjective variable name are hardly a problem. However, if your code seems careless or confusing, or if no signi cant e ort was made to document the code, then points will be deducted.




In further projects we will continue to expect a reasonable attempt at documentation and style as detailed in this section. If you are confused about the style guide, please talk with a TA.


































2
CSCI 1933 PROJECT 3 INTRODUCTION










Introduction




In this project you are going to implement a list [1] interface to construct your own ArrayList and LinkedList data structures. Using these you will construct a Celestial Bodies database to include a list of di erent types of Stars present in our universe. You are required to make the database compatible for multiple type of Stars such as Sequence Stars, White Dwarfs, and Red Giants using the concept of class inheritance [2]. Finally, you are going to nd the biggest and the brightest stars in our own small universe of objects (and also look out for black holes).







[1]. Lists:




A List is a list of ordered items that can also contain duplicates. In Java, lists are constructed either using an array or linked list data structure. The implementations for each have certain pros and cons with respect to cost of space and runtime. In this project, you will implement lists using both array and linked list data structure from a custom List interface.










[2]. Inheritance: Interface and Abstract classes:




Interfaces and abstract classes are an important aspect of inheritance in object oriented programming. None of the methods de ned in an interface are implemented, and therefore must be implemented by an inheriting class; abstract classes can have a combination of implemented and non-implemented (abstract) methods. In Java an interface is inherited by other classes using the keyword implements while an abstract class must be inherited using extends. See the example code below for more details.










List: An interface



A list must consist of speci c methods, regardless of the underlying data structure. These meth-ods are de ned as part of an interface that you are required to implement in your ArrayList and LinkedList implementations. Refer to the provided List.java for the methods and their de - nitions. Note that the methods have generic types , so you are also required to implement your inherited classes using generic types.











































A generic type is a generic class or interface that is parameterized over types. In the context of List, T is the type of the object that is in the list, and note that T extends Comparable.




3
CSCI 1933 PROJECT 3 1. LIST: AN INTERFACE













Inheritance Java Example:




An interface.



interface IName f




public void printName();




g




An abstract.



abstract class Name f




Abstract have variables unlike interface. String rstName;



String secondName;




An abstract method.



abstract void printName();




g




This class implements the Name interface.



class PeopleName implements IName f String rstName;




String secondName;




Need to implement printName().



public void printName() f




System.out.println("%s %s", this. rstName, this.secondName);




g




g




This class extends the Name class.



class PeopleName extends Name f public void printName() f

System.out.println("%s %s", this. rstName, this.secondName);




g




g










1.1 Array List Implementation




The rst part of this project will be to implement an array list. Create a class ArrayList that implements all the methods in the List interface. Recall that to implement the List interface and use the generic compatibility with your code, ArrayList should have following structure:










public class ArrayList<T extends Comparable<T implements List<T {




...




}







The underlying structure of an array list is (obviously) an array, which means you will need to have an instance variable that is an array. Since our implementation is generic, the type of this array will be T[]. Due to Java’s implementation of genericsy, you CANNOT simply create a generic array with:




T[] a = new T[size];







Rather, you have to create a Comparable (since T extends Comparable)z array and cast it to an array of type T.







speci cally because of type erasure



zhad T not extended Comparable, you would say T[] a = (T[])new Object[size];




4
CSCI 1933 PROJECT 3 1. LIST: AN INTERFACE
















T[] a = (T[]) new Comparable[size];







Your ArrayList class should have a single constructor:




public ArrayList() {




...




}







that initializes the underlying array to a length of 2.







Implementation Details




When the underlying array becomes full, both add methods will automatically add more space by creating a new array that is twice the length of the original array, copying over everything from the original array to the new array, and nally setting the instance variable




to the new array.




Hint: You may nd it useful to write a separate private method that does the growing and copying




When calling either remove method, the underlying array should no longer have that spot. For example, if the array was ["hi", "bye", "hello", "okay", ...] and you called remove with index 1, the array would be ["hi", "hello", "okay", ...]. Basically, the only null elements of the array should be after all the data.




Initially and after a call to clear(), the size method should return 0. The \size" refers to the number of elements in the list , NOT the length of the array. After a call to clear(), the underlying array should be reset to a length of 2 as in the constructor.




After you have implemented your ArrayList class, include a main method that tests all func-tionality.




1.2 Linked List Implementation




The second part of this project will be to implement a linked list. Create a class LinkedList that implements all the methods in List. Recall again that to implement the List interface, LinkedList should be structured as follows:




public class LinkedList<T extends Comparable<T implements List<T {




...




}







The underlying structure of a linked list is a node. This means you will have an instance variable that is the rst node of the list. The provided NGen.java contains a generic node class that you










5
CSCI 1933 PROJECT 3 1. LIST: AN INTERFACE










will use for your linked list implementationx.




Your LinkedList class should have a single constructor:




public LinkedList() {




...




}







that initializes the list to an empty list.







Implementation Details




Initially and after a call to clear(), the size should be zero and your list should be empty. In sort(), do not use an array or ArrayList to sort the elements. You are required to sort the values using only the linked list data structure. You can move nodes or swap values

but you cannot use an array to store values while sorting.




Depending on your implementation, remember that after sorting, the former rst node may not be the current rst node.







After you have implemented your LinkedList class, include a main method that tests all func-tionality.



























































































xYou may implement your linked list as a headed list, i.e., the rst node in the list is a ‘dummy’ node and the second node is the rst element of the list, or a non-headed list, i.e., the rst node is the rst element of the list. Depending on how you choose to implement your list, there will be some small nuances.




6
CSCI 1933 PROJECT 3 2. A CELESTIAL DATABASE










A Celestial Database



You will now use your array list and linked list implementations to construct a celestial database.




For this project, this database will include a list of stars from our universe.




Stars are one of the most common form of celestial bodies present in our universe. They come in various forms and often characterized by their mass, size and lifespan. For this project, we will focus only on mass and size. Among the many types or phases of stars that exist, we will include only three types, namely Sequence Stars, Red Giants, and White Dwarfs. Sun, the closest star and the center of our solar system is one of the Sequence stars, characterized by average mass and size. The Red Giants, reddish or orange in color, are generally 100 times larger than size of Sequence Stars and often seen as the starting of dying phase of star. White Dwarfs are the remnant of an average-sized star that passed through the red giant stage of its life.




You will use the ArrayList or LinkedList data structure to construct this Celestial database of Stars. You are provided with Star.java (Refer to the Star.java le for details) which is an abstract class with the three properties name, mass, and size, setters and getters, a compareTo function, and two abstract methods:




public abstract boolean isBlackHole(); | Returns true if the star is a black hole; returns false otherwise.




public abstract String toString(); | Returns the description of each star as SequenceS-tar, RedGiant or WhiteDwarf with their respective mass and sizes.




You are required to extend the base Star class to create a new class for each star type (for a total of three classes): Sequence, RedGiant, and WhiteDwarf. Each of the three classes must have a constructor (Hint: You cannot instantiate an abstract class from its constructor. See the use of super().) and override the abstract methods isBlackHole() and the respective toString() method as follows:




Sequence




public Sequence(String name, double mass, double size)




public boolean isSun(): Return true if mass == 2 ( 1030 KG) and size == 430 ( 1000 miles), otherwise false.




public String toString(): Print description for the star in the following format: "< Name : A Sequence Star with mass = < XXX:Y Y KG; Size = < XXX:Y Y miles".




public boolean isBlackHole(): Return true if the mass of the star is greater than 1000 units ( 1030 KGs) and size less than 50 ( 1000 miles), otherwise false.




RedGiant




public RedGiant(String name, double mass, double size)







7
CSCI 1933 PROJECT 3 2. A CELESTIAL DATABASE










public boolean isSuperGiant(): Return true if mass 100 and size 100, otherwise false.




public boolean isBlackHole(): Return true if star is a Super RedGiant, otherwise false.




public String toString(): Print description for the star in following format: "< Name : A RedGiant (replace with SuperGiant if above condition is met) with mass = < XXX:Y Y KG and size = < XXX:Y Y miles.




WhiteDwarf




public WhiteDwarf(String name, double mass, double size) public boolean isBlackHole(): Return false (always).




public String toString(): Print description for the star in following format: "< Name : A WhiteDwarf with mass = < XXX:Y Y KG and size = < XXX:Y Y miles.




2.1 The Database




Now that you have your 3 types of stars, create a class CelestialDatabase. To create this database you will use your ArrayList and LinkedList classes as the underlying object list. The type for the object in the list will be Star. Your CelestialDatabase should include the following items:




private List<Star list { Your underlying list of Stars (instance variable).




public CelestialDatabase(String type) { The constructor for your Celestial Database. The constructor should initialize the underlying list based on what the value of type is. If type.equals("array"), your underlying list should be an ArrayList. If type.equals(" linked"), your underlying list should be a LinkedList. You may assume no other strings will be used with this constructor.




Hint: Both List<Star l = new ArrayList<Star(); and List<Star l = new LinkedList<Star(); are valid in Java since ArrayList and LinkedList both implement List.




public boolean add(Star s) { This will add s to the end of the list and return true if successful, false otherwise.




public Star find(String name) { This will try to nd a star with a name eld that con-tains name. Note that it is not necessary for the name of the Star and name to be exactly




the same. You can use the built in String method public boolean contains(String anotherString){. Return null if no Star was found.




public Star findSun() { This will try to nd a Sequence Star that matches the character-istics of Sun (see isSun()). Return null if no match was found.




public Star remove(int index) { This will remove the star object currently at index index. If index is out of bounds, return null.







{The actual signature of contains is public boolean contains(CharSequence s) but you don’t have to worry about that




8
CSCI 1933 PROJECT 3 3. ANALYSIS










public Star get(int index) { This will return the star object currently at index index. If index is out of bounds, return null.




public void sort() { This will sort the list in order of mass based on compareTo.




public Star[] getStarsByType(String type) { This will return an array of Star objects that have the type type. You can assume that type will only take on the values "sequence", "redgiant", or "whitedwarf".




Hint: instanceof




public Star[] listBlackHoles() { This will return an array of stars that are black holes. If there are no stars that meet the criteria for a black hole, return null.




public Star[] getTopKHeaviestStar(int k) { This will return an array of length k con-taining the top-k stars when sorted by their mass (among all types). If there are no objects in the list, return null. If the number of stars in the list (M) is less than k, return an array of length M.




public int[] countStars() { This will return an array of three integers where the rst index is the count of Sequence stars (Sequence object) in the list, the second is the count of RedGiant stars, and the third is the count of WhiteDwarf stars in the database. For example, if you have a list of two Sequence Stars, one RedGiant, and zero WhiteDwarfs, then the output should be: [2, 1, 0].




After you have implemented your CelestialDatabase class, include a main method that tests all functionality. For the methods where the return type is a Star object or Star[] array use the corresponding toString() method to print the details of the Stars.







Analysis



Now that you have implemented and used both an array list and linked list, which one is better?




Which methods in List are more e cient for each implementation?




For each of the 13 methods in List, compare the runtime (Big-O) for each method and implemen-tation. Include an analysis.txt or analysis.pdf with your submission structured as follows:










Method
ArrayList Runtime
LinkedList Runtime
Explanation
boolean add(T element)
O(...)
O(...)
...
boolean add(int index, T element)
O(...)
O(...)
...
.
.
.
.
.
.
.
.
.
.
.
.









Your explanation for each method only needs to be a couple sentences brie y justifying your runtimes.










9

More products