ÿþ<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns:mv="http://macVmlSchemaUri" xmlns="http://www.w3.org/TR/REC-html40"> <head> <meta name=Title content="CS401 Lab 3"> <meta name=Keywords content=""> <meta http-equiv=Content-Type content="text/html; charset=unicode"> <meta name=ProgId content=Word.Document> <meta name=Generator content="Microsoft Word 14"> <meta name=Originator content="Microsoft Word 14"> <link rel=File-List href="lab3_files/filelist.xml"> <link rel=Edit-Time-Data href="lab3_files/editdata.mso"> <!--[if !mso]> <style> v\:* {behavior:url(#default#VML);} o\:* {behavior:url(#default#VML);} w\:* {behavior:url(#default#VML);} .shape {behavior:url(#default#VML);} </style> <![endif]--> <title>CS401 Lab 3</title> <!--[if gte mso 9]><xml> <o:DocumentProperties> <o:LastAuthor>John Ramirez</o:LastAuthor> <o:Revision>7</o:Revision> <o:TotalTime>20</o:TotalTime> <o:Created>2009-09-17T19:55:00Z</o:Created> <o:LastSaved>2011-09-18T12:45:00Z</o:LastSaved> <o:Pages>1</o:Pages> <o:Words>537</o:Words> <o:Characters>3062</o:Characters> <o:Company>University of Pittsburgh</o:Company> <o:Lines>25</o:Lines> <o:Paragraphs>7</o:Paragraphs> <o:CharactersWithSpaces>3592</o:CharactersWithSpaces> <o:Version>14.0</o:Version> </o:DocumentProperties> <o:OfficeDocumentSettings> <o:AllowPNG/> </o:OfficeDocumentSettings> </xml><![endif]--> <link rel=themeData href="lab3_files/themedata.xml"> <!--[if gte mso 9]><xml> <w:WordDocument> <w:Zoom>150</w:Zoom> <w:SpellingState>Clean</w:SpellingState> <w:GrammarState>Clean</w:GrammarState> <w:TrackMoves>false</w:TrackMoves> <w:TrackFormatting/> <w:ValidateAgainstSchemas/> <w:SaveIfXMLInvalid>false</w:SaveIfXMLInvalid> <w:IgnoreMixedContent>false</w:IgnoreMixedContent> <w:AlwaysShowPlaceholderText>false</w:AlwaysShowPlaceholderText> <w:DoNotPromoteQF/> <w:LidThemeOther>EN-US</w:LidThemeOther> <w:LidThemeAsian>X-NONE</w:LidThemeAsian> <w:LidThemeComplexScript>X-NONE</w:LidThemeComplexScript> <w:Compatibility> <w:SplitPgBreakAndParaMark/> <w:UseFELayout/> </w:Compatibility> <m:mathPr> <m:mathFont m:val="Cambria Math"/> <m:brkBin m:val="before"/> <m:brkBinSub m:val="&#45;-"/> <m:smallFrac m:val="off"/> <m:dispDef/> <m:lMargin m:val="0"/> <m:rMargin m:val="0"/> <m:defJc m:val="centerGroup"/> <m:wrapIndent m:val="1440"/> <m:intLim m:val="subSup"/> <m:naryLim m:val="undOvr"/> </m:mathPr></w:WordDocument> </xml><![endif]--><!--[if gte mso 9]><xml> <w:LatentStyles DefLockedState="false" DefUnhideWhenUsed="false" DefSemiHidden="false" DefQFormat="false" LatentStyleCount="276"> <w:LsdException Locked="false" QFormat="true" Name="Normal"/> <w:LsdException Locked="false" QFormat="true" Name="heading 1"/> <w:LsdException Locked="false" QFormat="true" Name="heading 2"/> <w:LsdException Locked="false" QFormat="true" Name="heading 3"/> <w:LsdException Locked="false" SemiHidden="true" UnhideWhenUsed="true" QFormat="true" Name="heading 4"/> <w:LsdException Locked="false" SemiHidden="true" UnhideWhenUsed="true" QFormat="true" Name="heading 5"/> <w:LsdException Locked="false" SemiHidden="true" UnhideWhenUsed="true" QFormat="true" Name="heading 6"/> <w:LsdException Locked="false" SemiHidden="true" UnhideWhenUsed="true" QFormat="true" Name="heading 7"/> <w:LsdException Locked="false" SemiHidden="true" UnhideWhenUsed="true" QFormat="true" Name="heading 8"/> <w:LsdException Locked="false" SemiHidden="true" UnhideWhenUsed="true" QFormat="true" Name="heading 9"/> <w:LsdException Locked="false" SemiHidden="true" UnhideWhenUsed="true" QFormat="true" Name="caption"/> <w:LsdException Locked="false" QFormat="true" Name="Title"/> <w:LsdException Locked="false" Priority="1" Name="Default Paragraph Font"/> <w:LsdException Locked="false" QFormat="true" Name="Subtitle"/> <w:LsdException Locked="false" QFormat="true" Name="Strong"/> <w:LsdException Locked="false" QFormat="true" Name="Emphasis"/> <w:LsdException Locked="false" Priority="99" Name="No List"/> <w:LsdException Locked="false" QFormat="true" Name="No Spacing"/> <w:LsdException Locked="false" QFormat="true" Name="List Paragraph"/> <w:LsdException Locked="false" QFormat="true" Name="Quote"/> <w:LsdException Locked="false" QFormat="true" Name="Intense Quote"/> <w:LsdException Locked="false" QFormat="true" Name="Subtle Emphasis"/> <w:LsdException Locked="false" QFormat="true" Name="Intense Emphasis"/> <w:LsdException Locked="false" QFormat="true" Name="Subtle Reference"/> <w:LsdException Locked="false" QFormat="true" Name="Intense Reference"/> <w:LsdException Locked="false" QFormat="true" Name="Book Title"/> <w:LsdException Locked="false" SemiHidden="true" UnhideWhenUsed="true" QFormat="true" Name="TOC Heading"/> </w:LatentStyles> </xml><![endif]--> <style> <!-- /* Font Definitions */ @font-face {font-family:"Courier New"; panose-1:2 7 3 9 2 2 5 2 4 4; mso-font-charset:0; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:3 0 0 0 1 0;} @font-face {font-family:Times; panose-1:2 0 5 0 0 0 0 0 0 0; mso-font-charset:0; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:3 0 0 0 1 0;} @font-face {font-family:"-ÿ3ÿ fg"; panose-1:0 0 0 0 0 0 0 0 0 0; mso-font-charset:128; mso-generic-font-family:roman; mso-font-format:other; mso-font-pitch:fixed; mso-font-signature:1 134676480 16 0 131072 0;} @font-face {font-family:"-ÿ3ÿ fg"; panose-1:0 0 0 0 0 0 0 0 0 0; mso-font-charset:128; mso-generic-font-family:roman; mso-font-format:other; mso-font-pitch:fixed; mso-font-signature:1 134676480 16 0 131072 0;} /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {mso-style-unhide:no; mso-style-qformat:yes; mso-style-parent:""; margin:0in; margin-bottom:.0001pt; mso-pagination:widow-orphan; font-size:10.0pt; font-family:Times; mso-fareast-font-family:"-ÿ3ÿ fg"; mso-fareast-theme-font:minor-fareast; mso-bidi-font-family:"Times New Roman"; mso-bidi-theme-font:minor-bidi;} h2 {mso-style-unhide:no; mso-style-qformat:yes; mso-style-link:"Heading 2 Char"; margin:0in; margin-bottom:.0001pt; mso-pagination:widow-orphan; mso-outline-level:2; font-size:18.0pt; font-family:Times; mso-fareast-font-family:"-ÿ3ÿ fg"; mso-fareast-theme-font:minor-fareast; mso-bidi-font-family:"Times New Roman"; mso-bidi-theme-font:minor-bidi; font-weight:bold;} h3 {mso-style-unhide:no; mso-style-qformat:yes; mso-style-link:"Heading 3 Char"; margin:0in; margin-bottom:.0001pt; mso-pagination:widow-orphan; mso-outline-level:3; font-size:13.5pt; font-family:Times; mso-fareast-font-family:"-ÿ3ÿ fg"; mso-fareast-theme-font:minor-fareast; mso-bidi-font-family:"Times New Roman"; mso-bidi-theme-font:minor-bidi; font-weight:bold;} a:link, span.MsoHyperlink {mso-style-unhide:no; color:blue; mso-themecolor:hyperlink; text-decoration:underline; text-underline:single;} a:visited, span.MsoHyperlinkFollowed {mso-style-unhide:no; color:purple; mso-themecolor:followedhyperlink; text-decoration:underline; text-underline:single;} p {mso-style-unhide:no; margin:0in; margin-bottom:.0001pt; mso-pagination:widow-orphan; font-size:10.0pt; font-family:Times; mso-fareast-font-family:"-ÿ3ÿ fg"; mso-fareast-theme-font:minor-fareast; mso-bidi-font-family:"Times New Roman";} span.Heading2Char {mso-style-name:"Heading 2 Char"; mso-style-unhide:no; mso-style-locked:yes; mso-style-link:"Heading 2"; mso-ansi-font-size:13.0pt; mso-bidi-font-size:13.0pt; font-family:Calibri; mso-ascii-font-family:Calibri; mso-ascii-theme-font:major-latin; mso-fareast-font-family:"-ÿ3ÿ ´0·0Ã0¯0"; mso-fareast-theme-font:major-fareast; mso-hansi-font-family:Calibri; mso-hansi-theme-font:major-latin; mso-bidi-font-family:"Times New Roman"; mso-bidi-theme-font:major-bidi; color:#4F81BD; font-weight:bold;} span.Heading3Char {mso-style-name:"Heading 3 Char"; mso-style-unhide:no; mso-style-locked:yes; mso-style-link:"Heading 3"; mso-ansi-font-size:10.0pt; mso-bidi-font-size:10.0pt; font-family:Calibri; mso-ascii-font-family:Calibri; mso-ascii-theme-font:major-latin; mso-fareast-font-family:"-ÿ3ÿ ´0·0Ã0¯0"; mso-fareast-theme-font:major-fareast; mso-hansi-font-family:Calibri; mso-hansi-theme-font:major-latin; mso-bidi-font-family:"Times New Roman"; mso-bidi-theme-font:major-bidi; color:#4F81BD; font-weight:bold;} span.SpellE {mso-style-name:""; mso-spl-e:yes;} span.GramE {mso-style-name:""; mso-gram-e:yes;} .MsoChpDefault {mso-style-type:export-only; mso-default-props:yes; font-size:10.0pt; mso-ansi-font-size:10.0pt; mso-bidi-font-size:10.0pt;} @page WordSection1 {size:8.5in 11.0in; margin:1.0in 1.25in 1.0in 1.25in; mso-header-margin:.5in; mso-footer-margin:.5in; mso-paper-source:0;} div.WordSection1 {page:WordSection1;} /* List Definitions */ @list l0 {mso-list-id:588929119; mso-list-template-ids:-462011114;} @list l1 {mso-list-id:1795558374; mso-list-template-ids:779770702;} @list l1:level1 {mso-level-tab-stop:.5in; mso-level-number-position:left; text-indent:-.25in;} @list l1:level2 {mso-level-tab-stop:1.0in; mso-level-number-position:left; text-indent:-.25in;} @list l1:level3 {mso-level-tab-stop:1.5in; mso-level-number-position:left; text-indent:-.25in;} @list l1:level4 {mso-level-tab-stop:2.0in; mso-level-number-position:left; text-indent:-.25in;} @list l1:level5 {mso-level-tab-stop:2.5in; mso-level-number-position:left; text-indent:-.25in;} @list l1:level6 {mso-level-tab-stop:3.0in; mso-level-number-position:left; text-indent:-.25in;} @list l1:level7 {mso-level-tab-stop:3.5in; mso-level-number-position:left; text-indent:-.25in;} @list l1:level8 {mso-level-tab-stop:4.0in; mso-level-number-position:left; text-indent:-.25in;} @list l1:level9 {mso-level-tab-stop:4.5in; mso-level-number-position:left; text-indent:-.25in;} ol {margin-bottom:0in;} ul {margin-bottom:0in;} --> </style> <!--[if gte mso 10]> <style> /* Style Definitions */ table.MsoNormalTable {mso-style-name:"Table Normal"; mso-tstyle-rowband-size:0; mso-tstyle-colband-size:0; mso-style-noshow:yes; mso-style-priority:99; mso-style-parent:""; mso-padding-alt:0in 5.4pt 0in 5.4pt; mso-para-margin:0in; mso-para-margin-bottom:.0001pt; mso-pagination:widow-orphan; font-size:10.0pt; font-family:"Times New Roman";} </style> <![endif]--><!--[if gte mso 9]><xml> <o:shapedefaults v:ext="edit" spidmax="1027"/> </xml><![endif]--><!--[if gte mso 9]><xml> <o:shapelayout v:ext="edit"> <o:idmap v:ext="edit" data="1"/> </o:shapelayout></xml><![endif]--> </head> <body bgcolor=white lang=EN-US link=blue vlink=purple style='tab-interval:.5in'> <div class=WordSection1> <div class=MsoNormal align=center style='text-align:center'> <hr size=2 width="100%" align=center> </div> <h2 align=center style='margin-bottom:6.0pt;text-align:center'><span style='mso-fareast-font-family:"Times New Roman"'>CS401 Lab 3: Java Expressions and Control Structures<o:p></o:p></span></h2> <p align=center style='margin-bottom:6.0pt;text-align:center'><b style='mso-bidi-font-weight:normal'>Note: This lab is graded and will be worth 2 points toward your overall grade.<span style="mso-spacerun:yes">  </span>If you do not complete the lab this week you may finish it at the beginning of next week's lab.</b></p> <div> <div style='margin-bottom:6.0pt'> <div class=MsoNormal align=center style='text-align:center'> <hr size=2 width="100%" align=center> </div> </div> </div> <h3 style='margin-bottom:6.0pt'><span style='mso-fareast-font-family:"Times New Roman"'>Introduction<o:p></o:p></span></h3> <p style='margin-bottom:6.0pt'>Now that you have reviewed expressions and control structures, it is a good idea to practice using them.&nbsp; In lecture we looked at <b style='mso-bidi-font-weight:normal'><span style='font-family: "Courier New"'>while</span></b> loops, <b style='mso-bidi-font-weight:normal'><span style='font-family:"Courier New"'>for</span></b> loops and (briefly) <b style='mso-bidi-font-weight:normal'><span style='font-family:"Courier New"'>do</span></b> loops in Java.&nbsp; We also looked at <b style='mso-bidi-font-weight:normal'><span style='font-family:"Courier New"'>if</span></b><span style='font-family:"Times New Roman"'> statements</span> and <b style='mso-bidi-font-weight:normal'><span style='font-family:"Courier New"'>switch</span></b> statements.&nbsp; Furthermore, we looked at Java's <span style='font-family:"Courier New"'>Scanner</span> class to allow simple input from the console. In this lab your TA will give you a short programming problem for you to solve using Java during the lab session.&nbsp; Once you have completed the problem, you should do the following:</p> <ol style='margin-top:0in' start=1 type=1> <li class=MsoNormal style='margin-bottom:6.0pt;mso-list:l1 level1 lfo3; tab-stops:list .5in'>Show your TA that your program compiles and executes correctly (1 point)</li> <li class=MsoNormal style='margin-bottom:6.0pt;mso-list:l1 level1 lfo3; tab-stops:list .5in'>Answer a short question about how you solved the problem (0.5 point)</li> <li class=MsoNormal style='margin-bottom:6.0pt;mso-list:l1 level1 lfo3; tab-stops:list .5in'>Zip up your program and submit it to the  Lab A location on the <a href="http://www.cs.pitt.edu/~ramirez/cs401/submit/">CS401 Submission Site</a> (0.5 point)</li> </ol> <div> <div style='margin-bottom:6.0pt'> <div class=MsoNormal align=center style='text-align:center'> <hr size=2 width="100%" align=center> </div> </div> </div> <p style='margin-bottom:6.0pt'><b style='mso-bidi-font-weight:normal'><span style='font-size:13.5pt'>Background</span></b></p> <p style='margin-bottom:6.0pt'>A logarithm is a very useful mathematical function.&nbsp; Using a given base, b, we define <span style='font-family:"Courier New"'>Y = <span class=SpellE>log<sub>b</sub>X</span></span> to be an exponent such that <span class=SpellE><span style='font-family:"Courier New"'>b<sup>Y</sup></span></span><span style='font-family:"Courier New"'> = X</span>.&nbsp; If we are dealing exclusively with integer values, we typically round the log up or down, using the ceiling or floor function.&nbsp; Consider rounding down, using the floor function.&nbsp; This will give us the exponent Y such that <span class=SpellE>b<sup>Y</sup></span> &lt;= X but b<sup>Y+1</sup> &gt; <span class=GramE>X .&nbsp;</span> For example, consider b = 10 (i.e. we are considering base 10 logarithms):</p> <p style='margin-bottom:6.0pt'><span class=GramE><b style='mso-bidi-font-weight: normal'><span style='font-family:"Courier New"'>floor</span></b></span><b style='mso-bidi-font-weight:normal'><span style='font-family:"Courier New"'> (log<sub>10</sub> (123456)) = 5</span></b><span style='font-family:"Courier New"'>&nbsp; </span>because 10<sup>5</sup> = 100000 &lt;= 123456 and 10<sup>6</sup> = 1000000 &gt; 123456</p> <p style='margin-bottom:6.0pt'>As another example, consider b = 2 (i.e. we are considering base 2 logarithms):</p> <p style='margin-bottom:6.0pt'><span class=GramE><b style='mso-bidi-font-weight: normal'><span style='font-family:"Courier New"'>floor</span></b></span><b style='mso-bidi-font-weight:normal'><span style='font-family:"Courier New"'> (log<sub>2</sub> (64)) = 6</span></b><span style='font-family:"Courier New"'>&nbsp; </span>because 2<sup>6</sup> = 64 &lt;= 64 and 2<sup>7</sup> = 128 &gt; 64</p> <p style='margin-bottom:6.0pt'>One way of calculating the floor of the <span class=SpellE>log<sub>b</sub></span> X is by repeated integer division.&nbsp; Divide by the base until the quotient is less than the base.&nbsp; The number of completed divisions is equal to the floor of the <span class=SpellE><span class=GramE>log<sub>b</sub>X</span></span><span class=GramE> .&nbsp;</span> For example, consider the cases above, and one additional case:</p> <p style='margin-bottom:6.0pt'>123456 / 10 = 12345&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 12345 / 10 = 1234&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 1234 / 10 = 123&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 123 / 10 = 12&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 12 / 10 = 1 &lt; 10&nbsp;&nbsp;&nbsp; =&gt; 5 divisions</p> <p style='margin-bottom:6.0pt'>64 / 2 = 32&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 32 / 2 = 16&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 16 / 2 = 8&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 8 / 2 = 4&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 4 / 2 = 2&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 2 / 2 = 1&nbsp;&nbsp;&lt; 2&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =&gt; 6 divisions</p> <p style='margin-bottom:6.0pt'>4567 / 10 = 456&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 456 / 10 = 45&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 45 / 10 = 4 &lt; 10&nbsp;&nbsp;&nbsp; =&gt; 3 divisions</p> <div> <div style='margin-bottom:6.0pt'> <div class=MsoNormal align=center style='text-align:center'> <hr size=2 width="100%" align=center> </div> </div> </div> <p style='margin-bottom:6.0pt'><b style='mso-bidi-font-weight:normal'><span style='font-size:13.5pt'>Program, Grading and Submission</span></b></p> <p style='margin-bottom:6.0pt'>You are to write a Java program that does the following:</p> <p style='margin-bottom:6.0pt'>Asks the user to enter an integer base b &gt; 1 (if the number is &lt;= 1 you should quit the program)<br> Asks the user to enter a positive integer X.&nbsp; Numbers &lt;= 0 should be rejected and the user must re-enter.<br> Calculates the floor of <span class=SpellE>log<sub>b</sub>X</span> in the manner indicated above and outputs the result<br> <span class=GramE>Repeats</span></p> <p style='margin-bottom:6.0pt'>Demonstrate that your program works correctly to your TA by running it with various inputs, showing that it handles all of the cases indicated.</p> <p style='margin-bottom:6.0pt'>Answer the question asked by your TA.</p> <p style='margin-bottom:6.0pt'>Submit your zipped program to the  Lab A location on the <a href="http://www.cs.pitt.edu/~ramirez/cs401/submit/">CS 401 Submission Site</a> in front of the TA (so that he / she can see you do it). </p> <div> <div style='margin-bottom:6.0pt'> <div class=MsoNormal align=center style='text-align:center'> <hr size=2 width="100%" align=center> </div> </div> </div> <p style='margin-bottom:6.0pt'><b style='mso-bidi-font-weight:normal'><span style='font-size:13.5pt'>Notes and Hints</span></b></p> <p style='margin-bottom:6.0pt'>You will need to use nested loops to solve this problem.&nbsp; However, the exact combination of loops is up to you, as long as your logic is correct.&nbsp; Look at some of the example programs from lecture for some ideas.&nbsp; If you need help as you are working on the program, ask your TA.</p> <p style='margin-bottom:6.0pt'>If you do not finish this program during your lab session, you will be given the opportunity to demonstrate it to your TA at the beginning of next week's lab.</p> <p style='margin-bottom:6.0pt'><br> &nbsp;</p> </div> </body> </html>