package questions; import java.io.BufferedReader; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStreamReader; public class Folder { private File[] files; /** * DO NOT...

Java Programming assignments. Three files to be completed: File, Folder and client. The test for file and folder is also uploaded.


package questions; import java.io.BufferedReader; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStreamReader; public class Folder { private File[] files; /** * DO NOT MODIFY * @param files */ public File[] getFiles(){ return files; } public File getFile(int i) { return files[i]; } /** * DO NOT MODIFY * @param files */ public void setFile(int i, File f) { files[i]=f; } /** * DO NOT MODIFY * Loads collection from input file * @param input: name of input file * @throws IOException */ public Folder(String input) throws IOException { FileInputStream inputStream1 = new FileInputStream(input); BufferedReader bufferedReader1 = new BufferedReader(new InputStreamReader(inputStream1)); int count = 0; while (bufferedReader1.readLine() != null) { count++; } bufferedReader1.close(); FileInputStream inputStream2 = new FileInputStream(input); BufferedReader bufferedReader2 = new BufferedReader(new InputStreamReader(inputStream2)); files = new File[count]; String line = null; for (int i = 0; i < count;="" i++)="" {="" line="bufferedReader2.readLine();" string="" tokens[]="line.split(",");" string="" base="tokens[0];" string="" ext="tokens[1];" int="" size="Integer.parseInt(tokens[2]);" int="" perm="Integer.parseInt(tokens[3]);" files[i]="new" file(base,="" ext,="" size,="" perm);="" }="" bufferedreader2.close();="" }="" **="" *="" do="" not="" modify="" *="" @param="" files="" */="" public="" folder(file[]="" files)="" {="" this.files="files;" }="" **="" *="" do="" not="" modify="" *="" @return="" string="" representation="" of="" the="" collection="" */="" public="" string="" tostring()="" {="" string="" result="" ;="" for(int="" i="0;" i="">< files.length;="" i++)="" {="" result="result" +="" files[i].tostring()+"\n";="" }="" return="" result+"\n";="" }="" **="" *="" do="" not="" modify="" *="" @return="" the="" total="" size="" of="" the="" collection.="" *="" for="" example,="" if="" there="" are="" 3="" files="" in="" the="" collection,="" *="" with="" sizes="" 20,="" 8000="" and="" 700="" bytes,="" the="" size="" *="" of="" the="" collection="" is="" 8720="" bytes="" */="" public="" int="" collectionsize()="" {="" int="" result="0;" for(file="" file:="" files)="" result+="file.getSize();" return="" result;="" }="" **="" *="" *="" @param="" extension="" *="" @return="" number="" of="" files="" with="" the="" given="" extension="" *="" (case="" insensitive)="" *="" note:="" don't="" compare="" strings="" using="=" *="" google="" "string="" case="" insensitive="" comparison="" java"="" */="" public="" int="" getcountbyextension(string="" extension)="" {="" to="" be="" completed="" return="" -10;="" }="" **="" *="" *="" @param="" min="" *="" @param="" max="" *="" @return="" number="" of="" files="" in="" the="" given="" size="" range="" *="" (inclusive="" on="" both="" sides)="" */="" public="" int="" countfilesinsizerange(int="" min,="" int="" max)="" {="" to="" be="" completed="" return="" -10;="" }="" **="" *="" *="" this="" method="" should="" wipe="" all="" files="" within="" the="" folder="" (set="" their="" size="" to="" 0)="" *="" */="" public="" void="" wipeall()="" {="" to="" be="" completed="" }="" **="" *="" *="" this="" method="" should="" set="" the="" permissions="" of="" every="" file="" object="" in="" the="" files="" array="" *="" whose="" base="" contains="" the="" parameter="" variable="" term="" to="" the="" parameter="" variable="" p.="" *="" *="" @param="" term="" -="" the="" term="" to="" search="" for="" within="" the="" files="" base="" name.="" *="" @param="" p="" -="" the="" permission="" to="" set="" on="" a="" file="" whose="" base="" name="" includes="" term.="" *="" @return="" the="" number="" of="" files="" whose="" permissions="" were="" changed.="" *="" return="" 0="" if="" the="" files="" array="" is="" empty="" or="" null.="" *="" *="" hint:="" the="" method="" should="" only="" return="" the="" number="" of="" files="" whose="" permission="" was="" changed.="" *="" */="" public="" int="" changepermissions(string="" term,int="" p)="" {="" to="" be="" completed="" return="" -10;="" }="" **="" *="" *="" @param="" target="" *="" @return="" the="" first="" index="" at="" which="" the="" parameter="" file="" object="" (target)="" exists="" in="" the="" files="" array,="" *="" -1="" if="" file="" doesn't="" exist="" in="" the="" folder="" *="" note:="" use="" the="" equals="" method="" defined="" in="" file="" class="" for="" comparison,="" not="=" */="" public="" int="" indexof(file="" target)="" {="" to="" be="" completed="" return="" -10;="" }="" **="" *="" *="" @param="" target="" *="" @return="" the="" last="" index="" at="" which="" the="" parameter="" file="" object="" (target)="" exists="" in="" the="" files="" array,="" *="" -1="" if="" file="" doesn't="" exist="" in="" the="" folder="" *="" note:="" use="" the="" equals="" method="" defined="" in="" file="" class="" for="" comparison,="" not="=" */="" public="" int="" lastindexof(file="" target)="" {="" to="" be="" completed="" return="" -10;="" }="" **="" *="" *="" @return="" the="" name="" of="" the="" biggest="" file="" */="" public="" string="" getbiggestfilesname()="" {="" to="" be="" completed="" return="" "";="" }="" **="" *="" this="" method="" should="" add="" a="" prefix="" to="" the="" base="" name="" of="" every="" file="" in="" the="" files="" array="" *="" which="" has="" write="" permissions.="" *="" https://www.tutorialspoint.com/unix/unix-file-permission.htm="" *="" *="" @param="" prefix="" -="" the="" prefix="" to="" add="" to="" each="" file="" with="" write="" permissions="" *="" @return="" the="" number="" of="" files="" in="" which="" a="" prefix="" was="" added="" *="" */="" public="" int="" addprefix(string="" prefix)="" {="" to="" be="" completed="" return="" -10;="" }="" **="" *="" @return="" an="" array="" containing="" all="" file="" objects="" with="" execute="" permissions="" *="" https://www.tutorialspoint.com/unix/unix-file-permission.htm="" */="" public="" file[]="" getexecutables()="" {="" to="" be="" completed="" return="" null;="" }="" **="" *="" *="" @param="" file="" *="" @return="" the="" number="" of="" times="" the="" given="" file="" exists="" *="" in="" the="" collection.="" *="" note:="" use="" the="" equals="" method="" defined="" in="" file="" class="" for="" comparison,="" not="=" */="" public="" int="" count(file="" file)="" {="" to="" be="" completed="" return="" -10;="" }="" **="" *="" hd="" component="" 1="" *="" *="" this="" method="" should="" return="" a="" deep="" copy="" of="" the="" folder="" object="" *="" */="" public="" folder="" copy()="" {="" to="" be="" completed="" return="" null;="" }="" **="" *="" hd="" component="" 2="" *="" *="" @param="" other="" *="" *="" this="" method="" should="" add="" all="" files="" contained="" within="" the="" parameter="" folder="" other="" *="" to="" the="" files="" array.="" *="" */="" public="" void="" paste(folder="" other)="" {="" to="" be="" completed="" }="" **="" *="" hd="" component="" 3="" *="" @param="" other="" *="" @return="" true="" if="" the="" calling="" object="" contains="" *="" all="" files="" in="" the="" parameter="" object,="" false="" otherwise="" */="" public="" boolean="" contains(folder="" other)="" {="" to="" be="" completed="" return="" true;="" }="" **="" *="" hd="" component="" 4="" *="" @param="" other="" *="" @return="" true="" if="" the="" calling="" object="" and="" parameter="" object="" *="" represent="" the="" same="" collection="" (it's="" ok="" if="" they="" are="" not="" in="" the="" same="" order)="" */="" public="" boolean="" identical(folder="" other)="" {="" to="" be="" completed="" return="" true;="" }="" }="" package="" tests;="" import="" static="" org.junit.jupiter.api.assertions.*;="" import="" java.io.ioexception;="" import="" org.junit.jupiter.api.afterall;="" import="" org.junit.jupiter.api.test;="" import="" questions.file;="" class="" filetest="" {="" private="" static="" int="" score="0;" @test="" void="" testsetbase()="" {="" file="" f="new" file();="" f.setbase(null);="" assertequals("default",="" f.getbase(),="" "base="" should="" be="" \"default\"="" if="" null="" string="" passed");="" f.setbase("log");="" assertequals("log",="" f.getbase());="" f.setbase("");="" assertequals("default",="" f.getbase(),="" "base="" should="" be="" \"default\"="" if="" empty="" string="" passed");="" score+="3;" }="" @test="" void="" testsetextension()="" {="" file="" f="new" file();="" f.setextension(null);="" assertequals("txt",="" f.getextension(),="" "extension="" should="" be="" \"txt\"="" if="" null="" string="" passed");="" f.setextension("java");="" assertequals("java",="" f.getextension());="" f.setextension("");="" assertequals("txt",="" f.getextension(),="" "extension="" should="" be="" \"txt\"="" if="" empty="" string="" passed");="" score+="3;" }="" @test="" void="" testsetsize()="" {="" file="" f="new" file();="" f.setsize(-1);="" assertequals(0,="" f.getsize(),="" "size="" cannot="" be="" negative");="" f.setsize(1);="" assertequals(1,="" f.getsize());="" f.setsize(0);="" assertequals(0,="" f.getsize(),="" "size="" cannot="" be="" negative");="" f.setsize(2147483647);="" assertequals(2147483647,="" f.getsize());="" score+="3;" }="" @test="" void="" testsetpermissions()="" {="" file="" f="new" file();="" f.setpermissions(-1);="" assertequals(0,="" f.getpermissions(),="" "permissions="" cannot="" be="" negative");="" f.setpermissions(8);="" assertequals(7,="" f.getpermissions(),="" "permissions="" cannot="" be="" more="" than="" 7");="" for(int="" p="0;"><8; p++) { f.setpermissions(p); assertequals(p, f.getpermissions()); } score+=3; } @test void testfilestringstringintint() { file f = new file("log", "java", 4500, 5); assertequals("log", f.getbase()); assertequals("java", f.getextension()); assertequals(4500, f.getsize()); assertequals(5, f.getpermissions()); f = new file(null, null, -10, 20); assertequals("default", f.getbase(), "constructor should call setters"); assertequals("txt", f.getextension(), "constructor should call setters"); assertequals(0, f.getsize(), "constructor should call setters"); assertequals(7, f.getpermissions(), "constructor should call setters"); score+=3; } @test void testwipe() { file f = new file("log", "java", 4500, 5); assertequals("log",f.getbase()); assertequals("java",f.getextension()); assertequals(4500,f.getsize()); assertequals(5,f.getpermissions()); f.wipe(); assertnull(f.getbase()); assertnull(f.getextension()); assertequals(0,f.getsize()); assertequals(0,f.getpermissions()); score+=3; } @test void testcompareto() { file a = new file(); a.setsize(2000); file b = new file(); b.setsize(2001); file c = new file(); c.setsize(2000); assertequals(-1, a.compareto(b)); assertequals(1, b.compareto(c)); assertequals(0, a.compareto(c)); score+=4; } @test void testequals() { file a = new file("log","txt",10, 5); file b = new file("log","txt",10, 5); asserttrue(a.equals(b)); file c = new file("log","txt",10, 5); asserttrue(a.equals(c)); //case doesn't matter file d = new file("log","txt",11, 5); assertfalse(a.equals(d)); //different size file e = new file("log","txt",10, 4); assertfalse(a.equals(e)); //different permissions file f = new file("data","txt",10, 5); assertfalse(a.equals(f)); //different base name file g = new file("log","java",10, 5); assertfalse(a.equals(g)); //different extension score+=4; } @test void testclone() { file f = new file("log","txt",1200, 3); file c = f.clone(); assertnotnull(c); // clone must match original assertequals("log",c.getbase()); assertequals("txt",c.getextension()); assertequals(1200,c.getsize()); assertequals(3,c.getpermissions()); // changing clone must not effect f (clone should be a deep copy) c.setextension("php"); assertequals("txt",f.getextension()); assertequals("php",c.getextension()); score+=4; } @test void testtostring() { file f = new file("log", "java", 4500, 5); assertequals("r-x log.java 4 kb", f.tostring()); f = new file("data", "txt", 10000000, 2); assertequals("-w- data.txt 9 mb", f.tostring()); f = new file("test", "md", 2147483647, 0); assertequals("--- test.md 1 gb", f.tostring()); f = new file("hello", "java", 1023, 7); assertequals("rwx hello.java 1023 b", f.tostring()); score+=4; } @afterall public static void wrapup() throws ioexception { system.out.println("score = "+score+" (out of 35)"); } } package tests; import static org.junit.jupiter.api.assertions.*; import java.io.filewriter; import java.io.ioexception; import java.text.simpledateformat; import java.util.date; import org.junit.afterclass; import org.junit.jupiter.api.afterall; import org.junit.jupiter.api.beforeall; import org.junit.jupiter.api.beforeeach; import org.junit.jupiter.api.test; import questions.file; import questions.folder; class foldertest { private folder c1, c2; private static int score = 0; @beforeeach void initialize() throws ioexception { c1 = new folder("input1.txt"); c2 = new folder("input2.txt"); } @test void testgetcountbyextension() throws ioexception { assertequals(4, c1.getcountbyextension("py")); assertequals(4, c1.getcountbyextension("py")); //case insensitive assertequals(0, c1.getcountbyextension("dll")); assertequals(1, c1.getcountbyextension("docx")); score+=3; } @test void testcountfilesinsizerange() { assertequals(4, c1.countfilesinsizerange(1000,200000)); assertequals(0, c1.countfilesinsizerange(0,19)); assertequals(1, c1.countfilesinsizerange(20,20)); score+=4; } @test void testwipeall() { // check theres at least one file assertnotnull(c1.getfile(0).getbase()); c1.wipeall(); for(file f : c1.getfiles()) { assertnull(f.getbase()); assertnull(f.getextension()); assertequals(0,f.getsize()); assertequals(0,f.getpermissions()); } score+=4; } @test void testchangepermission() throws ioexception{ assertequals(3,c1.changepermissions("in",1)); assertequals(1,c1.getfile(3).getpermissions()); assertequals(1,c1.getfile(5).getpermissions()); assertequals(1,c1.getfile(10).getpermissions()); // set them back to what they were c1 = new folder("input1.txt"); assertequals(2,c1.changepermissions("in",4)); assertequals(4,c1.getfile(3).getpermissions()); p++)="" {="" f.setpermissions(p);="" assertequals(p,="" f.getpermissions());="" }="" score+="3;" }="" @test="" void="" testfilestringstringintint()="" {="" file="" f="new" file("log",="" "java",="" 4500,="" 5);="" assertequals("log",="" f.getbase());="" assertequals("java",="" f.getextension());="" assertequals(4500,="" f.getsize());="" assertequals(5,="" f.getpermissions());="" f="new" file(null,="" null,="" -10,="" 20);="" assertequals("default",="" f.getbase(),="" "constructor="" should="" call="" setters");="" assertequals("txt",="" f.getextension(),="" "constructor="" should="" call="" setters");="" assertequals(0,="" f.getsize(),="" "constructor="" should="" call="" setters");="" assertequals(7,="" f.getpermissions(),="" "constructor="" should="" call="" setters");="" score+="3;" }="" @test="" void="" testwipe()="" {="" file="" f="new" file("log",="" "java",="" 4500,="" 5);="" assertequals("log",f.getbase());="" assertequals("java",f.getextension());="" assertequals(4500,f.getsize());="" assertequals(5,f.getpermissions());="" f.wipe();="" assertnull(f.getbase());="" assertnull(f.getextension());="" assertequals(0,f.getsize());="" assertequals(0,f.getpermissions());="" score+="3;" }="" @test="" void="" testcompareto()="" {="" file="" a="new" file();="" a.setsize(2000);="" file="" b="new" file();="" b.setsize(2001);="" file="" c="new" file();="" c.setsize(2000);="" assertequals(-1,="" a.compareto(b));="" assertequals(1,="" b.compareto(c));="" assertequals(0,="" a.compareto(c));="" score+="4;" }="" @test="" void="" testequals()="" {="" file="" a="new" file("log","txt",10,="" 5);="" file="" b="new" file("log","txt",10,="" 5);="" asserttrue(a.equals(b));="" file="" c="new" file("log","txt",10,="" 5);="" asserttrue(a.equals(c));="" case="" doesn't="" matter="" file="" d="new" file("log","txt",11,="" 5);="" assertfalse(a.equals(d));="" different="" size="" file="" e="new" file("log","txt",10,="" 4);="" assertfalse(a.equals(e));="" different="" permissions="" file="" f="new" file("data","txt",10,="" 5);="" assertfalse(a.equals(f));="" different="" base="" name="" file="" g="new" file("log","java",10,="" 5);="" assertfalse(a.equals(g));="" different="" extension="" score+="4;" }="" @test="" void="" testclone()="" {="" file="" f="new" file("log","txt",1200,="" 3);="" file="" c="f.clone();" assertnotnull(c);="" clone="" must="" match="" original="" assertequals("log",c.getbase());="" assertequals("txt",c.getextension());="" assertequals(1200,c.getsize());="" assertequals(3,c.getpermissions());="" changing="" clone="" must="" not="" effect="" f="" (clone="" should="" be="" a="" deep="" copy)="" c.setextension("php");="" assertequals("txt",f.getextension());="" assertequals("php",c.getextension());="" score+="4;" }="" @test="" void="" testtostring()="" {="" file="" f="new" file("log",="" "java",="" 4500,="" 5);="" assertequals("r-x="" log.java="" 4="" kb",="" f.tostring());="" f="new" file("data",="" "txt",="" 10000000,="" 2);="" assertequals("-w-="" data.txt="" 9="" mb",="" f.tostring());="" f="new" file("test",="" "md",="" 2147483647,="" 0);="" assertequals("---="" test.md="" 1="" gb",="" f.tostring());="" f="new" file("hello",="" "java",="" 1023,="" 7);="" assertequals("rwx="" hello.java="" 1023="" b",="" f.tostring());="" score+="4;" }="" @afterall="" public="" static="" void="" wrapup()="" throws="" ioexception="" {="" system.out.println("score="+score+" (out="" of="" 35)");="" }="" }="" package="" tests;="" import="" static="" org.junit.jupiter.api.assertions.*;="" import="" java.io.filewriter;="" import="" java.io.ioexception;="" import="" java.text.simpledateformat;="" import="" java.util.date;="" import="" org.junit.afterclass;="" import="" org.junit.jupiter.api.afterall;="" import="" org.junit.jupiter.api.beforeall;="" import="" org.junit.jupiter.api.beforeeach;="" import="" org.junit.jupiter.api.test;="" import="" questions.file;="" import="" questions.folder;="" class="" foldertest="" {="" private="" folder="" c1,="" c2;="" private="" static="" int="" score="0;" @beforeeach="" void="" initialize()="" throws="" ioexception="" {="" c1="new" folder("input1.txt");="" c2="new" folder("input2.txt");="" }="" @test="" void="" testgetcountbyextension()="" throws="" ioexception="" {="" assertequals(4,="" c1.getcountbyextension("py"));="" assertequals(4,="" c1.getcountbyextension("py"));="" case="" insensitive="" assertequals(0,="" c1.getcountbyextension("dll"));="" assertequals(1,="" c1.getcountbyextension("docx"));="" score+="3;" }="" @test="" void="" testcountfilesinsizerange()="" {="" assertequals(4,="" c1.countfilesinsizerange(1000,200000));="" assertequals(0,="" c1.countfilesinsizerange(0,19));="" assertequals(1,="" c1.countfilesinsizerange(20,20));="" score+="4;" }="" @test="" void="" testwipeall()="" {="" check="" theres="" at="" least="" one="" file="" assertnotnull(c1.getfile(0).getbase());="" c1.wipeall();="" for(file="" f="" :="" c1.getfiles())="" {="" assertnull(f.getbase());="" assertnull(f.getextension());="" assertequals(0,f.getsize());="" assertequals(0,f.getpermissions());="" }="" score+="4;" }="" @test="" void="" testchangepermission()="" throws="" ioexception{="" assertequals(3,c1.changepermissions("in",1));="" assertequals(1,c1.getfile(3).getpermissions());="" assertequals(1,c1.getfile(5).getpermissions());="" assertequals(1,c1.getfile(10).getpermissions());="" set="" them="" back="" to="" what="" they="" were="" c1="new" folder("input1.txt");="" assertequals(2,c1.changepermissions("in",4));="">
Oct 25, 2021
SOLUTION.PDF

Get Answer To This Question

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here