From 9e30ab7e422401eab48d2e17b3fa3a4a5494fdbd Mon Sep 17 00:00:00 2001 From: neha-peddinti <56367930+neha-peddinti@users.noreply.github.com> Date: Mon, 21 Sep 2020 22:41:41 -0400 Subject: [PATCH 01/69] Added examples and adv. subdirectories --- .../chapter13/examples/IterToRecursive.class | Bin 0 -> 1257 bytes .../chapter13/practice/NegativeSum.class | Bin 0 -> 1239 bytes .../chapter13/solutions/NegativeSum.class | Bin 0 -> 1381 bytes .../advanced/chapter14/examples/FileIO.class | Bin 0 -> 1118 bytes .../chapter14/examples/MyException.class | Bin 0 -> 281 bytes .../advanced/chapter14/examples/Throw.class | Bin 0 -> 716 bytes .../advanced/chapter14/examples/Throws.class | Bin 0 -> 654 bytes .../chapter14/examples/TryWithResources.class | Bin 0 -> 1222 bytes .../chapter14/examples/UserExceptions.class | Bin 0 -> 707 bytes .../advanced/chapter14/examples/names.txt | 6 +++ .../chapter14/examples/names_upper.txt | 6 +++ .../chapter13/examples/IterToRecursive.java | 42 ++++++++++++++++++ .../chapter13/practice/NegativeSum.java | 34 ++++++++++++++ .../chapter13/solutions/NegativeSum.java | 37 +++++++++++++++ .../advanced/chapter14/examples/FileIO.java | 28 ++++++++++++ .../chapter14/examples/MyException.java | 10 +++++ .../advanced/chapter14/examples/Throw.java | 21 +++++++++ .../advanced/chapter14/examples/Throws.java | 19 ++++++++ .../chapter14/examples/TryWithResources.java | 21 +++++++++ .../chapter14/examples/UserExceptions.java | 19 ++++++++ .../advanced/chapter14/examples/names.txt | 6 +++ .../chapter14/examples/names_upper.txt | 6 +++ 22 files changed, 255 insertions(+) create mode 100644 out/production/c4t-java/com/codefortomorrow/advanced/chapter13/examples/IterToRecursive.class create mode 100644 out/production/c4t-java/com/codefortomorrow/advanced/chapter13/practice/NegativeSum.class create mode 100644 out/production/c4t-java/com/codefortomorrow/advanced/chapter13/solutions/NegativeSum.class create mode 100644 out/production/c4t-java/com/codefortomorrow/advanced/chapter14/examples/FileIO.class create mode 100644 out/production/c4t-java/com/codefortomorrow/advanced/chapter14/examples/MyException.class create mode 100644 out/production/c4t-java/com/codefortomorrow/advanced/chapter14/examples/Throw.class create mode 100644 out/production/c4t-java/com/codefortomorrow/advanced/chapter14/examples/Throws.class create mode 100644 out/production/c4t-java/com/codefortomorrow/advanced/chapter14/examples/TryWithResources.class create mode 100644 out/production/c4t-java/com/codefortomorrow/advanced/chapter14/examples/UserExceptions.class create mode 100644 out/production/c4t-java/com/codefortomorrow/advanced/chapter14/examples/names.txt create mode 100644 out/production/c4t-java/com/codefortomorrow/advanced/chapter14/examples/names_upper.txt create mode 100644 src/com/codefortomorrow/advanced/chapter13/examples/IterToRecursive.java create mode 100644 src/com/codefortomorrow/advanced/chapter13/practice/NegativeSum.java create mode 100644 src/com/codefortomorrow/advanced/chapter13/solutions/NegativeSum.java create mode 100644 src/com/codefortomorrow/advanced/chapter14/examples/FileIO.java create mode 100644 src/com/codefortomorrow/advanced/chapter14/examples/MyException.java create mode 100644 src/com/codefortomorrow/advanced/chapter14/examples/Throw.java create mode 100644 src/com/codefortomorrow/advanced/chapter14/examples/Throws.java create mode 100644 src/com/codefortomorrow/advanced/chapter14/examples/TryWithResources.java create mode 100644 src/com/codefortomorrow/advanced/chapter14/examples/UserExceptions.java create mode 100644 src/com/codefortomorrow/advanced/chapter14/examples/names.txt create mode 100644 src/com/codefortomorrow/advanced/chapter14/examples/names_upper.txt diff --git a/out/production/c4t-java/com/codefortomorrow/advanced/chapter13/examples/IterToRecursive.class b/out/production/c4t-java/com/codefortomorrow/advanced/chapter13/examples/IterToRecursive.class new file mode 100644 index 0000000000000000000000000000000000000000..cec76219e220c68a9d881eeaebb704517718c279 GIT binary patch literal 1257 zcmaJ=O-~b16g`iA3@t+`P!T&7K~Y;&>bD}Oi8V?SK&1p1t{&|Z#Q zhYgIGID&D3fg&BOxXZtRbiQ7$2t=~QVy+lR21iXC3y_h9`&4-X;euOHOkA)XwbZPy zF8e zQRxl0;txb|CXQ+34V(@7b53CL?_t}{rdw6{b{pF*yCcim^|l0_bvL>LY?IDQr&3e? zbrLSNnF`#sD0qw{@dd>+kH$Oih``wN}L~#(*<_-ZsGPdAz&6AM|p*s z^nK+UxGZpdkG=g=8@MVk_J1fZLE9oGc&pIE6u6;qFbO z@PNEoqGJgC;QbWIK876Jj@%iF4IC$}=ZJ*gq^)q}BPP-tZ~hcZwCtqW5sQY$T4W)J zC~0_8Iy6finoQbyiAb6}+vrPM6^r`$k|wG3mvV$X14Trr)}SMk{Y#NJ&>2RLVUjJ^ Z5->$Cfm4Cg{GZ1~zQq|`qQ4m&`US?bAqW5f literal 0 HcmV?d00001 diff --git a/out/production/c4t-java/com/codefortomorrow/advanced/chapter13/practice/NegativeSum.class b/out/production/c4t-java/com/codefortomorrow/advanced/chapter13/practice/NegativeSum.class new file mode 100644 index 0000000000000000000000000000000000000000..da16c90c18f8c7ca6f0dc8aa6a7275e5ac3d6da5 GIT binary patch literal 1239 zcmaJ>T~8B16g@*}w=4^+e27rgs;Dg@E8rIt6lp_j0&1+mL|+`XW4qAqG`mv_zd-+i zKKbg4FZzH)6JLBY@!cQe*PXTrB@j2+xp!ypJ$ufXnf?Cf^H%_K$eHLs#6YKoC}Iru zCg0{xh5IGv{>G+o6+>*n^F4KoA(Be3cOi~01Ez&;SO>}rJE0O)s*-hT8d_}tdD3|h zc)lvAK=5iNj$Q^cuSCF=w=L#P*f?gO-@*U}83vB%VK8bMq2d#0s)^d5N06Rh%B0t0 zOQb|!CQe|)z^H`;#u$3{rL1vPCT2DHf+3p9<(zjQQrhXce7S7--!*G>viLCTp zt{!`;Oj)S7uR{IheERUW;}|D74~1I~!ls-HxM(0{A&m)!j4P{-D~sZ}43wDM%M6{wW}QNo zuo?PRJzw0bS2slPh;LMArnAaDvg>&2>0!>gv8SN8yS2t^t$MQ{>wzm)Jl$-!v6#{p z($un)DpUcltqE0@#jr1i8zydI&cM9(F2gXs@3!Y}%PrwF7pQ6H3MrJq4#S)FC2SQo zLGNpq>$+`=4{`3_AZB4{zoT8_b6dA?n z#3!&1lrT6n>etXG5LYlwxi0-evk{8rk1+ascF||gEZC{V#Mra9m`%JK#Lymw$6C=H z#L3rhVJGZ(d11`{RbG7e_h%wTd9*_555l0v&o~Vbl14n&jP{J3+Qa!>T+(bh8f<1@ zQQl2_EcBuWgEUT}kC@r?XBogE&EznQhZw;VjG{#6a)vs(5cy3%0ybe%N++cyWR#-z lq9gL2#&*F;5;&{h;3}?BjMLlebT8l*J(-l<#vNoa@()RRDdqqG literal 0 HcmV?d00001 diff --git a/out/production/c4t-java/com/codefortomorrow/advanced/chapter13/solutions/NegativeSum.class b/out/production/c4t-java/com/codefortomorrow/advanced/chapter13/solutions/NegativeSum.class new file mode 100644 index 0000000000000000000000000000000000000000..e215d4c11ab91329c0b5c89c2e781a1b2ac9ca78 GIT binary patch literal 1381 zcmaJ>zjGT!6#mwhbn^LZUmQolI>k}2O{64P#DV;h9E@d5l(7vn@|XgOtmPf+tka!F zI+<}nLjyGw(2$lU4HUuSA`MhD{6R2*Z%-mo#u~2Jef#!%?|tvv-TULk>2m=0P%$xr zQ3EjxaU=xnLwO_%p7ak2AMYKiS}2exxxO1d5E#wncTFTQX27&C4(l~|^|%wNra;n{ zez4;mus3ZJxHKdgprC7VSP(>QNn#=-@p1kMk^2pBCL z;rRlwTqVDoSY;3TGm(a4AYZ z=D2~k4J=tG;2nYSZFL|+_efQ{R59VJgUE>IDwTYN4VQy`N;&p*+@J!?=PQe0TaAC6m>7KlQ;66tlS1&#^o2NNcCF>zwDb5!7#yFb`yKzMK)>^;G&^_L zIJrMfC+bez)7DG2DV_xX3CuUoY74X$v4pEp^%zFXd7jqo2z>{4IZv|gJ+gQ|8b3gp NK9lQpY~Ueg{{`?GIWqtN literal 0 HcmV?d00001 diff --git a/out/production/c4t-java/com/codefortomorrow/advanced/chapter14/examples/FileIO.class b/out/production/c4t-java/com/codefortomorrow/advanced/chapter14/examples/FileIO.class new file mode 100644 index 0000000000000000000000000000000000000000..b059c401ddd52e07a3019be9fb733c7ed18d12f6 GIT binary patch literal 1118 zcmZuwOHUI~6#j0Ra@%2OOG^tC5m1yCO7V&E)KWof3Nz zLo|^;hbua^VQx+dh#*7PMTnePtvlq`)ryP&Iz)Pyq2na|q>!81$;7>(#1mH~Wb|5vXwmSJ1;CQJD>Qn4FAWT;u3d(2oIz$a%BbE!9>vN4FW;9P5<; z%c?bT}`h# zhGqIOiZsWVg4?)riTvW*nxj{!O{-2Th&FkoG=V{|vI<(zLp5 z>lf!KAfKn!H9 z4VpKYH%xu0UMcDJvRWz&1}dsyGQ<*V|Jyo4yNkG}R^51xr>8x+w{3aLwsx98^4Tyo z2Dxa}ZA}-|X&22V(qd5Ahg&!Wj4d*r(nC^hf4t z6GEEQR~~DwG*NY>$DO2vZt`)O!RO8sfPyZx6CpvQ5Te`|X2_DExOv10C5$Dyhb!nL zycpKWr;%@yd?x7*>AR%ABmF)7Bp=X&IjS%S|1kzQ0vr(z-o;;V49Dom{vj4g@ZZ$K QJVi>x?J&Iy^zFg3e`w02!xB-BDQLUlAmf7r+hdvduGK9)+N@Bw@%F%uHDc(XJ6 zdvE9Yx<3H)P|Co9?IMvw5)Prf^G|+v@0)FRd|s<#qys~62HNOoNU$5tr8rW8rwNsY z_cP3+P@8SPxpa{wWT%(Egv2P=C_>TGM$O{GN`(u5wdYA@9>n2VO|&$nKTk)3QGMY?TP7)xN>~v7g}7mn<-0q4)+?d@xP` literal 0 HcmV?d00001 diff --git a/out/production/c4t-java/com/codefortomorrow/advanced/chapter14/examples/Throw.class b/out/production/c4t-java/com/codefortomorrow/advanced/chapter14/examples/Throw.class new file mode 100644 index 0000000000000000000000000000000000000000..1cfdffd9d87ba1931d1a9e8bccc793c831568e74 GIT binary patch literal 716 zcmZuvT~8B16g|^zJMFU23ewV+$^w4u@<5*rYGRC|3(1#lZ%HZrg{vJP^{Gn5a+k!X!Y zwBOnt9LPX33a;8=l6>!pvYl6n1k~ly!+-*%aBm&k_~su1=%~ zcWf-6#8Gx|1&a*1-YAKWNnxmt)KF5wYq$NjjjLGZs5q!%g<$Uwp zN1dVhxB2^t(xXsn6+C<$$ca{QRKP03MsFk&=^l$iS46J(gL36>*tU_sX0+GHhIj5G z9SE6F(f*F#w^4&tzy?F*ajab(yD}QO@k{sIhv!pM4`O4e)KQUqIt>Rh>50LZGTBfl zYP#aRxQv-$zN>WP?K;O%ZaO#=BynPNK~*tW5Wb(&>{-Vm@rlS7`?O^TYdGum@o zXJ99EV7N}Jxi-)uZr~=J4YW_Op{RWXFZ0|d%%5WEJ)923i;Yt>Ptqa>B{G~zX)2@y za5%6jTR@ozSLiR?O0$_=jv9Rs^Sl=td!%g9Y+2;{9K#z>LEH literal 0 HcmV?d00001 diff --git a/out/production/c4t-java/com/codefortomorrow/advanced/chapter14/examples/Throws.class b/out/production/c4t-java/com/codefortomorrow/advanced/chapter14/examples/Throws.class new file mode 100644 index 0000000000000000000000000000000000000000..10fdb92a257c275a332e4652d129c2c3666931da GIT binary patch literal 654 zcmY*WT}vB56g`uynPju8Ch@zqO+SeW=1mZbFVcdL)SHk(?1k| zqM(mLf0TG;V4LlGWxt;0Av8Zb~npa%>}wMI%|qWTxp)mtn35|aXz|ChUa zpZ^BBpO`2tU>YTfvWE(0o=G>42Z`=cF&fgq>=Wbn*n~+liM8r&`u@B%N#^v5U!WZx2YLux$QPG3$16CTjI3m;hlXD9ll1*RG%)Zd1^ zwvJn>-Q|tbQ-;NJwVxx4&#MLeW}<@MUseAR?>@bqopPJeFb=fsl0b3P$aUK$ykV0w z91oC%uyf)5ihs*I1<#4q3y4z&0^~j d=On#@19nhiNjc@=YdpN;wZlrE>wOCU@Bjvndf(uI4j+T&1{v1CjFA^Us0@BnFNjjVv*oDZ&?H$SLpVNn zLYH=I!(1Jc6wM6nrzuiqvFwmv=mix`Xp!(BL(4Jz_#zkD%FuBdx>$BytHOpoBB##s7Rn!Lm&DX z8ueT*O&QywR1%b1$7$Va2&!M!(ZzA=A0;W1TAbd>ilSp!rXP23mt#o7Fz%gcWqP~h zhyoRDm8qfOYF7ySoLX`${Fo0T43SAQmRgL}C}UPOTefYH>sK*`agGNXCS+@?TAig} z;6+K8H&qPck%mdBL$iC9cJ%DWCDN1>Nm6c0_V#PTS$io;R@u&q5-FRsa-?JPoMDQE za$!Z-OHwCt6m-L6Xp6r+Z<y73%ODZ6t*8Pr-?be)=ZFP0Nbiu|8WzZ?a0 zVv4>F`FKDLMLn&L=`Bi@k@a@(f_KCFjF$lz9|G7^jJwpo*r+hVTM!$Y$^k zAIPd$M}eB=aSIuxF#~=_cX`5R0F%0Ydu;6CzXqCR(t8AU>fLvxH1T zJ4BR&b2M>;+GjWtNASVRk*b~285k|T19++d^cOl6Q1$Wyuqlo~4bNO{870XuM|%(E F{{o}z32FcU literal 0 HcmV?d00001 diff --git a/out/production/c4t-java/com/codefortomorrow/advanced/chapter14/examples/UserExceptions.class b/out/production/c4t-java/com/codefortomorrow/advanced/chapter14/examples/UserExceptions.class new file mode 100644 index 0000000000000000000000000000000000000000..9768edc2754dc8265b1f7240de6b33293aac8b08 GIT binary patch literal 707 zcmZuvO>fgc5Pg%lb>i4*>ofsM11_IQkjM=Q6(OMtxTJs-D5^MZoK?ETACa@E2!Dt( z90;NkJtFa=5VI~yiNwLPJL7q8-puT;-`{@#c!+xr3MksJT&!S~uzoB~L^l%2sQYqo zEJICLeWVhl9}|kL_JM;E$~GJq6}W`zn~BVxehB4Qt2CJqtWZP|q2B6U5cPGYlF@Ga zz($o&*`F-{WvpYvhVP<=&86b~*+k2jYo=3{P(No=Y4;UVX(p5+-Yua)@b|=YbfklS zSRGu(6&qVFt{RQCT*)$)V2=$`l-Otznipc9WJ({!Qmb&GvV`k|&7nF`Lm3Qa!AF^; zfy}ZrbFhsaGdb6Rha$JXmkuRit)~+CVj2%*_F4=gj##l!38B$?_rJjqyuKFU`+YI~ zo9`^D^H80BI?Y1)Oc|!y^2qNS>Db}56nLRP5o9*W_9lBxK8eqj&K&faFN9lc&2N)h z#% b) + a -= b; + else if (b>a) + b -= a; + } + System.out.println("GCD:" + a); + + System.out.println("With recursive function:"); + System.out.println("GCD:" + gcd(72, 20)); + } + + // Recursive function + public static int power(int base, int exp) { + // Base case: exp = 0, so the result is 1. + if(exp == 0) + return 1; + + // Multiply once by the previous power. + return power(base, exp-1)*base; + } + + public static int gcd(int a, int b) { + // Base case + if(a == b) + return a; + else if(a > b) + return gcd(a-b, b); + else + return gcd(a, b-a); + } +} diff --git a/src/com/codefortomorrow/advanced/chapter13/practice/NegativeSum.java b/src/com/codefortomorrow/advanced/chapter13/practice/NegativeSum.java new file mode 100644 index 0000000..f00a526 --- /dev/null +++ b/src/com/codefortomorrow/advanced/chapter13/practice/NegativeSum.java @@ -0,0 +1,34 @@ +package com.codefortomorrow.advanced.chapter13.practice; + +/* +Implement the iterative code as a recursive +function to clarify what the algorithm does. +*/ + +public class NegativeSum { + + public static void main(String[] args) { + System.out.println("Iterative:"); + int a = 50, b = 40; + while(a + b > 0) { + if(Math.pow(a, 2) + Math.pow(b, 2) > 10000) { + a *= 8; + b *= -4; + } + else { + a *= 3; + b /= 2; + } + } + System.out.println("a + b: " + (a + b)); + + System.out.println("Recursive:"); + System.out.println(negSum(50, 40)); + } + + public static int negSum(int a, int b) { + + // Insert code here and change return statement. + return 0; + } +} diff --git a/src/com/codefortomorrow/advanced/chapter13/solutions/NegativeSum.java b/src/com/codefortomorrow/advanced/chapter13/solutions/NegativeSum.java new file mode 100644 index 0000000..247e7a7 --- /dev/null +++ b/src/com/codefortomorrow/advanced/chapter13/solutions/NegativeSum.java @@ -0,0 +1,37 @@ +package com.codefortomorrow.advanced.chapter13.practice; + +/* +Implement the iterative code as a recursive +function to clarify what the algorithm does. +*/ + +public class NegativeSum { + + public static void main(String[] args) { + System.out.println("Iterative:"); + int a = 50, b = 40; + while(a + b > 0) { + if(Math.pow(a, 2) + Math.pow(b, 2) > 10000) { + a *= 8; + b *= -4; + } + else { + a *= 3; + b /= 2; + } + } + System.out.println("a + b: " + (a + b)); + + System.out.println("Recursive:"); + System.out.println(negSum(50, 40)); + } + + public static int negSum(int a, int b) { + if(a + b <= 0) + return a + b; + if(Math.pow(a, 2) + Math.pow(b, 2) > 10000) + return negSum(a * 8, b * -4); + else + return negSum(a * 3, b / 2); + } +} diff --git a/src/com/codefortomorrow/advanced/chapter14/examples/FileIO.java b/src/com/codefortomorrow/advanced/chapter14/examples/FileIO.java new file mode 100644 index 0000000..4dd61d1 --- /dev/null +++ b/src/com/codefortomorrow/advanced/chapter14/examples/FileIO.java @@ -0,0 +1,28 @@ +package com.codefortomorrow.advanced.chapter14.examples +import java.io.*; + +public class FileIO { + + public static void main(String[] args) throws IOException { + FileInputStream in = null; + FileOutputStream out = null; + File file = new File("names.txt"); + + try { + in = new FileInputStream(file); + out = new FileOutputStream("names_upper.txt"); + int c; + while ((c = in.read()) != -1) + out.write(Character.toUpperCase((char)c)); + + } catch (IOException e) { + System.out.println("An IO Exception occurred."); + e.printStackTrace(); // Prints error output stream. + } finally { + if (in != null) + in.close(); + if (out != null) + out.close(); + } + } +} diff --git a/src/com/codefortomorrow/advanced/chapter14/examples/MyException.java b/src/com/codefortomorrow/advanced/chapter14/examples/MyException.java new file mode 100644 index 0000000..f45ee55 --- /dev/null +++ b/src/com/codefortomorrow/advanced/chapter14/examples/MyException.java @@ -0,0 +1,10 @@ +package com.codefortomorrow.advanced.chapter14.examples; + +public class MyException extends Exception { + + public MyException() {} + + public MyException(String message) { + super(message); + } +} diff --git a/src/com/codefortomorrow/advanced/chapter14/examples/Throw.java b/src/com/codefortomorrow/advanced/chapter14/examples/Throw.java new file mode 100644 index 0000000..af53211 --- /dev/null +++ b/src/com/codefortomorrow/advanced/chapter14/examples/Throw.java @@ -0,0 +1,21 @@ +package com.codefortomorrow.advanced.chapter14.examples; + +public class Throw { + + public static void main(String[] args) { + try { + System.out.println(divide(7, 2)); + } catch(ArithmeticException e) { + System.out.println("There was an arithmetic exception."); + } + } + + public static int divide(int a, int b) { + int result = a / b; + if(result != (double)a / (double)b) + throw new ArithmeticException(); + System.out.println("Got to end of divide()"); + return a / b; + } + +} diff --git a/src/com/codefortomorrow/advanced/chapter14/examples/Throws.java b/src/com/codefortomorrow/advanced/chapter14/examples/Throws.java new file mode 100644 index 0000000..b47d1b6 --- /dev/null +++ b/src/com/codefortomorrow/advanced/chapter14/examples/Throws.java @@ -0,0 +1,19 @@ +package com.codefortomorrow.advanced.chapter14.examples; +import java.io.*; + +public class Throws { + + public static void main(String[] args) { + try { + File file = null; + access(file); + } catch(IOException e) { + System.out.println("There was an IOException."); + } + } + + public static void access(File file) throws IOException { + if(file == null) + throw new IOException(); + } +} diff --git a/src/com/codefortomorrow/advanced/chapter14/examples/TryWithResources.java b/src/com/codefortomorrow/advanced/chapter14/examples/TryWithResources.java new file mode 100644 index 0000000..9bf56c8 --- /dev/null +++ b/src/com/codefortomorrow/advanced/chapter14/examples/TryWithResources.java @@ -0,0 +1,21 @@ +package com.codefortomorrow.advanced.chapter14.examples +import java.io.*; + +public class TryWithResources { + + public static void main(String[] args) throws IOException { + File file = new File("names.txt"); + + try (FileInputStream in = new FileInputStream(file); + FileOutputStream out = new FileOutputStream("names_upper.txt")) { + + int c; + while ((c = in.read()) != -1) + out.write(Character.toUpperCase((char)c)); + + } catch (IOException e) { + System.out.println("An IO Exception occurred."); + e.printStackTrace(); // Prints error output stream. + } + } +} diff --git a/src/com/codefortomorrow/advanced/chapter14/examples/UserExceptions.java b/src/com/codefortomorrow/advanced/chapter14/examples/UserExceptions.java new file mode 100644 index 0000000..64b259e --- /dev/null +++ b/src/com/codefortomorrow/advanced/chapter14/examples/UserExceptions.java @@ -0,0 +1,19 @@ +package com.codefortomorrow.advanced.chapter14.examples; + +public class UserExceptions { + + public static void main(String[] args) { + try { + call(null); + } catch(MyException e) { + System.out.println("Caught MyException"); + } + + throw new ArithmeticException("divide by zero error"); + } + + public static void call(String s) throws MyException { + if(s == null) + throw new MyException(); + } +} diff --git a/src/com/codefortomorrow/advanced/chapter14/examples/names.txt b/src/com/codefortomorrow/advanced/chapter14/examples/names.txt new file mode 100644 index 0000000..0351a47 --- /dev/null +++ b/src/com/codefortomorrow/advanced/chapter14/examples/names.txt @@ -0,0 +1,6 @@ +Susan +Jeremiah +Josh +Kati +Sher +Lin diff --git a/src/com/codefortomorrow/advanced/chapter14/examples/names_upper.txt b/src/com/codefortomorrow/advanced/chapter14/examples/names_upper.txt new file mode 100644 index 0000000..5e2ad34 --- /dev/null +++ b/src/com/codefortomorrow/advanced/chapter14/examples/names_upper.txt @@ -0,0 +1,6 @@ +SUSAN +JEREMIAH +JOSH +KATI +SHER +LIN From 0225037231f55e92b94bedcaa4be32ff6e409fca Mon Sep 17 00:00:00 2001 From: neha-peddinti Date: Tue, 22 Sep 2020 03:23:42 +0000 Subject: [PATCH 02/69] Bot: Prettified Java code! --- .../chapter13/examples/IterToRecursive.java | 20 +++++--------- .../chapter13/practice/NegativeSum.java | 16 +++++------- .../chapter13/solutions/NegativeSum.java | 26 +++++++++---------- .../advanced/chapter14/examples/Throw.java | 6 ++--- .../advanced/chapter14/examples/Throws.java | 6 ++--- .../chapter14/examples/UserExceptions.java | 5 ++-- 6 files changed, 33 insertions(+), 46 deletions(-) diff --git a/src/com/codefortomorrow/advanced/chapter13/examples/IterToRecursive.java b/src/com/codefortomorrow/advanced/chapter13/examples/IterToRecursive.java index bc422dc..8c9fef1 100644 --- a/src/com/codefortomorrow/advanced/chapter13/examples/IterToRecursive.java +++ b/src/com/codefortomorrow/advanced/chapter13/examples/IterToRecursive.java @@ -9,10 +9,7 @@ public static void main(String[] args) { int a = 72, b = 20; while (a != b) { - if (a > b) - a -= b; - else if (b>a) - b -= a; + if (a > b) a -= b; else if (b > a) b -= a; } System.out.println("GCD:" + a); @@ -23,20 +20,17 @@ else if (b>a) // Recursive function public static int power(int base, int exp) { // Base case: exp = 0, so the result is 1. - if(exp == 0) - return 1; + if (exp == 0) return 1; // Multiply once by the previous power. - return power(base, exp-1)*base; + return power(base, exp - 1) * base; } public static int gcd(int a, int b) { // Base case - if(a == b) - return a; - else if(a > b) - return gcd(a-b, b); - else - return gcd(a, b-a); + if (a == b) return a; else if (a > b) return gcd( + a - b, + b + ); else return gcd(a, b - a); } } diff --git a/src/com/codefortomorrow/advanced/chapter13/practice/NegativeSum.java b/src/com/codefortomorrow/advanced/chapter13/practice/NegativeSum.java index f00a526..aa25480 100644 --- a/src/com/codefortomorrow/advanced/chapter13/practice/NegativeSum.java +++ b/src/com/codefortomorrow/advanced/chapter13/practice/NegativeSum.java @@ -10,14 +10,13 @@ public class NegativeSum { public static void main(String[] args) { System.out.println("Iterative:"); int a = 50, b = 40; - while(a + b > 0) { - if(Math.pow(a, 2) + Math.pow(b, 2) > 10000) { - a *= 8; - b *= -4; - } - else { - a *= 3; - b /= 2; + while (a + b > 0) { + if (Math.pow(a, 2) + Math.pow(b, 2) > 10000) { + a *= 8; + b *= -4; + } else { + a *= 3; + b /= 2; } } System.out.println("a + b: " + (a + b)); @@ -27,7 +26,6 @@ public static void main(String[] args) { } public static int negSum(int a, int b) { - // Insert code here and change return statement. return 0; } diff --git a/src/com/codefortomorrow/advanced/chapter13/solutions/NegativeSum.java b/src/com/codefortomorrow/advanced/chapter13/solutions/NegativeSum.java index 247e7a7..370528d 100644 --- a/src/com/codefortomorrow/advanced/chapter13/solutions/NegativeSum.java +++ b/src/com/codefortomorrow/advanced/chapter13/solutions/NegativeSum.java @@ -10,14 +10,13 @@ public class NegativeSum { public static void main(String[] args) { System.out.println("Iterative:"); int a = 50, b = 40; - while(a + b > 0) { - if(Math.pow(a, 2) + Math.pow(b, 2) > 10000) { - a *= 8; - b *= -4; - } - else { - a *= 3; - b /= 2; + while (a + b > 0) { + if (Math.pow(a, 2) + Math.pow(b, 2) > 10000) { + a *= 8; + b *= -4; + } else { + a *= 3; + b /= 2; } } System.out.println("a + b: " + (a + b)); @@ -27,11 +26,10 @@ public static void main(String[] args) { } public static int negSum(int a, int b) { - if(a + b <= 0) - return a + b; - if(Math.pow(a, 2) + Math.pow(b, 2) > 10000) - return negSum(a * 8, b * -4); - else - return negSum(a * 3, b / 2); + if (a + b <= 0) return a + b; + if (Math.pow(a, 2) + Math.pow(b, 2) > 10000) return negSum( + a * 8, + b * -4 + ); else return negSum(a * 3, b / 2); } } diff --git a/src/com/codefortomorrow/advanced/chapter14/examples/Throw.java b/src/com/codefortomorrow/advanced/chapter14/examples/Throw.java index af53211..66c645f 100644 --- a/src/com/codefortomorrow/advanced/chapter14/examples/Throw.java +++ b/src/com/codefortomorrow/advanced/chapter14/examples/Throw.java @@ -5,17 +5,15 @@ public class Throw { public static void main(String[] args) { try { System.out.println(divide(7, 2)); - } catch(ArithmeticException e) { + } catch (ArithmeticException e) { System.out.println("There was an arithmetic exception."); } } public static int divide(int a, int b) { int result = a / b; - if(result != (double)a / (double)b) - throw new ArithmeticException(); + if (result != (double) a / (double) b) throw new ArithmeticException(); System.out.println("Got to end of divide()"); return a / b; } - } diff --git a/src/com/codefortomorrow/advanced/chapter14/examples/Throws.java b/src/com/codefortomorrow/advanced/chapter14/examples/Throws.java index b47d1b6..0178da9 100644 --- a/src/com/codefortomorrow/advanced/chapter14/examples/Throws.java +++ b/src/com/codefortomorrow/advanced/chapter14/examples/Throws.java @@ -1,4 +1,5 @@ package com.codefortomorrow.advanced.chapter14.examples; + import java.io.*; public class Throws { @@ -7,13 +8,12 @@ public static void main(String[] args) { try { File file = null; access(file); - } catch(IOException e) { + } catch (IOException e) { System.out.println("There was an IOException."); } } public static void access(File file) throws IOException { - if(file == null) - throw new IOException(); + if (file == null) throw new IOException(); } } diff --git a/src/com/codefortomorrow/advanced/chapter14/examples/UserExceptions.java b/src/com/codefortomorrow/advanced/chapter14/examples/UserExceptions.java index 64b259e..088fa10 100644 --- a/src/com/codefortomorrow/advanced/chapter14/examples/UserExceptions.java +++ b/src/com/codefortomorrow/advanced/chapter14/examples/UserExceptions.java @@ -5,7 +5,7 @@ public class UserExceptions { public static void main(String[] args) { try { call(null); - } catch(MyException e) { + } catch (MyException e) { System.out.println("Caught MyException"); } @@ -13,7 +13,6 @@ public static void main(String[] args) { } public static void call(String s) throws MyException { - if(s == null) - throw new MyException(); + if (s == null) throw new MyException(); } } From 27663f6327a5514fd039b2b4e7b68c735d8b8603 Mon Sep 17 00:00:00 2001 From: neha-peddinti <56367930+neha-peddinti@users.noreply.github.com> Date: Sun, 27 Sep 2020 01:21:47 -0400 Subject: [PATCH 03/69] Add Ch 14 practice problems --- .../advanced/chapter14/practice/Average.java | 31 ++++++ .../advanced/chapter14/practice/Bank.java | 37 +++++++ .../advanced/chapter14/practice/numbers.txt | 100 ++++++++++++++++++ .../advanced/chapter14/solutions/Average.java | 50 +++++++++ .../advanced/chapter14/solutions/Bank.java | 86 +++++++++++++++ .../advanced/chapter14/solutions/numbers.txt | 100 ++++++++++++++++++ 6 files changed, 404 insertions(+) create mode 100644 src/com/codefortomorrow/advanced/chapter14/practice/Average.java create mode 100644 src/com/codefortomorrow/advanced/chapter14/practice/Bank.java create mode 100644 src/com/codefortomorrow/advanced/chapter14/practice/numbers.txt create mode 100644 src/com/codefortomorrow/advanced/chapter14/solutions/Average.java create mode 100644 src/com/codefortomorrow/advanced/chapter14/solutions/Bank.java create mode 100644 src/com/codefortomorrow/advanced/chapter14/solutions/numbers.txt diff --git a/src/com/codefortomorrow/advanced/chapter14/practice/Average.java b/src/com/codefortomorrow/advanced/chapter14/practice/Average.java new file mode 100644 index 0000000..d4611d5 --- /dev/null +++ b/src/com/codefortomorrow/advanced/chapter14/practice/Average.java @@ -0,0 +1,31 @@ +package com.codefortomorrow.advanced.chapter14.practice; +import java.io.*; + +/* +The file “numbers.txt” has 100 random numbers +(one on each line). Use file i/o to find the +average of these numbers. Write “Average: ” +and display the average on the next (101st) +line of the file. + +Hint: Just like BufferedReader and FileReader, +there are BufferedWriter and FileWriter classes +that allow you to add full words to files, +instead of just bytes. You should do a quick +Google search to explore these classes first, +but just to get you started: + +new FileWriter(“fileName.txt”, true) + +will create an instance of FileWriter, and +the “true” puts it in append mode, so you +start writing to the end of the file instead +of writing over the existing content from the beginning. +*/ + +public class Average { + public static void main(String[] args) { + + // Add code here. + } +} diff --git a/src/com/codefortomorrow/advanced/chapter14/practice/Bank.java b/src/com/codefortomorrow/advanced/chapter14/practice/Bank.java new file mode 100644 index 0000000..f63ecf8 --- /dev/null +++ b/src/com/codefortomorrow/advanced/chapter14/practice/Bank.java @@ -0,0 +1,37 @@ +package com.codefortomorrow.advanced.chapter14.practice; + +/* +Create a class called BankAccount (a separate +class from your main program) that contains a +private “balance” attribute for the amount of +money in the account. + +The user should be able to set the original +balance upon initialization (through the constructor). +The class should have deposit and withdraw methods +that add and subtract money from the account. +There should also be a getter method to access +the balance, since the attribute is private. + +Create at least 2 instances of BankAccount. +Deposit, withdraw, and display the final balance of each. + +Create a NotEnoughMoneyException class that is a +checked exception. The withdraw method should throw +this exception if the user tries to withdraw an +amount that is greater than their current balance. +Handle this exception by displaying the difference +between the balance and the amount to be withdrawn, +and do this exception handling in the main program +where you have created instances of BankAccount. +*/ + +public class Bank { + + public static void main(String[] args) { + + // Your code here. + } +} + +// Add other classes here. diff --git a/src/com/codefortomorrow/advanced/chapter14/practice/numbers.txt b/src/com/codefortomorrow/advanced/chapter14/practice/numbers.txt new file mode 100644 index 0000000..54aac38 --- /dev/null +++ b/src/com/codefortomorrow/advanced/chapter14/practice/numbers.txt @@ -0,0 +1,100 @@ +35 +94 +16 +38 +82 +87 +52 +60 +86 +59 +72 +7 +60 +56 +63 +11 +67 +77 +67 +82 +28 +13 +100 +92 +85 +69 +75 +6 +83 +100 +76 +51 +13 +84 +59 +30 +21 +72 +15 +28 +15 +95 +87 +61 +80 +22 +45 +9 +73 +85 +85 +2 +90 +75 +60 +80 +91 +21 +80 +41 +82 +27 +48 +43 +37 +52 +78 +21 +54 +20 +15 +49 +42 +89 +63 +24 +10 +62 +44 +63 +51 +34 +36 +21 +2 +49 +64 +77 +34 +80 +7 +83 +51 +46 +11 +25 +59 +96 +21 +65 diff --git a/src/com/codefortomorrow/advanced/chapter14/solutions/Average.java b/src/com/codefortomorrow/advanced/chapter14/solutions/Average.java new file mode 100644 index 0000000..c0d555d --- /dev/null +++ b/src/com/codefortomorrow/advanced/chapter14/solutions/Average.java @@ -0,0 +1,50 @@ +package com.codefortomorrow.advanced.chapter14.solutions; +import java.io.*; + +/* +The file “numbers.txt” has 100 random numbers +(one on each line). Use file i/o to find the +average of these numbers. Write “Average: ” +and display the average on the next (101st) +line of the file. + +Hint: Just like BufferedReader and FileReader, +there are BufferedWriter and FileWriter classes +that allow you to add full words to files, +instead of just bytes. You should do a quick +Google search to explore these classes first, +but just to get you started: + +new FileWriter(“fileName.txt”, true) + +will create an instance of FileWriter, and +the “true” puts it in append mode, so you +start writing to the end of the file instead +of writing over the existing content from the beginning. +*/ + +public class Average { + public static void main(String[] args) { + File file = new File("numbers.txt"); + + // try with resources block, which closes streams automatically + try(BufferedReader in = new BufferedReader( + new FileReader(file)); BufferedWriter out = new BufferedWriter( + new FileWriter(file, true))) { + + // read the numbers (String input) and find average + double average = 0; + String contentLine = in.readLine(); + while (contentLine != null) { + average += Integer.parseInt(contentLine); + contentLine = in.readLine(); + } + average /= 100.; + + // append average to end of file + out.write("Average: " + average); + } catch (IOException e) { + e.printStackTrace(); + } + } +} diff --git a/src/com/codefortomorrow/advanced/chapter14/solutions/Bank.java b/src/com/codefortomorrow/advanced/chapter14/solutions/Bank.java new file mode 100644 index 0000000..4d980c4 --- /dev/null +++ b/src/com/codefortomorrow/advanced/chapter14/solutions/Bank.java @@ -0,0 +1,86 @@ +package com.codefortomorrow.advanced.chapter14.solutions; + +/* +Create a class called BankAccount (a separate +class from your main program) that contains a +private “balance” attribute for the amount of +money in the account. + +The user should be able to set the original +balance upon initialization (through the constructor). +The class should have deposit and withdraw methods +that add and subtract money from the account. +There should also be a getter method to access +the balance, since the attribute is private. + +Create at least 2 instances of BankAccount. +Deposit, withdraw, and display the final balance of each. + +Create a NotEnoughMoneyException class that is a +checked exception. The withdraw method should throw +this exception if the user tries to withdraw an +amount that is greater than their current balance. +Handle this exception by displaying the difference +between the balance and the amount to be withdrawn, +and do this exception handling in the main program +where you have created instances of BankAccount. +*/ + +public class Bank { + + public static void main(String[] args) { + BankAccount account1 = new BankAccount(500); + BankAccount account2 = new BankAccount(1000); + + account1.deposit(200); + withdraw(account1, 300); + System.out.println("Account 1: " + account1.getBalance()); + + account2.deposit(200); + withdraw(account2, 1500); + System.out.println("Account 2: " + account2.getBalance()); + } + + public static void withdraw(BankAccount account, int amount) { + try { + account.withdraw(amount); + } catch (NotEnoughMoneyException e) { + int diff = Math.abs(amount - account.getBalance()); + System.out.println(e.toString()); + } + } +} + + +class BankAccount { + private int balance; + + public BankAccount(int balance) { + this.balance = balance; + } + + public void deposit(int amount) { + balance += amount; + } + + public void withdraw(int amount) throws NotEnoughMoneyException { + if(amount > balance) + throw new NotEnoughMoneyException( + "Bank balance is short $" + Math.abs(balance - amount)); + balance -= amount; + } + + public int getBalance() { + return balance; + } +} + + +class NotEnoughMoneyException extends Exception { + + public NotEnoughMoneyException() {} + + public NotEnoughMoneyException(String message) { + super(message); + } +} diff --git a/src/com/codefortomorrow/advanced/chapter14/solutions/numbers.txt b/src/com/codefortomorrow/advanced/chapter14/solutions/numbers.txt new file mode 100644 index 0000000..54aac38 --- /dev/null +++ b/src/com/codefortomorrow/advanced/chapter14/solutions/numbers.txt @@ -0,0 +1,100 @@ +35 +94 +16 +38 +82 +87 +52 +60 +86 +59 +72 +7 +60 +56 +63 +11 +67 +77 +67 +82 +28 +13 +100 +92 +85 +69 +75 +6 +83 +100 +76 +51 +13 +84 +59 +30 +21 +72 +15 +28 +15 +95 +87 +61 +80 +22 +45 +9 +73 +85 +85 +2 +90 +75 +60 +80 +91 +21 +80 +41 +82 +27 +48 +43 +37 +52 +78 +21 +54 +20 +15 +49 +42 +89 +63 +24 +10 +62 +44 +63 +51 +34 +36 +21 +2 +49 +64 +77 +34 +80 +7 +83 +51 +46 +11 +25 +59 +96 +21 +65 From 8d4141f2244873aef2a85983b15038654b06fd4a Mon Sep 17 00:00:00 2001 From: neha-peddinti <56367930+neha-peddinti@users.noreply.github.com> Date: Sun, 27 Sep 2020 01:22:34 -0400 Subject: [PATCH 04/69] Add class files for Ch 14 practice files --- .../advanced/chapter14/practice/Average.class | Bin 0 -> 307 bytes .../advanced/chapter14/practice/Bank.class | Bin 0 -> 301 bytes .../advanced/chapter14/practice/numbers.txt | 100 ++++++++++++++++++ .../chapter14/solutions/Average.class | Bin 0 -> 1735 bytes .../advanced/chapter14/solutions/Bank.class | Bin 0 -> 1537 bytes .../chapter14/solutions/BankAccount.class | Bin 0 -> 1177 bytes .../solutions/NotEnoughMoneyException.class | Bin 0 -> 335 bytes .../advanced/chapter14/solutions/numbers.txt | 100 ++++++++++++++++++ 8 files changed, 200 insertions(+) create mode 100644 out/production/c4t-java/com/codefortomorrow/advanced/chapter14/practice/Average.class create mode 100644 out/production/c4t-java/com/codefortomorrow/advanced/chapter14/practice/Bank.class create mode 100644 out/production/c4t-java/com/codefortomorrow/advanced/chapter14/practice/numbers.txt create mode 100644 out/production/c4t-java/com/codefortomorrow/advanced/chapter14/solutions/Average.class create mode 100644 out/production/c4t-java/com/codefortomorrow/advanced/chapter14/solutions/Bank.class create mode 100644 out/production/c4t-java/com/codefortomorrow/advanced/chapter14/solutions/BankAccount.class create mode 100644 out/production/c4t-java/com/codefortomorrow/advanced/chapter14/solutions/NotEnoughMoneyException.class create mode 100644 out/production/c4t-java/com/codefortomorrow/advanced/chapter14/solutions/numbers.txt diff --git a/out/production/c4t-java/com/codefortomorrow/advanced/chapter14/practice/Average.class b/out/production/c4t-java/com/codefortomorrow/advanced/chapter14/practice/Average.class new file mode 100644 index 0000000000000000000000000000000000000000..6f5f5ce4d89b5d3811491bc19ebc806c7d24b787 GIT binary patch literal 307 zcmZ9H&q@P94934~|E$*4dJ+^AytN)21VKa*MDSGfQ0c|f>}*`8bZ5#iU7yR7;K2v* zp~Q>`f;l8#_))96_kCVR(rZHGoUyNsj!PKvU;ai*F;&_C7`F*9s^0}eet(EtDd literal 0 HcmV?d00001 diff --git a/out/production/c4t-java/com/codefortomorrow/advanced/chapter14/practice/Bank.class b/out/production/c4t-java/com/codefortomorrow/advanced/chapter14/practice/Bank.class new file mode 100644 index 0000000000000000000000000000000000000000..72b028b6d8970f33ad3342ad4ce63733a4a18c54 GIT binary patch literal 301 zcmZ9GJx>Bb5Qg754$eaqDiaF}t-!*K360TMXhKD_z|dID?X9@T?UK!czst(R!XMy| zGS1qVIK_Ou&wPA+e|!SCK);I&tpa%uZFC5OC2v`+*e%q3wKO^sI#<@&cui;xNAm(Z zgt7LG(!MrNK1AR65d5p+ddp6mTIpwAM-$G+Y8_ZdtBty0_d>{TWrWb5T4(MyO=ZFZ zS1ak{4O>Sz7(Pz_&d*}7ZgDx96S_0M3EE6-g3Gk*EO9tSA>SvV677eT#QH&$8k<0CA~f0$FUd8mgx#Ip4eCta z+L?By&(6??_LXN}NJnPsTOazT^gr;W^xSM{LP@(byZ7F6&i$Qpe&^h~fBg08Hvn^Z zq@xGD9GZa;!VH5?#HL8rgjG#0tv-=upCNqJv`qgRLvMU~nIpnrG>uFz;I$8WWN6Rv2qQSPm{Wh zsX|IWP%fjGQ0g-WTn2kvA-!W@5>pHjmvYOQmSh-?Pamd4$2r6~rVX6O4CUA6ENl6) zDqVtbgzHIK{s*bq@Qo5xl%64alLjv062s_*SeF^wDhq$b^w((S`NHx&1#+uvAW^($ z;C)rQ&{IW^O7pUT4^?x7$~D`Q9CMU$JH+CeYd;mMH8Q<|w1KPmh~cQHR0?&+ai!53{5t7Ys84Fihi#b!4aRZyhZ5T-@dk3T$2{PZ$~+Jwvtr)kfTqfok-ax5M=9zi!V z=mop(mZfS7IvK5S5{e!&pSNw_^IhTOrN3rZyn!$ZI*Pc@v8+n8!Z6up(zG`1b(sv1 zgQ_ivvTwW38MZnJY#9cicZ5}`NpCV|+v{~FeK^Su$$^c<=Z@@R-kE!F2c$bYr3H}@ zwOWDtBOO7BNNDAqnqWji*0Q9VsR=5*$MFTj)S(=^h0gI6!|DGwrRylj6K8OXt}gZT zfLaq_`YzDw6pf6=(D??~OPVlzOkbr2o#-RDgDlO-lx>lelk}He@Y2wc2KsWl=r8SJ zur$-aPy-|RT|{X;R+`ZoIN8AXV*i|WVFz!g!m;r07>#PN@UJ-geR*HHz4vES8^#5D zPMeKtuaE$C@b2smKG;Syr3Dis6^d!G&<;{D?KPoY+s4RZF!qs6Oe4+g>up5xB)s{O zlAFOJROxt`*nmUh7{0-`G=7I4@FR^nUSNyPN>4M_yUjb`BuU4~a)NeGQXo@wtk2Sw ze~yTYBTrY&easSWf{@1vFF}aogmaOQ68H&8{ESPuOZ+%6_GojM;}Z_f(a#a3e?}gN z?jZ)PQx3*)We*XAI;A9QDk5*tv(Pl4UrvWXpyhAC<~gow=*1HG?ZKz`jK&Dn>mI$I Q;{mO7nmxpq_!_al0T+Cr1^@s6 literal 0 HcmV?d00001 diff --git a/out/production/c4t-java/com/codefortomorrow/advanced/chapter14/solutions/Bank.class b/out/production/c4t-java/com/codefortomorrow/advanced/chapter14/solutions/Bank.class new file mode 100644 index 0000000000000000000000000000000000000000..dc05dccb9b0d5376f27b2e8476c66f12f99a7924 GIT binary patch literal 1537 zcmbVM-A)@v6#fPSYcI=>3BMsw(vsAGn=Sck8WU3NLUC&oDo0dRFREcZ#T(l**6tX> zBlHdO2E9%$NR&vqj`9q7fIL92tDf1lgd?m-$zJTtoO9+o=ljmgy#4RxYXIM4&qf~- z7Wy3wV31+#5kKN?gUdtr!NDWpX@Uuh zq}n9dWGNESSMFZmOI^`{;LV(&dC9>%78vY9q4S(n_CzFWah#g5=-@KGWSDO9x+o~= zas3;WPqR>SsY3!Lxnk_7K?Ll}LjqpH6$@7#e2omlg&xUC*p?}!Aq~iRh~@opFf5u9 zSB>OD7QSKF`DDR%mEMx7eON0iDNeQ?d!l9fZ^Om67S%~Hll#PVD)UD#vJAr& z&Aob=w>t55MYRJ@Z2M+&%AD3~rbudXUMU^wfVav**Hkqe9mJ-MJPH8yKcSMzD!d>=BccdA>Sj{Ykv~Y1hKa+$mQ6!AyB>g}|#X zaBYLCphP#RfTuA_B=ug ZG>*{h2LjuU{yX@YY&NZbK^YG)`#%u)gVO*2 literal 0 HcmV?d00001 diff --git a/out/production/c4t-java/com/codefortomorrow/advanced/chapter14/solutions/BankAccount.class b/out/production/c4t-java/com/codefortomorrow/advanced/chapter14/solutions/BankAccount.class new file mode 100644 index 0000000000000000000000000000000000000000..740fa6580334a6fac699cd7008dfefec037156b8 GIT binary patch literal 1177 zcmb7DT~8B16g^X*+b%62%9j3yLPD#6%hhO(;H;V9*z*yJNew-D!5GApgW4 z;G-`l8Vn}-?vFCwVJnnCOnlfockbMC?>%Q{e*HfE0pJ;)nuws!Kz{-Qh%$^E@I!7j zxmUAZRu6=&7@}*g=c?xnec9Yz95KWVmX%2jpp z0>wQ=sbiG7eNm;NeAlZLOeAqzOPA6aSI={MVH3R+NhXHjZPyb!omN%&yS&<@h~bE~ z3|x{Uc0@fLpC2(KD~j8VGH-X2%yVWmi&+zD{jYNLS4tGB$c}G|O;=ZpYmt}qu=GRM zrBs3PdAlrBT{^*V6gd-l%o|wHtQQ$F7rb2WP&R}W(hG&y$8B*<`kS8o@ER6+#qX9V@@BK*Dp82RVwlvV)7=Y|c7t?K zr#qR>Fos0Q^Mqe)@*og_fkzCpR~TPM#K0=U%)e9VeT1x$rl%31yTr)S;~=k}`~ln{ zJ59Sh#r3(FKLN|9zQXu?mOVpu2z_MJgt&(|?i1qfIc|>5G;T1z{{=?s?IoaozQ8O@ zJS0$>K#{X%uc?$i$))lH`iQCTNbYMl<$S~NDaL>kO#KW?I^B{H=vl^TW&#qMJVr53 zu?1wYNRxpkVKX#dNG(YnA0R_k)0?K1(K?{Jd?JUWBO5&nE)#N=b`ctLj@B4Ov-B-t OnRX_5D|n12nEC@+a2qTD literal 0 HcmV?d00001 diff --git a/out/production/c4t-java/com/codefortomorrow/advanced/chapter14/solutions/NotEnoughMoneyException.class b/out/production/c4t-java/com/codefortomorrow/advanced/chapter14/solutions/NotEnoughMoneyException.class new file mode 100644 index 0000000000000000000000000000000000000000..97c1471b9526fd96ebb71d60ac10b3fbe2e2f72a GIT binary patch literal 335 zcmYk1%}N6?6opTwKVxgPg18jKRa?Oz2vRF9l!}YmO{M#ECQhR>xh2h1eJoc67e0Uw zCEnnoW^rzA@}2wh{qy++ppU&KB2;2jTd1Kfu>Y)9O6JODa`=|$g*Rjc>Q}}Ze=Shy zbf>|w74U|@L1(lvOuRET8+4~Jwgv7JDr7>bej?|oAm`{+s&u7nqEng7)xv9cb}pBc zm!Zp2j>!)#mD&7(tbYI3zIw}L1ll8G^|&l%+C8dS&M(agl`heDCM1m;WnWIiI5 Date: Sun, 27 Sep 2020 05:25:30 +0000 Subject: [PATCH 05/69] Bot: Prettified Java code! --- .../advanced/chapter14/practice/Average.java | 3 ++- .../advanced/chapter14/practice/Bank.java | 4 +--- .../advanced/chapter14/solutions/Average.java | 12 +++++++----- .../advanced/chapter14/solutions/Bank.java | 10 ++++------ 4 files changed, 14 insertions(+), 15 deletions(-) diff --git a/src/com/codefortomorrow/advanced/chapter14/practice/Average.java b/src/com/codefortomorrow/advanced/chapter14/practice/Average.java index d4611d5..8a4bad3 100644 --- a/src/com/codefortomorrow/advanced/chapter14/practice/Average.java +++ b/src/com/codefortomorrow/advanced/chapter14/practice/Average.java @@ -1,4 +1,5 @@ package com.codefortomorrow.advanced.chapter14.practice; + import java.io.*; /* @@ -24,8 +25,8 @@ and display the average on the next (101st) */ public class Average { - public static void main(String[] args) { + public static void main(String[] args) { // Add code here. } } diff --git a/src/com/codefortomorrow/advanced/chapter14/practice/Bank.java b/src/com/codefortomorrow/advanced/chapter14/practice/Bank.java index f63ecf8..12ddc90 100644 --- a/src/com/codefortomorrow/advanced/chapter14/practice/Bank.java +++ b/src/com/codefortomorrow/advanced/chapter14/practice/Bank.java @@ -27,11 +27,9 @@ balance upon initialization (through the constructor). */ public class Bank { - - public static void main(String[] args) { + public static void main(String[] args) { // Your code here. } } - // Add other classes here. diff --git a/src/com/codefortomorrow/advanced/chapter14/solutions/Average.java b/src/com/codefortomorrow/advanced/chapter14/solutions/Average.java index c0d555d..ea3e6ae 100644 --- a/src/com/codefortomorrow/advanced/chapter14/solutions/Average.java +++ b/src/com/codefortomorrow/advanced/chapter14/solutions/Average.java @@ -1,4 +1,5 @@ package com.codefortomorrow.advanced.chapter14.solutions; + import java.io.*; /* @@ -24,14 +25,15 @@ and display the average on the next (101st) */ public class Average { + public static void main(String[] args) { File file = new File("numbers.txt"); - - // try with resources block, which closes streams automatically - try(BufferedReader in = new BufferedReader( - new FileReader(file)); BufferedWriter out = new BufferedWriter( - new FileWriter(file, true))) { + // try with resources block, which closes streams automatically + try ( + BufferedReader in = new BufferedReader(new FileReader(file)); + BufferedWriter out = new BufferedWriter(new FileWriter(file, true)) + ) { // read the numbers (String input) and find average double average = 0; String contentLine = in.readLine(); diff --git a/src/com/codefortomorrow/advanced/chapter14/solutions/Bank.java b/src/com/codefortomorrow/advanced/chapter14/solutions/Bank.java index 4d980c4..5fec90d 100644 --- a/src/com/codefortomorrow/advanced/chapter14/solutions/Bank.java +++ b/src/com/codefortomorrow/advanced/chapter14/solutions/Bank.java @@ -27,7 +27,7 @@ balance upon initialization (through the constructor). */ public class Bank { - + public static void main(String[] args) { BankAccount account1 = new BankAccount(500); BankAccount account2 = new BankAccount(1000); @@ -51,7 +51,6 @@ public static void withdraw(BankAccount account, int amount) { } } - class BankAccount { private int balance; @@ -64,9 +63,9 @@ public void deposit(int amount) { } public void withdraw(int amount) throws NotEnoughMoneyException { - if(amount > balance) - throw new NotEnoughMoneyException( - "Bank balance is short $" + Math.abs(balance - amount)); + if (amount > balance) throw new NotEnoughMoneyException( + "Bank balance is short $" + Math.abs(balance - amount) + ); balance -= amount; } @@ -75,7 +74,6 @@ public int getBalance() { } } - class NotEnoughMoneyException extends Exception { public NotEnoughMoneyException() {} From deea919a6279b183ed6d07643367fb80a8c65ed3 Mon Sep 17 00:00:00 2001 From: JoshJ Date: Sun, 27 Sep 2020 12:06:10 -0400 Subject: [PATCH 06/69] Fix Semicolon --- .../codefortomorrow/advanced/chapter14/examples/FileIO.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/com/codefortomorrow/advanced/chapter14/examples/FileIO.java b/src/com/codefortomorrow/advanced/chapter14/examples/FileIO.java index 4dd61d1..83b65e3 100644 --- a/src/com/codefortomorrow/advanced/chapter14/examples/FileIO.java +++ b/src/com/codefortomorrow/advanced/chapter14/examples/FileIO.java @@ -1,4 +1,4 @@ -package com.codefortomorrow.advanced.chapter14.examples +package com.codefortomorrow.advanced.chapter14.examples; import java.io.*; public class FileIO { @@ -7,7 +7,7 @@ public static void main(String[] args) throws IOException { FileInputStream in = null; FileOutputStream out = null; File file = new File("names.txt"); - + try { in = new FileInputStream(file); out = new FileOutputStream("names_upper.txt"); From 9ec28624c309a7b2eb352c4e2e410ba967c4fe42 Mon Sep 17 00:00:00 2001 From: JJ27 Date: Sun, 27 Sep 2020 16:06:52 +0000 Subject: [PATCH 07/69] Bot: Prettified Java code! --- .../advanced/chapter14/examples/FileIO.java | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/src/com/codefortomorrow/advanced/chapter14/examples/FileIO.java b/src/com/codefortomorrow/advanced/chapter14/examples/FileIO.java index 83b65e3..bdf5379 100644 --- a/src/com/codefortomorrow/advanced/chapter14/examples/FileIO.java +++ b/src/com/codefortomorrow/advanced/chapter14/examples/FileIO.java @@ -1,4 +1,5 @@ package com.codefortomorrow.advanced.chapter14.examples; + import java.io.*; public class FileIO { @@ -12,17 +13,15 @@ public static void main(String[] args) throws IOException { in = new FileInputStream(file); out = new FileOutputStream("names_upper.txt"); int c; - while ((c = in.read()) != -1) - out.write(Character.toUpperCase((char)c)); - + while ((c = in.read()) != -1) out.write( + Character.toUpperCase((char) c) + ); } catch (IOException e) { System.out.println("An IO Exception occurred."); - e.printStackTrace(); // Prints error output stream. + e.printStackTrace(); // Prints error output stream. } finally { - if (in != null) - in.close(); - if (out != null) - out.close(); + if (in != null) in.close(); + if (out != null) out.close(); } } } From 61d313ee76e784a8871839ff0453924168ad955e Mon Sep 17 00:00:00 2001 From: JoshJ Date: Sun, 27 Sep 2020 12:09:40 -0400 Subject: [PATCH 08/69] Fix one more semicolon --- .../advanced/chapter14/examples/TryWithResources.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/com/codefortomorrow/advanced/chapter14/examples/TryWithResources.java b/src/com/codefortomorrow/advanced/chapter14/examples/TryWithResources.java index 9bf56c8..98a30db 100644 --- a/src/com/codefortomorrow/advanced/chapter14/examples/TryWithResources.java +++ b/src/com/codefortomorrow/advanced/chapter14/examples/TryWithResources.java @@ -1,11 +1,11 @@ -package com.codefortomorrow.advanced.chapter14.examples +package com.codefortomorrow.advanced.chapter14.examples; import java.io.*; public class TryWithResources { public static void main(String[] args) throws IOException { File file = new File("names.txt"); - + try (FileInputStream in = new FileInputStream(file); FileOutputStream out = new FileOutputStream("names_upper.txt")) { From d7220b0cd04012f195ca8900be34328949c47fcf Mon Sep 17 00:00:00 2001 From: JJ27 Date: Sun, 27 Sep 2020 16:10:27 +0000 Subject: [PATCH 09/69] Bot: Prettified Java code! --- .../chapter14/examples/TryWithResources.java | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/src/com/codefortomorrow/advanced/chapter14/examples/TryWithResources.java b/src/com/codefortomorrow/advanced/chapter14/examples/TryWithResources.java index 98a30db..d58d994 100644 --- a/src/com/codefortomorrow/advanced/chapter14/examples/TryWithResources.java +++ b/src/com/codefortomorrow/advanced/chapter14/examples/TryWithResources.java @@ -1,4 +1,5 @@ package com.codefortomorrow.advanced.chapter14.examples; + import java.io.*; public class TryWithResources { @@ -6,16 +7,17 @@ public class TryWithResources { public static void main(String[] args) throws IOException { File file = new File("names.txt"); - try (FileInputStream in = new FileInputStream(file); - FileOutputStream out = new FileOutputStream("names_upper.txt")) { - + try ( + FileInputStream in = new FileInputStream(file); + FileOutputStream out = new FileOutputStream("names_upper.txt") + ) { int c; - while ((c = in.read()) != -1) - out.write(Character.toUpperCase((char)c)); - + while ((c = in.read()) != -1) out.write( + Character.toUpperCase((char) c) + ); } catch (IOException e) { System.out.println("An IO Exception occurred."); - e.printStackTrace(); // Prints error output stream. + e.printStackTrace(); // Prints error output stream. } } } From 264b21353e2e6cf82c246dec0d5c44c0032490b6 Mon Sep 17 00:00:00 2001 From: JoshJ Date: Sun, 27 Sep 2020 12:44:55 -0400 Subject: [PATCH 10/69] Add Lexico Problem --- .../advanced/chapter14/practice/Lexico.java | 21 +++++++++ .../advanced/chapter14/practice/f1.txt | 28 ++++++++++++ .../advanced/chapter14/practice/f2.txt | 28 ++++++++++++ .../advanced/chapter14/solutions/Lexico.class | Bin 0 -> 1018 bytes .../advanced/chapter14/solutions/Lexico.java | 41 ++++++++++++++++++ .../advanced/chapter14/solutions/f1.txt | 28 ++++++++++++ .../advanced/chapter14/solutions/f2.txt | 28 ++++++++++++ 7 files changed, 174 insertions(+) create mode 100644 src/com/codefortomorrow/advanced/chapter14/practice/Lexico.java create mode 100644 src/com/codefortomorrow/advanced/chapter14/practice/f1.txt create mode 100644 src/com/codefortomorrow/advanced/chapter14/practice/f2.txt create mode 100644 src/com/codefortomorrow/advanced/chapter14/solutions/Lexico.class create mode 100644 src/com/codefortomorrow/advanced/chapter14/solutions/Lexico.java create mode 100644 src/com/codefortomorrow/advanced/chapter14/solutions/f1.txt create mode 100644 src/com/codefortomorrow/advanced/chapter14/solutions/f2.txt diff --git a/src/com/codefortomorrow/advanced/chapter14/practice/Lexico.java b/src/com/codefortomorrow/advanced/chapter14/practice/Lexico.java new file mode 100644 index 0000000..a98c415 --- /dev/null +++ b/src/com/codefortomorrow/advanced/chapter14/practice/Lexico.java @@ -0,0 +1,21 @@ +package com.codefortomorrow.advanced.chapter14.practice; + +import java.io.*; + +/* +Practice: Use File I/O to compare two files lexicographically. +Lexicographical order is very similar to alphabetical order, +except that it includes more than just lowercase alphabets. +Given two text files f1 and f2, write a program that will +compare their contents and output to the console the +title of the file which comes first lexicographically. + +Hint: Java’s String.compareTo method may be of use to you. +*/ + +public class Lexico{ + + public static void main(String[] args){ + // Add code here + } +} diff --git a/src/com/codefortomorrow/advanced/chapter14/practice/f1.txt b/src/com/codefortomorrow/advanced/chapter14/practice/f1.txt new file mode 100644 index 0000000..684900a --- /dev/null +++ b/src/com/codefortomorrow/advanced/chapter14/practice/f1.txt @@ -0,0 +1,28 @@ +All the world's a stage, +And all the men and women merely players; +They have their exits and their entrances, +And one man in his time plays many parts, +His acts being seven ages. At first, the infant, +Mewling and puking in the nurse's arms. +Then the whining schoolboy, with his satchel +And shining morning face, creeping like snail +Unwillingly to school. And then the lover, +Sighing like furnace, with a woeful ballad +Made to his mistress' eyebrow. Then a soldier, +Full of strange oaths and bearded like the pard, +Jealous in honor, sudden and quick in quarrel, +Seeking the bubble reputation +Even in the cannon's mouth. And then the justice, +In fair round belly with good capon lined, +With eyes severe and beard of formal cut, +Full of wise saws and modern instances; +And so he plays his part. The sixth age shifts +Into the lean and slippered pantaloon, +With spectacles on nose and pouch on side; +His youthful hose, well saved, a world too wide +For his shrunk shank, and his big manly voice, +Turning again toward childish treble, pipes +And whistles in his sound. Last scene of all, +That ends this strange eventful history, +Is second childishness and mere oblivion, +Sans teeth, sans eyes, sans taste, sans everything. diff --git a/src/com/codefortomorrow/advanced/chapter14/practice/f2.txt b/src/com/codefortomorrow/advanced/chapter14/practice/f2.txt new file mode 100644 index 0000000..31cf35c --- /dev/null +++ b/src/com/codefortomorrow/advanced/chapter14/practice/f2.txt @@ -0,0 +1,28 @@ +All the world's a stage, +And all the men and women merely players; +They have their exits and their entrances, +And one man in his time plays many parts, +His acts being seven ages. At first, the infant, +Mewling and puking in the nurse's arms. +Then the whining schoolboy, with his satchel +And shining morning face, creeping like snail +Unwillingly to school. And then the lover, +Sighing like furnace, with a woeful ballad +Made to his mistress' eyebrow. Then a soldier, +Full of strange oaths and bearded like the pard, +Jealous in honor, sudden and quick in quarrel, +Seeking the bubble reputation +Even in the cannon's mouth. And than the justice, +In fair round belly with good capon lined, +With eyes severe and beard of formal cut, +Full of wise saws and modern instances; +And so he plays his part. The sixth age shifts +Into the lean and slippered pantaloon, +With spectacles on nose and pouch on side; +His youthful hose, well saved, a world too wide +For his shrunk shank, and his big manly voice, +Turning again toward childish treble, pipes +And whistles in his sound. Last scene of all, +That ends this strange eventful history, +Is second childishness and mere oblivion, +Sans teeth, sans eyes, sans taste, sans everything. diff --git a/src/com/codefortomorrow/advanced/chapter14/solutions/Lexico.class b/src/com/codefortomorrow/advanced/chapter14/solutions/Lexico.class new file mode 100644 index 0000000000000000000000000000000000000000..5c15d6b63864c5e53fed1a00e7979fa22b8c3a7c GIT binary patch literal 1018 zcmZ`%T~8B16g^Y6o$a!G*iy9R`@03{T11c!d4OnwDPW`_AwCV;DK56V&9X)OD?a$j zGd`3=5`Tcd#Gl}kcxNjr0XLi3yLZk#_sp65?dO-T0G6?4Ac8gy%|sM2hVCPABCM)# z53QYpBWVW=u~o-)f;EP=Y;KQ3XXp({Mlg-^`HjqIdN56>y4Bj|st2IO1_G)$E%aZpWZJTinV}xVW#2ChJa(p-r zWQ{!X8f00j)t+R(qEiLbK#JOmj!9Ckd>NiIkVTGT+Qf{qB6;oFKX?XSSJhQl$Lw`s zfy4R-zPa=K)Ry(Y@mvFW6im#iTkHx8cLQO+EBV5f9P=UFmD}u)rKNGwufk=m;fjiF%0VSRv_mKpBi(ui@KLdBf1&>YG=dU<8k8Le&!iQ!`5I6Z#vVFQadC z`U32n0)_=z)i%&gQ?`hw6cdz1!ZM)2ifro=eE$;h{Y3i(Ixf&%{F6ww5?eD(q_!W@ zvaHRfwI92=&dXb{}okW@jA%g}y86lIJP&lE9DpiUJ5jnEP? ijUjeLL5yRWgNJX1BSSu9x_*GKbL2G=u}aVgp8W!Z&EU)c literal 0 HcmV?d00001 diff --git a/src/com/codefortomorrow/advanced/chapter14/solutions/Lexico.java b/src/com/codefortomorrow/advanced/chapter14/solutions/Lexico.java new file mode 100644 index 0000000..1131b42 --- /dev/null +++ b/src/com/codefortomorrow/advanced/chapter14/solutions/Lexico.java @@ -0,0 +1,41 @@ +package com.codefortomorrow.advanced.chapter14.solutions; + +import java.io.*; + +/* +Practice: Use File I/O to compare two files lexicographically. +Lexicographical order is very similar to alphabetical order, +except that it includes more than just lowercase alphabets. +Given two text files f1 and f2, write a program that will +compare their contents and output to the console the +title of the file which comes first lexicographically. + +Hint: Java’s String.compareTo method may be of use to you. +*/ + +public class Lexico{ + + public static void main(String[] args){ + BufferedReader br = null; + BufferedReader br2 = null; + try{ + br = new BufferedReader(new FileReader("f1.txt")); + br2 = new BufferedReader(new FileReader("f2.txt")); + + String line1 = br.readLine(); + String line2 = br2.readLine(); + int c = line1.compareTo(line2); + while (c == 0){ + line1 = br.readLine(); + line2 = br2.readLine(); + c = line1.compareTo(line2); + } + if(c > 0) + System.out.println("File 2"); + else + System.out.println("File 1"); + } catch(IOException e){ + e.printStackTrace(); + } + } +} diff --git a/src/com/codefortomorrow/advanced/chapter14/solutions/f1.txt b/src/com/codefortomorrow/advanced/chapter14/solutions/f1.txt new file mode 100644 index 0000000..684900a --- /dev/null +++ b/src/com/codefortomorrow/advanced/chapter14/solutions/f1.txt @@ -0,0 +1,28 @@ +All the world's a stage, +And all the men and women merely players; +They have their exits and their entrances, +And one man in his time plays many parts, +His acts being seven ages. At first, the infant, +Mewling and puking in the nurse's arms. +Then the whining schoolboy, with his satchel +And shining morning face, creeping like snail +Unwillingly to school. And then the lover, +Sighing like furnace, with a woeful ballad +Made to his mistress' eyebrow. Then a soldier, +Full of strange oaths and bearded like the pard, +Jealous in honor, sudden and quick in quarrel, +Seeking the bubble reputation +Even in the cannon's mouth. And then the justice, +In fair round belly with good capon lined, +With eyes severe and beard of formal cut, +Full of wise saws and modern instances; +And so he plays his part. The sixth age shifts +Into the lean and slippered pantaloon, +With spectacles on nose and pouch on side; +His youthful hose, well saved, a world too wide +For his shrunk shank, and his big manly voice, +Turning again toward childish treble, pipes +And whistles in his sound. Last scene of all, +That ends this strange eventful history, +Is second childishness and mere oblivion, +Sans teeth, sans eyes, sans taste, sans everything. diff --git a/src/com/codefortomorrow/advanced/chapter14/solutions/f2.txt b/src/com/codefortomorrow/advanced/chapter14/solutions/f2.txt new file mode 100644 index 0000000..31cf35c --- /dev/null +++ b/src/com/codefortomorrow/advanced/chapter14/solutions/f2.txt @@ -0,0 +1,28 @@ +All the world's a stage, +And all the men and women merely players; +They have their exits and their entrances, +And one man in his time plays many parts, +His acts being seven ages. At first, the infant, +Mewling and puking in the nurse's arms. +Then the whining schoolboy, with his satchel +And shining morning face, creeping like snail +Unwillingly to school. And then the lover, +Sighing like furnace, with a woeful ballad +Made to his mistress' eyebrow. Then a soldier, +Full of strange oaths and bearded like the pard, +Jealous in honor, sudden and quick in quarrel, +Seeking the bubble reputation +Even in the cannon's mouth. And than the justice, +In fair round belly with good capon lined, +With eyes severe and beard of formal cut, +Full of wise saws and modern instances; +And so he plays his part. The sixth age shifts +Into the lean and slippered pantaloon, +With spectacles on nose and pouch on side; +His youthful hose, well saved, a world too wide +For his shrunk shank, and his big manly voice, +Turning again toward childish treble, pipes +And whistles in his sound. Last scene of all, +That ends this strange eventful history, +Is second childishness and mere oblivion, +Sans teeth, sans eyes, sans taste, sans everything. From 1a4f2365ac8b2502c399e2f8a872d385c4bb8075 Mon Sep 17 00:00:00 2001 From: JoshJ Date: Sun, 27 Sep 2020 14:32:11 -0400 Subject: [PATCH 11/69] Remove extra class file --- .../advanced/chapter14/solutions/Lexico.class | Bin 1018 -> 0 bytes 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 src/com/codefortomorrow/advanced/chapter14/solutions/Lexico.class diff --git a/src/com/codefortomorrow/advanced/chapter14/solutions/Lexico.class b/src/com/codefortomorrow/advanced/chapter14/solutions/Lexico.class deleted file mode 100644 index 5c15d6b63864c5e53fed1a00e7979fa22b8c3a7c..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 1018 zcmZ`%T~8B16g^Y6o$a!G*iy9R`@03{T11c!d4OnwDPW`_AwCV;DK56V&9X)OD?a$j zGd`3=5`Tcd#Gl}kcxNjr0XLi3yLZk#_sp65?dO-T0G6?4Ac8gy%|sM2hVCPABCM)# z53QYpBWVW=u~o-)f;EP=Y;KQ3XXp({Mlg-^`HjqIdN56>y4Bj|st2IO1_G)$E%aZpWZJTinV}xVW#2ChJa(p-r zWQ{!X8f00j)t+R(qEiLbK#JOmj!9Ckd>NiIkVTGT+Qf{qB6;oFKX?XSSJhQl$Lw`s zfy4R-zPa=K)Ry(Y@mvFW6im#iTkHx8cLQO+EBV5f9P=UFmD}u)rKNGwufk=m;fjiF%0VSRv_mKpBi(ui@KLdBf1&>YG=dU<8k8Le&!iQ!`5I6Z#vVFQadC z`U32n0)_=z)i%&gQ?`hw6cdz1!ZM)2ifro=eE$;h{Y3i(Ixf&%{F6ww5?eD(q_!W@ zvaHRfwI92=&dXb{}okW@jA%g}y86lIJP&lE9DpiUJ5jnEP? ijUjeLL5yRWgNJX1BSSu9x_*GKbL2G=u}aVgp8W!Z&EU)c From 1df26ee4950cff0dbf24ab35fd0451d45ddc34fa Mon Sep 17 00:00:00 2001 From: JoshJ Date: Sun, 27 Sep 2020 16:28:11 -0400 Subject: [PATCH 12/69] Measurable Problem --- .../chapter15/practice/MeasurableProblem.java | 24 ++++++ .../solutions/MeasurableProblem.java | 77 +++++++++++++++++++ 2 files changed, 101 insertions(+) create mode 100644 src/com/codefortomorrow/advanced/chapter15/practice/MeasurableProblem.java create mode 100644 src/com/codefortomorrow/advanced/chapter15/solutions/MeasurableProblem.java diff --git a/src/com/codefortomorrow/advanced/chapter15/practice/MeasurableProblem.java b/src/com/codefortomorrow/advanced/chapter15/practice/MeasurableProblem.java new file mode 100644 index 0000000..4277ada --- /dev/null +++ b/src/com/codefortomorrow/advanced/chapter15/practice/MeasurableProblem.java @@ -0,0 +1,24 @@ +package com.codefortomorrow.advanced.chapter15.practice; +/* +Create a Measurable interface +that is used to “measure” anything. Add necessary methods. +Required Methods: double average(Measurable[] items), +largest(Measurable[] items) or smallest(Measurable[] items). +Create a Student class that implements Measurable and +it’s methods(you can compare whatever you measure, i.e. GPA). +Note: for the largest/smallest methods don’t worry +about the case if they are equal. + +Problem adapted from Java SE 9 Textbook +*/ +public class MeasurableProblem{ + public static void main(String[] args){ + //Add testing code + } +} +interface Measurable{ + //add interface code here +} +class Student implements Measurable{ + //Add student code here +} diff --git a/src/com/codefortomorrow/advanced/chapter15/solutions/MeasurableProblem.java b/src/com/codefortomorrow/advanced/chapter15/solutions/MeasurableProblem.java new file mode 100644 index 0000000..388329a --- /dev/null +++ b/src/com/codefortomorrow/advanced/chapter15/solutions/MeasurableProblem.java @@ -0,0 +1,77 @@ +package com.codefortomorrow.advanced.chapter15.solutions; +import java.util.Arrays; +/* +Create a Measurable interface +that is used to “measure” anything. Add necessary methods. +Required Methods: double average(Measurable[] items), +largest(Measurable[] items) or smallest(Measurable[] items). +Create a Student class that implements Measurable and +it’s methods(you can compare whatever you measure, i.e. GPA). +Note: for the largest/smallest methods don’t worry +about the case if they are equal. + +Problem adapted from Java SE 9 Textbook +*/ +public class MeasurableProblem{ + public static void main(String[] args){ + Object[] students = { + new Student("John", 2.7), + new Student("Josh", 1.2), + new Student("Jacob", 3.4), + new Student("Rebecca", 3.5), + new Student("Arnav", 4.0) + }; + Student test = new Student(); + System.out.println("Average: " + test.average(students)); + System.out.println("Largest: " + test.largest(students)); + System.out.println("Smallest: " + test.smallest(students)); + } +} +interface Measurable{ + abstract double average(Object[] students); + abstract String largest(Object[] students); + abstract String smallest(Object[] students); +} +class Student implements Measurable{ + private double GPA; + private String name; + public Student(String name, double GPA){ + this.name = name; + this.GPA = GPA; + } + public Student(){ + name = ""; + GPA = 0.0; + } + public double average(Object[] orig){ + Student[] students = Arrays.copyOf(orig, orig.length, Student[].class); //Casting + double sum = 0; + for(Student s: students) + sum += s.getGPA(); + return sum / students.length; + } + public String largest(Object[] orig){ + Student[] students = Arrays.copyOf(orig, orig.length, Student[].class); //Casting + Student curr = students[0]; + for(Student s: students){ + if(s.getGPA() > curr.getGPA()) + curr = s; + } + return curr.getName(); + } + public String smallest(Object[] orig){ + Student[] students = Arrays.copyOf(orig, orig.length, Student[].class); //Casting + Student curr = students[0]; + for(Student s: students){ + if(s.getGPA() < curr.getGPA()) + curr = s; + } + return curr.getName(); + } + public double getGPA(){ + return GPA; + } + public String getName(){ + return name; + } +} From 7e8c2fe0ebe2b7a31da0266f78e9388b466656c3 Mon Sep 17 00:00:00 2001 From: JJ27 Date: Sun, 27 Sep 2020 20:30:08 +0000 Subject: [PATCH 13/69] Bot: Prettified Java code! --- .../advanced/chapter14/practice/Lexico.java | 4 +- .../advanced/chapter14/solutions/Lexico.java | 45 +++---- .../chapter15/practice/MeasurableProblem.java | 20 +-- .../solutions/MeasurableProblem.java | 127 ++++++++++-------- 4 files changed, 105 insertions(+), 91 deletions(-) diff --git a/src/com/codefortomorrow/advanced/chapter14/practice/Lexico.java b/src/com/codefortomorrow/advanced/chapter14/practice/Lexico.java index a98c415..67914b1 100644 --- a/src/com/codefortomorrow/advanced/chapter14/practice/Lexico.java +++ b/src/com/codefortomorrow/advanced/chapter14/practice/Lexico.java @@ -13,9 +13,9 @@ Hint: Java’s String.compareTo method may be of use to you. */ -public class Lexico{ +public class Lexico { - public static void main(String[] args){ + public static void main(String[] args) { // Add code here } } diff --git a/src/com/codefortomorrow/advanced/chapter14/solutions/Lexico.java b/src/com/codefortomorrow/advanced/chapter14/solutions/Lexico.java index 1131b42..2f20545 100644 --- a/src/com/codefortomorrow/advanced/chapter14/solutions/Lexico.java +++ b/src/com/codefortomorrow/advanced/chapter14/solutions/Lexico.java @@ -13,29 +13,28 @@ Hint: Java’s String.compareTo method may be of use to you. */ -public class Lexico{ +public class Lexico { - public static void main(String[] args){ - BufferedReader br = null; - BufferedReader br2 = null; - try{ - br = new BufferedReader(new FileReader("f1.txt")); - br2 = new BufferedReader(new FileReader("f2.txt")); + public static void main(String[] args) { + BufferedReader br = null; + BufferedReader br2 = null; + try { + br = new BufferedReader(new FileReader("f1.txt")); + br2 = new BufferedReader(new FileReader("f2.txt")); - String line1 = br.readLine(); - String line2 = br2.readLine(); - int c = line1.compareTo(line2); - while (c == 0){ - line1 = br.readLine(); - line2 = br2.readLine(); - c = line1.compareTo(line2); - } - if(c > 0) - System.out.println("File 2"); - else - System.out.println("File 1"); - } catch(IOException e){ - e.printStackTrace(); - } - } + String line1 = br.readLine(); + String line2 = br2.readLine(); + int c = line1.compareTo(line2); + while (c == 0) { + line1 = br.readLine(); + line2 = br2.readLine(); + c = line1.compareTo(line2); + } + if (c > 0) System.out.println("File 2"); else System.out.println( + "File 1" + ); + } catch (IOException e) { + e.printStackTrace(); + } + } } diff --git a/src/com/codefortomorrow/advanced/chapter15/practice/MeasurableProblem.java b/src/com/codefortomorrow/advanced/chapter15/practice/MeasurableProblem.java index 4277ada..03c5f00 100644 --- a/src/com/codefortomorrow/advanced/chapter15/practice/MeasurableProblem.java +++ b/src/com/codefortomorrow/advanced/chapter15/practice/MeasurableProblem.java @@ -1,4 +1,5 @@ package com.codefortomorrow.advanced.chapter15.practice; + /* Create a Measurable interface that is used to “measure” anything. Add necessary methods. @@ -11,14 +12,17 @@ Problem adapted from Java SE 9 Textbook */ -public class MeasurableProblem{ - public static void main(String[] args){ - //Add testing code - } +public class MeasurableProblem { + + public static void main(String[] args) { + //Add testing code + } } -interface Measurable{ - //add interface code here + +interface Measurable { + //add interface code here } -class Student implements Measurable{ - //Add student code here + +class Student implements Measurable { + //Add student code here } diff --git a/src/com/codefortomorrow/advanced/chapter15/solutions/MeasurableProblem.java b/src/com/codefortomorrow/advanced/chapter15/solutions/MeasurableProblem.java index 388329a..54a4b67 100644 --- a/src/com/codefortomorrow/advanced/chapter15/solutions/MeasurableProblem.java +++ b/src/com/codefortomorrow/advanced/chapter15/solutions/MeasurableProblem.java @@ -1,5 +1,7 @@ package com.codefortomorrow.advanced.chapter15.solutions; + import java.util.Arrays; + /* Create a Measurable interface that is used to “measure” anything. Add necessary methods. @@ -12,66 +14,75 @@ Problem adapted from Java SE 9 Textbook */ -public class MeasurableProblem{ - public static void main(String[] args){ - Object[] students = { - new Student("John", 2.7), - new Student("Josh", 1.2), - new Student("Jacob", 3.4), - new Student("Rebecca", 3.5), - new Student("Arnav", 4.0) - }; - Student test = new Student(); - System.out.println("Average: " + test.average(students)); - System.out.println("Largest: " + test.largest(students)); - System.out.println("Smallest: " + test.smallest(students)); - } +public class MeasurableProblem { + + public static void main(String[] args) { + Object[] students = { + new Student("John", 2.7), + new Student("Josh", 1.2), + new Student("Jacob", 3.4), + new Student("Rebecca", 3.5), + new Student("Arnav", 4.0), + }; + Student test = new Student(); + System.out.println("Average: " + test.average(students)); + System.out.println("Largest: " + test.largest(students)); + System.out.println("Smallest: " + test.smallest(students)); + } } -interface Measurable{ - abstract double average(Object[] students); - abstract String largest(Object[] students); - abstract String smallest(Object[] students); + +interface Measurable { + abstract double average(Object[] students); + + abstract String largest(Object[] students); + + abstract String smallest(Object[] students); } -class Student implements Measurable{ - private double GPA; - private String name; - public Student(String name, double GPA){ - this.name = name; - this.GPA = GPA; - } - public Student(){ - name = ""; - GPA = 0.0; - } - public double average(Object[] orig){ - Student[] students = Arrays.copyOf(orig, orig.length, Student[].class); //Casting - double sum = 0; - for(Student s: students) - sum += s.getGPA(); - return sum / students.length; - } - public String largest(Object[] orig){ - Student[] students = Arrays.copyOf(orig, orig.length, Student[].class); //Casting - Student curr = students[0]; - for(Student s: students){ - if(s.getGPA() > curr.getGPA()) - curr = s; + +class Student implements Measurable { + private double GPA; + private String name; + + public Student(String name, double GPA) { + this.name = name; + this.GPA = GPA; + } + + public Student() { + name = ""; + GPA = 0.0; + } + + public double average(Object[] orig) { + Student[] students = Arrays.copyOf(orig, orig.length, Student[].class); //Casting + double sum = 0; + for (Student s : students) sum += s.getGPA(); + return sum / students.length; + } + + public String largest(Object[] orig) { + Student[] students = Arrays.copyOf(orig, orig.length, Student[].class); //Casting + Student curr = students[0]; + for (Student s : students) { + if (s.getGPA() > curr.getGPA()) curr = s; + } + return curr.getName(); } - return curr.getName(); - } - public String smallest(Object[] orig){ - Student[] students = Arrays.copyOf(orig, orig.length, Student[].class); //Casting - Student curr = students[0]; - for(Student s: students){ - if(s.getGPA() < curr.getGPA()) - curr = s; + + public String smallest(Object[] orig) { + Student[] students = Arrays.copyOf(orig, orig.length, Student[].class); //Casting + Student curr = students[0]; + for (Student s : students) { + if (s.getGPA() < curr.getGPA()) curr = s; + } + return curr.getName(); + } + + public double getGPA() { + return GPA; + } + + public String getName() { + return name; } - return curr.getName(); - } - public double getGPA(){ - return GPA; - } - public String getName(){ - return name; - } } From 2d5ec4d34a8fc557490cda5eb6b9fb937a926468 Mon Sep 17 00:00:00 2001 From: JoshJ Date: Sun, 27 Sep 2020 16:31:23 -0400 Subject: [PATCH 14/69] Close Readers --- .../codefortomorrow/advanced/chapter14/solutions/Lexico.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/com/codefortomorrow/advanced/chapter14/solutions/Lexico.java b/src/com/codefortomorrow/advanced/chapter14/solutions/Lexico.java index 1131b42..cab01af 100644 --- a/src/com/codefortomorrow/advanced/chapter14/solutions/Lexico.java +++ b/src/com/codefortomorrow/advanced/chapter14/solutions/Lexico.java @@ -36,6 +36,9 @@ public static void main(String[] args){ System.out.println("File 1"); } catch(IOException e){ e.printStackTrace(); + } finally{ + br.close(); + br2.close(); } } } From e4125f668c73de9a9222c575fd310a1f6bc5b958 Mon Sep 17 00:00:00 2001 From: JoshJ Date: Sun, 27 Sep 2020 16:35:28 -0400 Subject: [PATCH 15/69] Add Try With Resources --- .../advanced/chapter14/solutions/Lexico.java | 49 +++++-------------- 1 file changed, 12 insertions(+), 37 deletions(-) diff --git a/src/com/codefortomorrow/advanced/chapter14/solutions/Lexico.java b/src/com/codefortomorrow/advanced/chapter14/solutions/Lexico.java index 7438ca1..fb8ca13 100644 --- a/src/com/codefortomorrow/advanced/chapter14/solutions/Lexico.java +++ b/src/com/codefortomorrow/advanced/chapter14/solutions/Lexico.java @@ -16,47 +16,22 @@ public class Lexico { public static void main(String[] args) { - BufferedReader br = null; - BufferedReader br2 = null; - try { - br = new BufferedReader(new FileReader("f1.txt")); - br2 = new BufferedReader(new FileReader("f2.txt")); - -<<<<<<< HEAD - String line1 = br.readLine(); - String line2 = br2.readLine(); - int c = line1.compareTo(line2); - while (c == 0){ - line1 = br.readLine(); - line2 = br2.readLine(); - c = line1.compareTo(line2); - } - if(c > 0) - System.out.println("File 2"); - else - System.out.println("File 1"); - } catch(IOException e){ - e.printStackTrace(); - } finally{ - br.close(); - br2.close(); - } - } -======= + try(BufferedReader br = new BufferedReader(new FileReader("f1.txt")); + BufferedReader br2 = new BufferedReader(new FileReader("f2.txt"));) { String line1 = br.readLine(); String line2 = br2.readLine(); int c = line1.compareTo(line2); - while (c == 0) { - line1 = br.readLine(); - line2 = br2.readLine(); - c = line1.compareTo(line2); + while (c == 0){ + line1 = br.readLine(); + line2 = br2.readLine(); + c = line1.compareTo(line2); } - if (c > 0) System.out.println("File 2"); else System.out.println( - "File 1" - ); - } catch (IOException e) { - e.printStackTrace(); + if(c > 0) + System.out.println("File 2"); + else + System.out.println("File 1"); + } catch(IOException e){ + e.printStackTrace(); } } ->>>>>>> 7e8c2fe0ebe2b7a31da0266f78e9388b466656c3 } From 4b404bdde19ed21ee0cb9fabfdcb24ce3b3c5bee Mon Sep 17 00:00:00 2001 From: JJ27 Date: Sun, 27 Sep 2020 20:36:02 +0000 Subject: [PATCH 16/69] Bot: Prettified Java code! --- .../advanced/chapter14/solutions/Lexico.java | 25 ++++++++++--------- 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/src/com/codefortomorrow/advanced/chapter14/solutions/Lexico.java b/src/com/codefortomorrow/advanced/chapter14/solutions/Lexico.java index fb8ca13..0800c8a 100644 --- a/src/com/codefortomorrow/advanced/chapter14/solutions/Lexico.java +++ b/src/com/codefortomorrow/advanced/chapter14/solutions/Lexico.java @@ -16,22 +16,23 @@ public class Lexico { public static void main(String[] args) { - try(BufferedReader br = new BufferedReader(new FileReader("f1.txt")); - BufferedReader br2 = new BufferedReader(new FileReader("f2.txt"));) { + try ( + BufferedReader br = new BufferedReader(new FileReader("f1.txt")); + BufferedReader br2 = new BufferedReader(new FileReader("f2.txt")); + ) { String line1 = br.readLine(); String line2 = br2.readLine(); int c = line1.compareTo(line2); - while (c == 0){ - line1 = br.readLine(); - line2 = br2.readLine(); - c = line1.compareTo(line2); + while (c == 0) { + line1 = br.readLine(); + line2 = br2.readLine(); + c = line1.compareTo(line2); } - if(c > 0) - System.out.println("File 2"); - else - System.out.println("File 1"); - } catch(IOException e){ - e.printStackTrace(); + if (c > 0) System.out.println("File 2"); else System.out.println( + "File 1" + ); + } catch (IOException e) { + e.printStackTrace(); } } } From e1dc09ffc1a2d8c33a3a90c5786bb10bd3aa295b Mon Sep 17 00:00:00 2001 From: neha-peddinti <56367930+neha-peddinti@users.noreply.github.com> Date: Sat, 7 Nov 2020 14:54:42 -0500 Subject: [PATCH 17/69] Ch. 16 practice problems --- .../advanced/chapter16/practice/Access.java | 12 ++ .../advanced/chapter16/practice/Names.java | 18 +++ .../practice/SinglyLinkedListInsert.java | 31 +++++ .../advanced/chapter16/practice/Sort.java | 8 ++ .../advanced/chapter16/solutions/Access.java | 42 +++++++ .../advanced/chapter16/solutions/Names.java | 40 +++++++ .../solutions/SinglyLinkedListInsert.java | 112 ++++++++++++++++++ .../advanced/chapter16/solutions/Sort.java | 41 +++++++ 8 files changed, 304 insertions(+) create mode 100644 src/com/codefortomorrow/advanced/chapter16/practice/Access.java create mode 100644 src/com/codefortomorrow/advanced/chapter16/practice/Names.java create mode 100644 src/com/codefortomorrow/advanced/chapter16/practice/SinglyLinkedListInsert.java create mode 100644 src/com/codefortomorrow/advanced/chapter16/practice/Sort.java create mode 100644 src/com/codefortomorrow/advanced/chapter16/solutions/Access.java create mode 100644 src/com/codefortomorrow/advanced/chapter16/solutions/Names.java create mode 100644 src/com/codefortomorrow/advanced/chapter16/solutions/SinglyLinkedListInsert.java create mode 100644 src/com/codefortomorrow/advanced/chapter16/solutions/Sort.java diff --git a/src/com/codefortomorrow/advanced/chapter16/practice/Access.java b/src/com/codefortomorrow/advanced/chapter16/practice/Access.java new file mode 100644 index 0000000..f4692a0 --- /dev/null +++ b/src/com/codefortomorrow/advanced/chapter16/practice/Access.java @@ -0,0 +1,12 @@ +package com.codefortomorrow.advanced.chapter16.practice; + +/* +Using the methods outlined in the “Benefits of LinkedLists” section, +fill both a LinkedList and an ArrayList with 10,000 elements. +Then, find the time it takes to access the last element of each list. + +Determine which type of list can access elements more efficiently. + +Hint: You can use the get(int position) method +to access the element at a given position of a list. +*/ diff --git a/src/com/codefortomorrow/advanced/chapter16/practice/Names.java b/src/com/codefortomorrow/advanced/chapter16/practice/Names.java new file mode 100644 index 0000000..3b0f49b --- /dev/null +++ b/src/com/codefortomorrow/advanced/chapter16/practice/Names.java @@ -0,0 +1,18 @@ +package com.codefortomorrow.advanced.chapter16.practice; + +/* +Create a LinkedList of Strings, and ask the user to +input 10 names one by one into the list. + +Output this list. + +Remove each name that has 5 or fewer letters, and output the new list. + +Add the word “Apple” to the beginning of the list. + +Add the word “Peanut” to the second-to-last position of the list. + +(Hint: Make sure to take advantage of LinkedList methods!) + +Output the new list. +*/ diff --git a/src/com/codefortomorrow/advanced/chapter16/practice/SinglyLinkedListInsert.java b/src/com/codefortomorrow/advanced/chapter16/practice/SinglyLinkedListInsert.java new file mode 100644 index 0000000..0958a4e --- /dev/null +++ b/src/com/codefortomorrow/advanced/chapter16/practice/SinglyLinkedListInsert.java @@ -0,0 +1,31 @@ +package com.codefortomorrow.advanced.chapter16.practice; + +/* +Adding an insert method to a SinglyLinkedList +(details in SinglyLinkedList DIY section). + +Hint: It might be necessary to have a separate case for +inserting at position 0, because that’s when you want to +insert BEFORE the head. For all other positions (1, 2, etc.), +you can simply traverse down the list, keeping track of the +current node, and when you reach the position you want, +add the node AFTER the current node. + +Alternatively, you can always add BEFORE the current node +for all position cases (insert before the head to place the +new node at pos 0, insert before node 1 to place the new +node at pos 1, etc.), but then your exception case would +be when the user wants to insert a node at the very end of the list. +In this case, you’d have to write separate code to insert AFTER +the last node. + +Make sure you have a toString() method defined! + +The following code should output “{2, 3, 4, 7}”: +SinglyLinkedList lst = new SinglyLinkedList(); +lst.add(3); +lst.add(7); +lst.insert(4, 1); +lst.insert(2, 0); +System.out.println(lst); +*/ diff --git a/src/com/codefortomorrow/advanced/chapter16/practice/Sort.java b/src/com/codefortomorrow/advanced/chapter16/practice/Sort.java new file mode 100644 index 0000000..8cc6901 --- /dev/null +++ b/src/com/codefortomorrow/advanced/chapter16/practice/Sort.java @@ -0,0 +1,8 @@ +package com.codefortomorrow.advanced.chapter16.practice; + +/* +Write a method which takes in a LinkedList of Strings +and rearranges the elements (without creating a new list) +so it is sorted in ascending order of String length. +Don’t use any pre-existing sort methods… write your own. :) +*/ diff --git a/src/com/codefortomorrow/advanced/chapter16/solutions/Access.java b/src/com/codefortomorrow/advanced/chapter16/solutions/Access.java new file mode 100644 index 0000000..1e106c4 --- /dev/null +++ b/src/com/codefortomorrow/advanced/chapter16/solutions/Access.java @@ -0,0 +1,42 @@ +package com.codefortomorrow.advanced.chapter16.solutions; +import java.util.*; + +/* +Using the methods outlined in the “Benefits of LinkedLists” section, +fill both a LinkedList and an ArrayList with 10,000 elements. +Then, find the time it takes to access the last element of each list. + +Determine which type of list can access elements more efficiently. + +Hint: You can use the get(int position) method +to access the element at a given position of a list. +*/ + +public class Access { + + public static void main(String[] args) { + long a, b; + + // Define lists with 10,000 elements. + LinkedList list1 = new LinkedList(); + for(int i = 0; i < 10000; i++) + list1.add(1); + + ArrayList list2 = new ArrayList(); + for(int i = 0; i < 10000; i++) + list2.add(1); + + // Get time for each operation and output the difference. + a = System.nanoTime(); + int c = list1.get(9999); + b = System.nanoTime(); + System.out.println("\nLinked: " + (b-a) + " ns"); + + a = System.nanoTime(); + int d = list2.get(9999); + b = System.nanoTime(); + System.out.println("ArrayList: " + (b-a) + " ns"); + + // Note that the ArrayList takes less time. + } +} diff --git a/src/com/codefortomorrow/advanced/chapter16/solutions/Names.java b/src/com/codefortomorrow/advanced/chapter16/solutions/Names.java new file mode 100644 index 0000000..b783ad5 --- /dev/null +++ b/src/com/codefortomorrow/advanced/chapter16/solutions/Names.java @@ -0,0 +1,40 @@ +package com.codefortomorrow.advanced.chapter16.solutions; +import java.util.*; + +/* +Create a LinkedList of Strings, and ask the user to +input 10 names one by one into the list. + +Output this list. + +Remove each name that has 5 or fewer letters, and output the new list. + +Add the word “Apple” to the beginning of the list. + +Add the word “Peanut” to the second-to-last position of the list. + +(Hint: Make sure to take advantage of LinkedList methods!) + +Output the new list. +*/ + +public class Names { + public static void main(String[] args) { + LinkedList list = new LinkedList(); + Scanner sc = new Scanner(System.in); + for(int i = 0; i < 10; i++) { + System.out.print("Enter name " + (i+1) + ": "); + list.add(sc.nextLine()); + } + System.out.println(list); + + for(int i = 0; i < list.size(); i++) + if(list.get(i).length() <= 5) + list.remove(i--); + System.out.println(list); + + list.add(0, "Apple"); + list.add(list.size() - 1, "Peanut"); + System.out.println(list); + } +} diff --git a/src/com/codefortomorrow/advanced/chapter16/solutions/SinglyLinkedListInsert.java b/src/com/codefortomorrow/advanced/chapter16/solutions/SinglyLinkedListInsert.java new file mode 100644 index 0000000..f9a6e0b --- /dev/null +++ b/src/com/codefortomorrow/advanced/chapter16/solutions/SinglyLinkedListInsert.java @@ -0,0 +1,112 @@ +package com.codefortomorrow.advanced.chapter16.solutions; +import java.util.*; + +/* +Adding an insert method to a SinglyLinkedList +(details in SinglyLinkedList DIY section). + +Hint: It might be necessary to have a separate case for +inserting at position 0, because that’s when you want to +insert BEFORE the head. For all other positions (1, 2, etc.), +you can simply traverse down the list, keeping track of the +current node, and when you reach the position you want, +add the node AFTER the current node. + +Alternatively, you can always add BEFORE the current node +for all position cases (insert before the head to place the +new node at pos 0, insert before node 1 to place the new +node at pos 1, etc.), but then your exception case would +be when the user wants to insert a node at the very end of the list. +In this case, you’d have to write separate code to insert AFTER +the last node. + +Make sure you have a toString() method defined! + +The following code should output “{2, 3, 4, 7}”: +SinglyLinkedList lst = new SinglyLinkedList(); +lst.add(3); +lst.add(7); +lst.insert(4, 1); +lst.insert(2, 0); +System.out.println(lst); +*/ + +// NOTE: You need to make a separate class with a main() +// method to use this List object. This is simply the object +// definition. + +public class SinglyLinkedListInsert { + Node head; + + // add() method from SinglyLinkedList DIY section, adds to end of list + public void add(int data) { + // Create a new node with given data + Node new_node = new Node(data); + + // If the Linked List is empty, + // then make the new node as head + if (head == null) { + head = new_node; + } + else { + // Else traverse till the last node + // and insert the new_node there + Node last = head; + while (last.next != null) { + last = last.next; + } + + // Insert the new_node at last node + last.next = new_node; + } + } + + public void insert(int data, int pos) { + // Verify valid pos number + if (pos < 0) + System.out.print("Invalid position"); + + // Create new node and keep track of current node + Node new_node = new Node(data); + Node current = head; + + // If pos is 0, set new node as head + if (pos == 0) { + new_node.next = head; + head = new_node; + } else { + while (pos-- >= 0) { + if (pos == 0) { + // Add new node after current position + new_node.next = current.next; + current.next = new_node; + break; + } + // Check if pos is out of bounds (reaches end) + if(current.next == null) { + System.out.println("Out of bounds"); + break; + } + current = current.next; + } + } + } + + @Override + public String toString() { + // For empty lists + if(head == null) + return "{}"; + + String str = "{"; + + // Traverse the list until the last node + Node current = head; + while(current.next != null) { + str += "" + current.data + ", "; + current = current.next; + } + str += current.data + "}"; + return str; + } +} diff --git a/src/com/codefortomorrow/advanced/chapter16/solutions/Sort.java b/src/com/codefortomorrow/advanced/chapter16/solutions/Sort.java new file mode 100644 index 0000000..31f8f52 --- /dev/null +++ b/src/com/codefortomorrow/advanced/chapter16/solutions/Sort.java @@ -0,0 +1,41 @@ +package com.codefortomorrow.advanced.chapter16.solutions; +import java.util.*; + +/* +Write a method which takes in a LinkedList of Strings +and rearranges the elements (without creating a new list) +so it is sorted in ascending order of String length. +Don’t use any pre-existing sort methods… write your own. :) +*/ + +public class Sort { + public static void main(String[] args) { + LinkedList list = new LinkedList(); + list.addAll(Arrays.asList( + "Jeannette", "Sophia", "Computer", "Boots", + "Lava", "Lasagna", "Dirty", "Cleanie" + )); + sort(list); + System.out.println(list); + } + + public static void sort(LinkedList list) { + if(list.size() < 2) return; + for(int i = 1; i < list.size(); i++) { + String current = list.get(i); + // Check if current word is smaller than previous word + if(current.length() < list.get(i-1).length()) { + // Add current word before the node with + // same or greater word size + for(int j = 0; j < i; j++) { + if(list.get(j).length() >= current.length()) { + list.add(j, current); + // Remove duplicate current + list.remove(i+1); + break; + } + } + } + } + } +} From 38d53032e69d30fc3c080834deb06636f93d8bad Mon Sep 17 00:00:00 2001 From: neha-peddinti Date: Sat, 7 Nov 2020 19:59:12 +0000 Subject: [PATCH 18/69] Bot: Prettified Java code! --- .../advanced/chapter14/solutions/Bank.java | 1 + .../solutions/MeasurableProblem.java | 1 + .../advanced/chapter16/practice/Access.java | 1 - .../advanced/chapter16/practice/Names.java | 1 - .../practice/SinglyLinkedListInsert.java | 1 - .../advanced/chapter16/practice/Sort.java | 1 - .../advanced/chapter16/solutions/Access.java | 12 +- .../advanced/chapter16/solutions/Names.java | 14 +- .../solutions/SinglyLinkedListInsert.java | 139 +++++++++--------- .../advanced/chapter16/solutions/Sort.java | 30 ++-- .../beginner/chapter2/examples/Variables.java | 2 + .../chapter12/examples/Person.java | 1 + .../chapter12/examples/TestInstrument.java | 2 + .../chapter12/examples/TestPoint.java | 1 + .../chapter12/practice/Account.java | 1 + .../chapter12/practice/TestPiano.java | 1 + .../chapter12/practice/pokemon/Battle.java | 1 + .../chapter12/practice/pokemon/Move.java | 1 + .../chapter12/practice/pokemon/Pokemon.java | 1 + .../chapter12/solutions/Book.java | 1 + .../chapter12/solutions/Circle.java | 1 + .../chapter12/solutions/TestPiano.java | 2 + .../chapter12/solutions/TestTriangle.java | 1 + .../chapter12/solutions/TimeAndDate.java | 2 + .../chapter12/solutions/geometric/Circle.java | 1 + .../solutions/geometric/Rectangle.java | 1 + .../chapter12/solutions/geometric/Shape.java | 1 + .../chapter12/solutions/pokemon/Battle.java | 1 + .../chapter12/solutions/pokemon/Move.java | 1 + .../chapter12/solutions/pokemon/Pokemon.java | 1 + 30 files changed, 128 insertions(+), 97 deletions(-) diff --git a/src/com/codefortomorrow/advanced/chapter14/solutions/Bank.java b/src/com/codefortomorrow/advanced/chapter14/solutions/Bank.java index 5fec90d..78108b8 100644 --- a/src/com/codefortomorrow/advanced/chapter14/solutions/Bank.java +++ b/src/com/codefortomorrow/advanced/chapter14/solutions/Bank.java @@ -52,6 +52,7 @@ public static void withdraw(BankAccount account, int amount) { } class BankAccount { + private int balance; public BankAccount(int balance) { diff --git a/src/com/codefortomorrow/advanced/chapter15/solutions/MeasurableProblem.java b/src/com/codefortomorrow/advanced/chapter15/solutions/MeasurableProblem.java index 54a4b67..3c57162 100644 --- a/src/com/codefortomorrow/advanced/chapter15/solutions/MeasurableProblem.java +++ b/src/com/codefortomorrow/advanced/chapter15/solutions/MeasurableProblem.java @@ -40,6 +40,7 @@ interface Measurable { } class Student implements Measurable { + private double GPA; private String name; diff --git a/src/com/codefortomorrow/advanced/chapter16/practice/Access.java b/src/com/codefortomorrow/advanced/chapter16/practice/Access.java index f4692a0..917e424 100644 --- a/src/com/codefortomorrow/advanced/chapter16/practice/Access.java +++ b/src/com/codefortomorrow/advanced/chapter16/practice/Access.java @@ -1,5 +1,4 @@ package com.codefortomorrow.advanced.chapter16.practice; - /* Using the methods outlined in the “Benefits of LinkedLists” section, fill both a LinkedList and an ArrayList with 10,000 elements. diff --git a/src/com/codefortomorrow/advanced/chapter16/practice/Names.java b/src/com/codefortomorrow/advanced/chapter16/practice/Names.java index 3b0f49b..3d9a449 100644 --- a/src/com/codefortomorrow/advanced/chapter16/practice/Names.java +++ b/src/com/codefortomorrow/advanced/chapter16/practice/Names.java @@ -1,5 +1,4 @@ package com.codefortomorrow.advanced.chapter16.practice; - /* Create a LinkedList of Strings, and ask the user to input 10 names one by one into the list. diff --git a/src/com/codefortomorrow/advanced/chapter16/practice/SinglyLinkedListInsert.java b/src/com/codefortomorrow/advanced/chapter16/practice/SinglyLinkedListInsert.java index 0958a4e..1b8ec1b 100644 --- a/src/com/codefortomorrow/advanced/chapter16/practice/SinglyLinkedListInsert.java +++ b/src/com/codefortomorrow/advanced/chapter16/practice/SinglyLinkedListInsert.java @@ -1,5 +1,4 @@ package com.codefortomorrow.advanced.chapter16.practice; - /* Adding an insert method to a SinglyLinkedList (details in SinglyLinkedList DIY section). diff --git a/src/com/codefortomorrow/advanced/chapter16/practice/Sort.java b/src/com/codefortomorrow/advanced/chapter16/practice/Sort.java index 8cc6901..4b783e5 100644 --- a/src/com/codefortomorrow/advanced/chapter16/practice/Sort.java +++ b/src/com/codefortomorrow/advanced/chapter16/practice/Sort.java @@ -1,5 +1,4 @@ package com.codefortomorrow.advanced.chapter16.practice; - /* Write a method which takes in a LinkedList of Strings and rearranges the elements (without creating a new list) diff --git a/src/com/codefortomorrow/advanced/chapter16/solutions/Access.java b/src/com/codefortomorrow/advanced/chapter16/solutions/Access.java index 1e106c4..3ff7c97 100644 --- a/src/com/codefortomorrow/advanced/chapter16/solutions/Access.java +++ b/src/com/codefortomorrow/advanced/chapter16/solutions/Access.java @@ -1,4 +1,5 @@ package com.codefortomorrow.advanced.chapter16.solutions; + import java.util.*; /* @@ -19,24 +20,21 @@ public static void main(String[] args) { // Define lists with 10,000 elements. LinkedList list1 = new LinkedList(); - for(int i = 0; i < 10000; i++) - list1.add(1); + for (int i = 0; i < 10000; i++) list1.add(1); ArrayList list2 = new ArrayList(); - for(int i = 0; i < 10000; i++) - list2.add(1); + for (int i = 0; i < 10000; i++) list2.add(1); // Get time for each operation and output the difference. a = System.nanoTime(); int c = list1.get(9999); b = System.nanoTime(); - System.out.println("\nLinked: " + (b-a) + " ns"); + System.out.println("\nLinked: " + (b - a) + " ns"); a = System.nanoTime(); int d = list2.get(9999); b = System.nanoTime(); - System.out.println("ArrayList: " + (b-a) + " ns"); - + System.out.println("ArrayList: " + (b - a) + " ns"); // Note that the ArrayList takes less time. } } diff --git a/src/com/codefortomorrow/advanced/chapter16/solutions/Names.java b/src/com/codefortomorrow/advanced/chapter16/solutions/Names.java index b783ad5..b14b55d 100644 --- a/src/com/codefortomorrow/advanced/chapter16/solutions/Names.java +++ b/src/com/codefortomorrow/advanced/chapter16/solutions/Names.java @@ -1,4 +1,5 @@ package com.codefortomorrow.advanced.chapter16.solutions; + import java.util.*; /* @@ -19,18 +20,19 @@ */ public class Names { + public static void main(String[] args) { LinkedList list = new LinkedList(); Scanner sc = new Scanner(System.in); - for(int i = 0; i < 10; i++) { - System.out.print("Enter name " + (i+1) + ": "); - list.add(sc.nextLine()); + for (int i = 0; i < 10; i++) { + System.out.print("Enter name " + (i + 1) + ": "); + list.add(sc.nextLine()); } System.out.println(list); - for(int i = 0; i < list.size(); i++) - if(list.get(i).length() <= 5) - list.remove(i--); + for (int i = 0; i < list.size(); i++) if ( + list.get(i).length() <= 5 + ) list.remove(i--); System.out.println(list); list.add(0, "Apple"); diff --git a/src/com/codefortomorrow/advanced/chapter16/solutions/SinglyLinkedListInsert.java b/src/com/codefortomorrow/advanced/chapter16/solutions/SinglyLinkedListInsert.java index f9a6e0b..7fb84c1 100644 --- a/src/com/codefortomorrow/advanced/chapter16/solutions/SinglyLinkedListInsert.java +++ b/src/com/codefortomorrow/advanced/chapter16/solutions/SinglyLinkedListInsert.java @@ -1,4 +1,5 @@ package com.codefortomorrow.advanced.chapter16.solutions; + import java.util.*; /* @@ -36,77 +37,75 @@ Make sure you have a toString() method defined! // definition. public class SinglyLinkedListInsert { - Node head; - - // add() method from SinglyLinkedList DIY section, adds to end of list - public void add(int data) { - // Create a new node with given data - Node new_node = new Node(data); - - // If the Linked List is empty, - // then make the new node as head - if (head == null) { - head = new_node; - } - else { - // Else traverse till the last node - // and insert the new_node there - Node last = head; - while (last.next != null) { - last = last.next; - } - - // Insert the new_node at last node - last.next = new_node; - } - } - - public void insert(int data, int pos) { - // Verify valid pos number - if (pos < 0) - System.out.print("Invalid position"); - - // Create new node and keep track of current node - Node new_node = new Node(data); - Node current = head; - - // If pos is 0, set new node as head - if (pos == 0) { - new_node.next = head; - head = new_node; - } else { - while (pos-- >= 0) { - if (pos == 0) { - // Add new node after current position - new_node.next = current.next; - current.next = new_node; - break; + + Node head; + + // add() method from SinglyLinkedList DIY section, adds to end of list + public void add(int data) { + // Create a new node with given data + Node new_node = new Node(data); + + // If the Linked List is empty, + // then make the new node as head + if (head == null) { + head = new_node; + } else { + // Else traverse till the last node + // and insert the new_node there + Node last = head; + while (last.next != null) { + last = last.next; + } + + // Insert the new_node at last node + last.next = new_node; + } + } + + public void insert(int data, int pos) { + // Verify valid pos number + if (pos < 0) System.out.print("Invalid position"); + + // Create new node and keep track of current node + Node new_node = new Node(data); + Node current = head; + + // If pos is 0, set new node as head + if (pos == 0) { + new_node.next = head; + head = new_node; + } else { + while (pos-- >= 0) { + if (pos == 0) { + // Add new node after current position + new_node.next = current.next; + current.next = new_node; + break; + } + // Check if pos is out of bounds (reaches end) + if (current.next == null) { + System.out.println("Out of bounds"); + break; + } + current = current.next; + } } - // Check if pos is out of bounds (reaches end) - if(current.next == null) { - System.out.println("Out of bounds"); - break; + } + + @Override + public String toString() { + // For empty lists + if (head == null) return "{}"; + + String str = "{"; + + // Traverse the list until the last node + Node current = head; + while (current.next != null) { + str += "" + current.data + ", "; + current = current.next; } - current = current.next; - } - } - } - - @Override - public String toString() { - // For empty lists - if(head == null) - return "{}"; - - String str = "{"; - - // Traverse the list until the last node - Node current = head; - while(current.next != null) { - str += "" + current.data + ", "; - current = current.next; + str += current.data + "}"; + return str; } - str += current.data + "}"; - return str; - } } diff --git a/src/com/codefortomorrow/advanced/chapter16/solutions/Sort.java b/src/com/codefortomorrow/advanced/chapter16/solutions/Sort.java index 31f8f52..4f90e8d 100644 --- a/src/com/codefortomorrow/advanced/chapter16/solutions/Sort.java +++ b/src/com/codefortomorrow/advanced/chapter16/solutions/Sort.java @@ -1,4 +1,5 @@ package com.codefortomorrow.advanced.chapter16.solutions; + import java.util.*; /* @@ -9,29 +10,38 @@ and rearranges the elements (without creating a new list) */ public class Sort { + public static void main(String[] args) { LinkedList list = new LinkedList(); - list.addAll(Arrays.asList( - "Jeannette", "Sophia", "Computer", "Boots", - "Lava", "Lasagna", "Dirty", "Cleanie" - )); + list.addAll( + Arrays.asList( + "Jeannette", + "Sophia", + "Computer", + "Boots", + "Lava", + "Lasagna", + "Dirty", + "Cleanie" + ) + ); sort(list); System.out.println(list); } public static void sort(LinkedList list) { - if(list.size() < 2) return; - for(int i = 1; i < list.size(); i++) { + if (list.size() < 2) return; + for (int i = 1; i < list.size(); i++) { String current = list.get(i); // Check if current word is smaller than previous word - if(current.length() < list.get(i-1).length()) { + if (current.length() < list.get(i - 1).length()) { // Add current word before the node with // same or greater word size - for(int j = 0; j < i; j++) { - if(list.get(j).length() >= current.length()) { + for (int j = 0; j < i; j++) { + if (list.get(j).length() >= current.length()) { list.add(j, current); // Remove duplicate current - list.remove(i+1); + list.remove(i + 1); break; } } diff --git a/src/com/codefortomorrow/beginner/chapter2/examples/Variables.java b/src/com/codefortomorrow/beginner/chapter2/examples/Variables.java index 41bf623..3adc394 100644 --- a/src/com/codefortomorrow/beginner/chapter2/examples/Variables.java +++ b/src/com/codefortomorrow/beginner/chapter2/examples/Variables.java @@ -21,9 +21,11 @@ public static void main(String[] args) { // scope class A { + String message = "World"; } class B { + String word = "Java"; } diff --git a/src/com/codefortomorrow/intermediate/chapter12/examples/Person.java b/src/com/codefortomorrow/intermediate/chapter12/examples/Person.java index f57e9b5..ba50d7e 100644 --- a/src/com/codefortomorrow/intermediate/chapter12/examples/Person.java +++ b/src/com/codefortomorrow/intermediate/chapter12/examples/Person.java @@ -1,6 +1,7 @@ package com.codefortomorrow.intermediate.chapter12.examples; public class Person { + private String name; private int age; private String address; diff --git a/src/com/codefortomorrow/intermediate/chapter12/examples/TestInstrument.java b/src/com/codefortomorrow/intermediate/chapter12/examples/TestInstrument.java index a5d070e..daaeff0 100644 --- a/src/com/codefortomorrow/intermediate/chapter12/examples/TestInstrument.java +++ b/src/com/codefortomorrow/intermediate/chapter12/examples/TestInstrument.java @@ -15,6 +15,7 @@ public static void main(String[] args) { } class Instrument { + private String name; public Instrument() { @@ -36,6 +37,7 @@ public String toString() { } class Guitar extends Instrument { + private String type; private String stringType; diff --git a/src/com/codefortomorrow/intermediate/chapter12/examples/TestPoint.java b/src/com/codefortomorrow/intermediate/chapter12/examples/TestPoint.java index 8268423..c741010 100644 --- a/src/com/codefortomorrow/intermediate/chapter12/examples/TestPoint.java +++ b/src/com/codefortomorrow/intermediate/chapter12/examples/TestPoint.java @@ -17,6 +17,7 @@ public static void main(String[] args) { } class Point { + public double x; public double y; diff --git a/src/com/codefortomorrow/intermediate/chapter12/practice/Account.java b/src/com/codefortomorrow/intermediate/chapter12/practice/Account.java index b52b57a..d87f957 100644 --- a/src/com/codefortomorrow/intermediate/chapter12/practice/Account.java +++ b/src/com/codefortomorrow/intermediate/chapter12/practice/Account.java @@ -5,6 +5,7 @@ */ public class Account { + private String name; private long number; private double balance; diff --git a/src/com/codefortomorrow/intermediate/chapter12/practice/TestPiano.java b/src/com/codefortomorrow/intermediate/chapter12/practice/TestPiano.java index d5f74bc..793d373 100644 --- a/src/com/codefortomorrow/intermediate/chapter12/practice/TestPiano.java +++ b/src/com/codefortomorrow/intermediate/chapter12/practice/TestPiano.java @@ -43,6 +43,7 @@ class Piano extends Instrument { } class Instrument { + private String name; public Instrument() { diff --git a/src/com/codefortomorrow/intermediate/chapter12/practice/pokemon/Battle.java b/src/com/codefortomorrow/intermediate/chapter12/practice/pokemon/Battle.java index 961e20b..1e855bf 100644 --- a/src/com/codefortomorrow/intermediate/chapter12/practice/pokemon/Battle.java +++ b/src/com/codefortomorrow/intermediate/chapter12/practice/pokemon/Battle.java @@ -3,6 +3,7 @@ import java.util.Scanner; public class Battle { + private Scanner sc = new Scanner(System.in); private Pokemon pokemonOne; private Pokemon pokemonTwo; diff --git a/src/com/codefortomorrow/intermediate/chapter12/practice/pokemon/Move.java b/src/com/codefortomorrow/intermediate/chapter12/practice/pokemon/Move.java index 7e2559f..4ac5e77 100644 --- a/src/com/codefortomorrow/intermediate/chapter12/practice/pokemon/Move.java +++ b/src/com/codefortomorrow/intermediate/chapter12/practice/pokemon/Move.java @@ -4,6 +4,7 @@ * represents a Pokemon's move */ public class Move { + private String name; private boolean poisoning; private boolean sleeping; diff --git a/src/com/codefortomorrow/intermediate/chapter12/practice/pokemon/Pokemon.java b/src/com/codefortomorrow/intermediate/chapter12/practice/pokemon/Pokemon.java index 277ffd6..784d22b 100644 --- a/src/com/codefortomorrow/intermediate/chapter12/practice/pokemon/Pokemon.java +++ b/src/com/codefortomorrow/intermediate/chapter12/practice/pokemon/Pokemon.java @@ -4,6 +4,7 @@ * represents a Pokemon object */ public class Pokemon { + private Move[] moveList; private int maxHP; private int currentHP; diff --git a/src/com/codefortomorrow/intermediate/chapter12/solutions/Book.java b/src/com/codefortomorrow/intermediate/chapter12/solutions/Book.java index 12092b9..f91012b 100644 --- a/src/com/codefortomorrow/intermediate/chapter12/solutions/Book.java +++ b/src/com/codefortomorrow/intermediate/chapter12/solutions/Book.java @@ -10,6 +10,7 @@ */ public class Book { + private String title; private String author; private String genre; diff --git a/src/com/codefortomorrow/intermediate/chapter12/solutions/Circle.java b/src/com/codefortomorrow/intermediate/chapter12/solutions/Circle.java index cc5aacb..c9b3651 100644 --- a/src/com/codefortomorrow/intermediate/chapter12/solutions/Circle.java +++ b/src/com/codefortomorrow/intermediate/chapter12/solutions/Circle.java @@ -5,6 +5,7 @@ */ public class Circle { + private double radius; private String color; diff --git a/src/com/codefortomorrow/intermediate/chapter12/solutions/TestPiano.java b/src/com/codefortomorrow/intermediate/chapter12/solutions/TestPiano.java index d2c7d2f..3d6b32a 100644 --- a/src/com/codefortomorrow/intermediate/chapter12/solutions/TestPiano.java +++ b/src/com/codefortomorrow/intermediate/chapter12/solutions/TestPiano.java @@ -38,6 +38,7 @@ public static void main(String[] args) { } class Piano extends Instrument { + private double hammerWeight; private String backPostMaterial; @@ -72,6 +73,7 @@ public String toString() { } class Instrument { + private String name; public Instrument() { diff --git a/src/com/codefortomorrow/intermediate/chapter12/solutions/TestTriangle.java b/src/com/codefortomorrow/intermediate/chapter12/solutions/TestTriangle.java index 855b809..a3d6241 100644 --- a/src/com/codefortomorrow/intermediate/chapter12/solutions/TestTriangle.java +++ b/src/com/codefortomorrow/intermediate/chapter12/solutions/TestTriangle.java @@ -44,6 +44,7 @@ public static void main(String[] args) { } class Triangle { + private double side1; private double side2; private double side3; diff --git a/src/com/codefortomorrow/intermediate/chapter12/solutions/TimeAndDate.java b/src/com/codefortomorrow/intermediate/chapter12/solutions/TimeAndDate.java index 8323ae6..f38238f 100644 --- a/src/com/codefortomorrow/intermediate/chapter12/solutions/TimeAndDate.java +++ b/src/com/codefortomorrow/intermediate/chapter12/solutions/TimeAndDate.java @@ -47,6 +47,7 @@ public static void main(String[] args) { } class MyTime { + public int hours; public int minutes; public int seconds; @@ -70,6 +71,7 @@ public String toString() { } class MyDate { + public int month; public int day; public int year; diff --git a/src/com/codefortomorrow/intermediate/chapter12/solutions/geometric/Circle.java b/src/com/codefortomorrow/intermediate/chapter12/solutions/geometric/Circle.java index 11523ed..40b158a 100644 --- a/src/com/codefortomorrow/intermediate/chapter12/solutions/geometric/Circle.java +++ b/src/com/codefortomorrow/intermediate/chapter12/solutions/geometric/Circle.java @@ -62,6 +62,7 @@ The getDistance() method returns the distance between this Shape and another Sha */ public class Circle extends Shape { + private double radius; /** Constructs a default Circle with radius 0 */ diff --git a/src/com/codefortomorrow/intermediate/chapter12/solutions/geometric/Rectangle.java b/src/com/codefortomorrow/intermediate/chapter12/solutions/geometric/Rectangle.java index f0277b5..9a3a040 100644 --- a/src/com/codefortomorrow/intermediate/chapter12/solutions/geometric/Rectangle.java +++ b/src/com/codefortomorrow/intermediate/chapter12/solutions/geometric/Rectangle.java @@ -62,6 +62,7 @@ The getDistance() method returns the distance between this Shape and another Sha */ public class Rectangle extends Shape { + private double length; private double width; diff --git a/src/com/codefortomorrow/intermediate/chapter12/solutions/geometric/Shape.java b/src/com/codefortomorrow/intermediate/chapter12/solutions/geometric/Shape.java index 095b8a0..edd5a8d 100644 --- a/src/com/codefortomorrow/intermediate/chapter12/solutions/geometric/Shape.java +++ b/src/com/codefortomorrow/intermediate/chapter12/solutions/geometric/Shape.java @@ -62,6 +62,7 @@ The getDistance() method returns the distance between this Shape and another Sha */ public class Shape { + public double x; // x coordinate of center of the shape public double y; // y coordinate of center of the shape diff --git a/src/com/codefortomorrow/intermediate/chapter12/solutions/pokemon/Battle.java b/src/com/codefortomorrow/intermediate/chapter12/solutions/pokemon/Battle.java index 9689388..8f94701 100644 --- a/src/com/codefortomorrow/intermediate/chapter12/solutions/pokemon/Battle.java +++ b/src/com/codefortomorrow/intermediate/chapter12/solutions/pokemon/Battle.java @@ -3,6 +3,7 @@ import java.util.Scanner; public class Battle { + private Scanner sc = new Scanner(System.in); private Pokemon pokemonOne; private Pokemon pokemonTwo; diff --git a/src/com/codefortomorrow/intermediate/chapter12/solutions/pokemon/Move.java b/src/com/codefortomorrow/intermediate/chapter12/solutions/pokemon/Move.java index 257c231..1f7f35a 100644 --- a/src/com/codefortomorrow/intermediate/chapter12/solutions/pokemon/Move.java +++ b/src/com/codefortomorrow/intermediate/chapter12/solutions/pokemon/Move.java @@ -4,6 +4,7 @@ * represents a Pokemon's move */ public class Move { + private String name; private boolean poisoning; private boolean sleeping; diff --git a/src/com/codefortomorrow/intermediate/chapter12/solutions/pokemon/Pokemon.java b/src/com/codefortomorrow/intermediate/chapter12/solutions/pokemon/Pokemon.java index 2050900..74fad5b 100644 --- a/src/com/codefortomorrow/intermediate/chapter12/solutions/pokemon/Pokemon.java +++ b/src/com/codefortomorrow/intermediate/chapter12/solutions/pokemon/Pokemon.java @@ -4,6 +4,7 @@ * represents a Pokemon object */ public class Pokemon { + private Move[] moveList; private int maxHP; private int currentHP; From e2308f512468811522fa494de56f097e9c0a7c45 Mon Sep 17 00:00:00 2001 From: Rebecca Dang <35876322+phrdang@users.noreply.github.com> Date: Wed, 10 Mar 2021 10:27:14 -0800 Subject: [PATCH 19/69] Create LICENSE --- LICENSE | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 LICENSE diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..77b1cea --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2021 Code 4 Tomorrow + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. From 03c3ff7de781b8b9ee1c84ec5999f6d8cd635e2e Mon Sep 17 00:00:00 2001 From: ArmeetJatyani Date: Mon, 15 Mar 2021 10:55:15 -0700 Subject: [PATCH 20/69] added HelloWorld Example and Solution! --- .gitignore | 5 ++++- .../chapter1/practice/HelloWorld.java | 19 +++++++++++++++++++ .../chapter1/solutions/HelloWorld.java | 19 +++++++++++++++++++ 3 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 src/com/codefortomorrow/beginner/chapter1/practice/HelloWorld.java create mode 100644 src/com/codefortomorrow/beginner/chapter1/solutions/HelloWorld.java diff --git a/.gitignore b/.gitignore index 1dbb98b..9a0f431 100644 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,7 @@ # ignore files c4t-java.iml -.DS_Store \ No newline at end of file +.DS_Store + +# ignore prettier files +.prettierrc.json diff --git a/src/com/codefortomorrow/beginner/chapter1/practice/HelloWorld.java b/src/com/codefortomorrow/beginner/chapter1/practice/HelloWorld.java new file mode 100644 index 0000000..e2a15fe --- /dev/null +++ b/src/com/codefortomorrow/beginner/chapter1/practice/HelloWorld.java @@ -0,0 +1,19 @@ +package com.codefortomorrow.beginner.chapter1.practice; + +/** + * @author ArmeetJatyani + * March 2021 + * + * Welcome to Java! + * This may be your first ever java program! + * We'll begin your journey with the infamous Hello World program! + */ + + // a class is an "object" where we will place all our code inside + public class HelloWorld{ + // the main method (below) is the first thing that runs when your program is run + public static void main(String[] args) { + // write code here (replace the "" with "Hello World!") + System.out.println(""); + } + } \ No newline at end of file diff --git a/src/com/codefortomorrow/beginner/chapter1/solutions/HelloWorld.java b/src/com/codefortomorrow/beginner/chapter1/solutions/HelloWorld.java new file mode 100644 index 0000000..dbce821 --- /dev/null +++ b/src/com/codefortomorrow/beginner/chapter1/solutions/HelloWorld.java @@ -0,0 +1,19 @@ +package com.codefortomorrow.beginner.chapter1.practice; + +/** + * @author ArmeetJatyani + * March 2021 + * + * Welcome to Java! + * This may be your first ever java program! + * We'll begin your journey with the infamous Hello World program! + */ + + // a class is an "object" where we will place all our code inside + public class HelloWorld{ + // the main method (below) is the first thing that runs when your program is run + public static void main(String[] args) { + // write code here (replace the "" with "Hello World!") + System.out.println("Hello World!"); + } + } \ No newline at end of file From 2a4843736205c3022ad45feae1b0910a932ad501 Mon Sep 17 00:00:00 2001 From: ArmeetJatyani Date: Mon, 15 Mar 2021 11:03:08 -0700 Subject: [PATCH 21/69] Added Comments exercise to practice comments (Java Beginners, Chapter 1) --- .../beginner/chapter1/practice/Comments.java | 38 +++++++++++++++++++ .../beginner/chapter1/solutions/Comments.java | 38 +++++++++++++++++++ 2 files changed, 76 insertions(+) create mode 100644 src/com/codefortomorrow/beginner/chapter1/practice/Comments.java create mode 100644 src/com/codefortomorrow/beginner/chapter1/solutions/Comments.java diff --git a/src/com/codefortomorrow/beginner/chapter1/practice/Comments.java b/src/com/codefortomorrow/beginner/chapter1/practice/Comments.java new file mode 100644 index 0000000..cebb705 --- /dev/null +++ b/src/com/codefortomorrow/beginner/chapter1/practice/Comments.java @@ -0,0 +1,38 @@ +package com.codefortomorrow.beginner.chapter1.practice; + +/** + * @author ArmeetJatyani + * March 2021 + * + * You'll be writing your first ever comment today! + * What we'll go over: + * - single line comments + * - multi line comments + */ + + // a class is an "object" where we will place all our code inside + public class Comments { + // the main method (below) is the first thing that runs when your program is run + public static void main(String[] args) { + // this is a single line comment, I can write anything here + // single line comments aren't run by Java! + // they can be used to annotate your code! + + /** + * this is a multi + * line + * comment + * + * It can span across multiple lines! + */ + + // YOUR ASSIGNMENT: write 1 single-line comment and 1 multi-line comment on the lines below... + + + + + + + + } + } \ No newline at end of file diff --git a/src/com/codefortomorrow/beginner/chapter1/solutions/Comments.java b/src/com/codefortomorrow/beginner/chapter1/solutions/Comments.java new file mode 100644 index 0000000..a14a123 --- /dev/null +++ b/src/com/codefortomorrow/beginner/chapter1/solutions/Comments.java @@ -0,0 +1,38 @@ +package com.codefortomorrow.beginner.chapter1.practice; + +/** + * @author ArmeetJatyani + * March 2021 + * + * You'll be writing your first ever comment today! + * What we'll go over: + * - single line comments + * - multi line comments + */ + + // a class is an "object" where we will place all our code inside + public class Comments { + // the main method (below) is the first thing that runs when your program is run + public static void main(String[] args) { + // this is a single line comment, I can write anything here + // single line comments aren't run by Java! + // they can be used to annotate your code! + + /** + * this is a multi + * line + * comment + * + * It can span across multiple lines! + */ + + // YOUR ASSIGNMENT: write 1 single-line comment and 1 multi-line comment on the lines below... + + // Hi my name is Armeet! + + /** + * I like teaching Java, and + * good luck on your journey! + */ + } + } \ No newline at end of file From bb68ef08cfdb2d99dba88794b254f703e2a55b8f Mon Sep 17 00:00:00 2001 From: ArmeetJatyani Date: Mon, 15 Mar 2021 11:47:45 -0700 Subject: [PATCH 22/69] finished VariableTypes exercise and solution --- .../chapter2/practice/VariableTypes.java | 34 +++++++++++++++++++ .../chapter2/solutions/VariableTypes.java | 34 +++++++++++++++++++ 2 files changed, 68 insertions(+) create mode 100644 src/com/codefortomorrow/beginner/chapter2/practice/VariableTypes.java create mode 100644 src/com/codefortomorrow/beginner/chapter2/solutions/VariableTypes.java diff --git a/src/com/codefortomorrow/beginner/chapter2/practice/VariableTypes.java b/src/com/codefortomorrow/beginner/chapter2/practice/VariableTypes.java new file mode 100644 index 0000000..3cb0ed4 --- /dev/null +++ b/src/com/codefortomorrow/beginner/chapter2/practice/VariableTypes.java @@ -0,0 +1,34 @@ +package com.codefortomorrow.beginner.chapter1.practice; + +/** + * @author ArmeetJatyani + * March 2021 + * + * Define different types of variables + */ + + public class VariableTypes { + public static void main(String[] args) { + // write your code here + + // define an integer variable on line 15 + + + // define a float variable on line 18 + + + // define a double variable on line 21 + + + // define a boolean variable on line 24 (Hint: true/false) on line 24 + + + // define a character variable on line 27 + + + // define a string variable on line 30 + + + } + } + \ No newline at end of file diff --git a/src/com/codefortomorrow/beginner/chapter2/solutions/VariableTypes.java b/src/com/codefortomorrow/beginner/chapter2/solutions/VariableTypes.java new file mode 100644 index 0000000..36a6b48 --- /dev/null +++ b/src/com/codefortomorrow/beginner/chapter2/solutions/VariableTypes.java @@ -0,0 +1,34 @@ +package com.codefortomorrow.beginner.chapter1.practice; + +/** + * @author ArmeetJatyani + * March 2021 + * + * Define different types of variables + */ + + public class VariableTypes { + public static void main(String[] args) { + // write your code here + + // define an integer variable on line 15 + int age = 23; + + // define a float variable on line 18 + float pi = 3.14f; + + // define a double variable on line 21 + double decimal = 23.2352536d; + + // define a boolean variable on line 24 (Hint: true/false) on line 24 + double dogsOut = true; // the dogs are out :) + + // define a character variable on line 27 + char letter = 'A'; + + // define a string variable on line 30 + char name = "Jeff"; + + } + } + \ No newline at end of file From a2a2eb84a3cd26b9502a8fc9f2420f0667b21f24 Mon Sep 17 00:00:00 2001 From: ArmeetJatyani Date: Mon, 15 Mar 2021 11:48:10 -0700 Subject: [PATCH 23/69] fixed VariableTypes package names --- .../beginner/chapter2/practice/VariableTypes.java | 2 +- .../beginner/chapter2/solutions/VariableTypes.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/com/codefortomorrow/beginner/chapter2/practice/VariableTypes.java b/src/com/codefortomorrow/beginner/chapter2/practice/VariableTypes.java index 3cb0ed4..a051136 100644 --- a/src/com/codefortomorrow/beginner/chapter2/practice/VariableTypes.java +++ b/src/com/codefortomorrow/beginner/chapter2/practice/VariableTypes.java @@ -1,4 +1,4 @@ -package com.codefortomorrow.beginner.chapter1.practice; +package com.codefortomorrow.beginner.chapter2.practice; /** * @author ArmeetJatyani diff --git a/src/com/codefortomorrow/beginner/chapter2/solutions/VariableTypes.java b/src/com/codefortomorrow/beginner/chapter2/solutions/VariableTypes.java index 36a6b48..f58142b 100644 --- a/src/com/codefortomorrow/beginner/chapter2/solutions/VariableTypes.java +++ b/src/com/codefortomorrow/beginner/chapter2/solutions/VariableTypes.java @@ -1,4 +1,4 @@ -package com.codefortomorrow.beginner.chapter1.practice; +package com.codefortomorrow.beginner.chapter2.practice; /** * @author ArmeetJatyani From b9f1238a4ddced74abe2b69d56b56ec4f9a84dca Mon Sep 17 00:00:00 2001 From: ArmeetJatyani Date: Mon, 15 Mar 2021 11:53:40 -0700 Subject: [PATCH 24/69] ApplesOranges exercise complete --- .../chapter2/practice/ApplesOranges.java | 28 +++++++++++++++++++ .../chapter2/solutions/ApplesOranges.java | 28 +++++++++++++++++++ 2 files changed, 56 insertions(+) create mode 100644 src/com/codefortomorrow/beginner/chapter2/practice/ApplesOranges.java create mode 100644 src/com/codefortomorrow/beginner/chapter2/solutions/ApplesOranges.java diff --git a/src/com/codefortomorrow/beginner/chapter2/practice/ApplesOranges.java b/src/com/codefortomorrow/beginner/chapter2/practice/ApplesOranges.java new file mode 100644 index 0000000..38be171 --- /dev/null +++ b/src/com/codefortomorrow/beginner/chapter2/practice/ApplesOranges.java @@ -0,0 +1,28 @@ +package com.codefortomorrow.beginner.chapter2.practice; + +/** + * @author ArmeetJatyani + * March 2021 + * + * Print out the number of apples and oranges! + */ + + public class ApplesOranges { + public static void main(String[] args) { + // write your code here + + // define an integer variable called numOranges with value 10 (line 15) + + + // define an integer variable called numApples with value 24 (line 18) + + + // print out number of oranges, output: "I have 10 oranges." (line 21) + + + // print out number of apples, output: "I have 24 apples." (line 24) + + + } + } + \ No newline at end of file diff --git a/src/com/codefortomorrow/beginner/chapter2/solutions/ApplesOranges.java b/src/com/codefortomorrow/beginner/chapter2/solutions/ApplesOranges.java new file mode 100644 index 0000000..9d35185 --- /dev/null +++ b/src/com/codefortomorrow/beginner/chapter2/solutions/ApplesOranges.java @@ -0,0 +1,28 @@ +package com.codefortomorrow.beginner.chapter2.practice; + +/** + * @author ArmeetJatyani + * March 2021 + * + * Print out the number of apples and oranges! + */ + + public class ApplesOranges { + public static void main(String[] args) { + // write your code here + + // define an integer variable called numOranges with value 10 (line 15) + int numOranges = 10; + + // define an integer variable called numApples with value 24 (line 18) + int numApples = 24; + + // print out number of oranges, output: "I have 10 oranges." (line 21) + System.out.println("I have " + numOranges + " oranges."); + + // print out number of apples, output: "I have 24 apples." (line 24) + System.out.println("I have " + numApples + " apples."); + + } + } + \ No newline at end of file From 3c4a0932d5db33c45a64ffbba701ba00937a5037 Mon Sep 17 00:00:00 2001 From: ArmeetJatyani Date: Mon, 15 Mar 2021 11:58:26 -0700 Subject: [PATCH 25/69] finished CarDealership exercise --- .../chapter4/practice/CarDealership.java | 28 +++++++++++++++++++ .../chapter4/solutions/CarDealership.java | 28 +++++++++++++++++++ 2 files changed, 56 insertions(+) create mode 100644 src/com/codefortomorrow/beginner/chapter4/practice/CarDealership.java create mode 100644 src/com/codefortomorrow/beginner/chapter4/solutions/CarDealership.java diff --git a/src/com/codefortomorrow/beginner/chapter4/practice/CarDealership.java b/src/com/codefortomorrow/beginner/chapter4/practice/CarDealership.java new file mode 100644 index 0000000..df2013f --- /dev/null +++ b/src/com/codefortomorrow/beginner/chapter4/practice/CarDealership.java @@ -0,0 +1,28 @@ +package com.codefortomorrow.beginner.chapter4.practice; + +/** + * @author ArmeetJatyani + * March 2021 + * + * Manage a car dealership and take inventory of all cars sold! + */ + + public class CarDealership { + public static void main(String[] args) { + // write your code here + + // define 3 integer constants, representing the prices of 3 cars sold in USD (line 15-17) + + + + + // define an integer variable, which represents the sum of the prices of these 3 cars (line 20) + + + // print out the formatted revenue after selling these 3 cars, output: "I sold 3 cars for $XXXXXX." (line 23) + + + + } + } + \ No newline at end of file diff --git a/src/com/codefortomorrow/beginner/chapter4/solutions/CarDealership.java b/src/com/codefortomorrow/beginner/chapter4/solutions/CarDealership.java new file mode 100644 index 0000000..fa3b1f4 --- /dev/null +++ b/src/com/codefortomorrow/beginner/chapter4/solutions/CarDealership.java @@ -0,0 +1,28 @@ +package com.codefortomorrow.beginner.chapter4.practice; + +/** + * @author ArmeetJatyani + * March 2021 + * + * Manage a car dealership and take inventory of all cars sold! + */ + + public class CarDealership { + public static void main(String[] args) { + // write your code here + + // define 3 integer constants, representing the prices of 3 cars sold in USD (line 15-17) + final int car1 = 30000; + final int car2 = 24650; + final int car3 = 253630; + + // define an integer variable, which represents the sum of the prices of these 3 cars (line 20) + int revenue = car1 + car2 + car3; + + // print out the formatted revenue after selling these 3 cars, output: "I sold 3 cars for $XXXXXX." (line 23) + System.out.println("I sold 3 cars for $" + revenue + "."); + + + } + } + \ No newline at end of file From f1858cbec7a53a3d9c1040eba0925a21e3db0a03 Mon Sep 17 00:00:00 2001 From: ArmeetJatyani Date: Mon, 15 Mar 2021 12:09:51 -0700 Subject: [PATCH 26/69] completed LinkedList implementation Challenge for Advanced Group --- .../chapter16/practice/LinkedList.java | 26 +++ .../chapter16/solutions/LinkedList.java | 159 ++++++++++++++++++ 2 files changed, 185 insertions(+) create mode 100644 src/com/codefortomorrow/advanced/chapter16/practice/LinkedList.java create mode 100644 src/com/codefortomorrow/advanced/chapter16/solutions/LinkedList.java diff --git a/src/com/codefortomorrow/advanced/chapter16/practice/LinkedList.java b/src/com/codefortomorrow/advanced/chapter16/practice/LinkedList.java new file mode 100644 index 0000000..6fe8bb7 --- /dev/null +++ b/src/com/codefortomorrow/advanced/chapter16/practice/LinkedList.java @@ -0,0 +1,26 @@ +package com.codefortomorrow.advanced.chapter16.practice; + +/** + * @author ArmeetJatyani + * March 2021 + * + * Implement a simple LinkedList + * You will need to create a LinkedListNode class, which represents each item/node in the linked list + * Required functionality... + * - head(): get head of list + * - tail(): get tail of list + * - add(): add to tail of list + * - push(): push to head of list + * - pop(): remove head of list + * - toString(): return a String representation of the list + */ + +public class LinkedList { + public static void main(String[] args) { + // write your code here + + } + +} + +class LinkedListNode \ No newline at end of file diff --git a/src/com/codefortomorrow/advanced/chapter16/solutions/LinkedList.java b/src/com/codefortomorrow/advanced/chapter16/solutions/LinkedList.java new file mode 100644 index 0000000..b9d18ae --- /dev/null +++ b/src/com/codefortomorrow/advanced/chapter16/solutions/LinkedList.java @@ -0,0 +1,159 @@ +package com.codefortomorrow.advanced.chapter16.solutions; + +// using Lombok for Getters and Setters +import lombok.Setter; +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.Setter; +// UUID to represent each node's unique ID +import java.util.UUID; + +/** + * @author ArmeetJatyani + * March 2021 + * + * Implement a simple LinkedList + * You will need to create a LinkedListNode class, which represents each item/node in the linked list + * Required functionality... + * - head(): get head of list + * - tail(): get tail of list + * - add(): add to tail of list + * - push(): push to head of list + * - pop(): remove head of list + * - toString(): return a String representation of the list + */ + +@Setter +public class LinkedList { + private LinkedListNode head; + + /** + * default constructor + */ + public LinkedList() { + head = null; + } + + /** + * constructor with first value + * @param value + */ + public LinkedList(int value) { + // create first node + head = new LinkedListNode(value, null); + } + + /** + * get head of Linked List + * @return + */ + public LinkedListNode head() { + return this.head; + } + + /** + * traverse and get tail of linked list + * @return + */ + public LinkedListNode tail() { + LinkedListNode current = head; + if(current == null) return null; + + while (current.getNext() != null) { + current = current.getNext(); + } + + return current; + } + + /** + * add new element (node) to linked list + * @param value + */ + public void add(int value) { + LinkedListNode tail = tail(); + if (tail == null) { + head = new LinkedListNode(value, null); + return; + } + + tail.setNext(new LinkedListNode(value, null)); + } + + /** + * push (add to head of linkedlist) + * @return + */ + public void push(int value) { + LinkedListNode newHead = new LinkedListNode(value, head); + head = newHead; + } + + /** + * pop (remove head of linkedlist) + * @return + */ + public LinkedListNode pop() { + LinkedListNode popped = head; + head = head.getNext(); + return popped; + } + + /** + * to String + * @return + */ + @Override + public String toString() { + String list = "["; + LinkedListNode current = head; + if(current == null) return null; + do { + list += Integer.toString(current.getValue()) + ", "; + current = current.getNext(); + } + while (current != null); + + list = list.substring(0, list.length() - 2); + return list + "]"; + } + +} + +@Getter +@Setter +class Node +{ + private UUID ID; + private int value; + + public Node(int value) + { + this.ID = UUID.randomUUID(); + this.value = value; + } + + public int value() + { + return this.value; + } +} + +@Getter +@Setter +class LinkedListNode extends Node +{ + private LinkedListNode next; + + public LinkedListNode(int value, LinkedListNode next) + { + super(value); + this.next = next; + } + + public LinkedListNode next() + { + return this.next; + } +} + From cfc050cf94f96eae351cf447a0b0388f2777a6af Mon Sep 17 00:00:00 2001 From: ArmeetJatyani Date: Mon, 15 Mar 2021 12:13:24 -0700 Subject: [PATCH 27/69] fix package name errors for Solutions --- .../codefortomorrow/beginner/chapter1/solutions/Comments.java | 2 +- .../codefortomorrow/beginner/chapter1/solutions/HelloWorld.java | 2 +- .../beginner/chapter2/solutions/ApplesOranges.java | 2 +- .../beginner/chapter2/solutions/VariableTypes.java | 2 +- .../beginner/chapter4/solutions/CarDealership.java | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/com/codefortomorrow/beginner/chapter1/solutions/Comments.java b/src/com/codefortomorrow/beginner/chapter1/solutions/Comments.java index a14a123..446b385 100644 --- a/src/com/codefortomorrow/beginner/chapter1/solutions/Comments.java +++ b/src/com/codefortomorrow/beginner/chapter1/solutions/Comments.java @@ -1,4 +1,4 @@ -package com.codefortomorrow.beginner.chapter1.practice; +package com.codefortomorrow.beginner.chapter1.solutions; /** * @author ArmeetJatyani diff --git a/src/com/codefortomorrow/beginner/chapter1/solutions/HelloWorld.java b/src/com/codefortomorrow/beginner/chapter1/solutions/HelloWorld.java index dbce821..39a0de1 100644 --- a/src/com/codefortomorrow/beginner/chapter1/solutions/HelloWorld.java +++ b/src/com/codefortomorrow/beginner/chapter1/solutions/HelloWorld.java @@ -1,4 +1,4 @@ -package com.codefortomorrow.beginner.chapter1.practice; +package com.codefortomorrow.beginner.chapter1.solutions; /** * @author ArmeetJatyani diff --git a/src/com/codefortomorrow/beginner/chapter2/solutions/ApplesOranges.java b/src/com/codefortomorrow/beginner/chapter2/solutions/ApplesOranges.java index 9d35185..28270b4 100644 --- a/src/com/codefortomorrow/beginner/chapter2/solutions/ApplesOranges.java +++ b/src/com/codefortomorrow/beginner/chapter2/solutions/ApplesOranges.java @@ -1,4 +1,4 @@ -package com.codefortomorrow.beginner.chapter2.practice; +package com.codefortomorrow.beginner.chapter2.solutions; /** * @author ArmeetJatyani diff --git a/src/com/codefortomorrow/beginner/chapter2/solutions/VariableTypes.java b/src/com/codefortomorrow/beginner/chapter2/solutions/VariableTypes.java index f58142b..d85af80 100644 --- a/src/com/codefortomorrow/beginner/chapter2/solutions/VariableTypes.java +++ b/src/com/codefortomorrow/beginner/chapter2/solutions/VariableTypes.java @@ -1,4 +1,4 @@ -package com.codefortomorrow.beginner.chapter2.practice; +package com.codefortomorrow.beginner.chapter2.solutions; /** * @author ArmeetJatyani diff --git a/src/com/codefortomorrow/beginner/chapter4/solutions/CarDealership.java b/src/com/codefortomorrow/beginner/chapter4/solutions/CarDealership.java index fa3b1f4..10fdd82 100644 --- a/src/com/codefortomorrow/beginner/chapter4/solutions/CarDealership.java +++ b/src/com/codefortomorrow/beginner/chapter4/solutions/CarDealership.java @@ -1,4 +1,4 @@ -package com.codefortomorrow.beginner.chapter4.practice; +package com.codefortomorrow.beginner.chapter4.solutions; /** * @author ArmeetJatyani From db58c32aee32be614f4029126a839eaac5494d9c Mon Sep 17 00:00:00 2001 From: ArmeetJatyani Date: Mon, 15 Mar 2021 12:24:43 -0700 Subject: [PATCH 28/69] formatted --- .../chapter16/solutions/LinkedList.java | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/src/com/codefortomorrow/advanced/chapter16/solutions/LinkedList.java b/src/com/codefortomorrow/advanced/chapter16/solutions/LinkedList.java index b9d18ae..0dbe171 100644 --- a/src/com/codefortomorrow/advanced/chapter16/solutions/LinkedList.java +++ b/src/com/codefortomorrow/advanced/chapter16/solutions/LinkedList.java @@ -122,37 +122,31 @@ public String toString() { @Getter @Setter -class Node -{ +class Node { private UUID ID; private int value; - public Node(int value) - { + public Node(int value) { this.ID = UUID.randomUUID(); this.value = value; } - public int value() - { + public int value() { return this.value; } } @Getter @Setter -class LinkedListNode extends Node -{ +class LinkedListNode extends Node { private LinkedListNode next; - public LinkedListNode(int value, LinkedListNode next) - { + public LinkedListNode(int value, LinkedListNode next) { super(value); this.next = next; } - public LinkedListNode next() - { + public LinkedListNode next() { return this.next; } } From 542423bdc96bedc8cc36ecf75b48a457071f2b8a Mon Sep 17 00:00:00 2001 From: ArmeetJatyani Date: Mon, 15 Mar 2021 12:27:20 -0700 Subject: [PATCH 29/69] attempt #2: fixing formatting --- .../chapter16/practice/LinkedList.java | 8 +++---- .../chapter16/solutions/LinkedList.java | 24 +++++++++---------- 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/src/com/codefortomorrow/advanced/chapter16/practice/LinkedList.java b/src/com/codefortomorrow/advanced/chapter16/practice/LinkedList.java index 6fe8bb7..834202e 100644 --- a/src/com/codefortomorrow/advanced/chapter16/practice/LinkedList.java +++ b/src/com/codefortomorrow/advanced/chapter16/practice/LinkedList.java @@ -3,7 +3,7 @@ /** * @author ArmeetJatyani * March 2021 - * + * * Implement a simple LinkedList * You will need to create a LinkedListNode class, which represents each item/node in the linked list * Required functionality... @@ -16,11 +16,11 @@ */ public class LinkedList { + public static void main(String[] args) { // write your code here - - } + } } -class LinkedListNode \ No newline at end of file +class LinkedListNode {} diff --git a/src/com/codefortomorrow/advanced/chapter16/solutions/LinkedList.java b/src/com/codefortomorrow/advanced/chapter16/solutions/LinkedList.java index 0dbe171..704e67e 100644 --- a/src/com/codefortomorrow/advanced/chapter16/solutions/LinkedList.java +++ b/src/com/codefortomorrow/advanced/chapter16/solutions/LinkedList.java @@ -1,17 +1,17 @@ package com.codefortomorrow.advanced.chapter16.solutions; -// using Lombok for Getters and Setters -import lombok.Setter; +// UUID to represent each node's unique ID +import java.util.UUID; import lombok.AllArgsConstructor; import lombok.Getter; +// using Lombok for Getters and Setters +import lombok.Setter; import lombok.Setter; -// UUID to represent each node's unique ID -import java.util.UUID; /** * @author ArmeetJatyani * March 2021 - * + * * Implement a simple LinkedList * You will need to create a LinkedListNode class, which represents each item/node in the linked list * Required functionality... @@ -25,6 +25,7 @@ @Setter public class LinkedList { + private LinkedListNode head; /** @@ -57,7 +58,7 @@ public LinkedListNode head() { */ public LinkedListNode tail() { LinkedListNode current = head; - if(current == null) return null; + if (current == null) return null; while (current.getNext() != null) { current = current.getNext(); @@ -107,22 +108,21 @@ public LinkedListNode pop() { public String toString() { String list = "["; LinkedListNode current = head; - if(current == null) return null; + if (current == null) return null; do { list += Integer.toString(current.getValue()) + ", "; current = current.getNext(); - } - while (current != null); + } while (current != null); list = list.substring(0, list.length() - 2); return list + "]"; } - } @Getter @Setter class Node { + private UUID ID; private int value; @@ -135,10 +135,11 @@ public int value() { return this.value; } } - + @Getter @Setter class LinkedListNode extends Node { + private LinkedListNode next; public LinkedListNode(int value, LinkedListNode next) { @@ -150,4 +151,3 @@ public LinkedListNode next() { return this.next; } } - From 85bda2c3b1ede2007e69b2c15285b56589ade446 Mon Sep 17 00:00:00 2001 From: ArmeetJatyani Date: Mon, 15 Mar 2021 14:43:18 -0700 Subject: [PATCH 30/69] adding to comments --- .../advanced/chapter16/solutions/LinkedList.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/com/codefortomorrow/advanced/chapter16/solutions/LinkedList.java b/src/com/codefortomorrow/advanced/chapter16/solutions/LinkedList.java index 704e67e..debfb34 100644 --- a/src/com/codefortomorrow/advanced/chapter16/solutions/LinkedList.java +++ b/src/com/codefortomorrow/advanced/chapter16/solutions/LinkedList.java @@ -4,7 +4,7 @@ import java.util.UUID; import lombok.AllArgsConstructor; import lombok.Getter; -// using Lombok for Getters and Setters +// using Lombok for Getters and Setters (saves lines) import lombok.Setter; import lombok.Setter; From e92fb417735dc9f608a56d26f7a9ebadcd9b5f1f Mon Sep 17 00:00:00 2001 From: Armeet Jatyani Date: Mon, 15 Mar 2021 17:14:33 -0700 Subject: [PATCH 31/69] Update src/com/codefortomorrow/advanced/chapter16/solutions/LinkedList.java Co-authored-by: Rebecca Dang <35876322+phrdang@users.noreply.github.com> --- .../advanced/chapter16/solutions/LinkedList.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/com/codefortomorrow/advanced/chapter16/solutions/LinkedList.java b/src/com/codefortomorrow/advanced/chapter16/solutions/LinkedList.java index debfb34..19c6d18 100644 --- a/src/com/codefortomorrow/advanced/chapter16/solutions/LinkedList.java +++ b/src/com/codefortomorrow/advanced/chapter16/solutions/LinkedList.java @@ -61,7 +61,7 @@ public LinkedListNode tail() { if (current == null) return null; while (current.getNext() != null) { - current = current.getNext(); + current = current.next(); } return current; From f6977c7b22c5bd5376bd743c987e99651f68a4a2 Mon Sep 17 00:00:00 2001 From: Armeet Jatyani Date: Mon, 15 Mar 2021 17:14:51 -0700 Subject: [PATCH 32/69] Update src/com/codefortomorrow/advanced/chapter16/solutions/LinkedList.java Co-authored-by: Rebecca Dang <35876322+phrdang@users.noreply.github.com> --- .../codefortomorrow/advanced/chapter16/solutions/LinkedList.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/com/codefortomorrow/advanced/chapter16/solutions/LinkedList.java b/src/com/codefortomorrow/advanced/chapter16/solutions/LinkedList.java index 19c6d18..5da7b83 100644 --- a/src/com/codefortomorrow/advanced/chapter16/solutions/LinkedList.java +++ b/src/com/codefortomorrow/advanced/chapter16/solutions/LinkedList.java @@ -75,7 +75,6 @@ public void add(int value) { LinkedListNode tail = tail(); if (tail == null) { head = new LinkedListNode(value, null); - return; } tail.setNext(new LinkedListNode(value, null)); From cca23c28f9924d178f83d2a1cc687908064ab964 Mon Sep 17 00:00:00 2001 From: Armeet Jatyani Date: Mon, 15 Mar 2021 17:15:00 -0700 Subject: [PATCH 33/69] Update src/com/codefortomorrow/beginner/chapter2/practice/VariableTypes.java Co-authored-by: Rebecca Dang <35876322+phrdang@users.noreply.github.com> --- .../beginner/chapter2/practice/VariableTypes.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/com/codefortomorrow/beginner/chapter2/practice/VariableTypes.java b/src/com/codefortomorrow/beginner/chapter2/practice/VariableTypes.java index a051136..e5b9678 100644 --- a/src/com/codefortomorrow/beginner/chapter2/practice/VariableTypes.java +++ b/src/com/codefortomorrow/beginner/chapter2/practice/VariableTypes.java @@ -20,7 +20,7 @@ public static void main(String[] args) { // define a double variable on line 21 - // define a boolean variable on line 24 (Hint: true/false) on line 24 + // define a boolean variable on line 24 (Hint: true/false) // define a character variable on line 27 @@ -31,4 +31,4 @@ public static void main(String[] args) { } } - \ No newline at end of file + From 47b672561dc240673993163f9a9e373f2dbf12bd Mon Sep 17 00:00:00 2001 From: Armeet Jatyani Date: Mon, 15 Mar 2021 17:15:10 -0700 Subject: [PATCH 34/69] Update src/com/codefortomorrow/beginner/chapter2/solutions/ApplesOranges.java Co-authored-by: Rebecca Dang <35876322+phrdang@users.noreply.github.com> --- .../beginner/chapter2/solutions/ApplesOranges.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/com/codefortomorrow/beginner/chapter2/solutions/ApplesOranges.java b/src/com/codefortomorrow/beginner/chapter2/solutions/ApplesOranges.java index 28270b4..ff9e4b3 100644 --- a/src/com/codefortomorrow/beginner/chapter2/solutions/ApplesOranges.java +++ b/src/com/codefortomorrow/beginner/chapter2/solutions/ApplesOranges.java @@ -17,7 +17,7 @@ public static void main(String[] args) { // define an integer variable called numApples with value 24 (line 18) int numApples = 24; - // print out number of oranges, output: "I have 10 oranges." (line 21) + // print out number of oranges using variables, output: "I have 10 oranges." (line 21) System.out.println("I have " + numOranges + " oranges."); // print out number of apples, output: "I have 24 apples." (line 24) @@ -25,4 +25,4 @@ public static void main(String[] args) { } } - \ No newline at end of file + From 199fb87262fca8da79471c86a00d17bd65fa44d4 Mon Sep 17 00:00:00 2001 From: Armeet Jatyani Date: Mon, 15 Mar 2021 17:15:15 -0700 Subject: [PATCH 35/69] Update src/com/codefortomorrow/beginner/chapter2/solutions/ApplesOranges.java Co-authored-by: Rebecca Dang <35876322+phrdang@users.noreply.github.com> --- .../beginner/chapter2/solutions/ApplesOranges.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/com/codefortomorrow/beginner/chapter2/solutions/ApplesOranges.java b/src/com/codefortomorrow/beginner/chapter2/solutions/ApplesOranges.java index ff9e4b3..b35c1e9 100644 --- a/src/com/codefortomorrow/beginner/chapter2/solutions/ApplesOranges.java +++ b/src/com/codefortomorrow/beginner/chapter2/solutions/ApplesOranges.java @@ -20,7 +20,7 @@ public static void main(String[] args) { // print out number of oranges using variables, output: "I have 10 oranges." (line 21) System.out.println("I have " + numOranges + " oranges."); - // print out number of apples, output: "I have 24 apples." (line 24) + // print out number of apples using variables, output: "I have 24 apples." (line 24) System.out.println("I have " + numApples + " apples."); } From 40b86623d2db965d3fc19c26fac7188467ad8971 Mon Sep 17 00:00:00 2001 From: Armeet Jatyani Date: Mon, 15 Mar 2021 17:15:56 -0700 Subject: [PATCH 36/69] Update src/com/codefortomorrow/beginner/chapter2/solutions/VariableTypes.java Co-authored-by: Rebecca Dang <35876322+phrdang@users.noreply.github.com> --- .../beginner/chapter2/solutions/VariableTypes.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/com/codefortomorrow/beginner/chapter2/solutions/VariableTypes.java b/src/com/codefortomorrow/beginner/chapter2/solutions/VariableTypes.java index d85af80..f6850ce 100644 --- a/src/com/codefortomorrow/beginner/chapter2/solutions/VariableTypes.java +++ b/src/com/codefortomorrow/beginner/chapter2/solutions/VariableTypes.java @@ -20,7 +20,7 @@ public static void main(String[] args) { // define a double variable on line 21 double decimal = 23.2352536d; - // define a boolean variable on line 24 (Hint: true/false) on line 24 + // define a boolean variable on line 24 (Hint: true/false) double dogsOut = true; // the dogs are out :) // define a character variable on line 27 @@ -31,4 +31,4 @@ public static void main(String[] args) { } } - \ No newline at end of file + From 1b3fbfda3036d56c80093e55f0385ab529e7c3b2 Mon Sep 17 00:00:00 2001 From: Armeet Jatyani Date: Mon, 15 Mar 2021 17:16:08 -0700 Subject: [PATCH 37/69] Update src/com/codefortomorrow/advanced/chapter16/solutions/LinkedList.java Co-authored-by: Rebecca Dang <35876322+phrdang@users.noreply.github.com> --- .../advanced/chapter16/solutions/LinkedList.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/com/codefortomorrow/advanced/chapter16/solutions/LinkedList.java b/src/com/codefortomorrow/advanced/chapter16/solutions/LinkedList.java index 5da7b83..cf19cbe 100644 --- a/src/com/codefortomorrow/advanced/chapter16/solutions/LinkedList.java +++ b/src/com/codefortomorrow/advanced/chapter16/solutions/LinkedList.java @@ -75,7 +75,7 @@ public void add(int value) { LinkedListNode tail = tail(); if (tail == null) { head = new LinkedListNode(value, null); - } + } else { tail.setNext(new LinkedListNode(value, null)); } From 66100104f2ee123900633b2512b744ce31a72fb1 Mon Sep 17 00:00:00 2001 From: Armeet Jatyani Date: Mon, 15 Mar 2021 17:17:10 -0700 Subject: [PATCH 38/69] Update src/com/codefortomorrow/advanced/chapter16/solutions/LinkedList.java Co-authored-by: Rebecca Dang <35876322+phrdang@users.noreply.github.com> --- .../advanced/chapter16/solutions/LinkedList.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/com/codefortomorrow/advanced/chapter16/solutions/LinkedList.java b/src/com/codefortomorrow/advanced/chapter16/solutions/LinkedList.java index cf19cbe..148ff74 100644 --- a/src/com/codefortomorrow/advanced/chapter16/solutions/LinkedList.java +++ b/src/com/codefortomorrow/advanced/chapter16/solutions/LinkedList.java @@ -77,7 +77,8 @@ public void add(int value) { head = new LinkedListNode(value, null); } else { - tail.setNext(new LinkedListNode(value, null)); + tail.setNext(new LinkedListNode(value, null)); + } } /** From 84ad074fe073ee6e82dabc2d13805a66fb255ba4 Mon Sep 17 00:00:00 2001 From: Armeet Jatyani Date: Mon, 15 Mar 2021 17:17:21 -0700 Subject: [PATCH 39/69] Update src/com/codefortomorrow/advanced/chapter16/solutions/LinkedList.java Co-authored-by: Rebecca Dang <35876322+phrdang@users.noreply.github.com> --- .../advanced/chapter16/solutions/LinkedList.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/com/codefortomorrow/advanced/chapter16/solutions/LinkedList.java b/src/com/codefortomorrow/advanced/chapter16/solutions/LinkedList.java index 148ff74..370ca83 100644 --- a/src/com/codefortomorrow/advanced/chapter16/solutions/LinkedList.java +++ b/src/com/codefortomorrow/advanced/chapter16/solutions/LinkedList.java @@ -96,7 +96,7 @@ public void push(int value) { */ public LinkedListNode pop() { LinkedListNode popped = head; - head = head.getNext(); + head = head.next(); return popped; } From 640cb85666225340e855552bc9b32bb1ec0def46 Mon Sep 17 00:00:00 2001 From: Armeet Jatyani Date: Mon, 15 Mar 2021 17:18:26 -0700 Subject: [PATCH 40/69] Update src/com/codefortomorrow/advanced/chapter16/practice/LinkedList.java Co-authored-by: Rebecca Dang <35876322+phrdang@users.noreply.github.com> --- .../codefortomorrow/advanced/chapter16/practice/LinkedList.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/com/codefortomorrow/advanced/chapter16/practice/LinkedList.java b/src/com/codefortomorrow/advanced/chapter16/practice/LinkedList.java index 834202e..04fd4f5 100644 --- a/src/com/codefortomorrow/advanced/chapter16/practice/LinkedList.java +++ b/src/com/codefortomorrow/advanced/chapter16/practice/LinkedList.java @@ -6,6 +6,7 @@ * * Implement a simple LinkedList * You will need to create a LinkedListNode class, which represents each item/node in the linked list + * Assume that the elements in the linked list are all of type int * Required functionality... * - head(): get head of list * - tail(): get tail of list From 80170097c62be724bcb0e5e7203187b8efcb03a9 Mon Sep 17 00:00:00 2001 From: Armeet Jatyani Date: Mon, 15 Mar 2021 17:18:40 -0700 Subject: [PATCH 41/69] Update src/com/codefortomorrow/advanced/chapter16/solutions/LinkedList.java Co-authored-by: Rebecca Dang <35876322+phrdang@users.noreply.github.com> --- .../advanced/chapter16/solutions/LinkedList.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/com/codefortomorrow/advanced/chapter16/solutions/LinkedList.java b/src/com/codefortomorrow/advanced/chapter16/solutions/LinkedList.java index 370ca83..5b26866 100644 --- a/src/com/codefortomorrow/advanced/chapter16/solutions/LinkedList.java +++ b/src/com/codefortomorrow/advanced/chapter16/solutions/LinkedList.java @@ -110,7 +110,7 @@ public String toString() { LinkedListNode current = head; if (current == null) return null; do { - list += Integer.toString(current.getValue()) + ", "; + list += Integer.toString(current.value()) + ", "; current = current.getNext(); } while (current != null); From e4e6db996f893a1b1093aa0f93f8e260dad90ec6 Mon Sep 17 00:00:00 2001 From: Armeet Jatyani Date: Mon, 15 Mar 2021 17:18:51 -0700 Subject: [PATCH 42/69] Update src/com/codefortomorrow/advanced/chapter16/solutions/LinkedList.java Co-authored-by: Rebecca Dang <35876322+phrdang@users.noreply.github.com> --- .../codefortomorrow/advanced/chapter16/solutions/LinkedList.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/com/codefortomorrow/advanced/chapter16/solutions/LinkedList.java b/src/com/codefortomorrow/advanced/chapter16/solutions/LinkedList.java index 5b26866..245d36b 100644 --- a/src/com/codefortomorrow/advanced/chapter16/solutions/LinkedList.java +++ b/src/com/codefortomorrow/advanced/chapter16/solutions/LinkedList.java @@ -114,6 +114,7 @@ public String toString() { current = current.getNext(); } while (current != null); + // Remove trailing comma and space after last list element list = list.substring(0, list.length() - 2); return list + "]"; } From 9ac5b2d43edc985b5a7268ce94fd95cd036b5338 Mon Sep 17 00:00:00 2001 From: ArmeetJatyani Date: Mon, 15 Mar 2021 17:20:45 -0700 Subject: [PATCH 43/69] made requested changes --- .gitignore | 2 -- .../advanced/chapter16/solutions/LinkedList.java | 12 ------------ 2 files changed, 14 deletions(-) diff --git a/.gitignore b/.gitignore index 9a0f431..0ec6afc 100644 --- a/.gitignore +++ b/.gitignore @@ -5,5 +5,3 @@ c4t-java.iml .DS_Store -# ignore prettier files -.prettierrc.json diff --git a/src/com/codefortomorrow/advanced/chapter16/solutions/LinkedList.java b/src/com/codefortomorrow/advanced/chapter16/solutions/LinkedList.java index debfb34..75c3dd1 100644 --- a/src/com/codefortomorrow/advanced/chapter16/solutions/LinkedList.java +++ b/src/com/codefortomorrow/advanced/chapter16/solutions/LinkedList.java @@ -2,11 +2,6 @@ // UUID to represent each node's unique ID import java.util.UUID; -import lombok.AllArgsConstructor; -import lombok.Getter; -// using Lombok for Getters and Setters (saves lines) -import lombok.Setter; -import lombok.Setter; /** * @author ArmeetJatyani @@ -23,7 +18,6 @@ * - toString(): return a String representation of the list */ -@Setter public class LinkedList { private LinkedListNode head; @@ -37,7 +31,6 @@ public LinkedList() { /** * constructor with first value - * @param value */ public LinkedList(int value) { // create first node @@ -69,7 +62,6 @@ public LinkedListNode tail() { /** * add new element (node) to linked list - * @param value */ public void add(int value) { LinkedListNode tail = tail(); @@ -119,8 +111,6 @@ public String toString() { } } -@Getter -@Setter class Node { private UUID ID; @@ -136,8 +126,6 @@ public int value() { } } -@Getter -@Setter class LinkedListNode extends Node { private LinkedListNode next; From 2d8da5dd3f1d449e1510860d8c79bd83bf3ee29a Mon Sep 17 00:00:00 2001 From: ArmeetJatyani Date: Mon, 15 Mar 2021 17:22:56 -0700 Subject: [PATCH 44/69] made all changes 2 --- .../advanced/chapter16/solutions/LinkedList.java | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/com/codefortomorrow/advanced/chapter16/solutions/LinkedList.java b/src/com/codefortomorrow/advanced/chapter16/solutions/LinkedList.java index 2ba949f..2f77dc2 100644 --- a/src/com/codefortomorrow/advanced/chapter16/solutions/LinkedList.java +++ b/src/com/codefortomorrow/advanced/chapter16/solutions/LinkedList.java @@ -53,7 +53,7 @@ public LinkedListNode tail() { LinkedListNode current = head; if (current == null) return null; - while (current.getNext() != null) { + while (current.next() != null) { current = current.next(); } @@ -68,7 +68,6 @@ public void add(int value) { if (tail == null) { head = new LinkedListNode(value, null); } else { - tail.setNext(new LinkedListNode(value, null)); } } @@ -103,7 +102,7 @@ public String toString() { if (current == null) return null; do { list += Integer.toString(current.value()) + ", "; - current = current.getNext(); + current = current.next(); } while (current != null); // Remove trailing comma and space after last list element @@ -139,4 +138,9 @@ public LinkedListNode(int value, LinkedListNode next) { public LinkedListNode next() { return this.next; } + + public void setNext(LinkedListNode next) { + this.next = next; + return; + } } From 17cb81d6e776cf8ea3edc1c5b7aadf1b75d12628 Mon Sep 17 00:00:00 2001 From: Armeet Jatyani Date: Mon, 15 Mar 2021 17:26:41 -0700 Subject: [PATCH 45/69] Update src/com/codefortomorrow/beginner/chapter1/practice/HelloWorld.java Co-authored-by: Rebecca Dang <35876322+phrdang@users.noreply.github.com> --- .../beginner/chapter1/practice/HelloWorld.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/com/codefortomorrow/beginner/chapter1/practice/HelloWorld.java b/src/com/codefortomorrow/beginner/chapter1/practice/HelloWorld.java index e2a15fe..832742a 100644 --- a/src/com/codefortomorrow/beginner/chapter1/practice/HelloWorld.java +++ b/src/com/codefortomorrow/beginner/chapter1/practice/HelloWorld.java @@ -10,10 +10,10 @@ */ // a class is an "object" where we will place all our code inside - public class HelloWorld{ + public class HelloWorld { // the main method (below) is the first thing that runs when your program is run public static void main(String[] args) { // write code here (replace the "" with "Hello World!") System.out.println(""); } - } \ No newline at end of file + } From acd3db80260f2cd82f3008ddae9a68b1353da769 Mon Sep 17 00:00:00 2001 From: Armeet Jatyani Date: Mon, 15 Mar 2021 17:26:57 -0700 Subject: [PATCH 46/69] Update src/com/codefortomorrow/beginner/chapter2/practice/ApplesOranges.java Co-authored-by: Rebecca Dang <35876322+phrdang@users.noreply.github.com> --- .../beginner/chapter2/practice/ApplesOranges.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/com/codefortomorrow/beginner/chapter2/practice/ApplesOranges.java b/src/com/codefortomorrow/beginner/chapter2/practice/ApplesOranges.java index 38be171..3a6cb45 100644 --- a/src/com/codefortomorrow/beginner/chapter2/practice/ApplesOranges.java +++ b/src/com/codefortomorrow/beginner/chapter2/practice/ApplesOranges.java @@ -17,7 +17,7 @@ public static void main(String[] args) { // define an integer variable called numApples with value 24 (line 18) - // print out number of oranges, output: "I have 10 oranges." (line 21) + // print out number of oranges using variables, output: "I have 10 oranges." (line 21) // print out number of apples, output: "I have 24 apples." (line 24) @@ -25,4 +25,4 @@ public static void main(String[] args) { } } - \ No newline at end of file + From 09959381b5dfd3e524a7db1b30f3cc87478613f3 Mon Sep 17 00:00:00 2001 From: Armeet Jatyani Date: Mon, 15 Mar 2021 17:27:03 -0700 Subject: [PATCH 47/69] Update src/com/codefortomorrow/beginner/chapter2/practice/ApplesOranges.java Co-authored-by: Rebecca Dang <35876322+phrdang@users.noreply.github.com> --- .../beginner/chapter2/practice/ApplesOranges.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/com/codefortomorrow/beginner/chapter2/practice/ApplesOranges.java b/src/com/codefortomorrow/beginner/chapter2/practice/ApplesOranges.java index 3a6cb45..bb28674 100644 --- a/src/com/codefortomorrow/beginner/chapter2/practice/ApplesOranges.java +++ b/src/com/codefortomorrow/beginner/chapter2/practice/ApplesOranges.java @@ -20,7 +20,7 @@ public static void main(String[] args) { // print out number of oranges using variables, output: "I have 10 oranges." (line 21) - // print out number of apples, output: "I have 24 apples." (line 24) + // print out number of apples using variables, output: "I have 24 apples." (line 24) } From 508db44ef4c6d814289390d83caf3bcd220935ee Mon Sep 17 00:00:00 2001 From: Rebecca Dang Date: Tue, 16 Mar 2021 15:24:33 -0700 Subject: [PATCH 48/69] Update/add javadoc comments (for consistency) --- .../chapter16/solutions/LinkedList.java | 39 +++++++++++++++---- 1 file changed, 31 insertions(+), 8 deletions(-) diff --git a/src/com/codefortomorrow/advanced/chapter16/solutions/LinkedList.java b/src/com/codefortomorrow/advanced/chapter16/solutions/LinkedList.java index 2f77dc2..848e482 100644 --- a/src/com/codefortomorrow/advanced/chapter16/solutions/LinkedList.java +++ b/src/com/codefortomorrow/advanced/chapter16/solutions/LinkedList.java @@ -9,6 +9,7 @@ * * Implement a simple LinkedList * You will need to create a LinkedListNode class, which represents each item/node in the linked list + * Assume that the elements in the linked list are all of type int * Required functionality... * - head(): get head of list * - tail(): get tail of list @@ -31,6 +32,7 @@ public LinkedList() { /** * constructor with first value + * @param value first element in the linked list */ public LinkedList(int value) { // create first node @@ -39,7 +41,7 @@ public LinkedList(int value) { /** * get head of Linked List - * @return + * @return first element of the linked list */ public LinkedListNode head() { return this.head; @@ -47,7 +49,7 @@ public LinkedListNode head() { /** * traverse and get tail of linked list - * @return + * @return last element of the linked list */ public LinkedListNode tail() { LinkedListNode current = head; @@ -62,6 +64,7 @@ public LinkedListNode tail() { /** * add new element (node) to linked list + * @param value element to add to the end of the linked list */ public void add(int value) { LinkedListNode tail = tail(); @@ -73,8 +76,7 @@ public void add(int value) { } /** - * push (add to head of linkedlist) - * @return + * push (add element to head of linkedlist) */ public void push(int value) { LinkedListNode newHead = new LinkedListNode(value, head); @@ -82,8 +84,8 @@ public void push(int value) { } /** - * pop (remove head of linkedlist) - * @return + * pop (remove and return head of linkedlist) + * @return the node that was removed */ public LinkedListNode pop() { LinkedListNode popped = head; @@ -92,8 +94,8 @@ public LinkedListNode pop() { } /** - * to String - * @return + * Returns a String version of the LinkedList + * @return a String version of the LinkedList */ @Override public String toString() { @@ -116,11 +118,19 @@ class Node { private UUID ID; private int value; + /** + * Constructs a list node with the given value + * @param value the value stored in this Node + */ public Node(int value) { this.ID = UUID.randomUUID(); this.value = value; } + /** + * Returns the value of this Node + * @return the value of this Node + */ public int value() { return this.value; } @@ -130,15 +140,28 @@ class LinkedListNode extends Node { private LinkedListNode next; + /** + * Constructs a LinkedListNode + * @param value the value stored in this node + * @param next the next node + */ public LinkedListNode(int value, LinkedListNode next) { super(value); this.next = next; } + /** + * Returns the next node + * @return the next node + */ public LinkedListNode next() { return this.next; } + /** + * Sets the next node + * @param next the next node + */ public void setNext(LinkedListNode next) { this.next = next; return; From 44713c450ca0f4208d10f17637b825716637e04a Mon Sep 17 00:00:00 2001 From: ArmeetJatyani Date: Mon, 22 Mar 2021 11:09:16 -0700 Subject: [PATCH 49/69] revision 3 --- .../advanced/chapter16/solutions/LinkedList.java | 5 ----- .../beginner/chapter2/solutions/VariableTypes.java | 2 +- 2 files changed, 1 insertion(+), 6 deletions(-) diff --git a/src/com/codefortomorrow/advanced/chapter16/solutions/LinkedList.java b/src/com/codefortomorrow/advanced/chapter16/solutions/LinkedList.java index 2f77dc2..1d69417 100644 --- a/src/com/codefortomorrow/advanced/chapter16/solutions/LinkedList.java +++ b/src/com/codefortomorrow/advanced/chapter16/solutions/LinkedList.java @@ -1,8 +1,5 @@ package com.codefortomorrow.advanced.chapter16.solutions; -// UUID to represent each node's unique ID -import java.util.UUID; - /** * @author ArmeetJatyani * March 2021 @@ -113,11 +110,9 @@ public String toString() { class Node { - private UUID ID; private int value; public Node(int value) { - this.ID = UUID.randomUUID(); this.value = value; } diff --git a/src/com/codefortomorrow/beginner/chapter2/solutions/VariableTypes.java b/src/com/codefortomorrow/beginner/chapter2/solutions/VariableTypes.java index f6850ce..0a3a64e 100644 --- a/src/com/codefortomorrow/beginner/chapter2/solutions/VariableTypes.java +++ b/src/com/codefortomorrow/beginner/chapter2/solutions/VariableTypes.java @@ -15,7 +15,7 @@ public static void main(String[] args) { int age = 23; // define a float variable on line 18 - float pi = 3.14f; + float decimal = 23.32544f; // define a double variable on line 21 double decimal = 23.2352536d; From 48dc66b16bf09c52caf6c13e078a8906f9c130ca Mon Sep 17 00:00:00 2001 From: Armeet Jatyani Date: Tue, 23 Mar 2021 10:14:01 -0700 Subject: [PATCH 50/69] Update src/com/codefortomorrow/beginner/chapter1/solutions/HelloWorld.java Co-authored-by: JJ27 <66833672+JJ27@users.noreply.github.com> --- .../beginner/chapter1/solutions/HelloWorld.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/com/codefortomorrow/beginner/chapter1/solutions/HelloWorld.java b/src/com/codefortomorrow/beginner/chapter1/solutions/HelloWorld.java index 39a0de1..a1152d2 100644 --- a/src/com/codefortomorrow/beginner/chapter1/solutions/HelloWorld.java +++ b/src/com/codefortomorrow/beginner/chapter1/solutions/HelloWorld.java @@ -10,10 +10,10 @@ */ // a class is an "object" where we will place all our code inside - public class HelloWorld{ + public class HelloWorld { // the main method (below) is the first thing that runs when your program is run public static void main(String[] args) { // write code here (replace the "" with "Hello World!") System.out.println("Hello World!"); } - } \ No newline at end of file + } From e0b585ac9b991b68551ea0bee2bd0b437a293c48 Mon Sep 17 00:00:00 2001 From: Rebecca Dang Date: Wed, 30 Jun 2021 18:13:58 -0700 Subject: [PATCH 51/69] Add actual TestSelectionSort solution --- .../solutions/TestSelectionSort.java | 74 ++++++++++++++----- 1 file changed, 55 insertions(+), 19 deletions(-) diff --git a/src/com/codefortomorrow/intermediate/chapter10/solutions/TestSelectionSort.java b/src/com/codefortomorrow/intermediate/chapter10/solutions/TestSelectionSort.java index d95b1a5..f7ddd4b 100644 --- a/src/com/codefortomorrow/intermediate/chapter10/solutions/TestSelectionSort.java +++ b/src/com/codefortomorrow/intermediate/chapter10/solutions/TestSelectionSort.java @@ -1,32 +1,68 @@ package com.codefortomorrow.intermediate.chapter10.solutions; +import java.util.Scanner; + /* - * Adapted from Exercise 8.5, - * Introduction to Java Programming (Comprehensive) - * by Y. Daniel Liang, 10th ed. - * - * Create a program called AddMatrices which - * adds two 3x3 matrices and prints the result. - * The program should prompt the user to enter - * data for 2 matrices and then displays those - * 2 matrices and the sum matrix. + * Create a driver class called TestSelectionSort + * which prompts the user to enter the length of + * an array and the elements (doubles) in the array. + * Then, your program should use selection sort to + * sort the array in place, and then print the resulting array. * * Example output: * - * Enter matrix1: 1 2 3 4 5 6 7 8 9 - * Enter matrix2: 0 2 4 1 4.5 2.2 1.1 4.3 5.2 - * The matrices are added as follows - * 1.0 2.0 3.0 0.0 2.0 4.0 1.0 4.0 7.0 - * 4.0 5.0 6.0 + 1.0 4.5 2.2 = 5.0 9.5 8.2 - * 7.0 8.0 9.0 1.1 4.3 5.2 8.1 12.3 14.2 - * - * Hint: You may need to use the Double.parseDouble("a string") - * to convert the user input to doubles. + * Enter the length of the array: 5 + * Enter the elements in the array: -1 236.3 2 6 0 + * -1.0 0.0 2.0 6.0 236.3 */ public class TestSelectionSort { public static void main(String[] args) { - // write code here + // Prompt user for length of array + System.out.print("Enter the length of the array: "); + Scanner input = new Scanner(System.in); + int arrayLength = input.nextInt(); + + // Initialize array + double[] list = new double[arrayLength]; + + // Prompt user for elements of the array + System.out.print("Enter the elements in the array: "); + for (int i = 0; i < arrayLength; i++) { + list[i] = input.nextDouble(); + } + + // goes up to index list.length - 1 + // because you don't need to check last element + // since it will be sorted already + for (int i = 0; i < list.length - 1; i++) { + double min = list[i]; + int minIndex = i; + + // find the current smallest element + for (int j = i + 1; j < list.length; j++) { + if (list[j] < min) { + min = list[j]; + minIndex = j; + } + } + + // swap it with whatever is in the + // first position of unsorted array if needed + if (minIndex != i) { + double temp = list[i]; + list[i] = min; + list[minIndex] = temp; + } + } + + // print the sorted array + for (double n : list) { + System.out.print(n + " "); + } + System.out.println(); // move cursor to next line + + input.close(); } } From 89c8bc7d8f39149d3cf65f35541e490815016712 Mon Sep 17 00:00:00 2001 From: phrdang Date: Thu, 1 Jul 2021 01:16:27 +0000 Subject: [PATCH 52/69] Bot: Prettified Java code! --- .../beginner/chapter1/practice/Comments.java | 23 ++++++++----------- .../chapter1/practice/HelloWorld.java | 11 +++++---- .../beginner/chapter1/solutions/Comments.java | 19 +++++++-------- .../chapter1/solutions/HelloWorld.java | 11 +++++---- .../chapter2/practice/ApplesOranges.java | 14 ++++------- .../chapter2/practice/VariableTypes.java | 18 +++++---------- .../chapter2/solutions/ApplesOranges.java | 15 ++++++------ .../chapter2/solutions/VariableTypes.java | 19 ++++++++------- .../chapter4/practice/CarDealership.java | 18 +++++---------- .../chapter4/solutions/CarDealership.java | 14 +++++------ 10 files changed, 70 insertions(+), 92 deletions(-) diff --git a/src/com/codefortomorrow/beginner/chapter1/practice/Comments.java b/src/com/codefortomorrow/beginner/chapter1/practice/Comments.java index cebb705..cb032a4 100644 --- a/src/com/codefortomorrow/beginner/chapter1/practice/Comments.java +++ b/src/com/codefortomorrow/beginner/chapter1/practice/Comments.java @@ -3,16 +3,17 @@ /** * @author ArmeetJatyani * March 2021 - * - * You'll be writing your first ever comment today! - * What we'll go over: + * + * You'll be writing your first ever comment today! + * What we'll go over: * - single line comments * - multi line comments */ - // a class is an "object" where we will place all our code inside - public class Comments { - // the main method (below) is the first thing that runs when your program is run +// a class is an "object" where we will place all our code inside +public class Comments { + + // the main method (below) is the first thing that runs when your program is run public static void main(String[] args) { // this is a single line comment, I can write anything here // single line comments aren't run by Java! @@ -22,17 +23,11 @@ public static void main(String[] args) { * this is a multi * line * comment - * + * * It can span across multiple lines! */ // YOUR ASSIGNMENT: write 1 single-line comment and 1 multi-line comment on the lines below... - - - - - - } - } \ No newline at end of file +} diff --git a/src/com/codefortomorrow/beginner/chapter1/practice/HelloWorld.java b/src/com/codefortomorrow/beginner/chapter1/practice/HelloWorld.java index 832742a..4fd1294 100644 --- a/src/com/codefortomorrow/beginner/chapter1/practice/HelloWorld.java +++ b/src/com/codefortomorrow/beginner/chapter1/practice/HelloWorld.java @@ -3,17 +3,18 @@ /** * @author ArmeetJatyani * March 2021 - * - * Welcome to Java! + * + * Welcome to Java! * This may be your first ever java program! * We'll begin your journey with the infamous Hello World program! */ - // a class is an "object" where we will place all our code inside - public class HelloWorld { +// a class is an "object" where we will place all our code inside +public class HelloWorld { + // the main method (below) is the first thing that runs when your program is run public static void main(String[] args) { // write code here (replace the "" with "Hello World!") System.out.println(""); } - } +} diff --git a/src/com/codefortomorrow/beginner/chapter1/solutions/Comments.java b/src/com/codefortomorrow/beginner/chapter1/solutions/Comments.java index 446b385..faaa644 100644 --- a/src/com/codefortomorrow/beginner/chapter1/solutions/Comments.java +++ b/src/com/codefortomorrow/beginner/chapter1/solutions/Comments.java @@ -3,16 +3,17 @@ /** * @author ArmeetJatyani * March 2021 - * - * You'll be writing your first ever comment today! - * What we'll go over: + * + * You'll be writing your first ever comment today! + * What we'll go over: * - single line comments * - multi line comments */ - // a class is an "object" where we will place all our code inside - public class Comments { - // the main method (below) is the first thing that runs when your program is run +// a class is an "object" where we will place all our code inside +public class Comments { + + // the main method (below) is the first thing that runs when your program is run public static void main(String[] args) { // this is a single line comment, I can write anything here // single line comments aren't run by Java! @@ -22,7 +23,7 @@ public static void main(String[] args) { * this is a multi * line * comment - * + * * It can span across multiple lines! */ @@ -31,8 +32,8 @@ public static void main(String[] args) { // Hi my name is Armeet! /** - * I like teaching Java, and + * I like teaching Java, and * good luck on your journey! */ } - } \ No newline at end of file +} diff --git a/src/com/codefortomorrow/beginner/chapter1/solutions/HelloWorld.java b/src/com/codefortomorrow/beginner/chapter1/solutions/HelloWorld.java index a1152d2..e645eff 100644 --- a/src/com/codefortomorrow/beginner/chapter1/solutions/HelloWorld.java +++ b/src/com/codefortomorrow/beginner/chapter1/solutions/HelloWorld.java @@ -3,17 +3,18 @@ /** * @author ArmeetJatyani * March 2021 - * - * Welcome to Java! + * + * Welcome to Java! * This may be your first ever java program! * We'll begin your journey with the infamous Hello World program! */ - // a class is an "object" where we will place all our code inside - public class HelloWorld { +// a class is an "object" where we will place all our code inside +public class HelloWorld { + // the main method (below) is the first thing that runs when your program is run public static void main(String[] args) { // write code here (replace the "" with "Hello World!") System.out.println("Hello World!"); } - } +} diff --git a/src/com/codefortomorrow/beginner/chapter2/practice/ApplesOranges.java b/src/com/codefortomorrow/beginner/chapter2/practice/ApplesOranges.java index bb28674..5c91b50 100644 --- a/src/com/codefortomorrow/beginner/chapter2/practice/ApplesOranges.java +++ b/src/com/codefortomorrow/beginner/chapter2/practice/ApplesOranges.java @@ -3,26 +3,22 @@ /** * @author ArmeetJatyani * March 2021 - * + * * Print out the number of apples and oranges! */ - public class ApplesOranges { +public class ApplesOranges { + public static void main(String[] args) { // write your code here - + // define an integer variable called numOranges with value 10 (line 15) - // define an integer variable called numApples with value 24 (line 18) - // print out number of oranges using variables, output: "I have 10 oranges." (line 21) - // print out number of apples using variables, output: "I have 24 apples." (line 24) - } - } - +} diff --git a/src/com/codefortomorrow/beginner/chapter2/practice/VariableTypes.java b/src/com/codefortomorrow/beginner/chapter2/practice/VariableTypes.java index e5b9678..b0848ac 100644 --- a/src/com/codefortomorrow/beginner/chapter2/practice/VariableTypes.java +++ b/src/com/codefortomorrow/beginner/chapter2/practice/VariableTypes.java @@ -3,32 +3,26 @@ /** * @author ArmeetJatyani * March 2021 - * + * * Define different types of variables */ - public class VariableTypes { +public class VariableTypes { + public static void main(String[] args) { // write your code here - + // define an integer variable on line 15 - // define a float variable on line 18 - // define a double variable on line 21 - // define a boolean variable on line 24 (Hint: true/false) - // define a character variable on line 27 - - // define a string variable on line 30 - + // define a string variable on line 30 } - } - +} diff --git a/src/com/codefortomorrow/beginner/chapter2/solutions/ApplesOranges.java b/src/com/codefortomorrow/beginner/chapter2/solutions/ApplesOranges.java index b35c1e9..e45b29a 100644 --- a/src/com/codefortomorrow/beginner/chapter2/solutions/ApplesOranges.java +++ b/src/com/codefortomorrow/beginner/chapter2/solutions/ApplesOranges.java @@ -3,26 +3,25 @@ /** * @author ArmeetJatyani * March 2021 - * + * * Print out the number of apples and oranges! */ - public class ApplesOranges { +public class ApplesOranges { + public static void main(String[] args) { // write your code here - + // define an integer variable called numOranges with value 10 (line 15) int numOranges = 10; - + // define an integer variable called numApples with value 24 (line 18) int numApples = 24; - + // print out number of oranges using variables, output: "I have 10 oranges." (line 21) System.out.println("I have " + numOranges + " oranges."); // print out number of apples using variables, output: "I have 24 apples." (line 24) System.out.println("I have " + numApples + " apples."); - } - } - +} diff --git a/src/com/codefortomorrow/beginner/chapter2/solutions/VariableTypes.java b/src/com/codefortomorrow/beginner/chapter2/solutions/VariableTypes.java index 0a3a64e..6d9785b 100644 --- a/src/com/codefortomorrow/beginner/chapter2/solutions/VariableTypes.java +++ b/src/com/codefortomorrow/beginner/chapter2/solutions/VariableTypes.java @@ -3,32 +3,31 @@ /** * @author ArmeetJatyani * March 2021 - * + * * Define different types of variables */ - public class VariableTypes { +public class VariableTypes { + public static void main(String[] args) { // write your code here - + // define an integer variable on line 15 int age = 23; - + // define a float variable on line 18 float decimal = 23.32544f; - + // define a double variable on line 21 double decimal = 23.2352536d; // define a boolean variable on line 24 (Hint: true/false) - double dogsOut = true; // the dogs are out :) + double dogsOut = true; // the dogs are out :) // define a character variable on line 27 char letter = 'A'; - // define a string variable on line 30 + // define a string variable on line 30 char name = "Jeff"; - } - } - +} diff --git a/src/com/codefortomorrow/beginner/chapter4/practice/CarDealership.java b/src/com/codefortomorrow/beginner/chapter4/practice/CarDealership.java index df2013f..27c3110 100644 --- a/src/com/codefortomorrow/beginner/chapter4/practice/CarDealership.java +++ b/src/com/codefortomorrow/beginner/chapter4/practice/CarDealership.java @@ -3,26 +3,20 @@ /** * @author ArmeetJatyani * March 2021 - * + * * Manage a car dealership and take inventory of all cars sold! */ - public class CarDealership { +public class CarDealership { + public static void main(String[] args) { // write your code here - - // define 3 integer constants, representing the prices of 3 cars sold in USD (line 15-17) - - + // define 3 integer constants, representing the prices of 3 cars sold in USD (line 15-17) // define an integer variable, which represents the sum of the prices of these 3 cars (line 20) - - - // print out the formatted revenue after selling these 3 cars, output: "I sold 3 cars for $XXXXXX." (line 23) - + // print out the formatted revenue after selling these 3 cars, output: "I sold 3 cars for $XXXXXX." (line 23) } - } - \ No newline at end of file +} diff --git a/src/com/codefortomorrow/beginner/chapter4/solutions/CarDealership.java b/src/com/codefortomorrow/beginner/chapter4/solutions/CarDealership.java index 10fdd82..98f48fb 100644 --- a/src/com/codefortomorrow/beginner/chapter4/solutions/CarDealership.java +++ b/src/com/codefortomorrow/beginner/chapter4/solutions/CarDealership.java @@ -3,14 +3,15 @@ /** * @author ArmeetJatyani * March 2021 - * + * * Manage a car dealership and take inventory of all cars sold! */ - public class CarDealership { +public class CarDealership { + public static void main(String[] args) { // write your code here - + // define 3 integer constants, representing the prices of 3 cars sold in USD (line 15-17) final int car1 = 30000; final int car2 = 24650; @@ -18,11 +19,8 @@ public static void main(String[] args) { // define an integer variable, which represents the sum of the prices of these 3 cars (line 20) int revenue = car1 + car2 + car3; - + // print out the formatted revenue after selling these 3 cars, output: "I sold 3 cars for $XXXXXX." (line 23) System.out.println("I sold 3 cars for $" + revenue + "."); - - } - } - \ No newline at end of file +} From bc785e4dbc71a5ef73a1da020bdcc4c886485056 Mon Sep 17 00:00:00 2001 From: Rebecca Dang Date: Wed, 30 Jun 2021 19:01:38 -0700 Subject: [PATCH 53/69] Fix package statement --- .../advanced/chapter13/solutions/NegativeSum.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/com/codefortomorrow/advanced/chapter13/solutions/NegativeSum.java b/src/com/codefortomorrow/advanced/chapter13/solutions/NegativeSum.java index 370528d..e0f2b71 100644 --- a/src/com/codefortomorrow/advanced/chapter13/solutions/NegativeSum.java +++ b/src/com/codefortomorrow/advanced/chapter13/solutions/NegativeSum.java @@ -1,4 +1,4 @@ -package com.codefortomorrow.advanced.chapter13.practice; +package com.codefortomorrow.advanced.chapter13.solutions; /* Implement the iterative code as a recursive From 87dc1123ed6e18286575814d57f61a8b5879d8a4 Mon Sep 17 00:00:00 2001 From: Rebecca Dang Date: Wed, 30 Jun 2021 19:04:14 -0700 Subject: [PATCH 54/69] Fix Account exception handling problem - Consolidate into 1 class called Account.java - Clarify instructions and post instructions in the file - Close Scanner object when done --- .../advanced/chapter14/practice/Account.java | 58 +++++++++ .../practice/account/InvalidAgeException.java | 3 - .../account/InvalidPasswordException.java | 3 - .../account/InvalidUsernameException.java | 3 - .../chapter14/practice/account/Main.java | 36 ------ .../account/PasswordMismatchException.java | 3 - .../advanced/chapter14/solutions/Account.java | 110 ++++++++++++++++++ .../account/InvalidAgeException.java | 3 - .../account/InvalidPasswordException.java | 3 - .../account/InvalidUsernameException.java | 3 - .../chapter14/solutions/account/Main.java | 57 --------- .../account/PasswordMismatchException.java | 3 - 12 files changed, 168 insertions(+), 117 deletions(-) create mode 100644 src/com/codefortomorrow/advanced/chapter14/practice/Account.java delete mode 100644 src/com/codefortomorrow/advanced/chapter14/practice/account/InvalidAgeException.java delete mode 100644 src/com/codefortomorrow/advanced/chapter14/practice/account/InvalidPasswordException.java delete mode 100644 src/com/codefortomorrow/advanced/chapter14/practice/account/InvalidUsernameException.java delete mode 100644 src/com/codefortomorrow/advanced/chapter14/practice/account/Main.java delete mode 100644 src/com/codefortomorrow/advanced/chapter14/practice/account/PasswordMismatchException.java create mode 100644 src/com/codefortomorrow/advanced/chapter14/solutions/Account.java delete mode 100644 src/com/codefortomorrow/advanced/chapter14/solutions/account/InvalidAgeException.java delete mode 100644 src/com/codefortomorrow/advanced/chapter14/solutions/account/InvalidPasswordException.java delete mode 100644 src/com/codefortomorrow/advanced/chapter14/solutions/account/InvalidUsernameException.java delete mode 100644 src/com/codefortomorrow/advanced/chapter14/solutions/account/Main.java delete mode 100644 src/com/codefortomorrow/advanced/chapter14/solutions/account/PasswordMismatchException.java diff --git a/src/com/codefortomorrow/advanced/chapter14/practice/Account.java b/src/com/codefortomorrow/advanced/chapter14/practice/Account.java new file mode 100644 index 0000000..9b6477b --- /dev/null +++ b/src/com/codefortomorrow/advanced/chapter14/practice/Account.java @@ -0,0 +1,58 @@ +package com.codefortomorrow.advanced.chapter14.practice; + +/* +Create the following classes that simply extend the `Exception` class: + +- `InvalidUsernameException` +- `InvalidPasswordException` +- `InvalidAgeException` +- `PasswordMismatchException` + +In the `Account` class, code a method called `createAccount` that takes a `username`, `age`, +`password`, and `confirmPassword` as parameters. + +In `createAccount`, throw an: + +- `InvalidUsernameException` - if the given username is <4 or >10 characters. +- `InvalidPasswordException` - if the given password is <4 or >10 characters. +- `InvalidAgeException` - if the given age is <18. +- `PasswordMismatchException` - if the given password does not match the given confirm password. + +In your `main` method (within the `Account` class): + +- To simulate the creation of an account online, prompt the user to enter their username, age, + password, and their password again (to confirm). +- Call the `createAccount` method continuously with the user's input and use `try`/`catch` to + handle the possible exceptions and print the appropriate error message. (You can assume that + the user input will only throw at most 1 exception.) +- Keep asking the user for input until the account is created successfully. + +Example output: + +``` +Welcome to Account Creation! +Enter username (4 to 10 characters): hi +Enter age: 18 +Enter password (4 to 10 characters): thisislong +Confirm password (4 to 10 characters): thisislong +Invalid username. +Enter username (4 to 10 characters): four +Enter age: 18 +Enter password (4 to 10 characters): thisislong +Confirm password (4 to 10 characters): thisislong +Account created successfully! +``` +*/ + +public class Account { + + public static void main(String[] args) { + // Write your code here + } + + public static void createAccount(String username, int age, String password, String confirmPassword) { + // Write your code here + } +} + +// Write exception classes here diff --git a/src/com/codefortomorrow/advanced/chapter14/practice/account/InvalidAgeException.java b/src/com/codefortomorrow/advanced/chapter14/practice/account/InvalidAgeException.java deleted file mode 100644 index 0842368..0000000 --- a/src/com/codefortomorrow/advanced/chapter14/practice/account/InvalidAgeException.java +++ /dev/null @@ -1,3 +0,0 @@ -package com.codefortomorrow.advanced.chapter14.practice.account; - -public class InvalidAgeException {} diff --git a/src/com/codefortomorrow/advanced/chapter14/practice/account/InvalidPasswordException.java b/src/com/codefortomorrow/advanced/chapter14/practice/account/InvalidPasswordException.java deleted file mode 100644 index f6f6029..0000000 --- a/src/com/codefortomorrow/advanced/chapter14/practice/account/InvalidPasswordException.java +++ /dev/null @@ -1,3 +0,0 @@ -package com.codefortomorrow.advanced.chapter14.practice.account; - -public class InvalidPasswordException {} diff --git a/src/com/codefortomorrow/advanced/chapter14/practice/account/InvalidUsernameException.java b/src/com/codefortomorrow/advanced/chapter14/practice/account/InvalidUsernameException.java deleted file mode 100644 index 677b349..0000000 --- a/src/com/codefortomorrow/advanced/chapter14/practice/account/InvalidUsernameException.java +++ /dev/null @@ -1,3 +0,0 @@ -package com.codefortomorrow.advanced.chapter14.practice.account; - -public class InvalidUsernameException {} diff --git a/src/com/codefortomorrow/advanced/chapter14/practice/account/Main.java b/src/com/codefortomorrow/advanced/chapter14/practice/account/Main.java deleted file mode 100644 index 5dcaf7a..0000000 --- a/src/com/codefortomorrow/advanced/chapter14/practice/account/Main.java +++ /dev/null @@ -1,36 +0,0 @@ -package com.codefortomorrow.advanced.chapter14.practice.account; - -import java.util.Scanner; - -public class Main { - - public static void main(String[] args) { - Scanner sc = new Scanner(System.in); - - System.out.println("Welcome to Account Creation!"); - while (true) { - System.out.print("Enter username (4 to 10 characters): "); - String username = sc.next(); - System.out.print("Enter age: "); - int age = sc.nextInt(); - System.out.print("Enter password (4 to 10 characters): "); - String password = sc.next(); - System.out.print("Confirm password (4 to 10 characters): "); - String confirmPassword = sc.next(); - - // TODO: try and catch to handle exceptions - createAccount(username, password, age, confirmPassword); - System.out.println("Account created successfully!"); - } - } - - public static void createAccount( - String username, - String password, - int age, - String confirmPassword - ) - throws InvalidAgeException, InvalidPasswordException, InvalidUsernameException, PasswordMismatchException { - // TODO: complete - } -} diff --git a/src/com/codefortomorrow/advanced/chapter14/practice/account/PasswordMismatchException.java b/src/com/codefortomorrow/advanced/chapter14/practice/account/PasswordMismatchException.java deleted file mode 100644 index e2f7c98..0000000 --- a/src/com/codefortomorrow/advanced/chapter14/practice/account/PasswordMismatchException.java +++ /dev/null @@ -1,3 +0,0 @@ -package com.codefortomorrow.advanced.chapter14.practice.account; - -public class PasswordMismatchException {} diff --git a/src/com/codefortomorrow/advanced/chapter14/solutions/Account.java b/src/com/codefortomorrow/advanced/chapter14/solutions/Account.java new file mode 100644 index 0000000..54990a4 --- /dev/null +++ b/src/com/codefortomorrow/advanced/chapter14/solutions/Account.java @@ -0,0 +1,110 @@ +package com.codefortomorrow.advanced.chapter14.solutions; + +import java.util.Scanner; + +/* +Create the following classes that simply extend the `Exception` class: + +- `InvalidUsernameException` +- `InvalidPasswordException` +- `InvalidAgeException` +- `PasswordMismatchException` + +In the `Account` class, code a method called `createAccount` that takes a `username`, `age`, +`password`, and `confirmPassword` as parameters. + +In `createAccount`, throw an: + +- `InvalidUsernameException` - if the given username is <4 or >10 characters. +- `InvalidPasswordException` - if the given password is <4 or >10 characters. +- `InvalidAgeException` - if the given age is <18. +- `PasswordMismatchException` - if the given password does not match the given confirm password. + +In your `main` method (within the `Account` class): + +- To simulate the creation of an account online, prompt the user to enter their username, age, + password, and their password again (to confirm). +- Call the `createAccount` method continuously with the user's input and use `try`/`catch` to + handle the possible exceptions and print the appropriate error message. (You can assume that + the user input will only throw at most 1 exception.) +- Keep asking the user for input until the account is created successfully. + +Example output: + +``` +Welcome to Account Creation! +Enter username (4 to 10 characters): hi +Enter age: 18 +Enter password (4 to 10 characters): thisislong +Confirm password (4 to 10 characters): thisislong +Invalid username. +Enter username (4 to 10 characters): four +Enter age: 18 +Enter password (4 to 10 characters): thisislong +Confirm password (4 to 10 characters): thisislong +Account created successfully! +``` +*/ + +public class Account { + + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + + System.out.println("Welcome to Account Creation!"); + while (true) { + System.out.print("Enter username (4 to 10 characters): "); + String username = sc.next(); + System.out.print("Enter age: "); + int age = sc.nextInt(); + System.out.print("Enter password (4 to 10 characters): "); + String password = sc.next(); + System.out.print("Confirm password (4 to 10 characters): "); + String confirmPassword = sc.next(); + + try { + createAccount(username, age, password, confirmPassword); + System.out.println("Account created successfully!"); + sc.close(); + break; + } catch (InvalidUsernameException e) { + System.out.println("Invalid username."); + } catch (InvalidPasswordException e) { + System.out.println("Invalid password."); + } catch (InvalidAgeException e) { + System.out.println("Must be 18 or older to create an account."); + } catch (PasswordMismatchException e) { + System.out.println("Passwords don't match!"); + } + } + } + + public static void createAccount( + String username, + int age, + String password, + String confirmPassword + ) + throws InvalidAgeException, InvalidPasswordException, InvalidUsernameException, PasswordMismatchException { + if (username.length() < 4 || username.length() > 10) { + throw new InvalidUsernameException(); + } + + if (age < 18) { + throw new InvalidAgeException(); + } + + if (password.length() < 4 || password.length() > 10) { + throw new InvalidPasswordException(); + } + + if (!password.equals(confirmPassword)) { + throw new PasswordMismatchException(); + } + } +} + +class InvalidUsernameException extends Exception {} +class InvalidPasswordException extends Exception {} +class InvalidAgeException extends Exception {} +class PasswordMismatchException extends Exception {} diff --git a/src/com/codefortomorrow/advanced/chapter14/solutions/account/InvalidAgeException.java b/src/com/codefortomorrow/advanced/chapter14/solutions/account/InvalidAgeException.java deleted file mode 100644 index 17084ef..0000000 --- a/src/com/codefortomorrow/advanced/chapter14/solutions/account/InvalidAgeException.java +++ /dev/null @@ -1,3 +0,0 @@ -package com.codefortomorrow.advanced.chapter14.solutions.account; - -public class InvalidAgeException extends Exception {} diff --git a/src/com/codefortomorrow/advanced/chapter14/solutions/account/InvalidPasswordException.java b/src/com/codefortomorrow/advanced/chapter14/solutions/account/InvalidPasswordException.java deleted file mode 100644 index 8f65f31..0000000 --- a/src/com/codefortomorrow/advanced/chapter14/solutions/account/InvalidPasswordException.java +++ /dev/null @@ -1,3 +0,0 @@ -package com.codefortomorrow.advanced.chapter14.solutions.account; - -public class InvalidPasswordException extends Exception {} diff --git a/src/com/codefortomorrow/advanced/chapter14/solutions/account/InvalidUsernameException.java b/src/com/codefortomorrow/advanced/chapter14/solutions/account/InvalidUsernameException.java deleted file mode 100644 index 68ccbc2..0000000 --- a/src/com/codefortomorrow/advanced/chapter14/solutions/account/InvalidUsernameException.java +++ /dev/null @@ -1,3 +0,0 @@ -package com.codefortomorrow.advanced.chapter14.solutions.account; - -public class InvalidUsernameException extends Exception {} diff --git a/src/com/codefortomorrow/advanced/chapter14/solutions/account/Main.java b/src/com/codefortomorrow/advanced/chapter14/solutions/account/Main.java deleted file mode 100644 index 2000e41..0000000 --- a/src/com/codefortomorrow/advanced/chapter14/solutions/account/Main.java +++ /dev/null @@ -1,57 +0,0 @@ -package com.codefortomorrow.advanced.chapter14.solutions.account; - -import java.util.Scanner; - -public class Main { - - public static void main(String[] args) { - Scanner sc = new Scanner(System.in); - - System.out.println("Welcome to Account Creation!"); - while (true) { - System.out.print("Enter username (4 to 10 characters): "); - String username = sc.next(); - System.out.print("Enter age: "); - int age = sc.nextInt(); - System.out.print("Enter password (4 to 10 characters): "); - String password = sc.next(); - System.out.print("Confirm password (4 to 10 characters): "); - String confirmPassword = sc.next(); - - try { - createAccount(username, password, age, confirmPassword); - System.out.println("Account created successfully!"); - break; - } catch (InvalidUsernameException e) { - System.out.println("Invalid username."); - } catch (InvalidPasswordException e) { - System.out.println("Invalid password."); - } catch (InvalidAgeException e) { - System.out.println("Must be 18 or older to create an account."); - } catch (PasswordMismatchException e) { - System.out.println("Passwords don't match!"); - } - } - } - - public static void createAccount( - String username, - String password, - int age, - String confirmPassword - ) - throws InvalidAgeException, InvalidPasswordException, InvalidUsernameException, PasswordMismatchException { - if (username.length() < 4 || username.length() > 10) { - throw new InvalidUsernameException(); - } - if (password.length() < 4 || password.length() > 10) { - throw new InvalidPasswordException(); - } - if (age < 18) { - throw new InvalidAgeException(); - } - if (!password.equals(confirmPassword)) { - throw new PasswordMismatchException(); - } - } -} diff --git a/src/com/codefortomorrow/advanced/chapter14/solutions/account/PasswordMismatchException.java b/src/com/codefortomorrow/advanced/chapter14/solutions/account/PasswordMismatchException.java deleted file mode 100644 index 248d178..0000000 --- a/src/com/codefortomorrow/advanced/chapter14/solutions/account/PasswordMismatchException.java +++ /dev/null @@ -1,3 +0,0 @@ -package com.codefortomorrow.advanced.chapter14.solutions.account; - -public class PasswordMismatchException extends Exception {} From 57e5e9770babfe66af466c963769665f9808cd44 Mon Sep 17 00:00:00 2001 From: phrdang Date: Thu, 1 Jul 2021 18:23:02 +0000 Subject: [PATCH 55/69] Bot: Prettified Java code! --- .../advanced/chapter14/practice/Account.java | 8 +++++-- .../advanced/chapter14/solutions/Account.java | 5 +++- .../beginner/chapter1/practice/Comments.java | 23 ++++++++----------- .../chapter1/practice/HelloWorld.java | 11 +++++---- .../beginner/chapter1/solutions/Comments.java | 19 +++++++-------- .../chapter1/solutions/HelloWorld.java | 11 +++++---- .../chapter2/practice/ApplesOranges.java | 14 ++++------- .../chapter2/practice/VariableTypes.java | 18 +++++---------- .../chapter2/solutions/ApplesOranges.java | 15 ++++++------ .../chapter2/solutions/VariableTypes.java | 19 ++++++++------- .../chapter4/practice/CarDealership.java | 18 +++++---------- .../chapter4/solutions/CarDealership.java | 14 +++++------ 12 files changed, 80 insertions(+), 95 deletions(-) diff --git a/src/com/codefortomorrow/advanced/chapter14/practice/Account.java b/src/com/codefortomorrow/advanced/chapter14/practice/Account.java index 9b6477b..af685e7 100644 --- a/src/com/codefortomorrow/advanced/chapter14/practice/Account.java +++ b/src/com/codefortomorrow/advanced/chapter14/practice/Account.java @@ -50,9 +50,13 @@ public static void main(String[] args) { // Write your code here } - public static void createAccount(String username, int age, String password, String confirmPassword) { + public static void createAccount( + String username, + int age, + String password, + String confirmPassword + ) { // Write your code here } } - // Write exception classes here diff --git a/src/com/codefortomorrow/advanced/chapter14/solutions/Account.java b/src/com/codefortomorrow/advanced/chapter14/solutions/Account.java index 54990a4..8bca545 100644 --- a/src/com/codefortomorrow/advanced/chapter14/solutions/Account.java +++ b/src/com/codefortomorrow/advanced/chapter14/solutions/Account.java @@ -97,7 +97,7 @@ public static void createAccount( if (password.length() < 4 || password.length() > 10) { throw new InvalidPasswordException(); } - + if (!password.equals(confirmPassword)) { throw new PasswordMismatchException(); } @@ -105,6 +105,9 @@ public static void createAccount( } class InvalidUsernameException extends Exception {} + class InvalidPasswordException extends Exception {} + class InvalidAgeException extends Exception {} + class PasswordMismatchException extends Exception {} diff --git a/src/com/codefortomorrow/beginner/chapter1/practice/Comments.java b/src/com/codefortomorrow/beginner/chapter1/practice/Comments.java index cebb705..cb032a4 100644 --- a/src/com/codefortomorrow/beginner/chapter1/practice/Comments.java +++ b/src/com/codefortomorrow/beginner/chapter1/practice/Comments.java @@ -3,16 +3,17 @@ /** * @author ArmeetJatyani * March 2021 - * - * You'll be writing your first ever comment today! - * What we'll go over: + * + * You'll be writing your first ever comment today! + * What we'll go over: * - single line comments * - multi line comments */ - // a class is an "object" where we will place all our code inside - public class Comments { - // the main method (below) is the first thing that runs when your program is run +// a class is an "object" where we will place all our code inside +public class Comments { + + // the main method (below) is the first thing that runs when your program is run public static void main(String[] args) { // this is a single line comment, I can write anything here // single line comments aren't run by Java! @@ -22,17 +23,11 @@ public static void main(String[] args) { * this is a multi * line * comment - * + * * It can span across multiple lines! */ // YOUR ASSIGNMENT: write 1 single-line comment and 1 multi-line comment on the lines below... - - - - - - } - } \ No newline at end of file +} diff --git a/src/com/codefortomorrow/beginner/chapter1/practice/HelloWorld.java b/src/com/codefortomorrow/beginner/chapter1/practice/HelloWorld.java index 832742a..4fd1294 100644 --- a/src/com/codefortomorrow/beginner/chapter1/practice/HelloWorld.java +++ b/src/com/codefortomorrow/beginner/chapter1/practice/HelloWorld.java @@ -3,17 +3,18 @@ /** * @author ArmeetJatyani * March 2021 - * - * Welcome to Java! + * + * Welcome to Java! * This may be your first ever java program! * We'll begin your journey with the infamous Hello World program! */ - // a class is an "object" where we will place all our code inside - public class HelloWorld { +// a class is an "object" where we will place all our code inside +public class HelloWorld { + // the main method (below) is the first thing that runs when your program is run public static void main(String[] args) { // write code here (replace the "" with "Hello World!") System.out.println(""); } - } +} diff --git a/src/com/codefortomorrow/beginner/chapter1/solutions/Comments.java b/src/com/codefortomorrow/beginner/chapter1/solutions/Comments.java index 446b385..faaa644 100644 --- a/src/com/codefortomorrow/beginner/chapter1/solutions/Comments.java +++ b/src/com/codefortomorrow/beginner/chapter1/solutions/Comments.java @@ -3,16 +3,17 @@ /** * @author ArmeetJatyani * March 2021 - * - * You'll be writing your first ever comment today! - * What we'll go over: + * + * You'll be writing your first ever comment today! + * What we'll go over: * - single line comments * - multi line comments */ - // a class is an "object" where we will place all our code inside - public class Comments { - // the main method (below) is the first thing that runs when your program is run +// a class is an "object" where we will place all our code inside +public class Comments { + + // the main method (below) is the first thing that runs when your program is run public static void main(String[] args) { // this is a single line comment, I can write anything here // single line comments aren't run by Java! @@ -22,7 +23,7 @@ public static void main(String[] args) { * this is a multi * line * comment - * + * * It can span across multiple lines! */ @@ -31,8 +32,8 @@ public static void main(String[] args) { // Hi my name is Armeet! /** - * I like teaching Java, and + * I like teaching Java, and * good luck on your journey! */ } - } \ No newline at end of file +} diff --git a/src/com/codefortomorrow/beginner/chapter1/solutions/HelloWorld.java b/src/com/codefortomorrow/beginner/chapter1/solutions/HelloWorld.java index a1152d2..e645eff 100644 --- a/src/com/codefortomorrow/beginner/chapter1/solutions/HelloWorld.java +++ b/src/com/codefortomorrow/beginner/chapter1/solutions/HelloWorld.java @@ -3,17 +3,18 @@ /** * @author ArmeetJatyani * March 2021 - * - * Welcome to Java! + * + * Welcome to Java! * This may be your first ever java program! * We'll begin your journey with the infamous Hello World program! */ - // a class is an "object" where we will place all our code inside - public class HelloWorld { +// a class is an "object" where we will place all our code inside +public class HelloWorld { + // the main method (below) is the first thing that runs when your program is run public static void main(String[] args) { // write code here (replace the "" with "Hello World!") System.out.println("Hello World!"); } - } +} diff --git a/src/com/codefortomorrow/beginner/chapter2/practice/ApplesOranges.java b/src/com/codefortomorrow/beginner/chapter2/practice/ApplesOranges.java index bb28674..5c91b50 100644 --- a/src/com/codefortomorrow/beginner/chapter2/practice/ApplesOranges.java +++ b/src/com/codefortomorrow/beginner/chapter2/practice/ApplesOranges.java @@ -3,26 +3,22 @@ /** * @author ArmeetJatyani * March 2021 - * + * * Print out the number of apples and oranges! */ - public class ApplesOranges { +public class ApplesOranges { + public static void main(String[] args) { // write your code here - + // define an integer variable called numOranges with value 10 (line 15) - // define an integer variable called numApples with value 24 (line 18) - // print out number of oranges using variables, output: "I have 10 oranges." (line 21) - // print out number of apples using variables, output: "I have 24 apples." (line 24) - } - } - +} diff --git a/src/com/codefortomorrow/beginner/chapter2/practice/VariableTypes.java b/src/com/codefortomorrow/beginner/chapter2/practice/VariableTypes.java index e5b9678..b0848ac 100644 --- a/src/com/codefortomorrow/beginner/chapter2/practice/VariableTypes.java +++ b/src/com/codefortomorrow/beginner/chapter2/practice/VariableTypes.java @@ -3,32 +3,26 @@ /** * @author ArmeetJatyani * March 2021 - * + * * Define different types of variables */ - public class VariableTypes { +public class VariableTypes { + public static void main(String[] args) { // write your code here - + // define an integer variable on line 15 - // define a float variable on line 18 - // define a double variable on line 21 - // define a boolean variable on line 24 (Hint: true/false) - // define a character variable on line 27 - - // define a string variable on line 30 - + // define a string variable on line 30 } - } - +} diff --git a/src/com/codefortomorrow/beginner/chapter2/solutions/ApplesOranges.java b/src/com/codefortomorrow/beginner/chapter2/solutions/ApplesOranges.java index b35c1e9..e45b29a 100644 --- a/src/com/codefortomorrow/beginner/chapter2/solutions/ApplesOranges.java +++ b/src/com/codefortomorrow/beginner/chapter2/solutions/ApplesOranges.java @@ -3,26 +3,25 @@ /** * @author ArmeetJatyani * March 2021 - * + * * Print out the number of apples and oranges! */ - public class ApplesOranges { +public class ApplesOranges { + public static void main(String[] args) { // write your code here - + // define an integer variable called numOranges with value 10 (line 15) int numOranges = 10; - + // define an integer variable called numApples with value 24 (line 18) int numApples = 24; - + // print out number of oranges using variables, output: "I have 10 oranges." (line 21) System.out.println("I have " + numOranges + " oranges."); // print out number of apples using variables, output: "I have 24 apples." (line 24) System.out.println("I have " + numApples + " apples."); - } - } - +} diff --git a/src/com/codefortomorrow/beginner/chapter2/solutions/VariableTypes.java b/src/com/codefortomorrow/beginner/chapter2/solutions/VariableTypes.java index 0a3a64e..6d9785b 100644 --- a/src/com/codefortomorrow/beginner/chapter2/solutions/VariableTypes.java +++ b/src/com/codefortomorrow/beginner/chapter2/solutions/VariableTypes.java @@ -3,32 +3,31 @@ /** * @author ArmeetJatyani * March 2021 - * + * * Define different types of variables */ - public class VariableTypes { +public class VariableTypes { + public static void main(String[] args) { // write your code here - + // define an integer variable on line 15 int age = 23; - + // define a float variable on line 18 float decimal = 23.32544f; - + // define a double variable on line 21 double decimal = 23.2352536d; // define a boolean variable on line 24 (Hint: true/false) - double dogsOut = true; // the dogs are out :) + double dogsOut = true; // the dogs are out :) // define a character variable on line 27 char letter = 'A'; - // define a string variable on line 30 + // define a string variable on line 30 char name = "Jeff"; - } - } - +} diff --git a/src/com/codefortomorrow/beginner/chapter4/practice/CarDealership.java b/src/com/codefortomorrow/beginner/chapter4/practice/CarDealership.java index df2013f..27c3110 100644 --- a/src/com/codefortomorrow/beginner/chapter4/practice/CarDealership.java +++ b/src/com/codefortomorrow/beginner/chapter4/practice/CarDealership.java @@ -3,26 +3,20 @@ /** * @author ArmeetJatyani * March 2021 - * + * * Manage a car dealership and take inventory of all cars sold! */ - public class CarDealership { +public class CarDealership { + public static void main(String[] args) { // write your code here - - // define 3 integer constants, representing the prices of 3 cars sold in USD (line 15-17) - - + // define 3 integer constants, representing the prices of 3 cars sold in USD (line 15-17) // define an integer variable, which represents the sum of the prices of these 3 cars (line 20) - - - // print out the formatted revenue after selling these 3 cars, output: "I sold 3 cars for $XXXXXX." (line 23) - + // print out the formatted revenue after selling these 3 cars, output: "I sold 3 cars for $XXXXXX." (line 23) } - } - \ No newline at end of file +} diff --git a/src/com/codefortomorrow/beginner/chapter4/solutions/CarDealership.java b/src/com/codefortomorrow/beginner/chapter4/solutions/CarDealership.java index 10fdd82..98f48fb 100644 --- a/src/com/codefortomorrow/beginner/chapter4/solutions/CarDealership.java +++ b/src/com/codefortomorrow/beginner/chapter4/solutions/CarDealership.java @@ -3,14 +3,15 @@ /** * @author ArmeetJatyani * March 2021 - * + * * Manage a car dealership and take inventory of all cars sold! */ - public class CarDealership { +public class CarDealership { + public static void main(String[] args) { // write your code here - + // define 3 integer constants, representing the prices of 3 cars sold in USD (line 15-17) final int car1 = 30000; final int car2 = 24650; @@ -18,11 +19,8 @@ public static void main(String[] args) { // define an integer variable, which represents the sum of the prices of these 3 cars (line 20) int revenue = car1 + car2 + car3; - + // print out the formatted revenue after selling these 3 cars, output: "I sold 3 cars for $XXXXXX." (line 23) System.out.println("I sold 3 cars for $" + revenue + "."); - - } - } - \ No newline at end of file +} From dd849b63e45931c0ee8a3f8c1f510017f81ab857 Mon Sep 17 00:00:00 2001 From: Rebecca Dang Date: Thu, 1 Jul 2021 11:34:54 -0700 Subject: [PATCH 56/69] Remove unused imports from Average, Lexico --- src/com/codefortomorrow/advanced/chapter14/practice/Average.java | 1 - src/com/codefortomorrow/advanced/chapter14/practice/Lexico.java | 1 - 2 files changed, 2 deletions(-) diff --git a/src/com/codefortomorrow/advanced/chapter14/practice/Average.java b/src/com/codefortomorrow/advanced/chapter14/practice/Average.java index 8a4bad3..923a3f7 100644 --- a/src/com/codefortomorrow/advanced/chapter14/practice/Average.java +++ b/src/com/codefortomorrow/advanced/chapter14/practice/Average.java @@ -1,6 +1,5 @@ package com.codefortomorrow.advanced.chapter14.practice; -import java.io.*; /* The file “numbers.txt” has 100 random numbers diff --git a/src/com/codefortomorrow/advanced/chapter14/practice/Lexico.java b/src/com/codefortomorrow/advanced/chapter14/practice/Lexico.java index 67914b1..432b964 100644 --- a/src/com/codefortomorrow/advanced/chapter14/practice/Lexico.java +++ b/src/com/codefortomorrow/advanced/chapter14/practice/Lexico.java @@ -1,6 +1,5 @@ package com.codefortomorrow.advanced.chapter14.practice; -import java.io.*; /* Practice: Use File I/O to compare two files lexicographically. From f15d68eea6c8dce563772f5e5e915d0cbd91b65e Mon Sep 17 00:00:00 2001 From: Rebecca Dang Date: Thu, 1 Jul 2021 11:35:48 -0700 Subject: [PATCH 57/69] Import java.util classes explicitly - Close Scanner in Names.java - Suppress unused warning in Access.java --- .../codefortomorrow/advanced/chapter16/solutions/Access.java | 4 +++- .../codefortomorrow/advanced/chapter16/solutions/Names.java | 5 ++++- .../codefortomorrow/advanced/chapter16/solutions/Sort.java | 3 ++- 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/src/com/codefortomorrow/advanced/chapter16/solutions/Access.java b/src/com/codefortomorrow/advanced/chapter16/solutions/Access.java index 3ff7c97..8c19ee6 100644 --- a/src/com/codefortomorrow/advanced/chapter16/solutions/Access.java +++ b/src/com/codefortomorrow/advanced/chapter16/solutions/Access.java @@ -1,6 +1,7 @@ package com.codefortomorrow.advanced.chapter16.solutions; -import java.util.*; +import java.util.LinkedList; +import java.util.ArrayList; /* Using the methods outlined in the “Benefits of LinkedLists” section, @@ -15,6 +16,7 @@ public class Access { + @SuppressWarnings("unused") public static void main(String[] args) { long a, b; diff --git a/src/com/codefortomorrow/advanced/chapter16/solutions/Names.java b/src/com/codefortomorrow/advanced/chapter16/solutions/Names.java index b14b55d..c862fdf 100644 --- a/src/com/codefortomorrow/advanced/chapter16/solutions/Names.java +++ b/src/com/codefortomorrow/advanced/chapter16/solutions/Names.java @@ -1,6 +1,7 @@ package com.codefortomorrow.advanced.chapter16.solutions; -import java.util.*; +import java.util.LinkedList; +import java.util.Scanner; /* Create a LinkedList of Strings, and ask the user to @@ -38,5 +39,7 @@ public static void main(String[] args) { list.add(0, "Apple"); list.add(list.size() - 1, "Peanut"); System.out.println(list); + + sc.close(); } } diff --git a/src/com/codefortomorrow/advanced/chapter16/solutions/Sort.java b/src/com/codefortomorrow/advanced/chapter16/solutions/Sort.java index 4f90e8d..f091f31 100644 --- a/src/com/codefortomorrow/advanced/chapter16/solutions/Sort.java +++ b/src/com/codefortomorrow/advanced/chapter16/solutions/Sort.java @@ -1,6 +1,7 @@ package com.codefortomorrow.advanced.chapter16.solutions; -import java.util.*; +import java.util.LinkedList; +import java.util.Arrays; /* Write a method which takes in a LinkedList of Strings From d296dfe5dc8b7bbf3313f399aaa4f466f69b0007 Mon Sep 17 00:00:00 2001 From: phrdang Date: Thu, 1 Jul 2021 18:41:58 +0000 Subject: [PATCH 58/69] Bot: Prettified Java code! --- .../advanced/chapter14/practice/Average.java | 1 - .../advanced/chapter14/practice/Lexico.java | 1 - .../advanced/chapter16/solutions/Access.java | 2 +- .../advanced/chapter16/solutions/Sort.java | 2 +- .../beginner/chapter1/practice/Comments.java | 23 ++++++++----------- .../chapter1/practice/HelloWorld.java | 11 +++++---- .../beginner/chapter1/solutions/Comments.java | 19 +++++++-------- .../chapter1/solutions/HelloWorld.java | 11 +++++---- .../chapter2/practice/ApplesOranges.java | 14 ++++------- .../chapter2/practice/VariableTypes.java | 18 +++++---------- .../chapter2/solutions/ApplesOranges.java | 15 ++++++------ .../chapter2/solutions/VariableTypes.java | 19 ++++++++------- .../chapter4/practice/CarDealership.java | 18 +++++---------- .../chapter4/solutions/CarDealership.java | 14 +++++------ 14 files changed, 72 insertions(+), 96 deletions(-) diff --git a/src/com/codefortomorrow/advanced/chapter14/practice/Average.java b/src/com/codefortomorrow/advanced/chapter14/practice/Average.java index 923a3f7..0309e0c 100644 --- a/src/com/codefortomorrow/advanced/chapter14/practice/Average.java +++ b/src/com/codefortomorrow/advanced/chapter14/practice/Average.java @@ -1,6 +1,5 @@ package com.codefortomorrow.advanced.chapter14.practice; - /* The file “numbers.txt” has 100 random numbers (one on each line). Use file i/o to find the diff --git a/src/com/codefortomorrow/advanced/chapter14/practice/Lexico.java b/src/com/codefortomorrow/advanced/chapter14/practice/Lexico.java index 432b964..38d9efe 100644 --- a/src/com/codefortomorrow/advanced/chapter14/practice/Lexico.java +++ b/src/com/codefortomorrow/advanced/chapter14/practice/Lexico.java @@ -1,6 +1,5 @@ package com.codefortomorrow.advanced.chapter14.practice; - /* Practice: Use File I/O to compare two files lexicographically. Lexicographical order is very similar to alphabetical order, diff --git a/src/com/codefortomorrow/advanced/chapter16/solutions/Access.java b/src/com/codefortomorrow/advanced/chapter16/solutions/Access.java index 8c19ee6..5a4889d 100644 --- a/src/com/codefortomorrow/advanced/chapter16/solutions/Access.java +++ b/src/com/codefortomorrow/advanced/chapter16/solutions/Access.java @@ -1,7 +1,7 @@ package com.codefortomorrow.advanced.chapter16.solutions; -import java.util.LinkedList; import java.util.ArrayList; +import java.util.LinkedList; /* Using the methods outlined in the “Benefits of LinkedLists” section, diff --git a/src/com/codefortomorrow/advanced/chapter16/solutions/Sort.java b/src/com/codefortomorrow/advanced/chapter16/solutions/Sort.java index f091f31..acbd9d0 100644 --- a/src/com/codefortomorrow/advanced/chapter16/solutions/Sort.java +++ b/src/com/codefortomorrow/advanced/chapter16/solutions/Sort.java @@ -1,7 +1,7 @@ package com.codefortomorrow.advanced.chapter16.solutions; -import java.util.LinkedList; import java.util.Arrays; +import java.util.LinkedList; /* Write a method which takes in a LinkedList of Strings diff --git a/src/com/codefortomorrow/beginner/chapter1/practice/Comments.java b/src/com/codefortomorrow/beginner/chapter1/practice/Comments.java index cebb705..cb032a4 100644 --- a/src/com/codefortomorrow/beginner/chapter1/practice/Comments.java +++ b/src/com/codefortomorrow/beginner/chapter1/practice/Comments.java @@ -3,16 +3,17 @@ /** * @author ArmeetJatyani * March 2021 - * - * You'll be writing your first ever comment today! - * What we'll go over: + * + * You'll be writing your first ever comment today! + * What we'll go over: * - single line comments * - multi line comments */ - // a class is an "object" where we will place all our code inside - public class Comments { - // the main method (below) is the first thing that runs when your program is run +// a class is an "object" where we will place all our code inside +public class Comments { + + // the main method (below) is the first thing that runs when your program is run public static void main(String[] args) { // this is a single line comment, I can write anything here // single line comments aren't run by Java! @@ -22,17 +23,11 @@ public static void main(String[] args) { * this is a multi * line * comment - * + * * It can span across multiple lines! */ // YOUR ASSIGNMENT: write 1 single-line comment and 1 multi-line comment on the lines below... - - - - - - } - } \ No newline at end of file +} diff --git a/src/com/codefortomorrow/beginner/chapter1/practice/HelloWorld.java b/src/com/codefortomorrow/beginner/chapter1/practice/HelloWorld.java index 832742a..4fd1294 100644 --- a/src/com/codefortomorrow/beginner/chapter1/practice/HelloWorld.java +++ b/src/com/codefortomorrow/beginner/chapter1/practice/HelloWorld.java @@ -3,17 +3,18 @@ /** * @author ArmeetJatyani * March 2021 - * - * Welcome to Java! + * + * Welcome to Java! * This may be your first ever java program! * We'll begin your journey with the infamous Hello World program! */ - // a class is an "object" where we will place all our code inside - public class HelloWorld { +// a class is an "object" where we will place all our code inside +public class HelloWorld { + // the main method (below) is the first thing that runs when your program is run public static void main(String[] args) { // write code here (replace the "" with "Hello World!") System.out.println(""); } - } +} diff --git a/src/com/codefortomorrow/beginner/chapter1/solutions/Comments.java b/src/com/codefortomorrow/beginner/chapter1/solutions/Comments.java index 446b385..faaa644 100644 --- a/src/com/codefortomorrow/beginner/chapter1/solutions/Comments.java +++ b/src/com/codefortomorrow/beginner/chapter1/solutions/Comments.java @@ -3,16 +3,17 @@ /** * @author ArmeetJatyani * March 2021 - * - * You'll be writing your first ever comment today! - * What we'll go over: + * + * You'll be writing your first ever comment today! + * What we'll go over: * - single line comments * - multi line comments */ - // a class is an "object" where we will place all our code inside - public class Comments { - // the main method (below) is the first thing that runs when your program is run +// a class is an "object" where we will place all our code inside +public class Comments { + + // the main method (below) is the first thing that runs when your program is run public static void main(String[] args) { // this is a single line comment, I can write anything here // single line comments aren't run by Java! @@ -22,7 +23,7 @@ public static void main(String[] args) { * this is a multi * line * comment - * + * * It can span across multiple lines! */ @@ -31,8 +32,8 @@ public static void main(String[] args) { // Hi my name is Armeet! /** - * I like teaching Java, and + * I like teaching Java, and * good luck on your journey! */ } - } \ No newline at end of file +} diff --git a/src/com/codefortomorrow/beginner/chapter1/solutions/HelloWorld.java b/src/com/codefortomorrow/beginner/chapter1/solutions/HelloWorld.java index a1152d2..e645eff 100644 --- a/src/com/codefortomorrow/beginner/chapter1/solutions/HelloWorld.java +++ b/src/com/codefortomorrow/beginner/chapter1/solutions/HelloWorld.java @@ -3,17 +3,18 @@ /** * @author ArmeetJatyani * March 2021 - * - * Welcome to Java! + * + * Welcome to Java! * This may be your first ever java program! * We'll begin your journey with the infamous Hello World program! */ - // a class is an "object" where we will place all our code inside - public class HelloWorld { +// a class is an "object" where we will place all our code inside +public class HelloWorld { + // the main method (below) is the first thing that runs when your program is run public static void main(String[] args) { // write code here (replace the "" with "Hello World!") System.out.println("Hello World!"); } - } +} diff --git a/src/com/codefortomorrow/beginner/chapter2/practice/ApplesOranges.java b/src/com/codefortomorrow/beginner/chapter2/practice/ApplesOranges.java index bb28674..5c91b50 100644 --- a/src/com/codefortomorrow/beginner/chapter2/practice/ApplesOranges.java +++ b/src/com/codefortomorrow/beginner/chapter2/practice/ApplesOranges.java @@ -3,26 +3,22 @@ /** * @author ArmeetJatyani * March 2021 - * + * * Print out the number of apples and oranges! */ - public class ApplesOranges { +public class ApplesOranges { + public static void main(String[] args) { // write your code here - + // define an integer variable called numOranges with value 10 (line 15) - // define an integer variable called numApples with value 24 (line 18) - // print out number of oranges using variables, output: "I have 10 oranges." (line 21) - // print out number of apples using variables, output: "I have 24 apples." (line 24) - } - } - +} diff --git a/src/com/codefortomorrow/beginner/chapter2/practice/VariableTypes.java b/src/com/codefortomorrow/beginner/chapter2/practice/VariableTypes.java index e5b9678..b0848ac 100644 --- a/src/com/codefortomorrow/beginner/chapter2/practice/VariableTypes.java +++ b/src/com/codefortomorrow/beginner/chapter2/practice/VariableTypes.java @@ -3,32 +3,26 @@ /** * @author ArmeetJatyani * March 2021 - * + * * Define different types of variables */ - public class VariableTypes { +public class VariableTypes { + public static void main(String[] args) { // write your code here - + // define an integer variable on line 15 - // define a float variable on line 18 - // define a double variable on line 21 - // define a boolean variable on line 24 (Hint: true/false) - // define a character variable on line 27 - - // define a string variable on line 30 - + // define a string variable on line 30 } - } - +} diff --git a/src/com/codefortomorrow/beginner/chapter2/solutions/ApplesOranges.java b/src/com/codefortomorrow/beginner/chapter2/solutions/ApplesOranges.java index b35c1e9..e45b29a 100644 --- a/src/com/codefortomorrow/beginner/chapter2/solutions/ApplesOranges.java +++ b/src/com/codefortomorrow/beginner/chapter2/solutions/ApplesOranges.java @@ -3,26 +3,25 @@ /** * @author ArmeetJatyani * March 2021 - * + * * Print out the number of apples and oranges! */ - public class ApplesOranges { +public class ApplesOranges { + public static void main(String[] args) { // write your code here - + // define an integer variable called numOranges with value 10 (line 15) int numOranges = 10; - + // define an integer variable called numApples with value 24 (line 18) int numApples = 24; - + // print out number of oranges using variables, output: "I have 10 oranges." (line 21) System.out.println("I have " + numOranges + " oranges."); // print out number of apples using variables, output: "I have 24 apples." (line 24) System.out.println("I have " + numApples + " apples."); - } - } - +} diff --git a/src/com/codefortomorrow/beginner/chapter2/solutions/VariableTypes.java b/src/com/codefortomorrow/beginner/chapter2/solutions/VariableTypes.java index 0a3a64e..6d9785b 100644 --- a/src/com/codefortomorrow/beginner/chapter2/solutions/VariableTypes.java +++ b/src/com/codefortomorrow/beginner/chapter2/solutions/VariableTypes.java @@ -3,32 +3,31 @@ /** * @author ArmeetJatyani * March 2021 - * + * * Define different types of variables */ - public class VariableTypes { +public class VariableTypes { + public static void main(String[] args) { // write your code here - + // define an integer variable on line 15 int age = 23; - + // define a float variable on line 18 float decimal = 23.32544f; - + // define a double variable on line 21 double decimal = 23.2352536d; // define a boolean variable on line 24 (Hint: true/false) - double dogsOut = true; // the dogs are out :) + double dogsOut = true; // the dogs are out :) // define a character variable on line 27 char letter = 'A'; - // define a string variable on line 30 + // define a string variable on line 30 char name = "Jeff"; - } - } - +} diff --git a/src/com/codefortomorrow/beginner/chapter4/practice/CarDealership.java b/src/com/codefortomorrow/beginner/chapter4/practice/CarDealership.java index df2013f..27c3110 100644 --- a/src/com/codefortomorrow/beginner/chapter4/practice/CarDealership.java +++ b/src/com/codefortomorrow/beginner/chapter4/practice/CarDealership.java @@ -3,26 +3,20 @@ /** * @author ArmeetJatyani * March 2021 - * + * * Manage a car dealership and take inventory of all cars sold! */ - public class CarDealership { +public class CarDealership { + public static void main(String[] args) { // write your code here - - // define 3 integer constants, representing the prices of 3 cars sold in USD (line 15-17) - - + // define 3 integer constants, representing the prices of 3 cars sold in USD (line 15-17) // define an integer variable, which represents the sum of the prices of these 3 cars (line 20) - - - // print out the formatted revenue after selling these 3 cars, output: "I sold 3 cars for $XXXXXX." (line 23) - + // print out the formatted revenue after selling these 3 cars, output: "I sold 3 cars for $XXXXXX." (line 23) } - } - \ No newline at end of file +} diff --git a/src/com/codefortomorrow/beginner/chapter4/solutions/CarDealership.java b/src/com/codefortomorrow/beginner/chapter4/solutions/CarDealership.java index 10fdd82..98f48fb 100644 --- a/src/com/codefortomorrow/beginner/chapter4/solutions/CarDealership.java +++ b/src/com/codefortomorrow/beginner/chapter4/solutions/CarDealership.java @@ -3,14 +3,15 @@ /** * @author ArmeetJatyani * March 2021 - * + * * Manage a car dealership and take inventory of all cars sold! */ - public class CarDealership { +public class CarDealership { + public static void main(String[] args) { // write your code here - + // define 3 integer constants, representing the prices of 3 cars sold in USD (line 15-17) final int car1 = 30000; final int car2 = 24650; @@ -18,11 +19,8 @@ public static void main(String[] args) { // define an integer variable, which represents the sum of the prices of these 3 cars (line 20) int revenue = car1 + car2 + car3; - + // print out the formatted revenue after selling these 3 cars, output: "I sold 3 cars for $XXXXXX." (line 23) System.out.println("I sold 3 cars for $" + revenue + "."); - - } - } - \ No newline at end of file +} From f8457d72e2b8b991c0c779cc2bde4362343cc42b Mon Sep 17 00:00:00 2001 From: Rebecca Dang Date: Thu, 1 Jul 2021 11:49:43 -0700 Subject: [PATCH 59/69] Fix VariableTypes solution errors - Rename duplicate `decimal` variable - Remove unnecessary `d` in the `number` double - Fix type of `dogsOut` and `name` variables - Suppress unused variable warnings --- .../beginner/chapter2/solutions/VariableTypes.java | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/com/codefortomorrow/beginner/chapter2/solutions/VariableTypes.java b/src/com/codefortomorrow/beginner/chapter2/solutions/VariableTypes.java index 0a3a64e..b578b4d 100644 --- a/src/com/codefortomorrow/beginner/chapter2/solutions/VariableTypes.java +++ b/src/com/codefortomorrow/beginner/chapter2/solutions/VariableTypes.java @@ -8,6 +8,7 @@ */ public class VariableTypes { + @SuppressWarnings("unused") public static void main(String[] args) { // write your code here @@ -18,16 +19,16 @@ public static void main(String[] args) { float decimal = 23.32544f; // define a double variable on line 21 - double decimal = 23.2352536d; + double number = 23.2352536; // define a boolean variable on line 24 (Hint: true/false) - double dogsOut = true; // the dogs are out :) + boolean dogsOut = true; // the dogs are out :) // define a character variable on line 27 char letter = 'A'; // define a string variable on line 30 - char name = "Jeff"; + String name = "Jeff"; } } From 38cfe255a6d6c03739a62664477cb725cef223bc Mon Sep 17 00:00:00 2001 From: phrdang Date: Thu, 1 Jul 2021 18:53:12 +0000 Subject: [PATCH 60/69] Bot: Prettified Java code! --- .../beginner/chapter1/practice/Comments.java | 23 ++++++++----------- .../chapter1/practice/HelloWorld.java | 11 +++++---- .../beginner/chapter1/solutions/Comments.java | 19 +++++++-------- .../chapter1/solutions/HelloWorld.java | 11 +++++---- .../chapter2/practice/ApplesOranges.java | 14 ++++------- .../chapter2/practice/VariableTypes.java | 18 +++++---------- .../chapter2/solutions/ApplesOranges.java | 15 ++++++------ .../chapter2/solutions/VariableTypes.java | 21 ++++++++--------- .../chapter4/practice/CarDealership.java | 18 +++++---------- .../chapter4/solutions/CarDealership.java | 14 +++++------ 10 files changed, 71 insertions(+), 93 deletions(-) diff --git a/src/com/codefortomorrow/beginner/chapter1/practice/Comments.java b/src/com/codefortomorrow/beginner/chapter1/practice/Comments.java index cebb705..cb032a4 100644 --- a/src/com/codefortomorrow/beginner/chapter1/practice/Comments.java +++ b/src/com/codefortomorrow/beginner/chapter1/practice/Comments.java @@ -3,16 +3,17 @@ /** * @author ArmeetJatyani * March 2021 - * - * You'll be writing your first ever comment today! - * What we'll go over: + * + * You'll be writing your first ever comment today! + * What we'll go over: * - single line comments * - multi line comments */ - // a class is an "object" where we will place all our code inside - public class Comments { - // the main method (below) is the first thing that runs when your program is run +// a class is an "object" where we will place all our code inside +public class Comments { + + // the main method (below) is the first thing that runs when your program is run public static void main(String[] args) { // this is a single line comment, I can write anything here // single line comments aren't run by Java! @@ -22,17 +23,11 @@ public static void main(String[] args) { * this is a multi * line * comment - * + * * It can span across multiple lines! */ // YOUR ASSIGNMENT: write 1 single-line comment and 1 multi-line comment on the lines below... - - - - - - } - } \ No newline at end of file +} diff --git a/src/com/codefortomorrow/beginner/chapter1/practice/HelloWorld.java b/src/com/codefortomorrow/beginner/chapter1/practice/HelloWorld.java index 832742a..4fd1294 100644 --- a/src/com/codefortomorrow/beginner/chapter1/practice/HelloWorld.java +++ b/src/com/codefortomorrow/beginner/chapter1/practice/HelloWorld.java @@ -3,17 +3,18 @@ /** * @author ArmeetJatyani * March 2021 - * - * Welcome to Java! + * + * Welcome to Java! * This may be your first ever java program! * We'll begin your journey with the infamous Hello World program! */ - // a class is an "object" where we will place all our code inside - public class HelloWorld { +// a class is an "object" where we will place all our code inside +public class HelloWorld { + // the main method (below) is the first thing that runs when your program is run public static void main(String[] args) { // write code here (replace the "" with "Hello World!") System.out.println(""); } - } +} diff --git a/src/com/codefortomorrow/beginner/chapter1/solutions/Comments.java b/src/com/codefortomorrow/beginner/chapter1/solutions/Comments.java index 446b385..faaa644 100644 --- a/src/com/codefortomorrow/beginner/chapter1/solutions/Comments.java +++ b/src/com/codefortomorrow/beginner/chapter1/solutions/Comments.java @@ -3,16 +3,17 @@ /** * @author ArmeetJatyani * March 2021 - * - * You'll be writing your first ever comment today! - * What we'll go over: + * + * You'll be writing your first ever comment today! + * What we'll go over: * - single line comments * - multi line comments */ - // a class is an "object" where we will place all our code inside - public class Comments { - // the main method (below) is the first thing that runs when your program is run +// a class is an "object" where we will place all our code inside +public class Comments { + + // the main method (below) is the first thing that runs when your program is run public static void main(String[] args) { // this is a single line comment, I can write anything here // single line comments aren't run by Java! @@ -22,7 +23,7 @@ public static void main(String[] args) { * this is a multi * line * comment - * + * * It can span across multiple lines! */ @@ -31,8 +32,8 @@ public static void main(String[] args) { // Hi my name is Armeet! /** - * I like teaching Java, and + * I like teaching Java, and * good luck on your journey! */ } - } \ No newline at end of file +} diff --git a/src/com/codefortomorrow/beginner/chapter1/solutions/HelloWorld.java b/src/com/codefortomorrow/beginner/chapter1/solutions/HelloWorld.java index a1152d2..e645eff 100644 --- a/src/com/codefortomorrow/beginner/chapter1/solutions/HelloWorld.java +++ b/src/com/codefortomorrow/beginner/chapter1/solutions/HelloWorld.java @@ -3,17 +3,18 @@ /** * @author ArmeetJatyani * March 2021 - * - * Welcome to Java! + * + * Welcome to Java! * This may be your first ever java program! * We'll begin your journey with the infamous Hello World program! */ - // a class is an "object" where we will place all our code inside - public class HelloWorld { +// a class is an "object" where we will place all our code inside +public class HelloWorld { + // the main method (below) is the first thing that runs when your program is run public static void main(String[] args) { // write code here (replace the "" with "Hello World!") System.out.println("Hello World!"); } - } +} diff --git a/src/com/codefortomorrow/beginner/chapter2/practice/ApplesOranges.java b/src/com/codefortomorrow/beginner/chapter2/practice/ApplesOranges.java index bb28674..5c91b50 100644 --- a/src/com/codefortomorrow/beginner/chapter2/practice/ApplesOranges.java +++ b/src/com/codefortomorrow/beginner/chapter2/practice/ApplesOranges.java @@ -3,26 +3,22 @@ /** * @author ArmeetJatyani * March 2021 - * + * * Print out the number of apples and oranges! */ - public class ApplesOranges { +public class ApplesOranges { + public static void main(String[] args) { // write your code here - + // define an integer variable called numOranges with value 10 (line 15) - // define an integer variable called numApples with value 24 (line 18) - // print out number of oranges using variables, output: "I have 10 oranges." (line 21) - // print out number of apples using variables, output: "I have 24 apples." (line 24) - } - } - +} diff --git a/src/com/codefortomorrow/beginner/chapter2/practice/VariableTypes.java b/src/com/codefortomorrow/beginner/chapter2/practice/VariableTypes.java index e5b9678..b0848ac 100644 --- a/src/com/codefortomorrow/beginner/chapter2/practice/VariableTypes.java +++ b/src/com/codefortomorrow/beginner/chapter2/practice/VariableTypes.java @@ -3,32 +3,26 @@ /** * @author ArmeetJatyani * March 2021 - * + * * Define different types of variables */ - public class VariableTypes { +public class VariableTypes { + public static void main(String[] args) { // write your code here - + // define an integer variable on line 15 - // define a float variable on line 18 - // define a double variable on line 21 - // define a boolean variable on line 24 (Hint: true/false) - // define a character variable on line 27 - - // define a string variable on line 30 - + // define a string variable on line 30 } - } - +} diff --git a/src/com/codefortomorrow/beginner/chapter2/solutions/ApplesOranges.java b/src/com/codefortomorrow/beginner/chapter2/solutions/ApplesOranges.java index b35c1e9..e45b29a 100644 --- a/src/com/codefortomorrow/beginner/chapter2/solutions/ApplesOranges.java +++ b/src/com/codefortomorrow/beginner/chapter2/solutions/ApplesOranges.java @@ -3,26 +3,25 @@ /** * @author ArmeetJatyani * March 2021 - * + * * Print out the number of apples and oranges! */ - public class ApplesOranges { +public class ApplesOranges { + public static void main(String[] args) { // write your code here - + // define an integer variable called numOranges with value 10 (line 15) int numOranges = 10; - + // define an integer variable called numApples with value 24 (line 18) int numApples = 24; - + // print out number of oranges using variables, output: "I have 10 oranges." (line 21) System.out.println("I have " + numOranges + " oranges."); // print out number of apples using variables, output: "I have 24 apples." (line 24) System.out.println("I have " + numApples + " apples."); - } - } - +} diff --git a/src/com/codefortomorrow/beginner/chapter2/solutions/VariableTypes.java b/src/com/codefortomorrow/beginner/chapter2/solutions/VariableTypes.java index b578b4d..f9946b0 100644 --- a/src/com/codefortomorrow/beginner/chapter2/solutions/VariableTypes.java +++ b/src/com/codefortomorrow/beginner/chapter2/solutions/VariableTypes.java @@ -3,33 +3,32 @@ /** * @author ArmeetJatyani * March 2021 - * + * * Define different types of variables */ - public class VariableTypes { - @SuppressWarnings("unused") +public class VariableTypes { + + @SuppressWarnings("unused") public static void main(String[] args) { // write your code here - + // define an integer variable on line 15 int age = 23; - + // define a float variable on line 18 float decimal = 23.32544f; - + // define a double variable on line 21 double number = 23.2352536; // define a boolean variable on line 24 (Hint: true/false) - boolean dogsOut = true; // the dogs are out :) + boolean dogsOut = true; // the dogs are out :) // define a character variable on line 27 char letter = 'A'; - // define a string variable on line 30 + // define a string variable on line 30 String name = "Jeff"; - } - } - +} diff --git a/src/com/codefortomorrow/beginner/chapter4/practice/CarDealership.java b/src/com/codefortomorrow/beginner/chapter4/practice/CarDealership.java index df2013f..27c3110 100644 --- a/src/com/codefortomorrow/beginner/chapter4/practice/CarDealership.java +++ b/src/com/codefortomorrow/beginner/chapter4/practice/CarDealership.java @@ -3,26 +3,20 @@ /** * @author ArmeetJatyani * March 2021 - * + * * Manage a car dealership and take inventory of all cars sold! */ - public class CarDealership { +public class CarDealership { + public static void main(String[] args) { // write your code here - - // define 3 integer constants, representing the prices of 3 cars sold in USD (line 15-17) - - + // define 3 integer constants, representing the prices of 3 cars sold in USD (line 15-17) // define an integer variable, which represents the sum of the prices of these 3 cars (line 20) - - - // print out the formatted revenue after selling these 3 cars, output: "I sold 3 cars for $XXXXXX." (line 23) - + // print out the formatted revenue after selling these 3 cars, output: "I sold 3 cars for $XXXXXX." (line 23) } - } - \ No newline at end of file +} diff --git a/src/com/codefortomorrow/beginner/chapter4/solutions/CarDealership.java b/src/com/codefortomorrow/beginner/chapter4/solutions/CarDealership.java index 10fdd82..98f48fb 100644 --- a/src/com/codefortomorrow/beginner/chapter4/solutions/CarDealership.java +++ b/src/com/codefortomorrow/beginner/chapter4/solutions/CarDealership.java @@ -3,14 +3,15 @@ /** * @author ArmeetJatyani * March 2021 - * + * * Manage a car dealership and take inventory of all cars sold! */ - public class CarDealership { +public class CarDealership { + public static void main(String[] args) { // write your code here - + // define 3 integer constants, representing the prices of 3 cars sold in USD (line 15-17) final int car1 = 30000; final int car2 = 24650; @@ -18,11 +19,8 @@ public static void main(String[] args) { // define an integer variable, which represents the sum of the prices of these 3 cars (line 20) int revenue = car1 + car2 + car3; - + // print out the formatted revenue after selling these 3 cars, output: "I sold 3 cars for $XXXXXX." (line 23) System.out.println("I sold 3 cars for $" + revenue + "."); - - } - } - \ No newline at end of file +} From 9b87e2fe589c90e8fbda5e0214231bcba87ff648 Mon Sep 17 00:00:00 2001 From: Rebecca Dang Date: Thu, 1 Jul 2021 13:11:23 -0700 Subject: [PATCH 61/69] Update Bank.java - Add string formatting to print money values in standard form - Change monetary parameters to `double`s instead of `int`s - Remove unused `diff` variable in `Bank.withdraw()` - Use `getMessage()` instead of `toString()` for handling `NotEnoughMoneyException` to avoid printing full exception name - Update `deposit` method to account for negative `amount`s --- .../advanced/chapter14/solutions/Bank.java | 31 ++++++++++--------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/src/com/codefortomorrow/advanced/chapter14/solutions/Bank.java b/src/com/codefortomorrow/advanced/chapter14/solutions/Bank.java index 78108b8..355e35d 100644 --- a/src/com/codefortomorrow/advanced/chapter14/solutions/Bank.java +++ b/src/com/codefortomorrow/advanced/chapter14/solutions/Bank.java @@ -34,43 +34,46 @@ public static void main(String[] args) { account1.deposit(200); withdraw(account1, 300); - System.out.println("Account 1: " + account1.getBalance()); + System.out.printf("Account 1 Balance: $%.2f\n", account1.getBalance()); account2.deposit(200); withdraw(account2, 1500); - System.out.println("Account 2: " + account2.getBalance()); + System.out.printf("Account 2 Balance: $%.2f\n", account2.getBalance()); } - public static void withdraw(BankAccount account, int amount) { + public static void withdraw(BankAccount account, double amount) { try { account.withdraw(amount); } catch (NotEnoughMoneyException e) { - int diff = Math.abs(amount - account.getBalance()); - System.out.println(e.toString()); + System.out.println(e.getMessage()); } } } class BankAccount { - private int balance; + private double balance; - public BankAccount(int balance) { + public BankAccount(double balance) { this.balance = balance; } - public void deposit(int amount) { - balance += amount; + public void deposit(double amount) { + if (amount > 0) { + balance += amount; + } } - public void withdraw(int amount) throws NotEnoughMoneyException { - if (amount > balance) throw new NotEnoughMoneyException( - "Bank balance is short $" + Math.abs(balance - amount) - ); + public void withdraw(double amount) throws NotEnoughMoneyException { + if (amount > balance) { + throw new NotEnoughMoneyException( + String.format("Bank balance is short $%.2f", Math.abs(balance - amount)) + ); + } balance -= amount; } - public int getBalance() { + public double getBalance() { return balance; } } From 73241e17d95f3dbd1b28cbfb3b6ba7556c79c087 Mon Sep 17 00:00:00 2001 From: phrdang Date: Thu, 1 Jul 2021 20:14:23 +0000 Subject: [PATCH 62/69] Bot: Prettified Java code! --- .../advanced/chapter14/solutions/Bank.java | 5 +++- .../beginner/chapter1/practice/Comments.java | 23 ++++++++----------- .../chapter1/practice/HelloWorld.java | 11 +++++---- .../beginner/chapter1/solutions/Comments.java | 19 +++++++-------- .../chapter1/solutions/HelloWorld.java | 11 +++++---- .../chapter2/practice/ApplesOranges.java | 14 ++++------- .../chapter2/practice/VariableTypes.java | 18 +++++---------- .../chapter2/solutions/ApplesOranges.java | 15 ++++++------ .../chapter2/solutions/VariableTypes.java | 19 ++++++++------- .../chapter4/practice/CarDealership.java | 18 +++++---------- .../chapter4/solutions/CarDealership.java | 14 +++++------ 11 files changed, 74 insertions(+), 93 deletions(-) diff --git a/src/com/codefortomorrow/advanced/chapter14/solutions/Bank.java b/src/com/codefortomorrow/advanced/chapter14/solutions/Bank.java index 355e35d..b2c45d0 100644 --- a/src/com/codefortomorrow/advanced/chapter14/solutions/Bank.java +++ b/src/com/codefortomorrow/advanced/chapter14/solutions/Bank.java @@ -67,7 +67,10 @@ public void deposit(double amount) { public void withdraw(double amount) throws NotEnoughMoneyException { if (amount > balance) { throw new NotEnoughMoneyException( - String.format("Bank balance is short $%.2f", Math.abs(balance - amount)) + String.format( + "Bank balance is short $%.2f", + Math.abs(balance - amount) + ) ); } balance -= amount; diff --git a/src/com/codefortomorrow/beginner/chapter1/practice/Comments.java b/src/com/codefortomorrow/beginner/chapter1/practice/Comments.java index cebb705..cb032a4 100644 --- a/src/com/codefortomorrow/beginner/chapter1/practice/Comments.java +++ b/src/com/codefortomorrow/beginner/chapter1/practice/Comments.java @@ -3,16 +3,17 @@ /** * @author ArmeetJatyani * March 2021 - * - * You'll be writing your first ever comment today! - * What we'll go over: + * + * You'll be writing your first ever comment today! + * What we'll go over: * - single line comments * - multi line comments */ - // a class is an "object" where we will place all our code inside - public class Comments { - // the main method (below) is the first thing that runs when your program is run +// a class is an "object" where we will place all our code inside +public class Comments { + + // the main method (below) is the first thing that runs when your program is run public static void main(String[] args) { // this is a single line comment, I can write anything here // single line comments aren't run by Java! @@ -22,17 +23,11 @@ public static void main(String[] args) { * this is a multi * line * comment - * + * * It can span across multiple lines! */ // YOUR ASSIGNMENT: write 1 single-line comment and 1 multi-line comment on the lines below... - - - - - - } - } \ No newline at end of file +} diff --git a/src/com/codefortomorrow/beginner/chapter1/practice/HelloWorld.java b/src/com/codefortomorrow/beginner/chapter1/practice/HelloWorld.java index 832742a..4fd1294 100644 --- a/src/com/codefortomorrow/beginner/chapter1/practice/HelloWorld.java +++ b/src/com/codefortomorrow/beginner/chapter1/practice/HelloWorld.java @@ -3,17 +3,18 @@ /** * @author ArmeetJatyani * March 2021 - * - * Welcome to Java! + * + * Welcome to Java! * This may be your first ever java program! * We'll begin your journey with the infamous Hello World program! */ - // a class is an "object" where we will place all our code inside - public class HelloWorld { +// a class is an "object" where we will place all our code inside +public class HelloWorld { + // the main method (below) is the first thing that runs when your program is run public static void main(String[] args) { // write code here (replace the "" with "Hello World!") System.out.println(""); } - } +} diff --git a/src/com/codefortomorrow/beginner/chapter1/solutions/Comments.java b/src/com/codefortomorrow/beginner/chapter1/solutions/Comments.java index 446b385..faaa644 100644 --- a/src/com/codefortomorrow/beginner/chapter1/solutions/Comments.java +++ b/src/com/codefortomorrow/beginner/chapter1/solutions/Comments.java @@ -3,16 +3,17 @@ /** * @author ArmeetJatyani * March 2021 - * - * You'll be writing your first ever comment today! - * What we'll go over: + * + * You'll be writing your first ever comment today! + * What we'll go over: * - single line comments * - multi line comments */ - // a class is an "object" where we will place all our code inside - public class Comments { - // the main method (below) is the first thing that runs when your program is run +// a class is an "object" where we will place all our code inside +public class Comments { + + // the main method (below) is the first thing that runs when your program is run public static void main(String[] args) { // this is a single line comment, I can write anything here // single line comments aren't run by Java! @@ -22,7 +23,7 @@ public static void main(String[] args) { * this is a multi * line * comment - * + * * It can span across multiple lines! */ @@ -31,8 +32,8 @@ public static void main(String[] args) { // Hi my name is Armeet! /** - * I like teaching Java, and + * I like teaching Java, and * good luck on your journey! */ } - } \ No newline at end of file +} diff --git a/src/com/codefortomorrow/beginner/chapter1/solutions/HelloWorld.java b/src/com/codefortomorrow/beginner/chapter1/solutions/HelloWorld.java index a1152d2..e645eff 100644 --- a/src/com/codefortomorrow/beginner/chapter1/solutions/HelloWorld.java +++ b/src/com/codefortomorrow/beginner/chapter1/solutions/HelloWorld.java @@ -3,17 +3,18 @@ /** * @author ArmeetJatyani * March 2021 - * - * Welcome to Java! + * + * Welcome to Java! * This may be your first ever java program! * We'll begin your journey with the infamous Hello World program! */ - // a class is an "object" where we will place all our code inside - public class HelloWorld { +// a class is an "object" where we will place all our code inside +public class HelloWorld { + // the main method (below) is the first thing that runs when your program is run public static void main(String[] args) { // write code here (replace the "" with "Hello World!") System.out.println("Hello World!"); } - } +} diff --git a/src/com/codefortomorrow/beginner/chapter2/practice/ApplesOranges.java b/src/com/codefortomorrow/beginner/chapter2/practice/ApplesOranges.java index bb28674..5c91b50 100644 --- a/src/com/codefortomorrow/beginner/chapter2/practice/ApplesOranges.java +++ b/src/com/codefortomorrow/beginner/chapter2/practice/ApplesOranges.java @@ -3,26 +3,22 @@ /** * @author ArmeetJatyani * March 2021 - * + * * Print out the number of apples and oranges! */ - public class ApplesOranges { +public class ApplesOranges { + public static void main(String[] args) { // write your code here - + // define an integer variable called numOranges with value 10 (line 15) - // define an integer variable called numApples with value 24 (line 18) - // print out number of oranges using variables, output: "I have 10 oranges." (line 21) - // print out number of apples using variables, output: "I have 24 apples." (line 24) - } - } - +} diff --git a/src/com/codefortomorrow/beginner/chapter2/practice/VariableTypes.java b/src/com/codefortomorrow/beginner/chapter2/practice/VariableTypes.java index e5b9678..b0848ac 100644 --- a/src/com/codefortomorrow/beginner/chapter2/practice/VariableTypes.java +++ b/src/com/codefortomorrow/beginner/chapter2/practice/VariableTypes.java @@ -3,32 +3,26 @@ /** * @author ArmeetJatyani * March 2021 - * + * * Define different types of variables */ - public class VariableTypes { +public class VariableTypes { + public static void main(String[] args) { // write your code here - + // define an integer variable on line 15 - // define a float variable on line 18 - // define a double variable on line 21 - // define a boolean variable on line 24 (Hint: true/false) - // define a character variable on line 27 - - // define a string variable on line 30 - + // define a string variable on line 30 } - } - +} diff --git a/src/com/codefortomorrow/beginner/chapter2/solutions/ApplesOranges.java b/src/com/codefortomorrow/beginner/chapter2/solutions/ApplesOranges.java index b35c1e9..e45b29a 100644 --- a/src/com/codefortomorrow/beginner/chapter2/solutions/ApplesOranges.java +++ b/src/com/codefortomorrow/beginner/chapter2/solutions/ApplesOranges.java @@ -3,26 +3,25 @@ /** * @author ArmeetJatyani * March 2021 - * + * * Print out the number of apples and oranges! */ - public class ApplesOranges { +public class ApplesOranges { + public static void main(String[] args) { // write your code here - + // define an integer variable called numOranges with value 10 (line 15) int numOranges = 10; - + // define an integer variable called numApples with value 24 (line 18) int numApples = 24; - + // print out number of oranges using variables, output: "I have 10 oranges." (line 21) System.out.println("I have " + numOranges + " oranges."); // print out number of apples using variables, output: "I have 24 apples." (line 24) System.out.println("I have " + numApples + " apples."); - } - } - +} diff --git a/src/com/codefortomorrow/beginner/chapter2/solutions/VariableTypes.java b/src/com/codefortomorrow/beginner/chapter2/solutions/VariableTypes.java index 0a3a64e..6d9785b 100644 --- a/src/com/codefortomorrow/beginner/chapter2/solutions/VariableTypes.java +++ b/src/com/codefortomorrow/beginner/chapter2/solutions/VariableTypes.java @@ -3,32 +3,31 @@ /** * @author ArmeetJatyani * March 2021 - * + * * Define different types of variables */ - public class VariableTypes { +public class VariableTypes { + public static void main(String[] args) { // write your code here - + // define an integer variable on line 15 int age = 23; - + // define a float variable on line 18 float decimal = 23.32544f; - + // define a double variable on line 21 double decimal = 23.2352536d; // define a boolean variable on line 24 (Hint: true/false) - double dogsOut = true; // the dogs are out :) + double dogsOut = true; // the dogs are out :) // define a character variable on line 27 char letter = 'A'; - // define a string variable on line 30 + // define a string variable on line 30 char name = "Jeff"; - } - } - +} diff --git a/src/com/codefortomorrow/beginner/chapter4/practice/CarDealership.java b/src/com/codefortomorrow/beginner/chapter4/practice/CarDealership.java index df2013f..27c3110 100644 --- a/src/com/codefortomorrow/beginner/chapter4/practice/CarDealership.java +++ b/src/com/codefortomorrow/beginner/chapter4/practice/CarDealership.java @@ -3,26 +3,20 @@ /** * @author ArmeetJatyani * March 2021 - * + * * Manage a car dealership and take inventory of all cars sold! */ - public class CarDealership { +public class CarDealership { + public static void main(String[] args) { // write your code here - - // define 3 integer constants, representing the prices of 3 cars sold in USD (line 15-17) - - + // define 3 integer constants, representing the prices of 3 cars sold in USD (line 15-17) // define an integer variable, which represents the sum of the prices of these 3 cars (line 20) - - - // print out the formatted revenue after selling these 3 cars, output: "I sold 3 cars for $XXXXXX." (line 23) - + // print out the formatted revenue after selling these 3 cars, output: "I sold 3 cars for $XXXXXX." (line 23) } - } - \ No newline at end of file +} diff --git a/src/com/codefortomorrow/beginner/chapter4/solutions/CarDealership.java b/src/com/codefortomorrow/beginner/chapter4/solutions/CarDealership.java index 10fdd82..98f48fb 100644 --- a/src/com/codefortomorrow/beginner/chapter4/solutions/CarDealership.java +++ b/src/com/codefortomorrow/beginner/chapter4/solutions/CarDealership.java @@ -3,14 +3,15 @@ /** * @author ArmeetJatyani * March 2021 - * + * * Manage a car dealership and take inventory of all cars sold! */ - public class CarDealership { +public class CarDealership { + public static void main(String[] args) { // write your code here - + // define 3 integer constants, representing the prices of 3 cars sold in USD (line 15-17) final int car1 = 30000; final int car2 = 24650; @@ -18,11 +19,8 @@ public static void main(String[] args) { // define an integer variable, which represents the sum of the prices of these 3 cars (line 20) int revenue = car1 + car2 + car3; - + // print out the formatted revenue after selling these 3 cars, output: "I sold 3 cars for $XXXXXX." (line 23) System.out.println("I sold 3 cars for $" + revenue + "."); - - } - } - \ No newline at end of file +} From 1a305eb06193d86e1f18f563d730e0780006f431 Mon Sep 17 00:00:00 2001 From: Alveera Munshi Date: Thu, 1 Jul 2021 17:27:48 -0400 Subject: [PATCH 63/69] Update Account.java Fixed a couple identation things and formatting on the createAccount method where parameters are initialized --- .../advanced/chapter14/solutions/Account.java | 61 +++++++++---------- 1 file changed, 28 insertions(+), 33 deletions(-) diff --git a/src/com/codefortomorrow/advanced/chapter14/solutions/Account.java b/src/com/codefortomorrow/advanced/chapter14/solutions/Account.java index 8bca545..7ad2588 100644 --- a/src/com/codefortomorrow/advanced/chapter14/solutions/Account.java +++ b/src/com/codefortomorrow/advanced/chapter14/solutions/Account.java @@ -3,7 +3,7 @@ import java.util.Scanner; /* -Create the following classes that simply extend the `Exception` class: +Create the following classes that simply extend the `Exception` class: - `InvalidUsernameException` - `InvalidPasswordException` @@ -11,7 +11,7 @@ - `PasswordMismatchException` In the `Account` class, code a method called `createAccount` that takes a `username`, `age`, -`password`, and `confirmPassword` as parameters. +`password`, and `confirmPassword` as parameters. In `createAccount`, throw an: @@ -53,39 +53,34 @@ public static void main(String[] args) { System.out.println("Welcome to Account Creation!"); while (true) { - System.out.print("Enter username (4 to 10 characters): "); - String username = sc.next(); - System.out.print("Enter age: "); - int age = sc.nextInt(); - System.out.print("Enter password (4 to 10 characters): "); - String password = sc.next(); - System.out.print("Confirm password (4 to 10 characters): "); - String confirmPassword = sc.next(); - - try { - createAccount(username, age, password, confirmPassword); - System.out.println("Account created successfully!"); - sc.close(); - break; - } catch (InvalidUsernameException e) { - System.out.println("Invalid username."); - } catch (InvalidPasswordException e) { - System.out.println("Invalid password."); - } catch (InvalidAgeException e) { - System.out.println("Must be 18 or older to create an account."); - } catch (PasswordMismatchException e) { - System.out.println("Passwords don't match!"); - } + System.out.print("Enter username (4 to 10 characters): "); + String username = sc.next(); + System.out.print("Enter age: "); + int age = sc.nextInt(); + System.out.print("Enter password (4 to 10 characters): "); + String password = sc.next(); + System.out.print("Confirm password (4 to 10 characters): "); + String confirmPassword = sc.next(); + + try { + createAccount(username, age, password, confirmPassword); + System.out.println("Account created successfully!"); + sc.close(); + break; + } catch (InvalidUsernameException e) { + System.out.println("Invalid username."); + } catch (InvalidPasswordException e) { + System.out.println("Invalid password."); + } catch (InvalidAgeException e) { + System.out.println("Must be 18 or older to create an account."); + } catch (PasswordMismatchException e) { + System.out.println("Passwords don't match!"); + } } } - public static void createAccount( - String username, - int age, - String password, - String confirmPassword - ) - throws InvalidAgeException, InvalidPasswordException, InvalidUsernameException, PasswordMismatchException { + public static void createAccount(String username, int age, String password, String confirmPassword) + throws InvalidAgeException, InvalidPasswordException, InvalidUsernameException, PasswordMismatchException { if (username.length() < 4 || username.length() > 10) { throw new InvalidUsernameException(); } @@ -101,7 +96,7 @@ public static void createAccount( if (!password.equals(confirmPassword)) { throw new PasswordMismatchException(); } - } + } } class InvalidUsernameException extends Exception {} From f80f9d35be6308af39cde04f4d9cfa0e6454108c Mon Sep 17 00:00:00 2001 From: AlveeraMunshi Date: Thu, 1 Jul 2021 21:28:23 +0000 Subject: [PATCH 64/69] Bot: Prettified Java code! --- .../advanced/chapter14/solutions/Account.java | 57 ++++++++++--------- 1 file changed, 31 insertions(+), 26 deletions(-) diff --git a/src/com/codefortomorrow/advanced/chapter14/solutions/Account.java b/src/com/codefortomorrow/advanced/chapter14/solutions/Account.java index 7ad2588..4fc6d77 100644 --- a/src/com/codefortomorrow/advanced/chapter14/solutions/Account.java +++ b/src/com/codefortomorrow/advanced/chapter14/solutions/Account.java @@ -53,34 +53,39 @@ public static void main(String[] args) { System.out.println("Welcome to Account Creation!"); while (true) { - System.out.print("Enter username (4 to 10 characters): "); - String username = sc.next(); - System.out.print("Enter age: "); - int age = sc.nextInt(); - System.out.print("Enter password (4 to 10 characters): "); - String password = sc.next(); - System.out.print("Confirm password (4 to 10 characters): "); - String confirmPassword = sc.next(); - - try { - createAccount(username, age, password, confirmPassword); - System.out.println("Account created successfully!"); - sc.close(); - break; - } catch (InvalidUsernameException e) { - System.out.println("Invalid username."); - } catch (InvalidPasswordException e) { - System.out.println("Invalid password."); - } catch (InvalidAgeException e) { - System.out.println("Must be 18 or older to create an account."); - } catch (PasswordMismatchException e) { - System.out.println("Passwords don't match!"); - } + System.out.print("Enter username (4 to 10 characters): "); + String username = sc.next(); + System.out.print("Enter age: "); + int age = sc.nextInt(); + System.out.print("Enter password (4 to 10 characters): "); + String password = sc.next(); + System.out.print("Confirm password (4 to 10 characters): "); + String confirmPassword = sc.next(); + + try { + createAccount(username, age, password, confirmPassword); + System.out.println("Account created successfully!"); + sc.close(); + break; + } catch (InvalidUsernameException e) { + System.out.println("Invalid username."); + } catch (InvalidPasswordException e) { + System.out.println("Invalid password."); + } catch (InvalidAgeException e) { + System.out.println("Must be 18 or older to create an account."); + } catch (PasswordMismatchException e) { + System.out.println("Passwords don't match!"); + } } } - public static void createAccount(String username, int age, String password, String confirmPassword) - throws InvalidAgeException, InvalidPasswordException, InvalidUsernameException, PasswordMismatchException { + public static void createAccount( + String username, + int age, + String password, + String confirmPassword + ) + throws InvalidAgeException, InvalidPasswordException, InvalidUsernameException, PasswordMismatchException { if (username.length() < 4 || username.length() > 10) { throw new InvalidUsernameException(); } @@ -96,7 +101,7 @@ public static void createAccount(String username, int age, String password, Stri if (!password.equals(confirmPassword)) { throw new PasswordMismatchException(); } - } + } } class InvalidUsernameException extends Exception {} From cef81634e98f568a77bf12c3b8b4ff43c44c7e7a Mon Sep 17 00:00:00 2001 From: Alveera Munshi Date: Thu, 1 Jul 2021 17:32:52 -0400 Subject: [PATCH 65/69] Update Account.java Identation fix again --- .../advanced/chapter14/solutions/Account.java | 57 +++++++++---------- 1 file changed, 26 insertions(+), 31 deletions(-) diff --git a/src/com/codefortomorrow/advanced/chapter14/solutions/Account.java b/src/com/codefortomorrow/advanced/chapter14/solutions/Account.java index 4fc6d77..7ad2588 100644 --- a/src/com/codefortomorrow/advanced/chapter14/solutions/Account.java +++ b/src/com/codefortomorrow/advanced/chapter14/solutions/Account.java @@ -53,39 +53,34 @@ public static void main(String[] args) { System.out.println("Welcome to Account Creation!"); while (true) { - System.out.print("Enter username (4 to 10 characters): "); - String username = sc.next(); - System.out.print("Enter age: "); - int age = sc.nextInt(); - System.out.print("Enter password (4 to 10 characters): "); - String password = sc.next(); - System.out.print("Confirm password (4 to 10 characters): "); - String confirmPassword = sc.next(); - - try { - createAccount(username, age, password, confirmPassword); - System.out.println("Account created successfully!"); - sc.close(); - break; - } catch (InvalidUsernameException e) { - System.out.println("Invalid username."); - } catch (InvalidPasswordException e) { - System.out.println("Invalid password."); - } catch (InvalidAgeException e) { - System.out.println("Must be 18 or older to create an account."); - } catch (PasswordMismatchException e) { - System.out.println("Passwords don't match!"); - } + System.out.print("Enter username (4 to 10 characters): "); + String username = sc.next(); + System.out.print("Enter age: "); + int age = sc.nextInt(); + System.out.print("Enter password (4 to 10 characters): "); + String password = sc.next(); + System.out.print("Confirm password (4 to 10 characters): "); + String confirmPassword = sc.next(); + + try { + createAccount(username, age, password, confirmPassword); + System.out.println("Account created successfully!"); + sc.close(); + break; + } catch (InvalidUsernameException e) { + System.out.println("Invalid username."); + } catch (InvalidPasswordException e) { + System.out.println("Invalid password."); + } catch (InvalidAgeException e) { + System.out.println("Must be 18 or older to create an account."); + } catch (PasswordMismatchException e) { + System.out.println("Passwords don't match!"); + } } } - public static void createAccount( - String username, - int age, - String password, - String confirmPassword - ) - throws InvalidAgeException, InvalidPasswordException, InvalidUsernameException, PasswordMismatchException { + public static void createAccount(String username, int age, String password, String confirmPassword) + throws InvalidAgeException, InvalidPasswordException, InvalidUsernameException, PasswordMismatchException { if (username.length() < 4 || username.length() > 10) { throw new InvalidUsernameException(); } @@ -101,7 +96,7 @@ public static void createAccount( if (!password.equals(confirmPassword)) { throw new PasswordMismatchException(); } - } + } } class InvalidUsernameException extends Exception {} From 956906cc0c4e1629ad4ce2c2bd76394adda2f906 Mon Sep 17 00:00:00 2001 From: AlveeraMunshi Date: Thu, 1 Jul 2021 21:33:28 +0000 Subject: [PATCH 66/69] Bot: Prettified Java code! --- .../advanced/chapter14/solutions/Account.java | 57 ++++++++++--------- 1 file changed, 31 insertions(+), 26 deletions(-) diff --git a/src/com/codefortomorrow/advanced/chapter14/solutions/Account.java b/src/com/codefortomorrow/advanced/chapter14/solutions/Account.java index 7ad2588..4fc6d77 100644 --- a/src/com/codefortomorrow/advanced/chapter14/solutions/Account.java +++ b/src/com/codefortomorrow/advanced/chapter14/solutions/Account.java @@ -53,34 +53,39 @@ public static void main(String[] args) { System.out.println("Welcome to Account Creation!"); while (true) { - System.out.print("Enter username (4 to 10 characters): "); - String username = sc.next(); - System.out.print("Enter age: "); - int age = sc.nextInt(); - System.out.print("Enter password (4 to 10 characters): "); - String password = sc.next(); - System.out.print("Confirm password (4 to 10 characters): "); - String confirmPassword = sc.next(); - - try { - createAccount(username, age, password, confirmPassword); - System.out.println("Account created successfully!"); - sc.close(); - break; - } catch (InvalidUsernameException e) { - System.out.println("Invalid username."); - } catch (InvalidPasswordException e) { - System.out.println("Invalid password."); - } catch (InvalidAgeException e) { - System.out.println("Must be 18 or older to create an account."); - } catch (PasswordMismatchException e) { - System.out.println("Passwords don't match!"); - } + System.out.print("Enter username (4 to 10 characters): "); + String username = sc.next(); + System.out.print("Enter age: "); + int age = sc.nextInt(); + System.out.print("Enter password (4 to 10 characters): "); + String password = sc.next(); + System.out.print("Confirm password (4 to 10 characters): "); + String confirmPassword = sc.next(); + + try { + createAccount(username, age, password, confirmPassword); + System.out.println("Account created successfully!"); + sc.close(); + break; + } catch (InvalidUsernameException e) { + System.out.println("Invalid username."); + } catch (InvalidPasswordException e) { + System.out.println("Invalid password."); + } catch (InvalidAgeException e) { + System.out.println("Must be 18 or older to create an account."); + } catch (PasswordMismatchException e) { + System.out.println("Passwords don't match!"); + } } } - public static void createAccount(String username, int age, String password, String confirmPassword) - throws InvalidAgeException, InvalidPasswordException, InvalidUsernameException, PasswordMismatchException { + public static void createAccount( + String username, + int age, + String password, + String confirmPassword + ) + throws InvalidAgeException, InvalidPasswordException, InvalidUsernameException, PasswordMismatchException { if (username.length() < 4 || username.length() > 10) { throw new InvalidUsernameException(); } @@ -96,7 +101,7 @@ public static void createAccount(String username, int age, String password, Stri if (!password.equals(confirmPassword)) { throw new PasswordMismatchException(); } - } + } } class InvalidUsernameException extends Exception {} From 191544184cdf6f1b8d16b0364dba9929142e3ce8 Mon Sep 17 00:00:00 2001 From: Alveera Munshi Date: Thu, 1 Jul 2021 17:50:13 -0400 Subject: [PATCH 67/69] move java.yml out of workflows --- .github/{workflows => }/java.yml | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename .github/{workflows => }/java.yml (100%) diff --git a/.github/workflows/java.yml b/.github/java.yml similarity index 100% rename from .github/workflows/java.yml rename to .github/java.yml From 49d5566fd372c808d2f87984017b8b352f1c6111 Mon Sep 17 00:00:00 2001 From: Alveera Munshi Date: Thu, 1 Jul 2021 17:57:28 -0400 Subject: [PATCH 68/69] Update Account.java Fixed the indentation with the right level --- .../advanced/chapter14/solutions/Account.java | 33 ++++++++----------- 1 file changed, 14 insertions(+), 19 deletions(-) diff --git a/src/com/codefortomorrow/advanced/chapter14/solutions/Account.java b/src/com/codefortomorrow/advanced/chapter14/solutions/Account.java index 4fc6d77..2bba577 100644 --- a/src/com/codefortomorrow/advanced/chapter14/solutions/Account.java +++ b/src/com/codefortomorrow/advanced/chapter14/solutions/Account.java @@ -79,29 +79,24 @@ public static void main(String[] args) { } } - public static void createAccount( - String username, - int age, - String password, - String confirmPassword - ) + public static void createAccount(String username, int age, String password, String confirmPassword) throws InvalidAgeException, InvalidPasswordException, InvalidUsernameException, PasswordMismatchException { - if (username.length() < 4 || username.length() > 10) { - throw new InvalidUsernameException(); - } + if (username.length() < 4 || username.length() > 10) { + throw new InvalidUsernameException(); + } - if (age < 18) { - throw new InvalidAgeException(); - } + if (age < 18) { + throw new InvalidAgeException(); + } - if (password.length() < 4 || password.length() > 10) { - throw new InvalidPasswordException(); - } + if (password.length() < 4 || password.length() > 10) { + throw new InvalidPasswordException(); + } - if (!password.equals(confirmPassword)) { - throw new PasswordMismatchException(); - } - } + if (!password.equals(confirmPassword)) { + throw new PasswordMismatchException(); + } + } } class InvalidUsernameException extends Exception {} From 0a10fce05d5f62fc86c1b7a18ae38c960005cf53 Mon Sep 17 00:00:00 2001 From: Alveera Munshi Date: Thu, 1 Jul 2021 18:18:46 -0400 Subject: [PATCH 69/69] Update Bank.java Moved the exception throw into one line --- .../codefortomorrow/advanced/chapter14/solutions/Bank.java | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/src/com/codefortomorrow/advanced/chapter14/solutions/Bank.java b/src/com/codefortomorrow/advanced/chapter14/solutions/Bank.java index b2c45d0..5c6079f 100644 --- a/src/com/codefortomorrow/advanced/chapter14/solutions/Bank.java +++ b/src/com/codefortomorrow/advanced/chapter14/solutions/Bank.java @@ -66,12 +66,7 @@ public void deposit(double amount) { public void withdraw(double amount) throws NotEnoughMoneyException { if (amount > balance) { - throw new NotEnoughMoneyException( - String.format( - "Bank balance is short $%.2f", - Math.abs(balance - amount) - ) - ); + throw new NotEnoughMoneyException(String.format("Bank balance is short $%.2f", Math.abs(balance - amount))); } balance -= amount; }