Question
Help with JAVA please: Look at the code for the two classes given, TabSeparatedFile, and CommaSeparatedFile and refactor the code to try to maximize re-use
Help with JAVA please:
- Look at the code for the two classes given, TabSeparatedFile, and CommaSeparatedFile and refactor the code to try to maximize re-use of common data elements and methods.
- Add new unit tests as needed to utilize new function names, etc.
- Create a new super class and extend that in the existing classes.
- Try to make the two files that were given as small as possible by forcing everything possible up into the super class
public class CommaSeparatedFile { private String fileName; private String separator; private int fileSize;
public CommaSeparatedFile (String file, int size) { this.separator = ","; this.fileName = file; this.fileSize = size; }
public String getFilename () { return this.fileName; }
public int getFileSize () { return this.fileSize; }
public String printComma () { return this.separator; } }
import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.*;
class FileTest { @Test public void Test_CSV () { CommaSeparatedFile csv = new CommaSeparatedFile("foo.txt", 1234); assertEquals("foo.txt", csv.getFilename()); assertEquals(1234, csv.getFileSize()); assertEquals(",", csv.printComma()); }
@Test public void Test_TSV () { TabSeparatedFile tsv = new TabSeparatedFile("bar.baz", 9876543); assertEquals("bar.baz", tsv.getFilename()); assertEquals(9876543, tsv.getFileSize()); assertEquals("\t", tsv.printTab()); } }
public class TabSeparatedFile { private String fileName; private String separator; private int fileSize;
public TabSeparatedFile (String file, int size) { this.separator = "\t"; this.fileName = file; this.fileSize = size; }
public String getFilename () { return this.fileName; }
public int getFileSize () { return this.fileSize; }
public String printTab () { return this.separator; } }
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