From 7d35e42bf4b69e9dc80fd44e8ec687caa2bdf12a Mon Sep 17 00:00:00 2001 From: Kody Ellis Date: Sun, 24 Sep 2017 23:35:26 -0500 Subject: [PATCH 01/11] Add files via upload --- lab1/ArrayList1.class | Bin 0 -> 2785 bytes lab1/ArrayList1.java | 130 ++++++++++++++++++ lab1/Student.class | Bin 0 -> 749 bytes lab1/Student.java | 87 ++++++++++++ lab1/student.dat | 5 + lab2/ArrayList1.java | 114 ++++++++++++++++ lab2/FormattedInput02.class | Bin 0 -> 2835 bytes lab2/FormattedInput02.java | 134 +++++++++++++++++++ lab2/Student.class | Bin 0 -> 925 bytes lab2/Student.java | 103 ++++++++++++++ lab2/student.dat | 5 + lab3/lab31.class | Bin 0 -> 844 bytes lab3/lab31.java | 34 +++++ lab3/lab32.class | Bin 0 -> 1671 bytes lab3/lab32.java | 52 ++++++++ lab3/readme | 6 + lab4/DoubleList$1.class | Bin 0 -> 181 bytes lab4/DoubleList$DoubleNode.class | Bin 0 -> 1311 bytes lab4/DoubleList.class | Bin 0 -> 2386 bytes lab4/DoubleList.java | 208 +++++++++++++++++++++++++++++ lab4/Driver01.class | Bin 0 -> 1651 bytes lab4/Driver01.java | 73 ++++++++++ lab4/List2.class | Bin 0 -> 525 bytes lab4/List2.java | 19 +++ lab5/IsPostFixDemonstration.java | 102 ++++++++++++++ lab5/IsPostFixExpression.class | Bin 0 -> 3406 bytes lab5/IsPostFixExpression.java | 145 ++++++++++++++++++++ lab6/BinaryNode.class | Bin 0 -> 410 bytes lab6/BinarySearchTree.class | Bin 0 -> 1478 bytes lab6/BinarySearchTree.java | 84 ++++++++++++ lab6/BinaryTree.class | Bin 0 -> 1624 bytes lab6/BinaryTree.java | 151 +++++++++++++++++++++ lab6/Item.class | Bin 0 -> 751 bytes lab6/Item.java | 40 ++++++ lab6/NodeProcess.class | Bin 0 -> 239 bytes lab6/NodeProcess.java | 9 ++ lab6/Print.class | Bin 0 -> 513 bytes lab6/Print.java | 11 ++ lab6/Print2.java | 14 ++ lab6/ShowBinarySearchTree01.class | Bin 0 -> 720 bytes lab6/ShowBinarySearchTree01.java | 32 +++++ lab6/ShowBinarySearchTree02.class | Bin 0 -> 2259 bytes lab6/ShowBinarySearchTree02.java | 90 +++++++++++++ lab6/ShowBinarySearchTree03.class | Bin 0 -> 1389 bytes lab6/ShowBinarySearchTree03.java | 39 ++++++ lab6/ShowBinarySearchTreeLab.class | Bin 0 -> 851 bytes lab6/ShowBinarySearchTreeLab.java | 51 +++++++ lab6/lab6.dat | 15 +++ lab6/lab6b.class | Bin 0 -> 2728 bytes lab6/lab6b.java | 138 +++++++++++++++++++ 50 files changed, 1891 insertions(+) create mode 100644 lab1/ArrayList1.class create mode 100644 lab1/ArrayList1.java create mode 100644 lab1/Student.class create mode 100644 lab1/Student.java create mode 100644 lab1/student.dat create mode 100644 lab2/ArrayList1.java create mode 100644 lab2/FormattedInput02.class create mode 100644 lab2/FormattedInput02.java create mode 100644 lab2/Student.class create mode 100644 lab2/Student.java create mode 100644 lab2/student.dat create mode 100644 lab3/lab31.class create mode 100644 lab3/lab31.java create mode 100644 lab3/lab32.class create mode 100644 lab3/lab32.java create mode 100644 lab3/readme create mode 100644 lab4/DoubleList$1.class create mode 100644 lab4/DoubleList$DoubleNode.class create mode 100644 lab4/DoubleList.class create mode 100644 lab4/DoubleList.java create mode 100644 lab4/Driver01.class create mode 100644 lab4/Driver01.java create mode 100644 lab4/List2.class create mode 100644 lab4/List2.java create mode 100644 lab5/IsPostFixDemonstration.java create mode 100644 lab5/IsPostFixExpression.class create mode 100644 lab5/IsPostFixExpression.java create mode 100644 lab6/BinaryNode.class create mode 100644 lab6/BinarySearchTree.class create mode 100644 lab6/BinarySearchTree.java create mode 100644 lab6/BinaryTree.class create mode 100644 lab6/BinaryTree.java create mode 100644 lab6/Item.class create mode 100644 lab6/Item.java create mode 100644 lab6/NodeProcess.class create mode 100644 lab6/NodeProcess.java create mode 100644 lab6/Print.class create mode 100644 lab6/Print.java create mode 100644 lab6/Print2.java create mode 100644 lab6/ShowBinarySearchTree01.class create mode 100644 lab6/ShowBinarySearchTree01.java create mode 100644 lab6/ShowBinarySearchTree02.class create mode 100644 lab6/ShowBinarySearchTree02.java create mode 100644 lab6/ShowBinarySearchTree03.class create mode 100644 lab6/ShowBinarySearchTree03.java create mode 100644 lab6/ShowBinarySearchTreeLab.class create mode 100644 lab6/ShowBinarySearchTreeLab.java create mode 100644 lab6/lab6.dat create mode 100644 lab6/lab6b.class create mode 100644 lab6/lab6b.java diff --git a/lab1/ArrayList1.class b/lab1/ArrayList1.class new file mode 100644 index 0000000000000000000000000000000000000000..644663ec26a0152536f37cf6b60c133d92ef7f3f GIT binary patch literal 2785 zcmZ`*X?qh@6n-a7J83$!5Zba840|Yr$|_Q<7Rpjh%TlDYtV1%CfpjKJCTxO&BDjm- zR#!w6#f3$hihUj*Kl@Ak4T|7<%LgUB#YqB#;!wklo%8dc)WUQ%?4)I1t7dq-5!n6HA31Mvb_w zK{|wkLLD*4h*MWZHjFy-2y;jb4u^0==uugY$?*#+j)yS=Cq(f@Ie96Blfs-5XD^5F ziZG`u@G1_=az-$p4dFGhdOd_UguWTVTSDI!n|DHZSH(F6p^TeN8kU<;P!r#;AJQAM zu9<3VaUA_<+|0O53d%dpUQ2hgj-ep>D4iAYPTxsWGu6vXYp+3*}gr7vsvFG z-h+L*X(_0UZS${5=~i!Jr|XzjZ<82?S070jY1g!^jEeWj_Zn`et2?fdRB@hx+iAmE z;{hGT^TdI6J?*(>fmK{!9jW6Oda_k_S@dlRDwAd=ozjnPGBVkeORw)MsLr3|6R^oh z*iKS`78^;4ho#fbItjzSt!gB<3+7A0HLL=&5iT5Scn+-^+GW`!%Vt@gLbHYsM0HG| zPWim5_)x<~_*i`PYxo48Dwwp|at$Zy?lYpMmCm|RsY=w+`;6sL6`yJN9A9Yo5*G>A zb73-=@D(n}?rR0(JZVvWS7yyr(r`3f#y2cYG-(@|sAaoR;|M!O!?(Do;fgqE z!87cG0^E@pH9U#s8ot9N_Ci#{_qeLz2mGkvX)#gr`4j{{Y4{n}G^`XsWHZA_@s?=9 zwp?~VhQ`;$_%d#=Rinf0tl=hpVTmNdTN<{Yiw)P|1+QU1f-Y_8WhOM_aFq;uPnbc0 za2dZcCoO(cMn$4vbRh}r_v|+k?r>bpTHE@OUQ;l+IF}u^yT;C1$s&1?Bfnc2R3Sla zESc`w4iP0%c7}aktba7)8hr`^()jslmFuOt^kwLMoUidQe;ytXNE?>eS;8(DZs4#K z4E-|Etmv39(xODGyxXES@Ww1?8Am9tj?H;^44HASBlxy(n0wf`1#Hr3o`W(y%|YbM z%zM-g4>`^E9s>=z_HY7K4`hm2QTOSY4iAwkz}f^l?{4FK3EygGy~l$`oTG7%xUsF( z%aooZqWfa@tKKAG1T*F_R;Di2_HfWe_{@=7)(&UKds~NV%oy%ZTmA}0!M93!l?mjI zed=$#_pvV4`v13LDj)u3{HB$Xa>AgXiCh3H_~QfO?Kxo{<#2kGgGH)Rmk$xw)#N6V zDx{_LgHUd8qhJl!GVd^qa;(KVx)jxUqyf@VLkM&aq9i^yhhV{^5+xK?BgC(chFY|d z3)0QA{PCH~J6SdnIk^q^ubp% z@ADer2C2Vm2;uIVs2GG6zll+i${ebQadi6-#&kCfBGNH0hq3*bP%nzZhl8jdK$mp z+;vbZf|5I^P_h1BEL5>U#m4dXg~4~9!AO4ZG7OdULdnvy`pN-JBFs9%nLL0geuf_F zN6O!g?#R>S!8~Sy92bdlnGp1WxBD7XmzLK1yJ-WMe!5H%ZW4df6TD&wGrAjUN`)Q7 z%sA;3W^7j1m3!wry_&MK_5fxVLT^SDA-1#D9jweohQFCTw1u_l!hCFHyL4j%daw=K znSvcSfZaF-9cTCl>O2fDB{c}#qoJyzlfI students; + private Scanner infile; + + public static void main(String[] args) + throws IOException + { + ArrayList1 current; + + current = new ArrayList1(); + current.getStarted(); + } // method main + + private double gpa; + private double sumGPA; + private double averageGPA; + /** + Starts the program + */ + public void getStarted() + throws FileNotFoundException + { + openFile(); + readData(); + displayResults(); + infile.close(); + } // method getStarted + + + /** + Opens the input data file for read access. + */ + public void openFile() throws FileNotFoundException + { + Scanner input; + String infile_name; + File file; + + input = new Scanner(System.in); + do + { + System.out.print("Enter the input file name: "); + infile_name = input.next(); + + // open the input data file + file = new File(infile_name); + if (file.exists()) + infile = new Scanner(new File(infile_name)); + else + System.out.println(infile_name + " does not exist"); + } while (!file.exists()); + } // method openFile + + + /** + Reads in text from file and save them in an array list. + @param infile the file which has been open successfully + */ + public void readData() + { + Student who; + String name, first_name, last_name; + int age; + + int count; + + count = 0; + sumGPA = 0; + + // instantiate an ArrayList object + students = new ArrayList(); + + // read in values from the file and assign them to the elements + while (infile.hasNext()) + { + first_name = infile.next(); + last_name = infile.next(); + name = first_name + " " + last_name; + age = infile.nextInt(); + gpa = infile.nextDouble(); + + count++; + sumGPA = sumGPA + gpa; + + + who = new Student(name, age, gpa); + students.add(who); + } + averageGPA = sumGPA / count; + + } // method readData + + + /** + Displays the student records stored in the array list. + */ + public void displayResults() + { + // display the array elements in sequence + System.out.println("The list contains " + students.size() + + " students"); + for (Student who: students) + displayStudentRecord(who); + } // method displayResults + + + /** + Displays the content of each student record. + @param who a reference to a Student object for display + */ + public void displayStudentRecord(Student who) + { + + System.out.println("Name: " + who.getName()); + System.out.println("Age: " + who.getAge()); + System.out.println("GPA: " + who.getGPA()); + + System.out.println("Average GPA:" + averageGPA); + } // method displayStudentRecord +} // class ArrayList1 diff --git a/lab1/Student.class b/lab1/Student.class new file mode 100644 index 0000000000000000000000000000000000000000..e69dca3def17c426ec1b51214499ba9d8fed1418 GIT binary patch literal 749 zcmZvYZBNrs6vzL!?OHagz}OfQP&S_0@~{^s@{u_W)jDCkGQRv&djWnu;`43!4_U1dQ142Lk!C zuK(4q_53)jHIhLThi?RQKjf3BGYYSLMyCSiaTG_%TY-`@+fqMuTa$Xyfz5@-h`_?+JfGX$pp)<> zj@v?ow={WW9hz)rq{)8eDM4>RQ2QC}JhNkx8Tx|v1H#)ILG#u|&^>nq students; + private Scanner infile; + + public static void main(String[] args) + throws IOException + { + ArrayList1 current; + + current = new ArrayList1(); + current.getStarted(); + } // method main + + + /** + Starts the program + */ + public void getStarted() + throws FileNotFoundException + { + openFile(); + readData(); + displayResults(); + infile.close(); + } // method getStarted + + + /** + Opens the input data file for read access. + */ + public void openFile() throws FileNotFoundException + { + Scanner input; + String infile_name; + File file; + + input = new Scanner(System.in); + do + { + System.out.print("Enter the input file name: "); + infile_name = input.next(); + + // open the input data file + file = new File(infile_name); + if (file.exists()) + infile = new Scanner(new File(infile_name)); + else + System.out.println(infile_name + " does not exist"); + } while (!file.exists()); + } // method openFile + + + /** + Reads in text from file and save them in an array list. + @param infile the file which has been open successfully + */ + public void readData() + { + Student who; + String name, first_name, last_name; + int age; + double gpa; + + // instantiate an ArrayList object + students = new ArrayList(); + + // read in values from the file and assign them to the elements + while (infile.hasNext()) + { + first_name = infile.next(); + last_name = infile.next(); + name = first_name + " " + last_name; + age = infile.nextInt(); + gpa = infile.nextDouble(); + who = new Student(name, age, gpa); + students.add(who); + } + } // method readData + + + /** + Displays the student records stored in the array list. + */ + public void displayResults() + { + // display the array elements in sequence + System.out.println("The list contains " + students.size() + + " students"); + for (Student who: students) + displayStudentRecord(who); + } // method displayResults + + + /** + Displays the content of each student record. + @param who a reference to a Student object for display + */ + public void displayStudentRecord(Student who) + { + System.out.println("Name: " + who.getName()); + System.out.println("Age: " + who.getAge()); + System.out.println("GPA: " + who.getGPA()); + } // method displayStudentRecord +} // class ArrayList1 diff --git a/lab2/FormattedInput02.class b/lab2/FormattedInput02.class new file mode 100644 index 0000000000000000000000000000000000000000..dc1c3466336c496bf1d3e317928bf337c2564e63 GIT binary patch literal 2835 zcmZ`*Sz{Ab6#k~$q{$RQC~es!$YNVs%BDz*XrV1c(}0Anpw?+Kqyy2ne{L z;I63PE~vP$r9c(+)hBt$^j9a>sNzp-YBAwOYsFR5qmeaOI zybbE6rJ$yMS57yfTk)2tlQOM%Tf;WW*V!?8njAJQBa#{HGg4dhJ_*j-In-|?9g~Tx zc!N=|HZ9XxqoA~2?7VTqi8^}9F=8s-RN%3bhSlY!RYiXP-flhVdSWN2c#8$6PRh_@ z9lE0{P-A8~nb3z-yiH$GJCo`+a@Nzj?9`y{&~L~}W}GF<77IuU{CDy-Jc6)>ZiI4QdVP{oHDKElTuPT{ly&2?znEfT)?px_gHD!b1#Jcx%B)VeA~$gjG*2^tE_8gVTgi!&!Wz;aib?hjS{v*YE>=RPmFB^SD4v z^L3Y_FKW1i%Nnv6A;cQ4h@Vrq$~s2eEHqq`=*!#VOi9Bi&Pg;slfxz!&M&wwoL`B2 z)b2O+L@;XfXHup!+^XR>vGo_y+Ss?p=x5XB>Oe3;8^_v5!R%s)BDT|IXRKI}yvR|m zGZh3}6L&H~tc32^soM^W4yPSsP(g__cYajlSSc2L82TW4zpg&l^LGfOeM{|3Va6!m z#DORnj-^q=CNfD;;&FMmMFq``l4T50TwUKVe$3a}`xbDj8)Sm0_y+WJgb~SPQ$>QyZ7b86zO;J-$h{B>O&D$&6^L&| z!m#4bfEzXM!QAw?iHA5Laf4OyB=uCirFx9e-R3FRmTe8h+hdNkQO&Ia-PT@&QOzTEXASdisK854#)r6Y$I($Rt9}Cf z3Y8H(4wQ=@^O;rOF&^!LwvXEBbnY5PkT2p2z9SWwfqQWusg?4~;C|Apis)9-ZAJ8I z(hn5TYiPr3$hGB-NI&b`eFr^Pq7z+|uP3LO-x7YSMxpeKpd`E~i_-ICDA>St668^v z2M=FK4GRz=RYuvv{P7~@Do&PAOX1hwID)dqQIykD#bu~>`e~wVGbUp(wRwZ*Ls&{m ztg9Nc@bFj3pSNHdpb?l^8IkZ12aPz~3L`>8UiEoLXQ?B)J4WH_xd3ehRpASm6quZa zpJ`3$9z~$1X#`Uv%~@2RM1Z0CdYVTt?J{c4U>0>pQOnrtnzEQ4n878BnOV$o)o+eS zOeHQX3yC3!qL0*6MpH*X<`R&4#Jg$ztg^*|MB$`Iyf$H}@A3y^9C~`!N#- mSf_)C;t*{9RXm2nIF2K3Vm_4IfJeo)zi^a!ZKpvAcKip>1)P2W literal 0 HcmV?d00001 diff --git a/lab2/FormattedInput02.java b/lab2/FormattedInput02.java new file mode 100644 index 0000000..93b20df --- /dev/null +++ b/lab2/FormattedInput02.java @@ -0,0 +1,134 @@ +/** + file name -- FormattedInput02.java + This program shows how to read formatted data from a text file. + + The data file contains student records which are formatted as follows: + + Field name Columns + ========================================== + Name 1 - 20 + Age 21 - 22 + gpa 23 - 27 + ssNumber 28 - 36 + ========================================== +*/ + + +import java.io.*; +import java.util.*; // for Scanner class + + +class FormattedInput02 +{ + private static final int NAME_WIDTH = 20; + private static final int AGE_WIDTH = 2; + private static final int GPA_WIDTH = 5; + private static final int SSNUMBER_WIDTH = 9; + + private Scanner infile; + private ArrayList list; + + public static void main(String list[]) throws IOException + { + FormattedInput02 one; + + one = new FormattedInput02(); + one.getStarted(); + } // method main + + + /** + Starts the program using an object of the class + */ + public FormattedInput02() + { + list = new ArrayList(); + } // constructor + + + /** + Starts the program using an object of the class + */ + public void getStarted() throws FileNotFoundException + { + openFile(); + readData(); + display(); + } // method getStarted + + + /** + Opens the input data file for read access. + */ + public void openFile() throws FileNotFoundException + { + Scanner input; + String infile_name; + File file; + + input = new Scanner(System.in); + do + { + System.out.print("Enter the input file name: "); + infile_name = input.next(); + file = new File(infile_name); + if (file.exists()) + infile = new Scanner(file); + else + System.out.println(infile_name + " does not exist"); + } while (!file.exists()); + } // method openFile + + + /** + Reads student records from the file and separate the fields of + each record using substring method. + */ + public void readData() + { + String name, text; + int age; + double gpa; + String ssNumber; + Student who; + + while (infile.hasNextLine()) + { + text = infile.nextLine(); + + // obtain name field + name = text.substring(0, NAME_WIDTH); // extract name field + name = name.trim(); // remove leading and trailing spaces + + // remove name field from text + text = text.substring(NAME_WIDTH, text.length()); + + // extract age field from text and convert it to an int type + age = Integer.parseInt(text.substring(0, AGE_WIDTH)); + + // extract gpa field from text and convert it to a double type + gpa = Double.parseDouble(text.substring(AGE_WIDTH, AGE_WIDTH+GPA_WIDTH)); //AGE_WIDTH + GPA_WIDTH + + ssNumber = text.substring(AGE_WIDTH+GPA_WIDTH,AGE_WIDTH+GPA_WIDTH+SSNUMBER_WIDTH); + + + who = new Student(name, age, gpa, ssNumber); + //socialSecurity + + list.add(who); + } // while infile still has data + } // method readData + + + public void display() + { + for (Student who : list) + { + System.out.println("Name: " + who.getName()); + System.out.println("Age: " + who.getAge()); + System.out.println("GPA: " + who.getGPA()); + System.out.println("Social Security:" + who.getssNumber()); + } // for still student records available + } // method display + +} // class FormattedInput02 diff --git a/lab2/Student.class b/lab2/Student.class new file mode 100644 index 0000000000000000000000000000000000000000..a048607a92448b07095e7e751b37322eb9751444 GIT binary patch literal 925 zcmZvZUr!T36vfZ%U)rwnUn#T*sIa>vlE|ZAQf)Ae1W78qnl0UImcT;VEuV@6NNh}e z@B{dvjAv%4HM@P8nLGD)&zyVb_n)7?0PJGB3>z;?u<%MQujR5Smn|Ev4Nt&Gg3C}~ z?(jUg33e`mB-&}Gy*P>93Ftw@lxXo3T?afh1*ZD_qrqh->mS@v2MF>BBe)Goi0R=YltOKTMC5m&FNkveY8ckd6$uHHtqSH)PzDafSJ@J3AWb zEx2CM3*`tXIoopw6thDrKHj?BR{dqw6wYd>%7IIo?G>v-FhX3&@3c1;B)n}O4Q_0Z{sc&doOxWC&Ly+HcjfTK)y z(;xQRVnZrE!*yEKm1V_cB88I-sgQRVNVoFD@j8B&W6{JCmKiczFNM{VyVkg*fpw=Z zZdx1_6PK`J;xeiXxqs`rJCtrm^i5pBRR(4$W!1zrTxW=d>3=5;o7rsbh<3nmD&&!V zrM~&6fxm#wH-kWQX=476;F<9}2>+Od5zt74)2$t%Ipk*GJq2=Uk{SE>L2^&*26c~^ zHM?G{(}tX0Pk0@MJlRKLel$JefaTBC5wc~=IITCs?#Y=iaa@FF^3r}U>3JY f-ykbXh$JEhnC8ea_8p5jO)ib8n?*@ z@C`f_UU{Vt4y#$KAHW~L#}Lja*F~Ls1?dG@`KvE3e1)z_qJDfEAcn*Ddj*pF<6T`o@hxw3}OSH(Y_qE zBTtlkg=#hU*3{&LLbUN39EQOJU5Xu9C<@PS@(uw_wIMz@Glu1L(IR-Xte2M!u zzQWfG)8}Et!V81SIUp~2x2*fo`C;p8f#{0qR6-F4(rF4IlN{>1tuPQlJEr9L<1t4@ zyAF3MezhthL5@LEwh059Hon1wV@l5y>~7m{R7AvZeS;X{k>LMl!+GozX^N+K&av?= zzEjip45=}yQO4m!0QHXJ$Bwf9fFEseY}xQYy^u+&%J9l*r>t!~6rN=0>(V}?O&tBO zu(Wp4Ee!cs$Q8chH`~op#68tQMZf0Dn7YwjQcZW}1?AmXiY6UUb?N299#w!rt1YQb z1#iw1qTAJ=pweNRj&~ZkPMv)kcyhoqYRa>voG9uD_6)_zov;FG<6_uOE)dBxN>C@h>s!)#MVXqjvG5cvFUlmzkm8 zeG!Q~j3n7c00-CTgQZ4_9=fz(c%7tcv@%*VBYR+fkihT;&3dBDq6cr{Eeau5_K38- zWcz0ysm(p4=jvu2)tZf%tG-^NJ3syQeH{Al7>hcvh!94S4isMY6rtA|5TFyD*8z7qx|V@ z7{C<_5>;1m9m5zQA}5K$Tg1u&d6$TzC9>4;4nnwyFp3>a;Aa%@g#If2#xz+$-|zGR pf%F=9+aTAIVZfyd91zBZLsL4PR>niUg&QPmRF4sQMoCU#>_6(Jjqv~g literal 0 HcmV?d00001 diff --git a/lab3/lab32.java b/lab3/lab32.java new file mode 100644 index 0000000..6fd5712 --- /dev/null +++ b/lab3/lab32.java @@ -0,0 +1,52 @@ +/** + file name -- ShowRead01.java + This program shows how to read data from the keyboard in Java. +*/ + + +import java.io.*; +import java.util.*; // for Scanner class +import static java.lang.Math.*; +import java.text.DecimalFormat; + + +class lab32 +{ + public static void main(String list[]) throws IOException + { + Scanner source; + double a,b,c,s,area; + DecimalFormat two = new DecimalFormat(); + two.setMaximumFractionDigits(2); + + source = new Scanner(System.in); + int error = 0; + + do { + //if statennt if side diesnt meet triangkle law then reorint message + System.out.print("Enter the length of the first side of the triangle: "); + a = source.nextDouble(); + + System.out.print("Enter the length of the second side of the triangle: "); + b = source.nextDouble(); + + System.out.print("Enter the length of the third side of the triangle: "); + c = source.nextDouble(); + error = 0; + + if ( a > b +c || b > a+c || c > a+c) + { + System.out.print("One side of the triangle doesnt meet the hyponenuse law, try a different number."); + error = 1; + } + + }while( error == 1); + + // calculate total cost of the shirt's purchased + s = ( (a + b + c) / 2); + area = sqrt( s*(s-a)*(s-b)*(s-c) ); + + System.out.println("The area of the triangle with sides: " + a + ", " + b + " and " + c + " is : " + two.format(area)); + + } +} // class Triangle diff --git a/lab3/readme b/lab3/readme new file mode 100644 index 0000000..c68c8a7 --- /dev/null +++ b/lab3/readme @@ -0,0 +1,6 @@ +Lab31 needs to be documented prperly. +lab31 random number generator is not properly used. It should generate number from 1 to 54. +-5 + +The documentation for lab32 is not prperly done. +-2 diff --git a/lab4/DoubleList$1.class b/lab4/DoubleList$1.class new file mode 100644 index 0000000000000000000000000000000000000000..cb3e92ae6f0c8df13afcbb17018bbfbf55b530f0 GIT binary patch literal 181 zcmX^0Z`VEs1_omW9(D$Hb_Om+2Cm@z(xT*4x6GVWMh1SD{L-YHRG-Y^61}X%vP7VW zYhH3resN}Ax^HSpMt%xA12-cB4?>xWAtM6=P{cDYFSW=yC$YFVwV07X0Hj$zCowNw x-#;lUHMs<64?>TC0#FAN&>0}W41^3o9t#62kY-_E1CpFT%mq}#2;vEF001Q#CshCd literal 0 HcmV?d00001 diff --git a/lab4/DoubleList$DoubleNode.class b/lab4/DoubleList$DoubleNode.class new file mode 100644 index 0000000000000000000000000000000000000000..192994ff8fa0ec193a25700a769b18000e35f8b7 GIT binary patch literal 1311 zcma)5O>fgc5Ph4Zv11G|p`}I8mX8351Nms+LP9P@ph{6WG~&PoIq9mnO&t_F2>umF zRFydJ1Nc#>Z<_==*pWDRc4z0!n>U`_-+zAo0?@?s95j@bv6ZP9G*8s-sR12T4Rs9- zfy|*3I0A+4vGduf4IS^O_O^d42SH09ci(NR+wfd2+lvbbMd>0_)vruP|I8MUPcYPksqzHM7A- z*DuytFG531w(WXu&=JU&qJi=|_PKj0FyD1Oxj#AW%W==4Yz;dC5ZGKuh$I&qEtPIK zgMsw@Vxvy{nLGb75hiBhzE(#LJ?>WzNKHm5De;z&tFo1Ux zXvMh%T5vv!*Ar;M8*wUwA`QthEb0YWMnt_R%OzPZ%Q70pSQJ;H7>{B?12pJUm5MUQ zN=B`wY8nz(s$0t?HB((t8p5^W$NXqh6t62|MZ<}_v!5^4tPcO^kcRkNajjxlTP7X) z0;h)Orz};TzqzS~MtXgZpqbv$!j@^OilrekT&xtW5e-f0t_9+b*-TpV#fqBQDlaQ@ z-XK5Xm5mKGUf#5B(z{r5B|2TxkeaiM!p5|*>7I?0RAtS&ZpUOU$zr9ZOzZNhrA!US z)A@B{+vqD9m9@Uv<#koCh6Fj4_L+;WJ6@w|wJJGAR#Zt@Lc~-xWl2G3T&`MHwM<6F z%C=D{a64A6ZmVU|p=c`0EUIlV2fS8?#5-^f8zUikDk z6ge?h-7*VmqA2xi^&8nM`Rh21b{%KX&XV|hfhNm|NgXF}QpcN^(s5Om<9J%fvtoM- zc^%W3(J?E_+jz$tC#O1Xjdj-0Z1ZH|0mPh!_H5f@12McrD~E}zxP6Fb+if882u({F?E%8OXwLlN{Pz%9^yTPcrfC>RIgYMpqQk>u#7%vEx}>LoSZ)4hkpVh?@edx+MBq?;ja z$sV!GJ_V-T!xS@1gbIJer2S+&5ohE%Brwl*EimClQhbjoFJTbZa1rlg3Lm&_OOscZ z$Lp5MYw{3YdVqq*F|tTP`je1aC#emRDswI>BvoZkHgO&|FpL@|UAX|ZTS(#) zQv8ff{~YJ=1z)o-S>3O&im#c-ZF@4Jq0IF#vwx#r%<-y2noZ zY^N4R=yZY@z5j=i^f83zj)zfa9mIG@j7P-yl^BorVf68yI+x`P5v67^r%9=wCCOXzll=rjy=M&4hT_cdN0^fAFIG=}ladD-GaPdWG;Bp5pf?r6c5swiw&mf@28% E2h?`ihX4Qo literal 0 HcmV?d00001 diff --git a/lab4/DoubleList.java b/lab4/DoubleList.java new file mode 100644 index 0000000..a02beee --- /dev/null +++ b/lab4/DoubleList.java @@ -0,0 +1,208 @@ +// file name -- DoubleList.java +// This file contains the definition of DoubleList class which is +// a circular doubly linked list. It also includes an inner class +// DoubleNode. Since it is an inner class, all members of this +// clas are public to the methods in the DoubleList class. + + +// ======================== packages ======================= +import java.io.*; +import java.util.*; + +public class DoubleList implements List2 +{ + private int size; + private DoubleNode head; + private DoubleNode current; + + + // ****** inner class DoubleNode **************************** + private class DoubleNode + { + private ItemType data; + private DoubleNode back_link; + private DoubleNode next_link; + }; // class DoubleNode + // ------------- end of inner class ************************ + + + public DoubleList() + // purpose -- initialize the list object + // precondition -- none + // postcondition -- size set to zero, use head to create a dummy + // node, and set both head and current point to the + // dummy node + { + makeEmpty(); + } // constructor + + + public void makeEmpty() + // purpose -- deallocate the spaces used to hold the list of nodes + // precondition -- none + // postcondition -- if list is not empty, the list nodes are freed + // from the heap. + { + head = new DoubleNode(); + head.next_link = head; + head.back_link = head; + size = 0; + current = head; + } // end makeEmpty + + + public boolean isEmpty() + // purpose -- check to see if the list is empty + // precondition -- none + // postcondition -- if list is empty, 1 is returned; otherwise 0 is + // returned. + { + return (size == 0); + } // end isEmpty + + + public int length() + // purpose -- return the length of the list. + // precondition -- none + // postcondition -- returns the number of nodes in the list. + { + return size; + } // end length + + + public void insertAfter(ItemType item) + // purpose -- insert item to the list. + // precondition -- none of the list nodes contains the same key value + // as that in item. + // postcondition -- the item is inserted after the current node of the + // list and the the size of the list is incremented + // by 1. + { + DoubleNode new_node; + + new_node = new DoubleNode(); + new_node.data = item; + + new_node.back_link = current; + new_node.next_link = current.next_link; + current.next_link.back_link = new_node; + current.next_link = new_node; + current = new_node; + + size ++; + } // end insertAfter + + + public void insertBefore(ItemType item) + // purpose -- insert item before the current node. + // precondition -- none of the list nodes contains the same key value + // as that in item. + // postcondition -- the item is inserted after the current in the list + // and the the size of the list is incremented by 1. + { + //do if condition for same key value + DoubleNode new_node; + + new_node = new DoubleNode(); + new_node.data = item; + + new_node.back_link = current.back_link; + current.back_link = new_node; + new_node.next_link = current; + new_node.back_link.next_link = new_node; + current = new_node; + + size++; + } // end insertBefore + + + public void delete(ItemType item) + // purpose -- delete a node from the list + // precondition -- the list has a node containing a key matching that + // of item. + // postcondition -- the node containing a key matching that of item is + // removed from the list and size of list is + // decremented by 1. + { + while (current.data != item) + { + current = current.next_link; + } + + if (current.data == item) + { + current.next_link.back_link = current.back_link; + current.back_link.next_link = current.next_link; + + size--; + } + } // end delete + + + public void resetList() + // purpose -- reset current so that it points to the + // beginning of the list. + // precondition -- none + // postcondition -- holds the address of the first node if list is not + // empty; otherwise, it holds NULL value. + { + //if (size != 0) {} + current = head.next_link; // goes to the link in next link of what head piints to + } // end resetList + + + public void bottom() + // purpose -- reset current so that it points to end of list. + // precondition -- none + // postcondition -- holds the address of the first node if list is not + // empty; otherwise, it holds NULL value. + { + current = head.back_link; + } // end bottom + + + public void advance() + // purpose -- advance current to next node. + // precondition -- current holds the address of a node. + // postcondition -- advance current to the next node. + { + if (current != head.back_link) + current = current.next_link; + else + current = head.next_link; + } // end advance + + + public void moveback() + // purpose -- move current to previous node. + // precondition -- current holds the address of a node. + // postcondition -- move current to the previous node. + { + if (current != head.next_link) + current = current.back_link; + else + current = head.back_link; + } // end moveback + + + public ItemType retrieve() + // purpose -- retrieve the current node data from the list. + // precondition -- current does not point to the last node of + // the list. + // postcondition -- the data of the node referenced to by the + // current is retrieved and copied to item. + { + return (current.data); + } // end retrieve + + + public void replace(ItemType data) + // purpose -- replace the data field of the current node. + // precondition -- none + // postcondition -- data field of current node is replaced with the + // data. + { + current.data = data; + } // end replace + +} // class Double List diff --git a/lab4/Driver01.class b/lab4/Driver01.class new file mode 100644 index 0000000000000000000000000000000000000000..5502ba52835f97c7c3bd5885c38b39fe23a181c4 GIT binary patch literal 1651 zcma)7&36-36#u=n%}m3ThL*N~fK$In8cK>q5KE!-11yH78blHNc*(q^gOiz@$xEaz zT(NZF+9ey;x)>DB@wjr~!kw=DJ3RPt?`~xupc>A7I=5T#3IX! zyj)_6C4P39cgrSLSXt#|&BPU!R}H*rU|k@2!S!8zQD7jGy(uub5;#g=wBY(`qg5}f z@P;gVWEreW*B6+~+%DXeZJGC^U(1(t==!wh4)}vcptX}rur+e+;Tlfg%&=*Z3}nsp@1Op9eiZrV|-#k zV$(vI35tBq>8A%(zOx&y!f1!lGhK8yzt3E~WzXA=@|0FiCtbazx>+hdl(|z}f%I;! z#qwQM(E{VK$qn-B#s50A2Uyx}YE`F^2wDW6=ro-0o78PhJy){6&_mN>7BCtN@@P~h zGEbkB1G^cm`&!jhNM3E}wN$an1Gb*+@#`We+&R_ffWS5N`WTQbX5t!{g$xg7IBs;G`cL*re zJ@}n>OIyM>rT^ zf-IBw6XKhHej`+bDP}m-VL1HE3`hDIp4-j#=rgnI$%9Llh)ec)T;EBUIE*xo;1Jc; zCR>I|I7dZ1kC(Ab+BIacff>3sPD0ZKe~jCH#&h{6Q>FutH5+5=pEH3s;F56fyZ1UwN;HKQa3R;~g=@0w?JCAhk3@zigcG F%)b~Ak4^vp literal 0 HcmV?d00001 diff --git a/lab4/Driver01.java b/lab4/Driver01.java new file mode 100644 index 0000000..651de38 --- /dev/null +++ b/lab4/Driver01.java @@ -0,0 +1,73 @@ +// file name - Driver01.java + +// This program serves as the test driver for DoubleList class. +// +// Classes needed: DoubleList +// +// Interface used: List2 + + +// ============================ packages =========================== +import java.io.*; +import java.util.*; + + +public class Driver01 +{ + + public static void main(String arguments[]) throws IOException + { + DoubleList one; + + one = new DoubleList(); + + // case 1: adding a node to an empty list + System.out.println("adding a node to an empty list"); + one.insertAfter(3); + + // case 2: adding a node after current node + System.out.println("adding a node after current node"); + one.insertAfter(5); + //output should be 3 5 7 8 + // 8 7 5 3 + + // case 3: adding a node after current node + System.out.println("adding a node after current node"); + one.insertAfter(8); + one.insertBefore(7); + + // display nodes in the list + System.out.println("displaying nodes from beginning of list"); + one.resetList(); + for (int index = 0; index < one.length(); index++) + { + System.out.print(one.retrieve() + " "); + one.advance(); + } + System.out.println(); + + // display nodes in the list in reverse order + System.out.println("displaying nodes from end of list"); + one.bottom(); + for (int index = 0; index < one.length(); index++) + { + System.out.print(one.retrieve() + " "); + one.moveback(); + } + System.out.println(); + + + // display nodes with 3 deleted from the list + System.out.println("display nodes with 3 deleted from the list"); + //one.resetList(); + one.delete(5); + for (int index = 0; index < one.length(); index++) + { + System.out.print(one.retrieve() + " "); + one.advance(); + } + System.out.println(); + + } // end of main + +} // class Driver01 diff --git a/lab4/List2.class b/lab4/List2.class new file mode 100644 index 0000000000000000000000000000000000000000..f07e22901acaf7927d5834178fe37c17ee85f070 GIT binary patch literal 525 zcmZWl%T5A85UfF9mzSb~?+;imY@#ZF=M +{ + public void makeEmpty(); + public boolean isEmpty(); + //public boolean isLast(); + public int length(); + public ItemType retrieve(); + public void insertBefore(ItemType one); + public void insertAfter(ItemType one); + public void delete(ItemType one); + public void resetList(); + public void bottom(); + public void replace(ItemType one); + public void advance(); + public void moveback(); +} // interface List2 diff --git a/lab5/IsPostFixDemonstration.java b/lab5/IsPostFixDemonstration.java new file mode 100644 index 0000000..1f06072 --- /dev/null +++ b/lab5/IsPostFixDemonstration.java @@ -0,0 +1,102 @@ +// FILE: IsBalancedDemonstration.java +// This small demonstration program showing the isBalanced method, which uses +// a stack of characters to determine whether a string has balanced +// parentheses. + +import java.util.Stack; +import java.util.Scanner; + +public class IsPostFixExpression +{ + public static int evaluate(String expression) + { + n + } + + + + public static void main(String[ ] args) + { + Scanner stdin = new Scanner(System.in); + String expression; + + System.out.println("Please type the post fix expression containing"); // + System.out.println(" a b c d e f- + / *. I'll check whether"); // + System.out.println("the postfix expresion are balanced."); // + + do + { + System.out.print("Your string: "); + evaluate(expression) = stdin.nextLine( ); + + result = evaluate(expression); // + //if (isBalanced(expression)) //evaluate + //System.out.println("That is balanced."); + //else + // System.out.println("That is not balanced."); + } + while (query(stdin, "Another string?")); + + System.out.println("Thanks for that balancing act."); + } + + public static boolean query(Scanner input, String prompt) + { + String answer; + + System.out.print(prompt + " [Y or N]: "); + answer = input.nextLine( ).toUpperCase( ); + while (!answer.startsWith("Y") && !answer.startsWith("N")) + { + System.out.print("Invalid response. Please type Y or N: "); + answer = input.nextLine( ).toUpperCase( ); + } + + return answer.startsWith("Y"); + } + + public static boolean isBalanced(String expression) + // Postcondition: A true return value indicates that the parentheses in the + // given expression are balanced. Otherwise the return value is false. + // Note that characters other than ( ) { } and [ ] are ignored. + { + // Meaningful names for characters + final char LEFT_NORMAL = '('; + final char RIGHT_NORMAL = ')'; + final char LEFT_CURLY = '{'; + final char RIGHT_CURLY = '}'; + final char LEFT_SQUARE = '['; + final char RIGHT_SQUARE = ']'; + + Stack store = new Stack( ); // Stores parens + int i; // An index into the string + boolean failed = false; // Change to true for a mismatch + + for (i = 0; !failed && (i < expression.length( )); i++) + { + switch (expression.charAt(i)) + { + case LEFT_NORMAL: //if it sees any otehr charcters than these total constants, it will "ignore" it + case LEFT_CURLY: + case LEFT_SQUARE: + store.push(expression.charAt(i)); //pushes these to stack at index + break; + case RIGHT_NORMAL: + if (store.isEmpty( ) || (store.pop( ) != LEFT_NORMAL)) //if when u pop it it doesnt have the matchign left curly, then string not balanced. + failed = true; + break; + case RIGHT_CURLY: + if (store.isEmpty( ) || (store.pop( ) != LEFT_CURLY)) + failed = true; + break; + case RIGHT_SQUARE: + if (store.isEmpty( ) || (store.pop( ) != LEFT_SQUARE)) + failed = true; + break; + } + } + + return (store.isEmpty( ) && !failed); // return ( true if fail doesnt return true)?? + } + +} diff --git a/lab5/IsPostFixExpression.class b/lab5/IsPostFixExpression.class new file mode 100644 index 0000000000000000000000000000000000000000..a6589c971e7a2d90aa375d932756ca9dadc71af1 GIT binary patch literal 3406 zcmaJ^>t7t@6@CU5c6Ku)VG|;lM2AF^T^1It8XF*?giG8O77XM<0L`#Fz=UOHGdr7r zmsop=sn%9|u}y5N)wZ;1YmMzfrB!?T(m$ae`ni9DKV$m5GXuN(3wHUm!&s)U>fBY;WqnhK!{kBQdf;&mxr2#cM9iF(ZicGUmgGc#7E&7GMe!D~ud2h?XtfO%>-Ly7o#lwEU(~V%&0(`4>Mfhq%-Qx)$2QE_&RCy>x=}|<&kt!i*Mw1GfP(l@ z5I>eszi#g8j9Iz7gqZKgh3}4cmO1X7(`+rx&2$P}p;2Sj)SQCN^^e?U*HvcMXB7yz z^XTXM3M^=bDPdQ1qRec4A6%>ELP58eBt$FLNo%G_Fq@d1B|BZ|tj}Y3)GF9%eZUZ` zH74^TR^Ayf7W)@-ww})$mYEPSQ_u@iwyhLDmjDt~HsUQQRIe$xj#m{NL$_%CM8Qw- zGYQ*WWj7AqwiUdF*A*N^H;Gs9bG$+J6}*XGDEK9Qr67f23HwTB$!u1i)v`VIY+*q+ zo&LqNo^uFNK=-Cp!^{;NRo}p`U>Uy-;x`K3!rKbo!Eb~3oq~7qdj;>|hJyF;2MLjN z^K!#C@kj1(ea6b^wk8%+P{fMZ=Bk9mNLJVKy6Pxx;oRQ#?^MUC7~t{X0vK~PEXIPm*#XjvnA|b_gQjD1gd81>a<2C z(|RVsR32v#)x4YA9jbyqi3ogvH3i4s-1k7brUbqZ1+bsGj39pl^By1_~=nK@c3S6VXLtt3#BP`Ai+0?ZKaV5;^ zVwMOtgXoc6X+)qo;Udd|P*L{R_e$EUb5>5m&giC2w`AOMT-pSSUPH96T-Pty5~@U1 zE@4lKy0d}3(Px_Y$|uC#P~$>m5jGioFPxQVymTS~wOTHxo0(F$E3U9IQalU)#L-Z8 zEN_TM!?8}%i`~mA#U#o*n(gFI8xFIff+2rVk!jvhzWtUayJ>47=iU_S3AhQO+YLL> z%&&qY{3Z&ZNmP5bn#3`6CC{HLd09}_LK1fk-~H_ErIaXZT2>&v$A*LfUkUuc35ojV zsY>_Lc@5`^Yp5DuLG|Do0^>BR8EP$}_8OL2Zy;ENd=0z3Rwxx;LwLMx1zU*>)JoFJUIcHVK8P;|@s;RJ90_7D zh#~nCgg!x4J?Z29%4G%9pOL&;%2!(mxaJKX@NTgSj`%1K2TJWE!TZ#tX}3;|@Fge_ zV~2b>))H+Dq*t-W4>EcYwTNaR_xWq@4x-$RJfnPx(OqONEitN3GLla-A1=FCWk{c5KZ8hkypw%+SABRx zTwo1xCVeYcZ;K@ODx#fWxaxr-?i*^2++V~OuH)|3$OCR;m%#L3na1+1|9DF8xIBnM zcBh*US9nsNV;WxN|1-~{lc_eqR1?V+Le-~eW=h?|IGoxzvi(FSe$A1GR&nTk)I}b4 m numbers = new Stack(); + //Stack operations = new Stack(); + + Scanner input = new Scanner(expression); + String next; + + while (input.hasNext()) + { + if (input.hasNext(UNSIGNED_INT)) + { + next = input.findInLine(UNSIGNED_INT); + numbers.push(new Double(next)); + } + + else + { + next = input.findInLine(CHARACTER); + Character operation = next.charAt(0); + + switch (operation) + { + case '+': + case '-': + case '*': + case '/': + evaluateStackTops(numbers, operation); + break; + default : + throw new IllegalArgumentException("Illegal input expression"); + } + } + } + + if (numbers.size() != 1) + { + throw new IllegalArgumentException("Illegal input expression"); + } + + System.out.println(numbers.get(0)); + return numbers.pop(); + } + + + + + + + public static void evaluateStackTops(Stack numbers, Character operation)//, Stack operations) + { + double operand1, operand2; + + if (numbers.size() < 2)//( || (operations.isEmpty())) + throw new IllegalArgumentException("Illegal expression"); + + operand2 = numbers.pop(); + operand1 = numbers.pop(); + + switch(operation)//operations.pop()) + { + case '+': + numbers.push(operand1 + operand2); + break; + case '-': + numbers.push(operand1 - operand2); + break; + case '*': + numbers.push(operand1 * operand2); + break; + case '/': + numbers.push(operand1 / operand2); + break; + + default: throw new IllegalArgumentException("Illegal operation"); + } + } + + + + + + + public static void main(String[ ] args) + { + Scanner stdin = new Scanner(System.in); + String expression; + + System.out.println("Please type the post fix expression containing"); // + System.out.println(" a b c d e f- + / *. I'll check whether"); // + System.out.println("the postfix expresion are balanced."); // + + do + { + System.out.print("Your string: "); + expression = stdin.nextLine(); //expression contains no value its just a variable atm + evaluate(expression); + } + while (query(stdin, "Thats is correct, do you want to input another string?")); + + System.out.println("Thanks for that postfix expression."); + } + + + + public static boolean query(Scanner input, String prompt) + { + String answer; + + System.out.print(prompt + " [Y or N]: "); + answer = input.nextLine( ).toUpperCase( ); + while (!answer.startsWith("Y") && !answer.startsWith("N")) + { + System.out.print("Invalid response. Please type Y or N: "); + answer = input.nextLine( ).toUpperCase( ); + } + + return answer.startsWith("Y"); + } + +} diff --git a/lab6/BinaryNode.class b/lab6/BinaryNode.class new file mode 100644 index 0000000000000000000000000000000000000000..8a106c76b0abca160a50439c1354449c54a94924 GIT binary patch literal 410 zcmZWlyH3ME5S(-TAjXgo9-={_1PWZxf#m`uBqS>ovFOj?5-xJi$g(5lv#5|L_y9f% zF-`;lQrzCo>}z&EKHuH}T;RY*3%efnJnReD+KdW;UNVt08RpWA!~1NahSilod!WZg zR&}Wa{PeC;({%o<7_f6Sssw`MS{qr;`^96!9{z&SDm`Y%DfRe?6>g-Bu3~}K$>{_8 z+^|xhlW3#*^)ypuDzltS7=2H>To)FvQ`9#o>T;-VbyMD1>Pkzc&YQgg1lVHEO0Gcn z|H3$i#mivvY&f7?A`2ev2HHQ^C*J&n1sP6Y#x}hp{te@ty+XXup&(j!LAq?XY}>^S HSqr@{?`>A# literal 0 HcmV?d00001 diff --git a/lab6/BinarySearchTree.class b/lab6/BinarySearchTree.class new file mode 100644 index 0000000000000000000000000000000000000000..0665611d0612882c1a4f12cfe29fbb1f21e87fd1 GIT binary patch literal 1478 zcmZ`(+fEZv6kVscp~C@M3MjX75utR-O$3qFDxe^y)@U1VZ$lYM5n3`#P520Z!0^ti zCS0N>#y1l`%DDGTVOrEQ*{A!Qwf0`K_L=$h`|EcAvshCQQz0-Oq!8zzaY!^n#e|9w zCRI#fn!}8OTfENl_H7kmCWSD^>AZqF3Kj$ueRDHg*)#;EW0~brSvLId0pE(vY|E6&Mz*@QVVHS_3AE%az3}d-zF*6Oxl*yLTUC?J zQuTdO`E|?K%OCC=$#l~FU*D$zBA^}^x>?wEG!}vg| zR4!R*0dI^`FPS3H9s7?7glLJ?iRA*0DuH0kRYB@l(O=zY-*xxd#*R_2Y}Ko&3o{lM z>kzL04VBWhS>#Q_XgDB`a+RuCFjh-Ei%w^%Iomb{PjYhtxzm>DL%+mbEJ`GilDNkq zjr$Vqh$wgiR;HCA6_7nZzAl{tAlhG2V@M_`|J`Vn4 zOeC}uc)uXv!4V=_oHAeH910ld>*C`QeTBNnEqZ*Qo%_p(gQ&1hT}}B(XW+JwwwmvWYVUe?YyI^$`(U;~7DG zHblD$IYQfoaCr>L|5>#BZ1;2p$4PL41W#fB(-^}HeSqe$jCo|SfLB> extends BinaryTree +{ + // purpose -- add an item to one of the nodes in the current binary + // search tree. + // preconditions -- item does not exist in the current binary + // serach tree. + // postconditions -- item is added to one of the nodes in the + // current binary search tree. + private BinaryNode addNode(BinaryNode start, + ItemType item) + { + if (start == null) // a new leaf node is created for insertion + { + start = new BinaryNode (); + start.info = item; + start.left = null; + start.right = null; + } + else // continue to find the place to insert a new node + { + if (item.compareTo(start.info) < 0) + start.left = addNode(start.left, item); + else + start.right = addNode(start.right, item); + } // else + + return start; + } // method addNode + + + // purpose -- search an item in the binary search tree. + // preconditions -- none + // postconditions -- if item is found in the tree, found is set + // true; otherwise, it is set to false + private void searchNode(BinaryNode start,Item item) + { + if (start == null) + { + item.setFound(false); + } // not found + else if (item.getItem().compareTo(start.info) == 0) + { + item.setItem(start.info); + item.setFound(true); + } + else + { + if (item.getItem().compareTo(start.info) < 0) + searchNode(start.left, item); + else + searchNode(start.right, item); + } + } // method searchNode + + public BinarySearchTree() {} + + + // purpose -- add an item to one of the nodes in the current binary + // search tree. + // preconditions -- item does not exist in the current binary + // serach tree. + // postconditions -- item is added to one of the nodes in the + // current binary search tree. + public void add(ItemType item) + { + root = addNode(this.root, item); + } // method add + + + // purpose -- search an item in the binary search tree. + // preconditions -- none + // postconditions -- if item is found in the tree, found is set + // true; otherwise, it is set to false + public void search(Item item) + { + searchNode(this.root, item); + } // method search + +} // class BinarySearchTree diff --git a/lab6/BinaryTree.class b/lab6/BinaryTree.class new file mode 100644 index 0000000000000000000000000000000000000000..d527884051e7a0a870646dc9895fcb9686457893 GIT binary patch literal 1624 zcmaJ>%Tg0T6g{2Sn8}2ff=?8DUi}GpWghDwev}jsAlh zH(H>j3zu5uM_HbpBxHDKrRvV@+kMZu_w?=j{`36@fKlY*IE9e}GB~ZgGdhwutGrR= zom1w#jy7C~)4Qn5r8q9*ijFLD8m?*>6NtEu;|Un~3ClL!)oEuzW(DE}YsogfnkxnN z1-5dZrUa4|S32&3bmy$9#n_=#ATOI&z_YGXlGSQF*j`}{I-fy^@8;nmK|Kia+Ym* z6d;^Z$2F@uxm@v9{qx70Be8*KuWgp<~-a0(!xzxg~kaQc}0q z@4V@RG;8Y`>QTm zHczW_hx+iuUalS@%pcG{^b<#hht?pzArwNIUU>KmBFc#>=MxWwu#dI}QQASm43TP> zFsIPRC%vE1FhpO2@c<7{{hfH4=&>exti`;QCr6kuHg%0qp8|OvZRZhchN!60ZIpb1-}dxZ>`I5%5l@SC=n9qpKeS!M1D??KB95n2 z>KS*A5V;O;nqLKwOs_%z1ugOck?=LSPd%|*Y6C_;&G5fSLeQHDGHL&xYH$B9PU z15`f5xSm#ULyh~YSEBnnHCD0G$Q@wr$$#g@T3yt0UorP}AUE~zTwm89?;Ga64df1P jMdRH +{ + ItemType info; + BinaryNode left; + BinaryNode right; +} // class BinaryNode + + +/* + * public int count() + * { + * //go to each node + } + + public int largest() + * { + * //keep moving right + * } + */ + +abstract class BinaryTree +{ + protected BinaryNode root; + int count = 0; + + // purpose -- visit and process nodes in a preorder traversal + // preconditions -- start points to the root of binary tree; one + // points to a function which will process a node in the + // tree. + // postconditions -- all nodes in the binary tree are visited in a preorder + // traversal (root, left subtree, right subtree) and each + // node is processed as it is visited. + protected void preorderVisit(BinaryNode start, + NodeProcess one) + { + if (start != null) + { + one.process(start.info); + preorderVisit(start.left, one); + preorderVisit(start.right, one); + } // if + } // method preorderVisit + + + // purpose -- visit and process nodes in a postorder traversal + // preconditions -- start points to the root of binary tree; one + // points to a function which will process a node in the + // tree. + // postconditions -- all nodes in the binary tree are visited in a + // postorder traversal (left subtree, right subtree, + // root) and each node is processed as it is visited. + protected void postorderVisit(BinaryNode start, + NodeProcess one) + { + if (start != null) + { + postorderVisit(start.left, one); + postorderVisit(start.right, one); + one.process(start.info); + } // if + } // end of binary::postorderVisit + + + // purpose -- visit and process nodes in an inorder traversal + // preconditions -- start points to the root of binary tree and + // one points to a function which will + // process a node in the tree. + // postconditions -- all nodes in the binary tree are visited in an + // inorder traversal (left subtree, root, right subtree) + // and each node is processed as it is visited. + protected void inorderVisit(BinaryNode start, + NodeProcess one) + { + if (start != null) + { + inorderVisit(start.left, one); + one.process(start.info); + inorderVisit(start.right, one); + } // if + } // end of binary::inorderVisit + + // purpose -- default constructor to set root to null + // preconditions -- none + // postconditions -- the root node is set to null + public BinaryTree() + { + root = null; + } // end of BinaryTree:BinaryTree() + + + public boolean isEmpty() + // purpose -- check to see if the tree is empty + // preconditions -- none + // postconditions -- return true if the binary tree is empty; + // otherwise, return false + { + return (root == null); + } // end of binary::isEmpty + + + public abstract void add(ItemType item); + + + public void preorderTraversal(NodeProcess one) + // purpose -- visit and process nodes in a preorder traversal + // preconditions -- one points to a function which will process + // a node in the tree. + // postconditions -- all nodes in the binary tree are visited in a preorder + // traversal (root, left subtree, right subtree) and each + // node is processed as it is visited. + { + preorderVisit(root, one); + } // end of binary::preorderTraversal + + + public void postorderTraversal(NodeProcess one) + // purpose -- visit and process nodes in a postorder traversal + // preconditions -- one points to a function which will process + // a node in the tree. + // postconditions -- all nodes in the binary tree are visited in a + // postorder traversal (left subtree, right subtree, + // root) and each node is processed as it is visited. + { + postorderVisit(root, one); + } // end of binary::postorderTraversal + + + public void inorderTraversal(NodeProcess one) + // purpose -- visit and process nodes in an inorder traversal + // preconditions -- one points to a function which will process + // a node in the tree. + // postconditions -- all nodes in the binary tree are visited in an + // inorder traversal (left subtree, root, right subtree) + // and each node is processed as it is visited. + { + inorderVisit(root, one); + } // end of binary::inorderTraversal + + + public void counter(NodeProcess one) + { + System.out.println(count); + } + + +} // class BinaryTree diff --git a/lab6/Item.class b/lab6/Item.class new file mode 100644 index 0000000000000000000000000000000000000000..41d29a69cfaf166015e3e0b5169fcb71c75752d0 GIT binary patch literal 751 zcmZvZ%}&Bl5QWceDYaNp5q}XCHCix{g*zo~Oh`+X6AyciIA}%ANgS7hAU1 zEMC^{oQ8iQU{u|vXZ!uGBao<_v#|Df@9>9q+v$5Z0^(XAUUEIxKNVQACN-|}S8Q?C zxuL>r#r2$ve!K2;Yj(ZGOwx)(gG#;W__B>a+R9I=37A$sLIiZTR}P)1R-Rt;-iTlP z$BgtcGuBeZQzV8GF)Q#;ER6;|o^)_Jnc`}yoqo6BlwCQKA?ZW8OcN${OvIs^NMM?K z2uGXyyJ-Qsze0}JqVcX2K7>GXL1Zu{&u@)66ElpH!JR5l`JOGjK@<)KPzvh xmWS%wd@HPo2NolDi6Fbhp>o3(E96!oY1ty;GQXw@xjl0GR0!wEV4Z}5>^C~Bfjj^J literal 0 HcmV?d00001 diff --git a/lab6/Item.java b/lab6/Item.java new file mode 100644 index 0000000..1da442a --- /dev/null +++ b/lab6/Item.java @@ -0,0 +1,40 @@ +// file name -- Item.java + +// This file contains the definition of Item class + +public class Item +{ + private ItemType item; + private boolean found; + + public Item(ItemType item, + boolean found) + { + this.item = item; + this.found = found; + } // constructor + + + public ItemType getItem() + { + return item; + } // method getItem + + + public boolean isFound() + { + return found; + } // method isFound + + + public void setItem(ItemType item) + { + this.item = item; + } // method setItem + + + public void setFound(boolean found) + { + this.found = found; + } // method setFound +} diff --git a/lab6/NodeProcess.class b/lab6/NodeProcess.class new file mode 100644 index 0000000000000000000000000000000000000000..f137c3e49d267bc864c3db8f2c688e3ed12c6c68 GIT binary patch literal 239 zcmX^0Z`VEs1_omW9(D#Ub_Q-n2KIuY{N&W)Vnzm04WF#UvPAuy#JqHU|D>$c;LP;A#FEmYR7M6~jS$b0)ZCEDf>f}az733Pg{j#NQ;3m)D>%QjC^^+FGY4p# zfM0$}Y5>FmdLYXg8Mu*zfC|uE#Ky?L$iM{j0s{jh3nK$F0}GJP3gj^{umLfI#m>M1 H7U2W{fjc}h literal 0 HcmV?d00001 diff --git a/lab6/NodeProcess.java b/lab6/NodeProcess.java new file mode 100644 index 0000000..78173d8 --- /dev/null +++ b/lab6/NodeProcess.java @@ -0,0 +1,9 @@ +// file name -- NodeProcess.java + +// This file defines the interface for processing a node in a binary tree. + + +public interface NodeProcess +{ + public void process(ItemType one); +} // interface NodeProcess diff --git a/lab6/Print.class b/lab6/Print.class new file mode 100644 index 0000000000000000000000000000000000000000..00b083f9ae3d5fa38fb9c44ffc17bec831719940 GIT binary patch literal 513 zcmZWmO;5r=5Pe%dmX;zY;x`f#52%S8yb0;W#AwnWNs0G@8?ppiQnn`iEInxA!5`p{ zGETQKh#Yp_?Atf9Z)d;0KRy9mpzkpGEkR^9~&G6jsqKq zIO3?=IEKxT(+Y;lf6olXKbNcsyv3Z%(@4Tt(R)*SGHmHqo{EG&B_mbcah29{B1e+| zC&<#!X~6UY1|hklXwt4u$Ov=i?_e*K&^svRQ<7p4HkBmOQ$mBV@CxJN4Vk6d(!5D+ REtE)MV1rhgl~K{m<_~Yfa1j6i literal 0 HcmV?d00001 diff --git a/lab6/Print.java b/lab6/Print.java new file mode 100644 index 0000000..4ab4355 --- /dev/null +++ b/lab6/Print.java @@ -0,0 +1,11 @@ +// file name -- Print.java + +// This class implements NodeProces interface + +public class Print implements NodeProcess +{ + public void process(ItemType item) + { + System.out.println(item); + } +} // class Print diff --git a/lab6/Print2.java b/lab6/Print2.java new file mode 100644 index 0000000..c4e157f --- /dev/null +++ b/lab6/Print2.java @@ -0,0 +1,14 @@ +// file name -- Print2.java + +// This class implements Item interface + +public class Print2 implements Item +{ + public void process(Item item) + { + System.out.println(item); + } +} // class Print + + +WRONG diff --git a/lab6/ShowBinarySearchTree01.class b/lab6/ShowBinarySearchTree01.class new file mode 100644 index 0000000000000000000000000000000000000000..c8a71b8eb65cbe1a9f13c4b079bfc01fc5536ac1 GIT binary patch literal 720 zcmZvaTW`}a6vzKg)1(W5rt8|R>n@B9)KQ6o_D0bj7#CG4>!eJR&>oPRdab868E#rY zf)B;ZhM2?$K;kPQj;n-Dh(z}J`CROCj{WoZ_a6ZE@W6qCl8v$h10@H`SkY9mvFgCm z8Us}uYc|#ytY09z;FCkZ;RS98N}Ep^kZXNE&m*Wynyjowm>UlsASv z>^J;a$*|wlfzHd1fw+ie6pn3d(AtkCDiE)v4zBux=)-dva`njezeOxw@&w51TjN#rjk*k)%lwuLt*aN z3S-e1iou@p;Y4)Kbd{~$8)YUQO==SF^%yF(8^(iZbitKw(yR}j3?tQ}wkbXpYRrd} zRIPnTT|H7!AjV_z!WI_jiu7lI>H2BDO{;a1jAZuV71&n-hC4J{Gi8o~^6)4`yZ%Ck z2>5ahf*Ib}zlgeDAQ6x67 F^as0TmbU-^ literal 0 HcmV?d00001 diff --git a/lab6/ShowBinarySearchTree01.java b/lab6/ShowBinarySearchTree01.java new file mode 100644 index 0000000..1c2117f --- /dev/null +++ b/lab6/ShowBinarySearchTree01.java @@ -0,0 +1,32 @@ +// file name -- ShowBinarySearchTree01.java + +// This program demonstrates how to use the BinarySearchTree class. + +// ============================ packages ============================= +import java.io.*; + +public class ShowBinarySearchTree01 +{ + + public static void main(String arguments[]) throws IOException + { + BinarySearchTree one; + + one = new BinarySearchTree(); + + one.add(9); + one.add(15); + one.add(51); + one.add(5); + one.add(1); + one.add(3); + one.add(23); + one.add(83); + one.add(21); + one.add(58); + one.add(71); + one.add(91); + one.inorderTraversal(new Print()); + + } // method main +} // class ShowBinarySearchTree01 diff --git a/lab6/ShowBinarySearchTree02.class b/lab6/ShowBinarySearchTree02.class new file mode 100644 index 0000000000000000000000000000000000000000..817ed14ee2d024bea1b6d4de7db3ed62b6abc279 GIT binary patch literal 2259 zcmaJ?S#uLd5dKE8z4EdcSzsH24a+Ut_y}i$u@me7aUvf<4%i{rNE++~c~{w88Jznd z$8X4M9`YP2h*Vsampr5@KML~AO0s2PQmUDq>FNIZo9>?0zfb@C3&2_YWZ;?!f$MsD z(?mb6w{taZ;4Kqv)y>-mGVPebtciBa>En5={f?e)=qanG1q1Jz=+q`NxS7B$6N^~V zlJ^YUHqoPf^x*vjKG5F}_4JX}`PhU+PJ7GiZ6N_=;Esu-+Mo@~39RU^lfYg5y=R~( z&=UGe38XTUjw}83tdf3yWr4CWf%dGk?8>n0Q{mKe3YRl?<(eERN_Tl=+6~pR^2a7< zmAI$YbDs1I0(+|}<sUi@cxw0rArUiCSd2SF&H@qo}Wff;2O)^@Qjw{fe zzTMy;8~TpBJl21c=TlyRg!W_{SIw4JbIMmZWP&ZibGVa&p1J&odJfn+m+g#io-bVj*o z4735)f`^iZ_{736Ml6hLoGW^o)YBB0jpUZ)4e1uVRSP}>feY2x26pJ#Ib}O;pmgZ; zHe6A5E-Dv0i0Z}GMPy4~U9t^?7Rp$&a32o@dbVn$n!?GlQ)C1!tm9J_fvp#xS@;}Z zSojia2EMZJHNLU%Exyx0WH`8{t0S;WECJ1+9IzB@>5@|vXnL1x$`54mk}c3WuhlF( z#P_W_r1Q?n&12Nv1mRXwCb&h@sS0xKF~;O%3Cc-U$={>l_YBwv`{Y~<|mz*Wq+CXy}Sy7 zNUO76EDdU@QoR^9g*d|I-OnBtM@&CT?oM+T!w?6bAJ-}Uh$%l4$cJVt`c_N7NKpJ3NxSXx#53kWen57MLl2t162zyieD(GsN=u4WvV0KDC{-$OHwx$z{ zA~uX&T#u5K32i;tq4_ tree; + private Scanner keyboard; + private final int MAX = 10; + + public static void main(String arguments[]) throws IOException + { + ShowBinarySearchTree02 one; + + one = new ShowBinarySearchTree02(); + one.getStarted(); + } // method main + + + // default constructor + public ShowBinarySearchTree02() + { + tree = new BinarySearchTree (); + keyboard = new Scanner(System.in); + } // default constructor + + + // create the binary search tree first and display the tree + // using three different traversal methods + public void getStarted() + { + createBinarySearchTree(); + displayInorder(); + displayPreorder(); + displayPostorder(); + } // method getStarted + + + void createBinarySearchTree() + // This method uses random numbers to create a binary search tree. + // The values are generated between -MAX+1 and MAX-1 inclusive. + { + Random number = new Random(); + int value; + int k; + + System.out.println("Numbers to be inserted to the binary search tree are: "); + for (k = 1; k <= MAX; k ++) + { + value = number.nextInt(10 * MAX); + if (k % 2 == 0) + value = -value; + System.out.print(value + " "); + tree.add(value); + } // for k + System.out.println(); + } // end of createBinarySearchTree + + + void displayInorder() + // This function displays the nodes in a binary search tree in an + // inorder traveral. + { + System.out.println("values printed using an inorder traversal: "); + tree.inorderTraversal(new Print()); + } // end of displayInorder + + + void displayPreorder() + // This function displays the nodes in a binary search tree in a + // preorder traveral. + { + System.out.println("values printed using an preorder traversal: "); + tree.preorderTraversal(new Print()); + } // end of displayPreorder + + + void displayPostorder() + // This function displays the nodes in a binary search tree in a + // postorder traveral. + { + System.out.println("values printed using an postorder traversal: "); + tree.postorderTraversal(new Print()); + } // end of displayPostorder +} // class ShowBinarySearchTree02 diff --git a/lab6/ShowBinarySearchTree03.class b/lab6/ShowBinarySearchTree03.class new file mode 100644 index 0000000000000000000000000000000000000000..a353892f952ae5b96b1ab707793afb5d150e7cd5 GIT binary patch literal 1389 zcmb7E*-{fh6g^FrY2qj$1Q0>RJqQ{UcNB#vN(_qzQE+XNmb4}_shNq(XZY@mpP&z+ zN~$d1wEQT`+a1)A;>A?u^xVGtxp(Hr&(B`~j6o%E7+DLW3A7>0aY~=3ImS57aGd2h z$8nzH0>?#;OB~}ImpQIjm`I=#S#5pQ!ZnV`1k#w&a;_cMal^uN0)5&djhhy3X<>#V zujSiXC}@~XU=H&Z7A)Lhh+kHY3MLpLnV}_yXwEH5hK_=APR+@;3z|KtC}H}t9Z({QN$t%c~O%-gOwMhm%dA-#3t*Ws?vF1R;#3m zO;%M&S9;eR)~uu&mA<$sv|TjsR>;jNs#?7jAm>R@BgX~mBCv4}nt+)=(p}u>q%Q8h zjR#n^uwvsO9x?Q8?SHDFs%7cfc#J31$X2zDr+CJ|25kteYN=$S3~AxHjWtw=)a~i{ zC)WJx3t0*nx=eWGW@qOAFiyi0uQsE9a;I)ZbN(09n+a literal 0 HcmV?d00001 diff --git a/lab6/ShowBinarySearchTree03.java b/lab6/ShowBinarySearchTree03.java new file mode 100644 index 0000000..8d9bd76 --- /dev/null +++ b/lab6/ShowBinarySearchTree03.java @@ -0,0 +1,39 @@ +// file name -- ShowBinarySearchTree03.java +// This program demonstrates how to use the BinarySearchTree class. + +// =========================== packages ============================== +import java.io.*; + + +public class ShowBinarySearchTree03 +{ + public static void main(String argumentts[]) throws IOException + { + BinarySearchTree one; + Item item; + + one = new BinarySearchTree(); + one.add("Chris"); + one.add("James"); + one.add("Jackson"); + one.add("April"); + one.add("Wells"); + one.add("Alice"); + one.add("Rice"); + one.add("Melanie"); + one.add("Gail"); + one.add("John"); + one.add("Simpson"); + one.add("Cream"); + one.inorderTraversal(new Print()); + + // perform search + item = new Item("Creame", true); + one.search(item); + System.out.println(item.getItem() + " " + item.isFound()); + + item = new Item("Melanie", false); + one.search(item); + System.out.println(item.getItem() + " " + item.isFound()); + } // end of main +} // class ShowBinaryTree03 diff --git a/lab6/ShowBinarySearchTreeLab.class b/lab6/ShowBinarySearchTreeLab.class new file mode 100644 index 0000000000000000000000000000000000000000..c22271fb89db5e7a4d8da427521b9fc9099edd18 GIT binary patch literal 851 zcmZ`%-Ez`E6#kY5LSwKL6hupZ)GCw;)oT3#d(n>0FhikZ7-!I{C9IGd5+;ONZ+s7* zz-w>7kr^MrXYm#MIh(PSaWs>hv*+aO`M&S$e*N+J3xI7r*06{b6{{LT(yytw8pSmY zVO-asU|pIU(%jULLtd6`s3>TNVMPX;DsHJLGDIqtWBI!bbGiI;24&yv2!@54<%s&I z-xgkzw{6NOeQr4nsocxj5kKZ7n>*c-;d_?TEz6BD!{_Gf13n1qi5Zb%(4L%_V&Ge@ zGgMKc*l?18mJ=sW*)G&_}(zHdE zzN7o8kgrS;YE3X#n?h+#5pE?S6Q~nJ4>re`e~%8H^ksuS%fA3R8oc=c literal 0 HcmV?d00001 diff --git a/lab6/ShowBinarySearchTreeLab.java b/lab6/ShowBinarySearchTreeLab.java new file mode 100644 index 0000000..e49324b --- /dev/null +++ b/lab6/ShowBinarySearchTreeLab.java @@ -0,0 +1,51 @@ +// file name -- ShowBinarySearchTree01.java + +// This program demonstrates how to use the BinarySearchTree class. + +// ============================ packages ============================= +import java.io.*; +import java.util.Scanner; + + +public class ShowBinarySearchTreeLab +{ + + public static void main(String arguments[]) throws IOException + { + BinarySearchTree one; + one = new BinarySearchTree(); + + Scanner infile; + int value; + + infile = new Scanner(new FileReader("lab6.dat")); //reads numbers from file lab6.dat + while (infile.hasNextInt()) //this is a Loop for getting numbers + { + value = infile.nextInt(); + one.add(value); + } +/* + one.add(9); + one.add(15); + one.add(51); + one.add(5); + one.add(1); + one.add(3); + one.add(23); + one.add(83); + one.add(21); + one.add(58); + one.add(71); + one.add(91); +*/ + one.inorderTraversal(new Print()); + + //one.count + //one.largest + + } // method main +} // class ShowBinarySearchTree01 + + + + diff --git a/lab6/lab6.dat b/lab6/lab6.dat new file mode 100644 index 0000000..be182fa --- /dev/null +++ b/lab6/lab6.dat @@ -0,0 +1,15 @@ +100 +50 +150 +40 +60 +30 +42 +55 +65 +140 +160 +120 +145 +155 +170 diff --git a/lab6/lab6b.class b/lab6/lab6b.class new file mode 100644 index 0000000000000000000000000000000000000000..b81cfa9e95a794ab237fa8f8c768e2bde202e4aa GIT binary patch literal 2728 zcmaJ@X?GK46n>^lCLN}wEtJgy0ScrkZ4nf;r6?dvBrRY|Dd55+ou*ULnJ}4DswgUm ziYtmD?hA?^_@Ep}J=Wv#n@9hOe*wkk&ZHzoj^{LY-sQgcKJRmvw}1Zg^RECl;(HYX z8WbE5F{oiZ4%BdTP{ojjP%t^9;&2U);0X;icv7~X678o&9Ic_2A)};-l;{nsFg47> zfiOnon-(!D!qPB?V_{^%7#Cx<3P;0IWMy((g&Rgr=APKf%i@HHXEe0qgy5M}@vMfZ zzzyNK8l1%QB3@8&N_1Y7$xA9;R#4@+rm3JYvCXm#ce39!+|=j**SZzd^jm4$@bWGd z*8WFfb7IVxFgi1ao$id=o|!h??kzkOK4wlP9m5?~FfUNadse2iKV{f9RTNbAZXHsf z#1+(TckG;J*xsO#$(xmAS|g%y!?G1Dh#o0((C@jHo$hWO!rLO=(eWu-W<2yNg1?L!y5@CCjV`j=@q0B&w?=1sVrWi0g>umpI&NIgqR7kZn1u4v1 z$AuQEo5nb6ZBg{D8-5+Rjziaac{Z|EoIDpx=KMR}HWxCotQ*!(d(p0c#{gV+v5d-= zikUzh&C&S(ZHGN)=7#fU%CXE24cum^3=m5qDa&MI&pf2?MX8>J_3{ULw73t>JT-Pp|i9du@0zAxF755(Dk4=YCj3oRupd~his#v{t1J$uOSTuFnrEANwhr>VHDMW6VL5HGlNI4zELu*-3aI6;E*86v@O8{B zp#ETK+;H$38mBRj9_KeU6|kUetAK?CEGl4enaYw`D)qNjO5m13CAc+ozm}Jel7d!5 z5F@lUjF7b%A|iy#mQjTJ2k8p&+@R81x^#k$8}A_iQrp}&jira$r?9N6N;DQ%O<{TO z6;7*5@Y;#4aRo(ydvR3(D=*#pW!(?-)q)PjIYNIDo({~R+`!o;)S`#!qT?aD z6~~dPp1^gp`0gyA;b@OPBn%ne>Fy-Yzd(1Vm}jT?-{2XR!CBv32+B=_%e_7BL+$?; D tree; + private Scanner keyboard; + private final int MAX = 10; + + public static void main(String arguments[]) throws IOException + { + lab6b one; + + one= new lab6b(); + one.getStarted(); + } // method main + + + + public lab6b() + // default constructor + { + tree= new BinarySearchTree (); + keyboard = new Scanner(System.in); + } // default constructor + + + public void getStarted() throws FileNotFoundException + // create the binary search tree first and display the tree + // using three different traversal methods + { + createBinarySearchTree(); + displayInorder(); + greatestNode(tree.root); + counter(tree.root); + System.out.println("The Number of nodes is: " + counter(tree.root)); //binary tree name + System.out.println("The Largest number is: " + greatestNode(tree.root)); + + } // method getStarted + + + public Scanner openFile() throws FileNotFoundException + { + Scanner input, infile = null; + String infile_name; + File outfile; + + input = new Scanner(System.in); + + do + { + System.out.println("Please enter the input file name: "); + + infile_name = input.next(); + + outfile = new File(infile_name); + + + try + { + infile = new Scanner(outfile); + } + + catch (FileNotFoundException ex) + { + System.out.println(infile_name + " does not exist"); + } + + }while(!outfile.exists()); + + return infile; + } + + + void createBinarySearchTree() throws FileNotFoundException + { + + Scanner infile = openFile(); + + while(infile.hasNext()) + { + tree.add(infile.nextInt()); + } + } // end of createBinarySearchTree + + + void displayInorder() + // This function displays the nodes in a binary search tree in an + // inorder traveral. + { + tree.inorderTraversal(new Print()); + } // end of displayInorder + + + public int greatestNode(BinaryNode node) + // This function will find the largest value in the tree. + { + int highest; + + if ( node == null) + return 0; // if tree is empty + + else + highest = greatestNode(node.right); //keeps finding nodes + + if(node.info > highest) //adds new highest if the current node is greater + highest = node.info; + + return highest; + } + //end of finding the highest number + + + + public int counter(BinaryNode node) + // This function count will count the number of nodes in the tree. + { + int count = 1; + + if (node == null) + return 0; + + else + { + count += counter(node.left); + count += counter(node.right); + + return count; + } + } +} + // class lab6b From a0f9c8af8032395f1b3763c71d6c007cc51924ad Mon Sep 17 00:00:00 2001 From: Kody Ellis Date: Mon, 25 Sep 2017 23:28:05 -0500 Subject: [PATCH 02/11] Update readme --- lab3/readme | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lab3/readme b/lab3/readme index c68c8a7..f222374 100644 --- a/lab3/readme +++ b/lab3/readme @@ -1,6 +1,6 @@ Lab31 needs to be documented prperly. lab31 random number generator is not properly used. It should generate number from 1 to 54. --5 + The documentation for lab32 is not prperly done. --2 + From 282437196762d247f5d558e1e6f6ed69a7bd9a33 Mon Sep 17 00:00:00 2001 From: Kody Ellis Date: Mon, 25 Sep 2017 23:31:45 -0500 Subject: [PATCH 03/11] Update README.md --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 7b13483..211222f 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,3 @@ # JavaDataStructures -A repository for the Java programming projects I did in my Data Structures class. +A repository for the Java programming projects I did in my Data Structures class. {{Under contruction}} I need to delete all unneccesary test files/ clean up some of my code. + From 7d7e965617daa5c6d249f45a191ee34667021034 Mon Sep 17 00:00:00 2001 From: Kody Ellis Date: Fri, 12 Jan 2018 18:14:36 -0600 Subject: [PATCH 04/11] Update Student.java added a comment line about getters --- lab1/Student.java | 1 + 1 file changed, 1 insertion(+) diff --git a/lab1/Student.java b/lab1/Student.java index 32179a1..0da5cb7 100644 --- a/lab1/Student.java +++ b/lab1/Student.java @@ -2,6 +2,7 @@ /** A class to represent students objects. + Many of the functions are easy to see representations of getters and setters. */ public class Student { From 88d6387126dbbe0d2c4c35befb4b0bfdb43a801b Mon Sep 17 00:00:00 2001 From: Kody Ellis Date: Fri, 12 Jan 2018 19:01:35 -0600 Subject: [PATCH 05/11] Update student.dat Eliminated space between GPA field and SS field on line 3 and 5 as before output would clip the last SS digit. It would only show a space and then 8 SS digits instead of all 9. --- lab2/student.dat | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lab2/student.dat b/lab2/student.dat index ff3014a..4a889ea 100644 --- a/lab2/student.dat +++ b/lab2/student.dat @@ -1,5 +1,5 @@ James Bond 48 3.25 784596322 Jim Smith 20 3.75 123456782 -Jean Wilson 193.523 276543212 +Jean Wilson 193.523276543212 John Doe 22 2.5 015789462 -Willie Fields 252.952 386418423 +Willie Fields 252.952386418423 From b4afa8191b4081c4fd6321e8142ff5d60b765844 Mon Sep 17 00:00:00 2001 From: Kody Ellis Date: Fri, 12 Jan 2018 19:05:21 -0600 Subject: [PATCH 06/11] Update FormattedInput02.java --- lab2/FormattedInput02.java | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/lab2/FormattedInput02.java b/lab2/FormattedInput02.java index 93b20df..2cffe80 100644 --- a/lab2/FormattedInput02.java +++ b/lab2/FormattedInput02.java @@ -4,7 +4,7 @@ The data file contains student records which are formatted as follows: - Field name Columns + Field name Columns (a.k.a. indexes) ========================================== Name 1 - 20 Age 21 - 22 @@ -20,6 +20,8 @@ class FormattedInput02 { + //final keyword makes these varibales, constant values, meaning you can't change them when aprogram is running. + //private keyword makes it so that these variables can only be accesed inside of the class. private static final int NAME_WIDTH = 20; private static final int AGE_WIDTH = 2; private static final int GPA_WIDTH = 5; @@ -32,13 +34,16 @@ public static void main(String list[]) throws IOException { FormattedInput02 one; + //makes a new instance of this class one = new FormattedInput02(); + one.getStarted(); } // method main /** - Starts the program using an object of the class + Default Constructor + This makes creates a new Student type array list. */ public FormattedInput02() { @@ -65,12 +70,16 @@ public void openFile() throws FileNotFoundException Scanner input; String infile_name; File file; - + + //Scanner will look for keyboard input. input = new Scanner(System.in); + do { System.out.print("Enter the input file name: "); + //gets files name. stops looking at keybaord when user presses ENTER infile_name = input.next(); + file = new File(infile_name); if (file.exists()) infile = new Scanner(file); @@ -99,14 +108,17 @@ public void readData() // obtain name field name = text.substring(0, NAME_WIDTH); // extract name field name = name.trim(); // remove leading and trailing spaces + //definetly need to use trim here, as we captured the name and all of the space behind them name and placed + //that into the name variable. - // remove name field from text + // remove the whole name field from text. Now substring index can start at 0 again for other variables. text = text.substring(NAME_WIDTH, text.length()); // extract age field from text and convert it to an int type age = Integer.parseInt(text.substring(0, AGE_WIDTH)); - // extract gpa field from text and convert it to a double type + // extract gpa field from text and convert it to a double type. We place AGE_WIDTH into the first parameter + //as gpa is after it. gpa = Double.parseDouble(text.substring(AGE_WIDTH, AGE_WIDTH+GPA_WIDTH)); //AGE_WIDTH + GPA_WIDTH ssNumber = text.substring(AGE_WIDTH+GPA_WIDTH,AGE_WIDTH+GPA_WIDTH+SSNUMBER_WIDTH); From d55a5efa051539ebf6489f93831a9611a1b6ee9f Mon Sep 17 00:00:00 2001 From: Kody Ellis Date: Fri, 12 Jan 2018 19:05:53 -0600 Subject: [PATCH 07/11] Delete ArrayList1.java not needed for this folder --- lab2/ArrayList1.java | 114 ------------------------------------------- 1 file changed, 114 deletions(-) delete mode 100644 lab2/ArrayList1.java diff --git a/lab2/ArrayList1.java b/lab2/ArrayList1.java deleted file mode 100644 index 50fdfe9..0000000 --- a/lab2/ArrayList1.java +++ /dev/null @@ -1,114 +0,0 @@ -// file name -- ArrayList1.java -import java.io.*; -import java.util.*; - -/** - This program shows how to read in student records from an input - data fle and store them in an array list. Student records are - stored using an ArrayList object. -*/ -public class ArrayList1 -{ - private ArrayList students; - private Scanner infile; - - public static void main(String[] args) - throws IOException - { - ArrayList1 current; - - current = new ArrayList1(); - current.getStarted(); - } // method main - - - /** - Starts the program - */ - public void getStarted() - throws FileNotFoundException - { - openFile(); - readData(); - displayResults(); - infile.close(); - } // method getStarted - - - /** - Opens the input data file for read access. - */ - public void openFile() throws FileNotFoundException - { - Scanner input; - String infile_name; - File file; - - input = new Scanner(System.in); - do - { - System.out.print("Enter the input file name: "); - infile_name = input.next(); - - // open the input data file - file = new File(infile_name); - if (file.exists()) - infile = new Scanner(new File(infile_name)); - else - System.out.println(infile_name + " does not exist"); - } while (!file.exists()); - } // method openFile - - - /** - Reads in text from file and save them in an array list. - @param infile the file which has been open successfully - */ - public void readData() - { - Student who; - String name, first_name, last_name; - int age; - double gpa; - - // instantiate an ArrayList object - students = new ArrayList(); - - // read in values from the file and assign them to the elements - while (infile.hasNext()) - { - first_name = infile.next(); - last_name = infile.next(); - name = first_name + " " + last_name; - age = infile.nextInt(); - gpa = infile.nextDouble(); - who = new Student(name, age, gpa); - students.add(who); - } - } // method readData - - - /** - Displays the student records stored in the array list. - */ - public void displayResults() - { - // display the array elements in sequence - System.out.println("The list contains " + students.size() + - " students"); - for (Student who: students) - displayStudentRecord(who); - } // method displayResults - - - /** - Displays the content of each student record. - @param who a reference to a Student object for display - */ - public void displayStudentRecord(Student who) - { - System.out.println("Name: " + who.getName()); - System.out.println("Age: " + who.getAge()); - System.out.println("GPA: " + who.getGPA()); - } // method displayStudentRecord -} // class ArrayList1 From e7c60fe5f284f6bc1e72db4970a6e3261971261c Mon Sep 17 00:00:00 2001 From: Kody Ellis Date: Fri, 12 Jan 2018 19:15:05 -0600 Subject: [PATCH 08/11] Update ArrayList1.java CHanged placement of code that would output the average gpa for all students. Previously it would output the average gpa after it displayed the details of each student, resulting in repeating information. --- lab1/ArrayList1.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lab1/ArrayList1.java b/lab1/ArrayList1.java index ae7148c..65d18d8 100644 --- a/lab1/ArrayList1.java +++ b/lab1/ArrayList1.java @@ -33,6 +33,7 @@ public void getStarted() openFile(); readData(); displayResults(); + System.out.println("Average GPA:" + averageGPA); infile.close(); } // method getStarted @@ -125,6 +126,5 @@ public void displayStudentRecord(Student who) System.out.println("Age: " + who.getAge()); System.out.println("GPA: " + who.getGPA()); - System.out.println("Average GPA:" + averageGPA); } // method displayStudentRecord } // class ArrayList1 From a96691e7321921a4cb405696e376b2f902f21eac Mon Sep 17 00:00:00 2001 From: Kody Ellis Date: Fri, 12 Jan 2018 19:16:05 -0600 Subject: [PATCH 09/11] Rename readme to readme.md --- lab3/{readme => readme.md} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename lab3/{readme => readme.md} (100%) diff --git a/lab3/readme b/lab3/readme.md similarity index 100% rename from lab3/readme rename to lab3/readme.md From 061f04e478c74195efb4e1b36febb87d0f580f2d Mon Sep 17 00:00:00 2001 From: Kody Ellis Date: Fri, 12 Jan 2018 19:31:42 -0600 Subject: [PATCH 10/11] Update lab32.java mainly changed some documentation to become clearer. --- lab3/lab32.java | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/lab3/lab32.java b/lab3/lab32.java index 6fd5712..6a6548b 100644 --- a/lab3/lab32.java +++ b/lab3/lab32.java @@ -1,5 +1,5 @@ /** - file name -- ShowRead01.java + file name -- lab32.java This program shows how to read data from the keyboard in Java. */ @@ -16,14 +16,17 @@ public static void main(String list[]) throws IOException { Scanner source; double a,b,c,s,area; + //creates new decimal format object called two. This will help us format decimal numbers DecimalFormat two = new DecimalFormat(); + //uses this method to set the decimal digits to 2 two.setMaximumFractionDigits(2); source = new Scanner(System.in); int error = 0; - + + //if side doesnt meet triangle law then resend message do { - //if statennt if side diesnt meet triangkle law then reorint message + //if side doesnt meet triangle law then resend message System.out.print("Enter the length of the first side of the triangle: "); a = source.nextDouble(); @@ -42,8 +45,10 @@ public static void main(String list[]) throws IOException }while( error == 1); - // calculate total cost of the shirt's purchased + // calculate the semi perimeter for this triangle.total cost of the shirt's purchased s = ( (a + b + c) / 2); + + //Uses Heron's formula area = sqrt( s*(s-a)*(s-b)*(s-c) ); System.out.println("The area of the triangle with sides: " + a + ", " + b + " and " + c + " is : " + two.format(area)); From c1c76cbbb1f3c48dd9689a44181dcc05f77c9711 Mon Sep 17 00:00:00 2001 From: Kody Ellis Date: Fri, 12 Jan 2018 19:46:07 -0600 Subject: [PATCH 11/11] Update lab31.java corrected code and documentation. --- lab3/lab31.java | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/lab3/lab31.java b/lab3/lab31.java index ff1e015..4ebd94a 100644 --- a/lab3/lab31.java +++ b/lab3/lab31.java @@ -1,6 +1,5 @@ /** - file name -- ShowRead01.java - This program shows how to read data from the keyboard in Java. + file name -- lab31.java */ import java.io.*; @@ -16,16 +15,16 @@ public static void main(String list[]) throws IOException int a; System.out.println("The Six Numbers Are: "); - a = rand.nextInt(54); + //for loops for(int i = 0; i < 6; i++) { //do the randomization -//while a == 0 -//a = rand.Int -//System print out - a = rand.nextInt(3-1+1)+1; + //need values from 1 to 54. So .nextInt(53) does 0 - 53 + //the 0 itself is set inside the function, so to bypass this we add +1 after than function + //this way if the funtion returned 0 it would be 1, if it returned 53 it would return 54. + a = rand.nextInt(53)+1; System.out.println(a + " "); }