0% found this document useful (0 votes)
4 views

Lab5 LinkedLists

Uploaded by

eire.euan
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Lab5 LinkedLists

Uploaded by

eire.euan
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

CS4222 Lab Sheet Week 6

The TrivialSpreadsheet class


import java.util.*;

public class UnfinishedTrivialSpreadSheet {


private int[][] spreadSheet;
public UnfinishedTrivialSpreadSheet(int numberOfRows, int
numberOfColumns) {
spreadSheet = new int[numberOfRows][numberOfColumns];
initialise();
}

public void initialise() {


for(int row = 0 ; row < spreadSheet.length; row++) {
for(int col = 0; col < spreadSheet[0].length; col++) {
spreadSheet[row][col] = 0; //
// Some cells are assigned values between 1 and 100
// Randomly decide if this cell gets a number other than 0
if(Math.random() < 0.5) {
// Put a number between 1 and 100 in the cell
spreadSheet[row][col] = (int)(Math.random()*100) + 1;
}
}
}
}

public String createCellRef(int row, int col) {


return createColRef(col) + createRowRef(row);
}

public String createColRef(int col) {


String colID = "";
int colIndex;
while (col > 0) {
colIndex = (col - 1) % 26;
colID = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".charAt(colIndex) +
colID;
col =((col - colIndex) / 26);
}
return colID;
}

public String createRowRef(int row) {


return row+"";
}

public int rowNumberOf(String cellRef) {


return Integer.parseInt(rowID(cellRef));
}

public int colNumberOf(String cellRef) {


String colID = colID(cellRef).toUpperCase();
int refLen = colID.length();
int colNumber = 0;
for(int i = 0; i < refLen; i++) {
char letter = colID.charAt(i);
int letterPos = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".indexOf(letter) + 1;
colNumber = colNumber +
(letterPos * (int)Math.pow(26,refLen-(i+1)));
}
return colNumber;
CS4222 Lab Sheet Week 6

public String colID(String cellRef) {


int i;
for(i = 0; Character.isLetter(cellRef.charAt(i)); i++) {}
return cellRef.substring(0,i);
}

public String rowID(String cellRef) {


int i;
for(i = 0; !Character.isDigit(cellRef.charAt(i)); i++) {}
return cellRef.substring(i);
}

public int valueAt(String cellRef) {


return spreadSheet[rowNumberOf(cellRef)-1][colNumberOf(cellRef)-
1];
}

public void displaySheet() {


for(int row = 0; row < spreadSheet.length; row++) {
for(int col = 0; col < spreadSheet[row].length; col++) {
if(spreadSheet[row][col] > 0) {
System.out.printf("%5d|",spreadSheet[row][col]);
} else {
System.out.printf("%5s|"," ");
}
}
System.out.println();
}
}

public int[][] getSheet() {


return spreadSheet;
}

//Q1
public LinkedList<String> specialCells(int cellType) {
LinkedList<String> X = new LinkedList<String>();
//add something clever
return X;
}

//Q2
public LinkedList<Integer> copy(LinkedList<String> cellRefs) {
LinkedList<Integer> Y = new LinkedList<Integer>();
//add something clever
return Y;
}

//Q3
public void paste(LinkedList<Integer> values, LinkedList<String>
cellRefs) {
//add something clever
}
}
CS4222 Lab Sheet Week 6

//Q1
public LinkedList<String> specialCells(int cellType) {
LinkedList<String> X = new LinkedList<String>();
//add something clever
return X;
}

//Q2
public LinkedList<Integer> copy(LinkedList<String> cellRefs) {
LinkedList<Integer> Y = new LinkedList<Integer>();
//add something clever
return Y;
}

//Q3
public void paste(LinkedList<Integer> values, LinkedList<String> cellRefs)
{
//add something clever
}
}

The Driver program (to test your solutions!)


import java.util.*;

