CO4206/CO7206 Assignment 2: Java Code, Analysis, and FreeCol System

Verified

Added on  2022/08/29

|4
|550
|20
Practical Assignment
AI Summary
This assignment, part of the CO4206/CO7206/CO7506 module, focuses on the dynamic analysis of the FreeCol legacy system, a turn-based strategy game implemented in Java. The task requires students to clone the FreeCol code repository, compile the project, and conduct dynamic analysis. The submission includes a PDF document with theoretical discussions, Java code for analysis, and operational outputs in CSV/PNG format. The provided Java code demonstrates writing data to and reading data from Excel files using Apache POI library. The assignment aims to assess the student's ability to analyze and re-engineer a legacy system, involving both theoretical understanding and practical coding skills. The student needs to submit the completed Assignment2 directory to their repository using git push.
Document Page
Task 1
i. Java code for writing into a file.
import java.io.File;
import java.io.FileOutputStream;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class WriteDataToExcel {
public static void main(String[] args) throws Exception {
//Create blank workbook
XSSFWorkbook workbook = new XSSFWorkbook();
//Create a blank sheet
XSSFSheet spreadsheet = workbook.createSheet( " Class");
//Create row object
XSSFRow row;
tabler-icon-diamond-filled.svg

Paraphrase This Document

Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser
Document Page
//This data needs to be written (Object[])
Map < String, Object[] > ClassInfo= new TreeMap < String, Object[] >();
empinfo.put( "1", new Object[] {
"ClassName", TotalOccurrences });
//Create a blank sheet
XSSFSheet spreadsheet = workbook.createSheet( " Class");
//Create row object
XSSFRow row;
//This data needs to be written (Object[])
Map < String, Object[] > ClassInfo= new TreeMap < String, Object[] >();
empinfo.put( "1", new Object[] {
"ClassName", TotalOccurrences });
//Iterate over data and write to sheet
Set < String > keyid = empinfo.keySet();
int rowid = 0;
for (String key : keyid) {
row = spreadsheet.createRow(rowid++);
Object [] objectArr = empinfo.get(key);
int cellid = 0;
for (Object obj : objectArr){
Document Page
Cell cell = row.createCell(cellid++);
cell.setCellValue((String)obj);
}
}
//Write the workbook in file system
FileOutputStream out = new FileOutputStream(
new File("C:/poiexcel/Writesheet.xlsx"));
workbook.write(out);
out.close();
System.out.println("Writesheet.xlsx written successfully");
}
}
ii. Java code for reading an excel file.
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.FormulaEvaluator;
import org.apache.poi.ss.usermodel.Row;
public class ReadExcelFileDemo
{
public static void main(String args[]) throws IOException
{
//obtaining input bytes from a file
FileInputStream fis=new FileInputStream(new File("C:\\demo\\student.xls"));
//creating workbook instance that refers to .xls file
HSSFWorkbook wb=new HSSFWorkbook(fis);
//creating a Sheet object to retrieve the object
HSSFSheet sheet=wb.getSheetAt(0);
//evaluating cell type
FormulaEvaluator formulaEvaluator=wb.getCreationHelper().createFormulaEvaluator();
for(Row row: sheet) //iteration over row using for each loop
{
Document Page
for(Cell cell: row) //iteration over cell using for each loop
{
switch(formulaEvaluator.evaluateInCell(cell).getCellType())
{
case Cell.CELL_TYPE_NUMERIC: //field that represents numeric cell type
//getting the value of the cell as a number
System.out.print(cell.getNumericCellValue()+ "\t\t");
break;
case Cell.CELL_TYPE_STRING: //field that represents string cell type
//getting the value of the cell as a string
System.out.print(cell.getStringCellValue()+ "\t\t");
break;
}
}
System.out.println();
}
}
}
chevron_up_icon
1 out of 4
circle_padding
hide_on_mobile
zoom_out_icon
[object Object]