Java程序辅导

C C++ Java Python Processing编程在线培训 程序编写 软件开发 视频讲解

客服在线QQ:2653320439 微信:ittutor Email:itutor@qq.com
wx: cjtutor
QQ: 2653320439
Java Lab 12: MyStore – Part 1                                               Wednesday, July 07, 2004 
 
Read instructions carefully! Following instructions is part of the grade for this lab.  
 
This lab is due at 5 pm today, at which point solutions will be posted at a location to be 
announced to the class. Having the lab due at 5 pm implies that you should save all 
your files and leave your machine by 5 pm. Failure to turn in the lab promptly at 5 pm 
will be penalized by taking off 1 point.  
 
Make sure you read through the entire lab before attempting any of the parts to manage 
your time appropriately. We will also be grading the neatness of and comments in your 
code. 1 point will be taken off for code that is not neat or has no comments. 
 
0. In the next two labs you will write software to run a store. The store can sell any 
products you want – it's up to you.  For this application, you will be putting all your .class 
files in a package called store. To do this in JCreator, save all your .java files in the 
lab12 folder and add a “package student;” statement to all your files. JCreator will then 
automatically put all the .class files in the folder lab12/store. 
 
1. (1 point) Create a new class called Product. This will represent a product sold in 
your store. Add fields to the Product class to store the name and price of the product.  
These fields should be assigned to values passed as arguments into the constructor. Add 
methods to Product to return the name and price of the product. Compile. 
 
2. (1 point) Change the Product constructor so it throws a 
NullPointerException is the name is null and an 
IllegalArgumentException if the price is negative. Compile. 
 
3. (1 point) Create a new class called MyStore.  MyStore should have a list field to 
contain all the products in your store.  Initialize the field to an empty list in the MyStore 
constructor. Compile. 
 
From now onwards, you will be calling methods that are defined in EasyReader. Copy 
EasyReader.java into your lab12 folder and add a package statement to declare it a 
member of a new package called easyreader.   
 
4. (2 points) Add a method to MyStore called readProducts. This method should 
accept no arguments and return nothing.  In the readProducts method, print 
messages to the console that ask the user to enter in a product name and price, and then 
read the name and price with EasyReader. Instantiate a Product with that name and 
price and add that product to the list of products. Compile. 
 
5. (1 point) Add a main method to MyStore. In the main method, instantiate a 
MyStore object and call the readProducts method on that object.  Compile and run. 
What happens when you type a word instead of a number when your program asks you to 
type in a price? Put the answer to this question in the file lab12.txt. 
 
6. (1 points) Write a new checked exception called ProductException.  Make sure 
you write both types of exception constructors. Compile. 
 
7. (3 points) Change the readProducts method in MyStore so that it catches the 
NumberFormatException thrown by EasyReader and throws a 
ProductException inside the catch clause. Change the main method in MyStore so 
that it catches a ProductException and prints out an appropriate message to the user. 
Compile and run. 
 
8. CheckOff  
Please ensure that you have all the required files in the folder named lab12. These files 
should compile successfully. Failure to compile will automatically cost you 1 point.   
  
If you are ready to be checked off before 5 pm, call one of the team members who will 
assign you a grade for the lab. Otherwise, you should leave the required files on your 
machine for us to evaluate at our own convenience.  
 
We will not accept modifications to the required files after the lab is officially due.