public class CopyOfLinkedListDriver {


public static void main(String[] args) {
int nSpreadSheets = 3;
ArrayList<UnfinishedTrivialSpreadSheet> workbook = new
ArrayList<UnfinishedTrivialSpreadSheet>();
UnfinishedTrivialSpreadSheet s;

// Create 3 small spreadsheets


while(workbook.size() < nSpreadSheets) {
s = new UnfinishedTrivialSpreadSheet(5,7);
workbook.add(s);
}
// Display them on the screen
for(int n=0; n < 3; n++) {
System.out.printf("Sheet %d %n",n);
System.out.println(" A B C D E F G");
workbook.get(n).displaySheet();
System.out.println();System.out.println();
}

//Exercise 1 - Special Cells


System.out.println("Q1:");
String[] types = {"zero","odd","even"};
for(int n=0; n < 3; n++) {
LinkedList<String> refList =
workbook.get(n).specialCells(n);
System.out.print("Sheet "+n+ " contains ");
System.out.print(refList.size());
System.out.print(" " + types[n] + " values");
System.out.println(" in the following cells " + refList);
}
System.out.println();System.out.println();System.out.println();
CS4222 Lab Sheet Week 6

//Exercise 2 - Copy Cells


System.out.println("Q2:");
LinkedList<String> cellRefs = new
LinkedList<String>(Arrays.asList("A3","D2","E4"));
LinkedList<Integer> copyValues = workbook.get(0).copy(cellRefs);
System.out.println("Copy values from Spreadsheet0 @ A3, D2,
E4");
System.out.println("These values are : " +copyValues);
System.out.println();

//Exercise 3 - Paste Cells


System.out.println("Q3:");
LinkedList<String> cellRefs2 = new
LinkedList<String>(Arrays.asList("C1","C2","C3"));
LinkedList<Integer> copyValues2 =
workbook.get(1).copy(cellRefs2);
LinkedList<String> sourceCellRefs = new
LinkedList<String>(Arrays.asList("A3","D2","E4"));
LinkedList<Integer> sourceValues =
workbook.get(0).copy(sourceCellRefs);
LinkedList<String> destinationCellRefs = new
LinkedList<String>(Arrays.asList("C1","C2","C3"));
workbook.get(1).paste(sourceValues,destinationCellRefs);
System.out.println("Copy values from Spreadsheet0 (A3,D2,E4) to
Spreadsheet1 (C1,C2,C3)");
System.out.println("Values in Spreadsheet1 (C1,C2,C3) were "+
copyValues2 +". Now, they are " + copyValues);
System.out.println();System.out.println("Sheet 1 (after copy &
paste operation)");
System.out.println(" A B C D E F G");
workbook.get(1).displaySheet();
}
}

This TrivialSpreadsheet class creates and populates a two-dimensional array of integers.


The 2D array is used as a spreadsheet-type structure and each position in the array is
identified by a “cell reference.”
[NOTE: In the spreadsheet the first row is numbered row 1, not row 0; the first column is
numbered column 1, not column 0.] Consequently, position 0,0 in the 2D array is treated as
position 1,1 in the spreadsheet.
CS4222 Lab Sheet Week 6

1 Add the code for a method with the following header to the TrivialSpreadsheet class

public LinkedList<String> specialCells(int cellType)


The method creates a LinkedList of cell references (i.e. Strings) that contains the cell references of
ALL the cells in the spreadsheet that have the specified “cellType.” The following cell types are
defined:

Cell Type Cell Value Example


specialCells(0) returns a list of cell references for ALL cells that
0 Zero
contain zeroes
specialCells(1) returns a list of cell references for ALL cells that
1 Odd
contain odd values
specialCells(2) returns a list of cell references for ALL cells that
2 Even
contain even values (excluding zero values)

// Select this line


// and this one to unhide a solution

2 Add the code for a method with the following header

public LinkedList<Integer> copy(LinkedList<String> cellRefs)


The method is passed a collection of cell references (e.g. A3,D2,E4 ) and returns a LinkedList of the
values in those cells in the spreadsheet. You can assume that all the cell references are valid (i.e. the
cells exist in the spreadsheet).
// Select this line
// and this one to unhide a solution

3 Add the code for a method with the following header

public void paste(LinkedList<Integer> values, LinkedList<String> cellRefs)


The method is passed a LinkedList of integer values AND a LinkedList of Strings containing cell
references. You can assume that all the cell references are valid (i.e. the cells exist in the
spreadsheet).
If both lists are not the same size then the method should do nothing.
Otherwise, the method should ‘paste’ the first value in the values list into the cell identified by the
first cell reference; the second value in the values list into the by the second cell identified by the
second cell reference; and so on.
// Select this line
// and this one to unhide a solution

4 Replace ALL references to LinkedList with ArrayList in BOTH the TrivialSpreadSheet and Driver classes
and rerun the Driver. You should get exactly the same outputs. What does that tell us about the
ArrayList and LinkedList collections?

// Select this line


// and this one to unhide a solution

You might also like