Question
I was given this code to practice for a future exam. I have no clue how to do any of the tasks. Any help understanding
I was given this code to practice for a future exam. I have no clue how to do any of the tasks. Any help understanding what to do would be greatly appreciated!!! Thanks!!
P.S. These are three different packages. Tests where provided for each. I removed the codes I had done. Where there's need for coding it is noted by the //to be completed comment
package toBeCompleted;
//only method to be completed in DataEntry class is inRange (10 marks)
public class DataEntry {
private int row, column;
private double value;
/**
* assign values to instance variables using setters
*/
public DataEntry(int r, int c, double val) {
setRow(r);
setColumn(c);
value = val;
}
/**
* assign the higher of 0 and r to row
* @param r
*/
public void setRow(int r) {
row = Math.max(0, r);
}
/**
* assign the higher of 0 and c to column
* @param c
*/
public void setColumn(int c) {
column = Math.max(0, c);
}
public void setValue(double val) {
value = val;
}
public int getRow() {
return row;
}
public int getColumn() {
return column;
}
public double getValue() {
return value;
}
//DO NOT MODIFY ANY CODE ABOVE THIS COMMENT
/**
* 10 marks
* @param row1
* @param column1
* @param row2
* @param column2
* @return true if the current item is in the range provided
* i.e., between rows row1 and row 2 (inclusive) and between
* columns column1 and column2 (inclusive), false otherwise
*/
public boolean inRange(int row1, int column1, int row2, int column2) {
//to be completed
}
}
package toBeCompleted;
import java.util.*;
/*
* 15 methods to be completed, each worth 4 marks,
* except for toString, which is worth 18 marks.
* for all methods except toString, you will get
* marks if your code passes the tests we eventually use.
* (our test data will be almost identical to the given
* test data but with just enough difference to catch
* any hard-coding)
* for toString, you will get partial marks based
* on the extent of similarity of your output to output given
* in specifications.
* toString marking guide:
* 8 marks: correct row and column heading
* 5 marks: correct indentation
* 5 marks: correct values
*/
public class Worksheet {
private ArrayList
private String title;
/**
* create a new worksheet with given title
* @param title
*/
public Worksheet(String title) {
data = new ArrayList
this.title = title;
}
/**
* @return a shallow copy of the data
*/
public ArrayList
return data;
}
/**
*
* @return title of the worksheet
*/
public String getTitle() {
return title;
}
/**
*
* @param row
* @param column
* @return value of item at given row and column (if any), null otherwise
*/
public Double get(int row, int column) {
//to be completed
}
/**
*
* @param row
* @param column
* @return index of DataEntry object in list data with given row and column
* return -1 if no such DataEntry object found
*/
public int indexOf(int row, int column) {
return -1;
//to be completed
}
/**
*
* @param row1
* @param column1
* @param row2
* @param column2
* @return number of DataEntry objects in given address range , 0 if none
* ((row1, column1) to (row2, column2))
*/
public int count(int row1, int column1, int row2, int column2) {
return 0; //to be completed
}
/**
*
* @param row1
* @param column1
* @param row2
* @param column2
* @return sum of values of all DataEntry objects in given address range
* ((row1, column1) to (row2, column2))
* return 0 if there are no items in the given range
*/
public double total(int row1, int column1, int row2, int column2) {
return 0; //to be completed
}
/**
*
* @param row1
* @param column1
* @param row2
* @param column2
* @return average of values of all DataEntry objects in given address range
* ((row1, column1) to (row2, column2))
* return 0 if there are no items in the given range
*/
public double average(int row1, int column1, int row2, int column2) {
return 0; //to be completed
}
/**
*
* @return value of the first row for which there is a DataEntry object
* in the list data
* return null if there is no data
*/
public Integer minRow() {
return null; //to be completed
}
/**
*
* @return value of the last row for which there is a DataEntry object
* in the list data
* return null if there is no data
*/
public Integer maxRow() {
return null; //to be completed
}
/**
*
* @return value of the first column for which there is a DataEntry object
* in the list data
* return null if there is no data
*/
public Integer minColumn() {
return null; //to be completed
}
/**
*
* @return value of the last column for which there is a DataEntry object
* in the list data
* return null if there is no data
*/
public Integer maxColumn() {
return null; //to be completed
}
/**
*
* @param row
* @return list of values of data items from given row
* return empty list if no items belong to the given row
*/
public ArrayList
return new ArrayList
}
/**
*
* @param column
* @return list of values of data items from given column
* return empty list if no items belong to the given column
*/
public ArrayList
return new ArrayList
}
/**
* replace all occurrences of key item by replacement item
* @param key
* @param replacement
*/
public void replace(double key, double replacement) {
//to be completed
}
/**
*
* @param key
* @return a list of DataEntry objects that have
* key as their value attribute
*/
public ArrayList
return new ArrayList
}
/**
* return String output of the object in format required
* in specifications
*
* NOTE there is no test for this method. output should resemble the sample output provided
* in the specifications
*/
public String toString() {
return ""; //to be completed
}
}
package toBeCompleted;
import java.util.*;
//only method to be completed in Workbook class is copyPaste (HD, 16 marks)
public class Workbook {
private String title;
private ArrayList
public Workbook(String title) {
this.title = title;
sheets = new ArrayList
}
public void add(Worksheet sheet) {
//if(sheets.contains(sheet) == false)
sheets.add(sheet);
}
public Worksheet get(int index) {
if(index >= 0 && index < sheets.size())
return sheets.get(index);
else
return null;
}
public String toString() {
String result = title+" ";
if(sheets.size()==0)
return title+": empty";
for(Worksheet sheet: sheets)
result = result + sheet.toString()+" ";
return result;
}
/**
* replace all occurrences of key item by replacement item
* across all worksheets
* @param key
* @param replacement
*/
public void replace(double key, double replacement) {
for(Worksheet sheet: sheets)
sheet.replace(key, replacement);
}
/**
* create a deep copy of worksheet at index idx (if any)
* with the given title
* @param idx
* @param title
*/
public void duplicate(int idx, String title) {
if(idx < 0 || idx >= sheets.size())
return;
Worksheet sheet = new Worksheet(title);
ArrayList
for(DataEntry item: data) {
int r = item.getRow();
int c = item.getColumn();
double val = item.getValue();
sheet.set(r, c, val);
}
sheets.add(sheet);
}
/**
* set value of cell at address (r, c) in sheet
* at index idx (if any) to val
* @param idx
* @param r
* @param c
* @param val
*/
public void set(int idx, int r, int c, double val) {
if(idx < 0 || idx >= sheets.size())
return;
Worksheet sheet = sheets.get(idx);
sheet.set(r, c, val);
}
public Double total(int idx, int row1, int column1, int row2, int column2) {
if(idx < 0 || idx >= sheets.size())
return null;
return sheets.get(idx).total(row1, column1, row2, column2);
}
public Double average(int idx, int row1, int column1, int row2, int column2) {
if(idx < 0 || idx >= sheets.size())
return null;
return sheets.get(idx).average(row1, column1, row2, column2);
}
public int count(int idx, int row1, int column1, int row2, int column2) {
if(idx < 0 || idx >= sheets.size())
return 0;
return sheets.get(idx).count(row1, column1, row2, column2);
}
/**
* ADVANCED (16 marks)
* copy cell range from sheet at sourceIndex (if any) from
* startRowSource, startColumnSource to endRowSource, endColumnSource
* and paste in sheet at destIndex (if any) starting at
* cell address startRowDest, startColumnDest
*
* @param sourceIndex
* @param startRowSource
* @param startColumnSource
* @param endRowSource
* @param endColumnSource
* @param destIndex
* @param startRowDest
* @param startColumnDest
*/
public void copyPaste(int sourceIndex, int startRowSource, int startColumnSource, int endRowSource, int endColumnSource,
int destIndex, int startRowDest, int startColumnDest) {
//to be completed
}
}
Step 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