Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I\'m trying to create a CelestialDatabase class that will hold star objects in a

ID: 3804282 • Letter: I

Question

I'm trying to create a CelestialDatabase class that will hold star objects in a list (either ArrayList or LinkedList). In the constructor method, we check if the type is 'array' or 'linked' to initialize the underlying list based on the value of the variable typed into the constructor. My question is after that, how will we go about writing the other methods for the database class? For example the add(Star S) method. Because the methods will work differently if the constructor is decided by the user to be an ArrayList or LinkedList. In addition, ArrayList and LinkedList are already written files with numerous methods, some similar to the methods desired in CelestialDatabase.

Note, I'm a beginner to Java so please answer correspondingly (nothing fancy please :) )

private List

Explanation / Answer

I just avoided some extra functionality so you can understand better.
Check out **usedArrayList** variable in **CelestialDatabase** class.

// Driver File
// CelestialDatabaseDriver.java
// ====

public class CelestialDatabaseDriver {
    public static void main( String[] args ) {
        String[] arr = {"Star1", "Star2"};
        CelestialDatabase cdb1 = new CelestialDatabase(arr);
        cdb1.add("star3");
        System.out.println(cdb1);      

        CelestialDatabase cdb2 = new CelestialDatabase("InVisible star");
        cdb2.add("SuperVisible Star");
        System.out.println(cdb2);

    }
}

// Class Object file
// CelestialDatabase.java
// ====

import java.util.ArrayList;
import java.util.LinkedList;

public class CelestialDatabase {
   private ArrayList<String> stars0;
   private LinkedList<String> stars1;  
// You'd like to have one of these to know
// what actually happened.
// ** IMP **
   private boolean usedArrayList;
  
   // I'd like to use an array list in case of a list
   // at start.
   public CelestialDatabase(String[] arrOfStars) {
        stars0 = new ArrayList<String>();
        for ( int i = 0; i < arrOfStars.length; ++i )
            stars0.add(arrOfStars[i]);
        usedArrayList = true;
   }
  
   // And I'd like to use a linked list in case of a
   // string, why? no reason
   public CelestialDatabase(String star) {
        stars1 = new LinkedList<String>();
        stars1.add(star);
        usedArrayList = false;
   }
  
   public void add(String newStar) {
       // ** IMP **
        if (usedArrayList) {
            System.out.println("Program has used an **ArrayList**!");
            stars0.add(newStar);
      }
      else {
            System.out.println("Program has used a **LinkedList**!");
            stars1.add(newStar);
      }
   }
  
   public String toString() {
        String out = "";
        out += "Stars are: ";
        if (usedArrayList) {
            for ( String star : stars0 )
                out += star + " ";              
        }
        else {
            for ( String star : stars1 )
                out += star + " ";
        }
        return out;
   }
}