?? exe1.html
字號:
<span class="dialogueheader">Take Assessment: Exercise 1</span><br><br>
<form name="Assm" id="Assm" method="post" action="https://seqcc.icarnegie.com:443/submitassmcmd.php" enctype="multipart/form-data">
<a name="top_of_page"></a><input name="object_id" id="object_id" value="657678" type="hidden"><table><tbody><tr>
</tr></tbody></table>
<table><tbody><tr>
<td width="5"><br></td> <td class="td0_instructions" align="left" valign="top">
<label class="instructions">Please answer the following question(s).<br>
If the assessment includes multiple-choice questions, click the "Submit Answers" button when you have completed those questions.</label></td>
</tr></tbody></table>
<input name="MAX_FILE_SIZE" id="MAX_FILE_SIZE" value="512000" type="hidden"><table><tbody><tr>
<td width="5"><br></td><script language="JavaScript">document.write('<td class=instructions>' + 'You have 120 minutes to take this assessment.' + '<br>' + 'Please complete this assessment by ' + ComputeLocalDate('2008-11-29 10:48:17 UTC') + '.' + '</td>');</script><td class="instructions">You have 120 minutes to take this assessment.<br>Please complete this assessment by Sat Nov 29 2008 18:48:17 GMT+0800.</td> <td width="5"><br></td></tr></tbody></table>
<a name="top_13829"></a><table><tbody><tr>
<td width="5"><br></td> <td class="td0_highlight-label" align="right" nowrap="nowrap" valign="top" width="12">
<label class="highlight-label">1.</label></td>
<td width="5"><br></td> <td class="td0_x" align="left" valign="top"> <a href="#13829">Go to bottom of question.</a> </td>
</tr></tbody></table>
<table><tbody><tr>
<td width="5"><br></td> <td class="td0_x" align="left" valign="top"> <!-- new pg 042903 ici -->
<h2 align="center"><cite>Shopping Cart Application</cite></h2>
<h3>Prerequisites, Goals, and Outcomes</h3>
<blockquote> <b><i>Prerequisites:</i></b> Before you begin this exercise, you
need mastery of the following:
<ul>
<li> <em>Java Language</em>
<ul>
<li>Knowledge of Java control structures</li>
</ul>
</li><li><i>Java API</i>
<ul>
<li>Knowledge of package <code>java.io</code>
<ul>
<li>How to get input from the keyboard</li>
<li>How to output results, prompts, and errors</li>
</ul>
</li>
<li>Knowledge of wrapper class <code>Integer</code>: how to read an integer
value as input</li>
<li>Knowledge of class <code>StringTokenizer</code>: how to parse a line
with multiple values</li>
</ul>
</li><li> <i>Exception Objects</i>
<ul>
<li>Knowledge of exception handling</li>
<li>Knowledge of <code>NumberFormatException</code></li>
</ul>
</li><li><i>Programming Practice</i>
<ul>
<li>How to produce Java applications</li>
<li>Knowledge of Sun's code conventions</li>
</ul>
<ul>
<li>Knowledge of Javadoc: how to document classes, methods, and variables</li>
</ul>
</li></ul>
<p> <b><i>Goals:</i></b> Reinforce your ability to use I/O classes, tokenizers,
wrapper classes, and exceptions
</p><p> <b><i></i></b><b><i>Outcomes:</i></b> You will demonstrate mastery in the
following:
</p><ul>
<li> Produce an application that reads input from the keyboard and uses:
<ul>
<li>Wrapper classes to read numerical data</li>
<li><code>StringTokenizer</code> to parse a line with multiple values</li>
<li> Exceptions to handle malformed data</li>
<li>Control structures to control the reading of data</li>
</ul>
</li></ul>
</blockquote>
<h3>Background </h3>
<p>This assignment asks you to complete an application that adds products to an
electronic shopping cart. The application uses three classes: <code>Product</code>,
<code>ShoppingCart</code>, and <code>ShoppingCartApplication</code>. Part of
the work has been done for you and is provided in the student archive. You will
implement the method in <code>ShoppingCartApplication</code> that reads product
information from the keyboard and creates a <code>Product</code> object.</p>
<h3>Description</h3>
<p> The application presents the user with a menu of options and prompts the user
for a choice:</p>
<ul>
<li>Choice 0 terminates the program.</li>
<li>Choice 1 adds a product to the shopping cart<cite></cite>.</li>
<li>Choice 2 displays the information of all the products stored in the shopping
cart.</li>
<li>Choice 3 displays the total cost of all the products in the shopping cart<cite></cite>.</li>
</ul>
<p>To add a product, the user enters a line with the following format:</p>
<blockquote> <em>name_quntity_price</em> </blockquote>
<p>Where:</p>
<ul>
<li><em>name</em> is the name of the product.</li>
<li><em>quantity</em> is the quantity of the product.</li>
<li><em>price</em> is the price of the product.</li>
</ul>
<p>The fields are delimited by an underscore ( <code>_</code> ). If the user's
input is invalid, the application displays an error message.</p>
<p>The following is a screen shot of the application after some products have
been added.</p>
<blockquote>
<table border="0" cellpadding="2" cellspacing="2" width="24%">
<tbody><tr>
<td><img src="/content/SSD/SSD3/4.2.0.1/normal/pg-class-dsgn/pg-java-apps/assm-exer-read-obj-data/pool-pr-apps-io-tokenzr-excptns/qn-pr-shopping-cart/handout/images/shopping-cart-execution.jpg" alt="Figure 1 Execution of ShoppingCartApplication" height="305" width="405"></td>
</tr>
<tr>
<td><strong>Figure 1</strong><em> Execution of ShoppingCartApplication</em></td>
</tr>
</tbody></table></blockquote>
<p>The application uses classes <code>Product</code> and <code>ShoppingCart</code>.
<code>ShoppingCart</code> maintains a collection of products. Complete implementations
of both are provided in the student archive <cite><a href="/content/SSD/SSD3/4.2.0.1/normal/pg-class-dsgn/pg-java-apps/assm-exer-read-obj-data/pool-pr-apps-io-tokenzr-excptns/qn-pr-shopping-cart/handout/student-files.zip"><code>student-files.zip</code></a></cite>.
Review their documentation and become familiar with it. </p>
<ul>
<li><a href="/content/SSD/SSD3/4.2.0.1/normal/pg-class-dsgn/pg-java-apps/assm-exer-read-obj-data/pool-pr-apps-io-tokenzr-excptns/qn-pr-shopping-cart/handout/Product.html" target="externalWindow"><code><em>Product</em></code></a>.
Documentation for class <code>Product</code></li>
<li><a href="/content/SSD/SSD3/4.2.0.1/normal/pg-class-dsgn/pg-java-apps/assm-exer-read-obj-data/pool-pr-apps-io-tokenzr-excptns/qn-pr-shopping-cart/handout/ShoppingCart.html" target="externalWindow"><code><em>ShoppingCart</em></code></a>.
Documentation for class <code>ShoppingCart</code></li>
</ul>
<p>A partial implement of <code>ShoppingCartApplication</code> is provided in
the student archive <cite><a href="/content/SSD/SSD3/4.2.0.1/normal/pg-class-dsgn/pg-java-apps/assm-exer-read-obj-data/pool-pr-apps-io-tokenzr-excptns/qn-pr-shopping-cart/handout/student-files.zip"><code>student-files.zip</code></a></cite>.
It contains some variables declarations and three methods that need no modification.
You should complete method <code>readProduct</code>, the method that reads product
information from the keyboard and returns a <code>Product</code> object.</p>
<h3>Files</h3>
The following files are needed to complete this assignment:
<ul>
<li><a href="/content/SSD/SSD3/4.2.0.1/normal/pg-class-dsgn/pg-java-apps/assm-exer-read-obj-data/pool-pr-apps-io-tokenzr-excptns/qn-pr-shopping-cart/handout/exe-shopping-cart.jar"><code><cite>exe-shopping-cart.jar</cite></code></a>.
Download this file. It is the sample executable.</li>
<li> <cite><a href="/content/SSD/SSD3/4.2.0.1/normal/pg-class-dsgn/pg-java-apps/assm-exer-read-obj-data/pool-pr-apps-io-tokenzr-excptns/qn-pr-shopping-cart/handout/student-files.zip"><code>student-files.zip</code></a></cite>.
Download this file. This archive contains the following:</li>
<ul>
<li><cite><code>Product.class</code></cite>. A complete implementation</li>
<li> <cite><code>ShoppingCart.class</code></cite>. A complete implementation</li>
<li><cite><code>Product.html</code></cite>. Documentation</li>
<li> <cite><code>ShoppingCart.html</code></cite>. Documentation</li>
<li><cite><code>ShoppingCartApplication.java</code></cite> — Use this
template to complete your implementation.</li>
</ul>
</ul>
<h3>Tasks</h3>
<p>To complete this assignment, you will finish the implementation of class <code>ShoppingCartApplication</code>.
The following steps will guide you through this assignment. Document using Javadoc
and follow Sun's code conventions. Work incrementally and test each increment.
Save often. </p>
<ol>
<li><strong>Extract</strong> the student archive by issuing the following command
at the command prompt:
<blockquote><tt>C:\></tt><kbd>unzip student-files.zip</kbd></blockquote>
</li>
</ol>
<ol start="2">
<li><strong>Run</strong> the sample executable by issuing the following command
at the command prompt:
<blockquote><tt>C:\></tt><kbd>java -jar exe-shopping-cart.jar</kbd> </blockquote>
<p>Observe how the program responds to the following types of input:</p>
<ul>
<li> Input with a quantity that is not a valid integer: <kbd>MP3 Player_1.0_150.0</kbd></li>
<li> Input with a price that is not a valid double: <kbd></kbd><kbd>MP3
Player_1_A</kbd>.</li>
<li>Input that contains negative numbers: <kbd>MP3 Player_-1_150.0</kbd>,
or <kbd>MP3 Player_1_-150.0</kbd>.</li>
<li>Input that contains more than three values: <kbd>MP3 Player_1_150.0_1</kbd>.</li>
<li>Input that contains fewer than three values: <kbd>MP3 Player_1</kbd>.</li>
<li>Valid input: <kbd>MP3 Player_1_150.0</kbd>, and <kbd>CD Walkman_2_48.75</kbd>.</li>
</ul>
</li>
</ol>
<ol start="2">
<li> <strong>Then</strong>, complete method <code>readProduct</code>: </li>
</ol>
<ul>
<li><code><em>private Product readProduct() throws IOException</em></code>.
This<code></code> method prompts the user for input, reads product information
from the keyboard, and creates an instance of class <code>Product</code>.
The product information should consist of three values, all entered on the
same line, and delimited by an underscore ( <code>_</code> ). The first value
should be a non-empty string that represents the name of the product. The
second value should be a positive integer that represents the quantity of
product. The third value should be a positive double that represents the price.
Use <a href="http://java.sun.com/j2se/1.5/docs/api/java/util/StringTokenizer.html" target="externalWindow">java.util.StringTokenizer</a>
to extract the three values from the input. </li>
</ul>
<blockquote>
<p><code>readProduct</code> validates the user's input:</p>
</blockquote>
<ol>
<blockquote>
<ul>
<li>If the user enters more than three values, an error message is displayed.</li>
<li>If the user enters fewer than three values, an error message is displayed.</li>
<li>If the user enters a quantity that is not a valid integer, <a href="http://java.sun.com/j2se/1.5/docs/api/java/lang/NumberFormatException.html" target="externalWindow">java.lang.NumberFormatException</a>
is caught and output.</li>
<li>If the user enters a price that is not a valid double, <a href="http://java.sun.com/j2se/1.5/docs/api/java/lang/NumberFormatException.html" target="externalWindow">java.lang.NumberFormatException</a>
is caught and output.</li>
<li> If the user enters a quantity that is negative or zero, an error message
is displayed.</li>
<li>If the user enters a price that is negative, an error message
is displayed.</li>
</ul>
</blockquote>
</ol>
<blockquote>
<p>The error messages displayed by your implementation should match the error
messages displayed by the sample executable.</p>
<p>If the input is invalid, <code>readProduct</code> re-prompts the user for
new input. Otherwise, it creates a new <code>Product</code> object using the
specified name, quantity, and price and returns a reference to the new object
to the calling method. </p>
</blockquote>
<h3>Submission</h3>
<p>Upon completion, submit <strong>only</strong> the following:</p>
<ol>
<li><code><cite>ShoppingCartApplication.java</cite></code></li>
</ol>
<p><code><cite><img src="/content/SSD/SSD3/4.2.0.1/normal/pg-class-dsgn/pg-java-apps/assm-exer-read-obj-data/pool-pr-apps-io-tokenzr-excptns/qn-pr-shopping-cart/handout/resources/inherit.gif" height="1" width="1"></cite></code></p>
</td>
<td width="5"><br></td></tr></tbody></table>
<a name="13829"></a><table><tbody><tr>
<td width="5"><br></td> <td class="td0_x" align="left" valign="top"> <a href="#top_13829">Go to top of question.</a> </td>
<td width="5"><br></td></tr></tbody></table>
<hr><table><tbody><tr>
<td width="5"><br></td> <td class="td0_x" align="left" valign="top"><label class="lbl0-x" for="file_to_add_13829">File to submit:</label><br> <input class="x" name="file_to_add_13829" value="" size="20" maxlength="500" type="file"> </td>
<td width="5"><br></td><td valign="bottom"> <input class="x" value="Upload File" name="add_file" id="add_file" style="width: 12em;" onclick='return SetUploadTime("https://seqcc.icarnegie.com:443/takeassmcmd.php?question_id=13829", 13829, false)' type="submit"></td><td width="5"><br></td></tr></tbody></table>
<hr><input name="pr_state_13829" id="pr_state_13829" value="a:2:{i:0;s:10:"incomplete";i:1;a:0:{}}" type="hidden"><table><tbody><tr>
</tr></tbody></table>
<input name="max_mins_per_try" id="max_mins_per_try" value="120" type="hidden"> <input value="2008-11-29 10:48:17 UTC" id="end_date" name="end_date" type="hidden"> <input value="421063" id="course_section_id" name="course_section_id" type="hidden"> <input value="13826" id="assm_id" name="assm_id" type="hidden"> <input value="657678" id="template_id" name="template_id" type="hidden"> <input value="657679" id="record_id" name="record_id" type="hidden"> <input value="" id="client_time" name="client_time" type="hidden"><table><tbody><tr>
<td width="5"><br></td> <td class="td0_x" align="left" valign="top"> <a href="#top_of_page">Go to top of assessment.</a> </td>
</tr></tbody></table>
<table><tbody><tr>
<td width="5"><br></td> <td class="td0_copyright" align="left" valign="top"> ? Copyright 2006 iCarnegie, Inc. All rights reserved. </td>
</tr></tbody></table>
</form>
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -