From 3b992386ae8d56bc707bb579d35ba45b12c20e15 Mon Sep 17 00:00:00 2001 From: Sami Frogell Date: Sun, 30 Sep 2018 21:39:54 +0300 Subject: [PATCH] Assignment 1 finished (partially completed, /wrists) --- bin/StringCalculator.class | Bin 405 -> 2945 bytes bin/StringCalculatorTest.class | Bin 505 -> 1346 bytes src/StringCalculator.java | 110 ++++++++++++++++++++++++++++++-- tests/StringCalculatorTest.java | 45 ++++++++++++- 4 files changed, 146 insertions(+), 9 deletions(-) diff --git a/bin/StringCalculator.class b/bin/StringCalculator.class index a937b0c43633d36f133837cd90ddd10fb14ac782..b525274db734db9a9a3b727673386cd2b9b42d0a 100644 GIT binary patch literal 2945 zcma)8NpKTK6n!np9$5?l1hOF*P*^Q_g8&9B0RaMuf@BthjloG6OM^UkG^310Y#}RT z-^d=alcl)iKsX30R8cwSoNG=wq$<~(19?4?ykV+R6yu)h-@o7c@BME5cITJd05;%9 z1wMhf{jOtJ`CXcsEt{HaI|>5)dQv;3b(@-%@7_0ZQqQ^qjY-2Y+-(B>w%C9`V3(cK zL-3?qR}h|puK#Sw)e9;XAVOqznLs1e#fIIzpUhmY*R?_-h!%nPpl)XE zf*y74s5`1hOCI+qQP#*ui;kUlv_e-Xghg1a;6Z`;9+s);RJ3A=fKrq@n8X}un+b9V z0!{J}Rq+s(3p5y3QDR)s7P~iI!AgOa>CAVQ4Kt@ZL98b6A-n8Ey&TcgTB$c0!V0uW z0hY*7Ttz!ND3w+$>Q+vmb0$>xY+T!cCF&CJN$M-GR>e9AIOy6{0VIp{Ds8h-U{?bJ0?{ z5H(63*R7HvJ=ofoy0?IJ9#XnTRP2xnHFyfqTAGum?dH$mEY{1bsOQS*)t9c6_NbFi zN&OgEGubrUtQWx^vR%y-5Gh)DY73k#*4rRnRblG-?JIzD@hyBwuD>|Ztj0Zyt}tq7E-;k08*8!n$T zJ9AFysD4+=j%Bo>cN!_6Op}hfx}%%=jl89~Wrv5@w*Twy`-xYqpCD7U4#7rK5VVqS z*b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v@%*pR!*UPqfHEB^i|J53gh%m_X230pcBJ2dW~VSA2mg*0r~Qe)5wBtw96w40A;a=r9EO$^gSv`C-^WoTf3peK=~^U_fI&y=m` z^FL5{cZ%DVey59NC8MBZtoCIj24pM}tU)9Nf?X#6ivu$9eHj{wyoWWSH3?if(Yv`n zC~P4~7!2z}g*B;UB`g=OMv}VNsOpbq@Ff4Gi>K#~>qt=48pWEVW1Tc@4Cu+7*)l_K NTg1ljoZ@46@eiTu8?OKW delta 173 zcmX@a^^=+F)W2Q(7#J8#7^Ek1DF|dGmL=-vB<7{-`zK|kCYMaK%4B6JNi8m!xK3Y@ zgF%RqLD(<9M4>XZL?JV`ASX39HLoN!g^NLiL5!V2bh0C(D6=?&#N splitInputs = calc.splitString(inputs); + int sumTotal = calc.add(splitInputs); + System.out.print("Added sum is "+sumTotal+".\n"); + } + + public static String input() + { + //takes the input, checks that it is valid, returns the input + + //could probably refine the checking to run an iterator through the input string + //and check char by char it through to detect '-' symbols in case of negative numbers + //and '\' 'n' for line changes, then parse them out or something. + + //but first, I want the basic functionality working. Despite being granted more time + //to finish this, I've been sick for the last few days so not much has come out of it :( + String inputs = null; + boolean ok = false; + Scanner reader = new Scanner(System.in); + System.out.println("Input the numbers: "); + + do + { + try + { + inputs = reader.nextLine(); + ok = true; + } + catch( InputMismatchException wrong) + { + //haven't been able to figure out the StringCalculatorException stuff yet, + //so using this as a placeholder. + inputs = "0"; + ok = true; + System.out.print("Illegal input, resolving to 0. "); + } + } + while(!ok); + + reader.close(); + return inputs; + } + + public static ArrayList splitString(String inputs) + { + //takes the inputs as a mess, tries to make sense of it and split it into int-parts + //form an ArrayList with those parts, return it for further use + + //Arrays.asList(str.split(",")); + ArrayList whole = new ArrayList(); + + //Welp, I'm giving up. Despite all logic, I'm repeatedly getting + //numberformatexception when the input tries to use \n instead of , + //even when I am replacing those with commas here. + //Also tried with read.next instead of read.nextLine, in case that input + //had an extra \n at the end which would then feed a "" to the array if that + //ending \n is replaced with a , but I don't think that is the case, because + //the error does not pop up with commas as separators. + + //How will I learn about testing if I have to code the testable software myself in a language + //I am not comfortable with first, I wonder. + String correction = inputs; + correction = correction.replaceAll("\\n", ","); + correction = correction.replaceAll("\n", ","); + + for (String chunks : correction.split(",|\\n")) + { + int temp = Integer.parseInt(chunks); + + whole.add(temp); + } + + return whole; + } + + public static int add(ArrayList numbers) + { + // Returns the sum of the numbers given in numbers + int sum = 0; - // not yet implemented - return 0; + for(int i = 0; i < numbers.size(); i++) + { + sum += numbers.get(i); + } + + return sum; } -} +} \ No newline at end of file diff --git a/tests/StringCalculatorTest.java b/tests/StringCalculatorTest.java index 4ec9afe..f4a4eb1 100644 --- a/tests/StringCalculatorTest.java +++ b/tests/StringCalculatorTest.java @@ -1,12 +1,51 @@ import static org.junit.Assert.*; +import java.util.ArrayList; + import org.junit.Test; -public class StringCalculatorTest { +//is this extends XYZ the way to access its methods the right way? +//got a lot 'cannot be resolved to' errors with testCalc +public class StringCalculatorTest extends StringCalculator +{ @Test - public void test() { - fail("Not yet implemented"); + public void testInputs() + { + String testInputs = null; + //input testCalc = new StringCalculator(); + + testInputs = StringCalculator.input(); + if(testInputs instanceof String ) + { + + } + else + fail("Not a string, should be"); } + @Test + public void testSplit() + { + //check that the return value from splitString is an ArrayList? + ArrayList testSplits = new ArrayList(); + testSplits = StringCalculator.splitString("1,2,3"); + + if(testSplits instanceof ArrayList) + { + + } + else + fail("Not an ArrayList, should be"); + } + + @Test + public void testSum() + { + //check that it adds the numbers together properly? + int testSum = 0; + testSum = StringCalculator.add(StringCalculator.splitString("1,2,3")); + + assertEquals("Did not seem to add them together",6,testSum); + } }