Answered step by step
Verified Expert Solution
Question
1 Approved Answer
How do I complete these tasks in java? Lab 6. Sparse Array In computer science, a sparse array or sparse matrix is one where most
How do I complete these tasks in java?
Lab 6. Sparse Array In computer science, a sparse array or sparse matrix is one where most of the elements are zero. In contrast, if most of the elements are non-zero, then the array is said to be dense. Sparse arrays happen all the time in machine learning and other situations. Matrices that get very large use up a lot of memory, but if the matrix is sparse, we don't have to store all the zeros. We can be smarter about how we store a matrix to save on space. In this lab, we're going to build an object-oriented Matrix framework that explores 1-2 different ways of storing sparse arrays. Task 1. Superclass (2/10 pts) Write an abstract superclass in Matrix.java that has these methods - public Matrix(int rows, int columns) -- Initialize a matrix of dimension rows x columns with all entries as zero. - public Matrix(double data[][]) -- Initializes a matrix with the provided two dimensional array of data. - public double get(int row, int col) -- Retrieve the entry at the given row and column - public void set(int row, int col, double value) -- Set the entry at the given row and column to the given value Task 2. List of Lists (6/10 pts) Write a subclass of Matrix called LOLMatrix. In this version, instead of storing values in a 2D-array, it will store values in a list of lists. Each node in the "outer" list contains a row index and another list. Each node in the "inner" list contains the column index and the value. 979899 \begin{tabular}{|c|c|c|} \hline 0 & 0 & 0 \\ \hline 0 & 0 & 0 \\ \hline 0 & 29 & 0 \\ \hline 0 & 0 & 0 \\ \hline 0 & 0 & 0 \\ \hline 0 & 33 & 0 \\ \hline \end{tabular} Consider the above two diagrams. The top represents a LOLMatrix, and the bottom represents the equivalent 2D-array Matrix. Take note of the sheer volume of memory saved by not recording 0's. Also take note that both representations have similar ways of representing rows, but different ways of representing columns. You have to implement your own List/Node class, and override and implement the get() and set() methods in the subclass. set() must maintain numeric order of nodes. Using the above example, set (2,7,50) must create a new node on row 2 , and between the nodes [col:6] and [ col:8]. This implementation can be very efficient for incremental access. Write a subclass of Matrix called DOKMatrix that stores the non-zero entries using java. util. HashMap. The key can be the row and column, and the value for the non-zero entry. You have to override and implement the get() and set() methods in the subclass. Hashmaps are interesting data structures similar to real dictionaries. If you're familiar with Python dictionaries, they are implemented with a hashmap. While arrays store data using an index/value pair, Hashmaps store data in key/value pairs. A key can be anything, from an int to a String to an Object. Likewise for a value. For example, you could represent a real dictionary using a hashmap, where the key is a String word, and the value is a String definition. This will require you to research the java documentation for HashMap. This is good practice for an important skill in CS - documentation diving and research. However, this task is optional. You may receive 3 extra credit points on this lab, meaning a possible score of 13/10. This approach is good for incrementally constructing a sparse array in random order, but inefficient for iterating over the array incrementally. Task 4. Write Main (2/10 pts) Write a main program that tests LOLMatrix and optionally DOKMatrix. Write code that will rigorously test that you can successfully get and set values into your matricesStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started