From 5a9c9516fdccb3f00d497870cef1f41bd5baa1b2 Mon Sep 17 00:00:00 2001 From: Michael Kowalczyk Date: Thu, 31 Oct 2024 09:23:18 -0400 Subject: [PATCH] Day 38 --- Day 24-27 (database)/sprint.txt | 3 - .../CampusVisits.db | Bin .../Database selection.txt | 0 .../Joining_Data_in_SQL_2.pdf | Bin .../indexes.txt | 0 .../notes.txt | 0 .../transactions.txt | 0 .../CouplingVsCohesion.svg | 177 ++++++++++++++++++ .../Improve the design/CoinInventory.class | Bin 0 -> 538 bytes .../Improve the design/CoinInventory.java | 26 +++ .../Improve the design/Item.class | Bin 0 -> 457 bytes .../Improve the design/Item.java | 18 ++ .../Improve the design/Player.class | Bin 0 -> 2675 bytes .../Improve the design/Player.java | 77 ++++++++ .../Improve the design/tp06bce7.BAT | 5 + .../fixed/CS495.class | Bin 0 -> 1046 bytes .../fixed/CS495.java | 18 ++ .../fixed/Student.class | Bin 0 -> 964 bytes .../fixed/Student.java | 17 ++ .../fixed/superheroes.txt | 12 ++ .../original/CS495.class | Bin 0 -> 1075 bytes .../original/CS495.java | 25 +++ .../original/Student.class | Bin 0 -> 913 bytes .../original/Student.java | 15 ++ Day 38 (class and function design)/notes.txt | 25 +++ 25 files changed, 415 insertions(+), 3 deletions(-) delete mode 100644 Day 24-27 (database)/sprint.txt rename {Day 24-27 (database) => Day 24-37 (database)}/CampusVisits.db (100%) rename {Day 24-27 (database) => Day 24-37 (database)}/Database selection.txt (100%) rename {Day 24-27 (database) => Day 24-37 (database)}/Joining_Data_in_SQL_2.pdf (100%) rename {Day 24-27 (database) => Day 24-37 (database)}/indexes.txt (100%) rename {Day 24-27 (database) => Day 24-37 (database)}/notes.txt (100%) rename {Day 24-27 (database) => Day 24-37 (database)}/transactions.txt (100%) create mode 100644 Day 38 (class and function design)/CouplingVsCohesion.svg create mode 100644 Day 38 (class and function design)/Improve the design/CoinInventory.class create mode 100644 Day 38 (class and function design)/Improve the design/CoinInventory.java create mode 100644 Day 38 (class and function design)/Improve the design/Item.class create mode 100644 Day 38 (class and function design)/Improve the design/Item.java create mode 100644 Day 38 (class and function design)/Improve the design/Player.class create mode 100644 Day 38 (class and function design)/Improve the design/Player.java create mode 100644 Day 38 (class and function design)/Improve the design/tp06bce7.BAT create mode 100644 Day 38 (class and function design)/Information hiding example/fixed/CS495.class create mode 100644 Day 38 (class and function design)/Information hiding example/fixed/CS495.java create mode 100644 Day 38 (class and function design)/Information hiding example/fixed/Student.class create mode 100644 Day 38 (class and function design)/Information hiding example/fixed/Student.java create mode 100644 Day 38 (class and function design)/Information hiding example/fixed/superheroes.txt create mode 100644 Day 38 (class and function design)/Information hiding example/original/CS495.class create mode 100644 Day 38 (class and function design)/Information hiding example/original/CS495.java create mode 100644 Day 38 (class and function design)/Information hiding example/original/Student.class create mode 100644 Day 38 (class and function design)/Information hiding example/original/Student.java create mode 100644 Day 38 (class and function design)/notes.txt diff --git a/Day 24-27 (database)/sprint.txt b/Day 24-27 (database)/sprint.txt deleted file mode 100644 index 6560251..0000000 --- a/Day 24-27 (database)/sprint.txt +++ /dev/null @@ -1,3 +0,0 @@ - -Sprint planning: - Determine tasks to be completed this week, with quick difficulty estimates. Tasks should be relatively small, specific, completable pieces of work. Plan how it's going to get done. Use the kanban in Gitea to populate it. Spring ends with next Monday's class meeting. diff --git a/Day 24-27 (database)/CampusVisits.db b/Day 24-37 (database)/CampusVisits.db similarity index 100% rename from Day 24-27 (database)/CampusVisits.db rename to Day 24-37 (database)/CampusVisits.db diff --git a/Day 24-27 (database)/Database selection.txt b/Day 24-37 (database)/Database selection.txt similarity index 100% rename from Day 24-27 (database)/Database selection.txt rename to Day 24-37 (database)/Database selection.txt diff --git a/Day 24-27 (database)/Joining_Data_in_SQL_2.pdf b/Day 24-37 (database)/Joining_Data_in_SQL_2.pdf similarity index 100% rename from Day 24-27 (database)/Joining_Data_in_SQL_2.pdf rename to Day 24-37 (database)/Joining_Data_in_SQL_2.pdf diff --git a/Day 24-27 (database)/indexes.txt b/Day 24-37 (database)/indexes.txt similarity index 100% rename from Day 24-27 (database)/indexes.txt rename to Day 24-37 (database)/indexes.txt diff --git a/Day 24-27 (database)/notes.txt b/Day 24-37 (database)/notes.txt similarity index 100% rename from Day 24-27 (database)/notes.txt rename to Day 24-37 (database)/notes.txt diff --git a/Day 24-27 (database)/transactions.txt b/Day 24-37 (database)/transactions.txt similarity index 100% rename from Day 24-27 (database)/transactions.txt rename to Day 24-37 (database)/transactions.txt diff --git a/Day 38 (class and function design)/CouplingVsCohesion.svg b/Day 38 (class and function design)/CouplingVsCohesion.svg new file mode 100644 index 0000000..e587b60 --- /dev/null +++ b/Day 38 (class and function design)/CouplingVsCohesion.svg @@ -0,0 +1,177 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + a) Good (loose coupling, high cohesion) + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + b) Bad (high coupling, low cohesion) + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Day 38 (class and function design)/Improve the design/CoinInventory.class b/Day 38 (class and function design)/Improve the design/CoinInventory.class new file mode 100644 index 0000000000000000000000000000000000000000..71aed65645864980a93058be15a788d6a3babf99 GIT binary patch literal 538 zcmZ9I%}&Bl5QWd}e@hhw6ct(Ms#GIOH^vQ#35n5#Mi;CKHdumf0tMqcxzI#nVvG;q zLmAI4C}Nt-+&TBmH*@>>_4Wba5LE{{Hf(6vlwr$6#l*IN(@CPZk&NPWD4?{O0>*t9 zb#GGvA&a9Zj?!ZR)vH|zXpQ716v&@Oadf30n>#ATVQFy zBd{V}^?nd{>*wuW*h#U&2kZO`3P1K`C$=){;6UUUT~_7QaMgRZ+ literal 0 HcmV?d00001 diff --git a/Day 38 (class and function design)/Improve the design/CoinInventory.java b/Day 38 (class and function design)/Improve the design/CoinInventory.java new file mode 100644 index 0000000..98e1739 --- /dev/null +++ b/Day 38 (class and function design)/Improve the design/CoinInventory.java @@ -0,0 +1,26 @@ + +public class CoinInventory { + private int[] coinCounts; //cp (pennies), sp (dimes), ep (half-dollars), gp (dollar bills), pp (five dollar bills) + private int weight; //The weight is the sum of all the coins. + + public CoinInventory() { + coinCounts = new int[5]; + } + + public void setCoins(int[] a) { + coinCounts = a; + } + + public int[] getCoins() { + return coinCounts; + } + + public void setWeight(int w) { + weight = w; + } + + public int getWeight() { + return weight; + } + +} diff --git a/Day 38 (class and function design)/Improve the design/Item.class b/Day 38 (class and function design)/Improve the design/Item.class new file mode 100644 index 0000000000000000000000000000000000000000..628a2ffb4bee64e6930162713b1eb76dbd746e19 GIT binary patch literal 457 zcmZWlO;5r=6r9&Wv86&q#EZ#-==ncu( zkxFmpR%X3Hvl5w$#r$f$vFs{vI^#(as!KhyX{Bm(n7z}5gKZZU3NBV)GdjBxcz;7& z_XaBD)0x-BJ_pb9Hw-Q|&35c#HNg-Ri7rn^yf<0P{yW4!n8NUPr!Zeh308>}8i5u$ tn%Imk5jhRYbAvrrp(5|U!F>L0vu31A^sq+EvKFPr%Ri|{QH7j=^&kJ+P1gVb literal 0 HcmV?d00001 diff --git a/Day 38 (class and function design)/Improve the design/Item.java b/Day 38 (class and function design)/Improve the design/Item.java new file mode 100644 index 0000000..0ff9e1e --- /dev/null +++ b/Day 38 (class and function design)/Improve the design/Item.java @@ -0,0 +1,18 @@ + +public class Item { + private String description; + private int weight; + + public Item(String description, int weight) { + this.description = description; + this.weight = weight; + } + + public int getWeight() { + return weight; + } + + public String getDescription() { + return description; + } +} diff --git a/Day 38 (class and function design)/Improve the design/Player.class b/Day 38 (class and function design)/Improve the design/Player.class new file mode 100644 index 0000000000000000000000000000000000000000..543f69cd4d38d2ff587c15f54789d48d2d8e68fd GIT binary patch literal 2675 zcmb7GX>Su(6g?B$GmaS&V;soBVjuy>28Xbh00{&Fp$=OpG$d^^v4>>vc*fXcAarkO z)17Wop{mp$(5k3aNmP*9kCpnR{R!>=s8G+FF^Nqgq)wu@+;{J}@11+!eg5OogI@vc z#g`#;<5WGGaazHd5PaU`tb%hP$lhdFET0$C=Y+xq(Oi`Kq6FX<;^Omi-|aqkCimXD z`OonmqA$z6zuXny*K+UQ?g|u)1aV2Ik17}o;)M{}@L~`z3DLL$EkZp#gcwGJ=&LIg+@x}Y>yuefR z+ga*1rVUF!MO(diUYjr}QM7DU0CNX%&x24`Gq!2!8E440be=019~7ThX`IxZb7jjS z0mB7d&r0ycJCBlzVMoj4hO|NjcRBT3>kFD$rk@7Rk5zA*X56?xuNjtvRq@fK_Z=Fx z%f*a7V2HggJ7a3odNCy;q2d4>6+5t#zH3-lu?xE;taRg5%d?6-*sEecdQ_BgO~rNG zP%(|0Do#Mq=sDo|FHSKmre1Q4Ois8u$gO%bH4JH!MkZFeZWpt$Z_c`c{++g3(%E*j1hMrmrqS88EHRk0YJF1Z)ZXO|g~ zNdLhV$bU#?%JLGldxkxW15zcaF+4wA(3eoJzT}S+ zZZSl^W}3r>qxUHIf)QVIzNPjiVcpa9uz`<2Hy;N-%Rj3IM|Gl5xIL=^--&(h_!-Js zAUIYiS-%M};A4>d;U~(3riwhVbccqo~ZPaFEsMuU3szRHC zvRM`ljqSr1Cu#a)Us{q+#dBaxOlw5A`TZ6vZL9G%13A$Pnk9Ow8{a_cPCC%1`) zEQJqzi|9kdM#SU+8O9to{+0}f0={sZVcg^@6>F5*D@wEIh+^|scjo~*9*|TIz~}RY zW#41_8#VTw^Y)1-l2!JJ2MmO3{}WP?u32ms`QXKDKN>hb$RB5i7_(kxLLas;VYe~` z2bhFsnF~2aZHBYY7{BjG^zWqlPqHLp8$sX^1{5?aXybo-+om?spLX$waGQOITz*X3 ZPnc^HoW~GAhO(^NPP(n(wmG-W{|BhXZn^*f literal 0 HcmV?d00001 diff --git a/Day 38 (class and function design)/Improve the design/Player.java b/Day 38 (class and function design)/Improve the design/Player.java new file mode 100644 index 0000000..0ea561c --- /dev/null +++ b/Day 38 (class and function design)/Improve the design/Player.java @@ -0,0 +1,77 @@ +//The design among these 3 classes needs improvement, and the code has a couple of bugs, too. +//Improve the design and fix the bugs. +//The public interface of Player should remain the same as it is now. + +//Hints: + //Avoid duplicate data + //Methods and instance data should have high cohesion + //Coupling should be loose between classes + +import java.util.*; + +public class Player { + private String name; + private CoinInventory coinInventory; + private ArrayList otherInventory; + private int encumbrance; + + public Player(String name) { + this.name = name; + coinInventory = new CoinInventory(); + otherInventory = new ArrayList(); + encumbrance = 0; + } + + public void addItem(Item item) { + otherInventory.add(item); + } + + public void collectMoney(int cp, int sp, int ep, int gp, int pp) { + int[] moneys = coinInventory.getCoins(); + moneys[0] += cp; + moneys[1] += sp; + moneys[2] += ep; + moneys[3] += gp; + moneys[4] += pp; + coinInventory.setCoins(moneys); + } + + public double getRunningSpeed() { + double speed = 5 * (1 - getEncumbrance() / 1000.0); //Speed 5 if unencumbered; speed 0 if 1000 units carried or more. + if (speed < 0) { + return 0; + } + else { + return speed; + } + } + + public double getMoneyValue() { + int[] moneys = coinInventory.getCoins(); + return .01 * moneys[0] + .1 * moneys[1] + .5 * moneys[2] + 1 * moneys[3] + 5 * moneys[4]; + } + + public int getEncumbrance() { + int[] moneys = coinInventory.getCoins(); + return moneys[0] + moneys[1] + moneys[2] + moneys[3] + moneys[4] + encumbrance; + } + + //Test method. + public static void main(String[] args) { + Player p = new Player("Whistlestick"); + Item sword = new Item("Magic sword +2", 20); + Item shield = new Item("Normal shield", 40); + p.addItem(sword); + p.addItem(shield); + if (p.getEncumbrance() != 60) throw new RuntimeException("getEncumbrance() should be 60, not " + p.getEncumbrance()); + p.collectMoney(10,2,0,40,2); + if (p.getEncumbrance() != 60+54) throw new RuntimeException("getEncumbrance() should be 114, not " + p.getEncumbrance()); + if (p.getMoneyValue() != 50.3) throw new RuntimeException("getMoneyValue() should be 50.3, not " + p.getMoneyValue()); + p.collectMoney(0,0,0,6,0); + if (p.getEncumbrance() != 120) throw new RuntimeException("getEncumbrance() should be 120, not " + p.getEncumbrance()); + if (p.getRunningSpeed() != 4.4) throw new RuntimeException("getRunningSpeed() should be 4.4, not " + p.getRunningSpeed()); + System.out.println("All tests passed."); + } + + +} \ No newline at end of file diff --git a/Day 38 (class and function design)/Improve the design/tp06bce7.BAT b/Day 38 (class and function design)/Improve the design/tp06bce7.BAT new file mode 100644 index 0000000..6a8868c --- /dev/null +++ b/Day 38 (class and function design)/Improve the design/tp06bce7.BAT @@ -0,0 +1,5 @@ +@ECHO OFF +C: +CD "\stuff\cs495\notes\Day 38 (class and function design)\Improve the design" +"C:\Program Files\Microsoft\jdk-11.0.12.7-hotspot\bin\java.exe" -cp .;"C:\Program Files\Java\javafx-sdk-17.0.0.1\lib\javafx.base.jar";"C:\Program Files\Java\javafx-sdk-17.0.0.1\lib\javafx.controls.jar";"C:\Program Files\Java\javafx-sdk-17.0.0.1\lib\javafx.fxml.jar";"C:\Program Files\Java\javafx-sdk-17.0.0.1\lib\javafx.graphics.jar";"C:\Program Files\Java\javafx-sdk-17.0.0.1\lib\javafx.media.jar";"C:\Program Files\Java\javafx-sdk-17.0.0.1\lib\javafx.swing.jar";"C:\Program Files\Java\javafx-sdk-17.0.0.1\lib\javafx.web.jar";"C:\Program Files\Java\javafx-sdk-17.0.0.1\lib\javafx-swt.jar" --module-path "C:\Program Files\Java\javafx-sdk-17.0.0.1\lib" --add-modules=javafx.base,javafx.controls,javafx.fxml,javafx.graphics,javafx.media,javafx.swing,javafx.web Player +PAUSE diff --git a/Day 38 (class and function design)/Information hiding example/fixed/CS495.class b/Day 38 (class and function design)/Information hiding example/fixed/CS495.class new file mode 100644 index 0000000000000000000000000000000000000000..552b3e0a31060620727e667b7a71758bf6d89932 GIT binary patch literal 1046 zcmZuw?M@Rx6g^Y6OxZ4!0tG6fprS1wts;m}{J;u|mSU{I6r;(~PGGU!ZMItjkHQo1 zTYt2{CdS`Bhl%fCRJ^k-MbvHf-kH-o=bn4_&X->wKLMD-nu-uEaa>m6k^TzDm=9M~ zcyUdIf`~LxX~sDwRQM(A!K6f{wk~+ST9b@q%t{$1{#?nA-_?uruT6xwmV2 z$&KfjV^Ej(a-!rImRaVwP5X>hv2$X@kbG(~Gc!LMlUy45F~HF4@>U$95MQ!weLrQC z9fsg3n#t*=DQpJK#SAOnpbC^LC1LLf+Y;rNv*$>@d1)3jB(SLA4yZn!aVmLX(!cj` zU&9gxG(133!$TRej1`VY8XjYn^ZXi?kCw~@R=>3FB1Kii{kWr{;xa@orx%& z_vQ*#nb>?rOKATIkK65&0ut1uW`6qKGAqN`j7`=v9m(vM9Z@6^R)yk24SiJ8Ga3zt z21MvZB5t+j517)GWKeP(YP#BX2}eKh>?@qmU817rtjH%y+9_m0=V-qV61Ot#$x z6?EVnhUkb4J)(Q!CI6-lPqv1(R2@pT4sW)ny#`)`Z*8)QjspymQ_~Z5__I?rXjOE+ zJUsqJVAn(yf%Mc7x)}}-rbUq9GyDt62lRxMDtZr(ze)`rS3`_0pj6@mr@ap&q-G2% zrU{>ghIw@1IRe;3H(n!%^AsI|@)KbW4~H`J13r%7@3=re&>d#DNbd-(JsABB5@+bu literal 0 HcmV?d00001 diff --git a/Day 38 (class and function design)/Information hiding example/fixed/CS495.java b/Day 38 (class and function design)/Information hiding example/fixed/CS495.java new file mode 100644 index 0000000..350d4f1 --- /dev/null +++ b/Day 38 (class and function design)/Information hiding example/fixed/CS495.java @@ -0,0 +1,18 @@ +import java.util.*; +import java.io.*; + +public class CS495 { + public static void main(String[] args) throws IOException { + ArrayList students = new ArrayList(); + Scanner in = new Scanner(new File("superheroes.txt")); + while(in.hasNextLine()) { + String s = in.nextLine(); + students.add(new Student(s)); + } + in.close(); + + for(Student s : students) { + System.out.println(s); + } + } +} diff --git a/Day 38 (class and function design)/Information hiding example/fixed/Student.class b/Day 38 (class and function design)/Information hiding example/fixed/Student.class new file mode 100644 index 0000000000000000000000000000000000000000..f0cecc250fe63c5095e66ecee4ae11513a67f238 GIT binary patch literal 964 zcmaJ*DZQY!Ql%}>S{_ZLF;SXE60{FA`rwNLOxi8W4A~tT`%nBC z-WroW_yhb=#xv|jFc2T+%;kLFcP?{&{l5JP;3;YuI9N|3i49{4#@x%1YsLqg&|+A%#sOl)b(TaMA>L%+G-SZ#ffM5Zk+0&$;!nN z@-9+HyT}-`2$$i`OQm$E13r8$^o8n$*(A18c;wr<%5$)1(%5GxO@e#!O7%tD z-AMj~ceM(x9Xz3Cx0LFShE#h~N)a@C9)^NqewyxmtPxfGip!oa!cs)6&t^9D@(=Os zdMM_=-%sJAPSvMtizOP|_uHNpbq9wGJ2Umo%Z*{{|2S;Z#5y#v38c+O)5$V_J>KcN z#~zwuEQ7|4ZZF7h(O#(j0IPn5b%O+1`@=U_)%_dTU&u*+Pj(FpWK*z7ZjnS+$Zn7% z!(HTJxm^-tgj%kCM}n4pYe=8}Y9z5bRB1$1BS}t0mF+5Gn1Q)hjvTTWcHNN`? zd@?4!dP&4+#CQLR|G+2l>@105Og1y$cjh~1&-rG)|N49YU>pS#?Ko%PJjVqSFFY#OP31$wDViFc?RDg=ent zx9xk>WOz2kkSh4~inJezh8O1E5w3l==5Gk<$69fnJ?Y!`>r#fa6v#bkKd5a=e>Y4N zzTdECT{k>zB32f@uy0dxjRNLc&1=}yxW7%Qt1_TMt3;$vwBWdI!!C$mHU7=_p0HPG{E9R%ZQ%xH4BWJE3$qro=wQ&*j0~ASeyx$&URfYLG4zz4UO} z>G?4v!*K}33DZ^4+C{9Jc1FA2zYq3K2~X0RP{BAQox?YQF~ybA1RE% zLIG(kqY3M1#uH>vC+#y*y&ykdl9O-A%TKf)kdxh1r4zA3yoPR|$G{o-7`ghBEatF} WnN3>g+6T1$Yqmx<_tIeuXMY3P?A~+$ literal 0 HcmV?d00001 diff --git a/Day 38 (class and function design)/Information hiding example/original/CS495.java b/Day 38 (class and function design)/Information hiding example/original/CS495.java new file mode 100644 index 0000000..63c4f9a --- /dev/null +++ b/Day 38 (class and function design)/Information hiding example/original/CS495.java @@ -0,0 +1,25 @@ +// Two problems with information hiding in this example program. + +public class CS495 { + public static int nextStudentID = 1; + + public static void main(String[] args) { + Student[] students = new Student[12]; + students[0] = new Student("Clark Kent"); + students[1] = new Student("Bruce Wayne"); + students[2] = new Student("Hal Jordan"); + students[3] = new Student("Oliver Queen"); + students[4] = new Student("Steve Rogers"); + students[5] = new Student("Barry Allen"); + students[6] = new Student("Barbara Gordon"); + students[7] = new Student("Tony Stark"); + students[8] = new Student("Peter Parker"); + students[9] = new Student("Billy Batson"); + students[10] = new Student("Bruce Banner"); + students[11] = new Student("Diana Prince"); + + for(Student s : students) { + System.out.println(s); + } + } +} \ No newline at end of file diff --git a/Day 38 (class and function design)/Information hiding example/original/Student.class b/Day 38 (class and function design)/Information hiding example/original/Student.class new file mode 100644 index 0000000000000000000000000000000000000000..201ce6ffb7548fa976ca582049563eb7c65ab5d2 GIT binary patch literal 913 zcmaKr-A)rh6vzLkExT>mf+7^~11c713sNsMMr%y8gt$r7ixRx>#%Vi&rQ4ZicS__< zdtZltD-+02>Qng99iIcLuP`hEEmz+*hf!p33-OUP$nVL37@3w(6j z#vL1LgoM{2kV{A@(UXLQ#;G_HRbQy%YSRRsI<66{XP)xRbHY+-^0<7&&UM{k=lO=G z66_el!mC`~woYZ+5R#=R-kEyy;gd&%oRS|+ zEO+;_*upj;A8!=g{ig-mEkuRk1!1`-y0WfSTbOsAIpI}kgfbyvyW~#hHYHsfr6|Z< zLh&jzPo3$mjH^AuABeWm!MTk+E`CGn?x4@bag~xm-4|gf`J11{?;>W5>hVgbjxWRF z$gwsV+1SoMj)&)cIYs_{Oh2k)Z9KOaQ5U}7^o*?8s1i0O@|zYLVeS7qzIuyW7)bEL z9WY#R6AWiA334FJ@!N}`0$8p509C#sae*Y;)Q4|KRCX?q`obdaP_}u@u(gn4@GL`b zFm{DIi~9xFW84GVY^-Rer#1!c88x F{s4pR-V^`; literal 0 HcmV?d00001 diff --git a/Day 38 (class and function design)/Information hiding example/original/Student.java b/Day 38 (class and function design)/Information hiding example/original/Student.java new file mode 100644 index 0000000..7a82e4c --- /dev/null +++ b/Day 38 (class and function design)/Information hiding example/original/Student.java @@ -0,0 +1,15 @@ + +public class Student { + private int id; + private String name; + + public Student(String name) { + id = CS495.nextStudentID; + CS495.nextStudentID++; + this.name = name; + } + + public String toString() { + return "Student ID: " +id+ ", name: "+name; + } +} diff --git a/Day 38 (class and function design)/notes.txt b/Day 38 (class and function design)/notes.txt new file mode 100644 index 0000000..c0e491f --- /dev/null +++ b/Day 38 (class and function design)/notes.txt @@ -0,0 +1,25 @@ +Most common reasons for project failure: Poor requirements/planning/management + +Most common *technical* reason for project failure: unmanaged complexity + +Abstraction: from the outside, we view an object or function as a coherent idea on a high level of detail + +Information hiding / encapsulation: from the outside, internal implementation details are not viewable + +Loose coupling: + * Size (prefer fewer methods and parameters) + * Visibility (e.g. avoid sneaking info between objects through a global variable) + * Flexibility (objects and functions can be rearanged with relative ease) + Example: Passing primitives is looser coupling than passing complex objects + Avoid: some object/function requires knowledge of *internal* workings of another object/function. + +High cohesion: Methods and data in a class are highly related and belong together + +All of this (abstraction, information hiding, loose coupling, high cohesion) mainly comes down to two motivations: + Limit the amount of complexity we deal with at a time + Limit the effects when a change is made to the code +This reduces the cognitive load required to understand any given part of the system at a time. + + + +[The CouplingVsCohesion.svg image is public domain, obtained from https://upload.wikimedia.org/wikipedia/commons/0/09/CouplingVsCohesion.svg]