CPSC-4355 ArrayList Assignment Demonstrate Defining Load ✓ Solved
CPSC-4355 ArrayList Assignment Demonstrate defining, load
CPSC-4355 ArrayList Assignment requires the demonstration of defining, loading, printing, and storing a Multidimensional ArrayList. Multidimensional ArrayLists are defined to contain elements that are themselves ArrayLists, similar to how multidimensional arrays work.
The first task is to define a multidimensional ArrayList named seats to hold the status (available or taken) of seats in a theater. This ArrayList will have three dimensions:
- The seating levels (main floor, 1st balcony, 2nd balcony)
- The rows on a given level
- The seats in a given row
The seats should be defined as an ArrayList of levels, where each level is an ArrayList of rows, and each row is an ArrayList of seats. Given that the status can only be true (seat is taken) or false (seat is available), the data type will be Boolean.
The second requirement is to initialize the ArrayList such that all seats are taken except for the first seat in each row. Thus, true represents a taken seat, and false represents an available seat.
The third task includes displaying the ArrayList by seating level on the screen.
Finally, the ArrayList needs to be stored by seating level to a file using PrintStream.
It is worth noting that the new command syntax permits the right side to get the data type from the left side of the statement without repeating it. For instance:
ArrayList variableName = new ArrayList();
As the ArrayList does not have predetermined sizes, the dimensions for the seating layout must be stored externally. Thus, we can use the provided two-dimensional array to hold the rows and seats for each seating level of the theater:
int [][] seatsLayout = {{4,5}, // rows and seats on the main floor
{3,4}, // rows and seats on the 1st balcony
{1,3}}; // rows and seats on the 2nd balcony
The output to the screen and the file should appear as follows:
[[false, true, true, true, true],
[false, true, true, true, true],
[false, true, true, true, true],
[false, true, true, true, true]]
[[false, true, true, true],
[false, true, true, true],
[false, true, true, true]]
[[false, true, true]]
Paper For Above Instructions
The objective of this assignment is to develop a multidimensional ArrayList in Java that reflects the seating arrangement of a theater. This arrangement will demonstrate each of the essential techniques outlined—defining, initializing, printing, and storing an ArrayList.
Defining the Multidimensional ArrayList
To start, we will define the multidimensional ArrayList named seats. First, we need to import necessary packages and create the ArrayList structure. We can do this by creating an ArrayList of ArrayLists:
import java.util.ArrayList;
import java.io.PrintStream;
import java.io.FileNotFoundException;
public class TheatreSeating {
public static void main(String[] args) {
ArrayList>> seats = new ArrayList();
Next, based on the provided array seatsLayout, we will populate our seats ArrayList:
int[][] seatsLayout = {{4, 5}, {3, 4}, {1, 3}};
for (int level = 0; level
ArrayList> levelList = new ArrayList();
for (int row = 0; row
ArrayList rowList = new ArrayList();
for (int seat = 0; seat
rowList.add(true); // Initially, all seats are taken
}
levelList.add(rowList);
}
seats.add(levelList);
}
Initializing Seats Status
Now that we have defined our ArrayList structure, we can initialize all seats to be 'taken' (true) except for the first seat in each row, which will be 'available' (false):
for (int level = 0; level
for (int row = 0; row
seats.get(level).get(row).set(0, false); // Mark first seat as available
}
}
Displaying the ArrayList
Next, we'll create a method to display the seating arrangements on the console:
public static void displaySeats(ArrayList>> seats) {
for (ArrayList> level : seats) {
System.out.println(level);
}
}
Calling this method will print the seating levels as required.
Storing the ArrayList to a File
The final step entails storing the seating arrangement in a file:
public static void storeSeats(ArrayList>> seats) {
try (PrintStream ps = new PrintStream("seats.txt")) {
for (ArrayList> level : seats) {
ps.println(level);
}
} catch (FileNotFoundException e) {
System.out.println("Error: " + e.getMessage());
}
}
Conclusion
This implementation effectively meets the requirements of the CPSC-4355 ArrayList Assignment by demonstrating how to define, load, print, and store a multidimensional ArrayList in Java. The illustrated approach provides a robust framework for similar applications where a structured data display is required.
References
- Deitel, P. J., & Deitel, H. M. (2018). Java: How to Program. Pearson.
- Goodrich, M. T., & Tamassia, R. (2014). Data Structures and Algorithms in Java. Wiley.
- Bloch, J. (2018). Effective Java. Addison-Wesley.
- Java Documentation. (n.d.). Retrieved from https://docs.oracle.com/en/java/
- Oracle. (n.d.). Java Tutorials. Retrieved from https://docs.oracle.com/javase/tutorial/
- Schildt, H. (2018). Java: The Complete Reference. McGraw-Hill.
- Shaw, Z. A. (2016). Learn Java the Hard Way. Addison-Wesley.
- Alonso, L. (2019). Programming in Java. Springer.
- Flanagan, D. (2020). Java in a Nutshell. O'Reilly Media.
- Gaddis, T. S. (2018). Starting Out with Java: From Control Structures through Objects. Pearson.