CS310 Java Help Needed
©Regis University, All Rights Reserved Unauthorized distribution (including uploading to any non-Regis Internet website) violates copyright law p. 1
Regis University CC&IS CS310 Data Structures Programming Assignment 1: Create class domains in NetBeans and test them Problem Scenario A local stock brokerage company is in need of a Java programmer to assist them with some of their needs. Various brokers in the office take care of multiple stocks in the broker’s portfolio. The company needs to be able to track information about their brokers and their traded stocks. For each Broker, the office needs to know:
• Broker’s license number (Unique String – three letters followed by five digits) • Broker’s first name (String) • Broker’s last name (String) • Broker’s department number (String of digits and a dash, formatted as “###-###”) • Broker’s commission rate (double representing a percentage)
The brokerage company has had issues with incorrect license numbers and department numbers, so they are requesting a way to be able to determine if the they are correct. For each StockTrade, the stock broker’s office wants to track:
• Stock symbol (Unique 3 or 4-char string, all uppercased) • Stock price per share (double) • Stock number of shares (int) NOTE: Only whole shares can be traded for this program • Broker’s license number (to match a license number in Broker) • Whether trade is taxable (Boolean)
The brokerage company has also had issues with incorrect stock symbols, prices, and number of shares, so they are requesting a way to be able to determine if the they are correct. Program Requirements
The assignment this week is to create and test some of the classes for the problem scenario above. These classes will be used to implement data structures in later assignments.
The program must follow the CS310 Coding Standards from Content section 1.9.
To set up a project directory for your program:
o Create a new project in NetBeans, and name it CS310 + last name (e.g. CS310Smith).
o Use the default Main Class name provided by NetBeans (e.g. cs310smith.CS310Smith). This will cause NetBeans to create a package for your source code files.
o NetBeans will create the default folders for the project.
o NetBeans will create the main method within the main class (e.g. CS310Smith.java)
The CS310<lastname>.java file will now contain your main class.
©Regis University, All Rights Reserved Unauthorized distribution (including uploading to any non-Regis Internet website) violates copyright law p. 2
After creating the project, when you view the Files tab, the project structure will look like this:
You will create two additional classes in the same CS310<lastname> folder as the CS310<lastname>.java file (click on the CS310<lastname> folder before selecting New File). The additional classes are:
• a Broker class • a StockTrade class
These classes will be used for creating and manipulating the Broker and StockTrade objects, and will not contain a main method. All class attributes/properties/data fields will be encapsulated within the objects, and therefore will be defined as private. After you create the Broker and StockTrade classes, use the tips provided in the NetBeans reader to generate the following methods for both classes (It is fine to also generate the hashCode method.):
constructors, getters, and setters equals() – must compare ALL attribute values toString() – must include ALL attribute values
The Broker and StockTrade classes should include two constructors each:
a constructor with no parameters and an empty body a constructor that accepts values for all of the attributes of the class as parameters
While NetBeans can help you create the basic methods, you will also need to add additional data error checking methods, as follows. Warning: Do not use Regular Expressions for these methods; otherwise no points will be awarded for them! Only use conditional expressions and loops for the error checking.
Error checking methods defined within the Broker class: • Check the broker’s license
o contains 3 letters followed by 5 digits within the String • Check the department number
o 7 chars long and contains a dash and digits in the correct places, and all of the first three digits are 1, 2, or 3
Error checking methods defined within the StockTrade class: • Check the stock symbol
o is 3 or 4 uppercased alphabetic characters long • Check the stock price
o is not under $0.00 or over $1000.00 per share • Check the number of shares
o is not under 0 or over 100,000
©Regis University, All Rights Reserved Unauthorized distribution (including uploading to any non-Regis Internet website) violates copyright law p. 3
All of these error checking methods will simply check the proper attribute and return a true or false. UML Class Diagrams* for the Broker and StockTrade classes:
*getters and setters are not shown, but must be defined for all attributes
Warning: The names used in the UML diagram are not optional or suggestions! In other words, you must use those names for the variables (fields) and methods. This week, your main class will be used to test your Broker and StockTrade classes. UML Class Diagram for the main class:
CS310Lastname
(no attributes)
+ main(String[]) <<static>> + setBrokerAttributes(brokerObj: Broker, line: String[]) <<static>> + displayBrokerAttributes(brokerObj: Broker) <<static>> + setStockTradeAttributes(stockTradeObj: StockTrade, line: String[]) <<static>> + displayStockTradeAttributes(stockTradeObj: StockTrade) <<static>>
Input Data File You will use a simple csv file input file for this program (see the online Content for information on how to read a csv file). The file will contain lines of data about brokers and stock trades. This week’s input file, assn1input.txt, will contain only a few data lines, in the following format:
BROKER,ADD/DEL,brokerLicense,firstName,lastName,department,commissionRate
TRADE,BUY/SELL,stockSymbol,sharePrice,numberOfShares,brokerLicense,taxable
NOTE: The taxable value will be stored in the data file as Y for yes, or N for no.
Broker StockTrade
- brokerLicense: String - firstName: String - lastName: String - dept: String - commissionRate: double
- stockSymbol: String - pricePerShare: double - wholeShares: int - brokerLicense: String - taxable: boolean
+ Broker() + Broker( brokerLicense: String, firstName: String, lastName: String, dept: String, commissionRate: double) + toString(): String + equals(brokerObj: Object): boolean + isValidLicense(): boolean + isValidDept(): boolean
+ StockTrade() + StockTrade(stockSymbol: String, pricePerShare: double, wholeShares: int, brokerLicense: String, taxable: boolean) + toString(): String + equals(stockTradeObj: Object): boolean + isValidStockSymbol(): boolean + isValidPrice(): boolean + isValidWholeShares(): boolean
©Regis University, All Rights Reserved Unauthorized distribution (including uploading to any non-Regis Internet website) violates copyright law p. 4
This file will be placed in an input directory within your project (see Deliverables section below for how to create new directories within your project). Within your code, define a global constant to hold this filename, as follows. Put it where class attributes normally would go, as this will make it easy to locate and change:
public static final String INPUT_FILENAME = "input/assn1input.txt"; In other words it would look similar to this in your program: public class CS310LastName {
public static final String INPUT_FILENAME = "input/assn1input.txt";
/* rest of class code here */
} Here is an example of a file containing two lines of Broker data, followed by two lines of StockTrade data:
Sample Input File Lines BROKER,ADD,XYZ98765,Jose,Fuentes,321-111,0.02 BROKER,DEL,ABC12345,Amy,Johnson,123-222,0.025 TRADE,BUY,MMM,88.88,650,XYZ98765,Y TRADE,SELL,DGLB,124.44,1022,ABC12345,N
Testing The main method in the CS310Lastname class will run 3 sets of tests.
NOTE: Before each test, your program must display a message stating which test is being run. It should also display a blank line between each of the tests.
Test Set 1 The first set of tests will test the constructors with parameters for each attribute and the toString() method for each class.
Test 1a: Instantiate a Broker object by hardcoding argument values of your choice in the call to the constructor. Then print the attribute values to the console, using the toString() instance method to send a message to the object. Test 1b: Instantiate a StockTrade object by hardcoding argument values of your choice in the call to the constructor. Then print the attribute values to the console, using the toString() instance method to send a message to the object. Example:
MyClass myObjectName = new MyClass("abc", 123, false); System.out.println( myObjectName.toString() );
©Regis University, All Rights Reserved Unauthorized distribution (including uploading to any non-Regis Internet website) violates copyright law p. 5
Test Set 2 The second set of tests will test the equals() methods for both classes.
Test 2a: • Instantiate a second Broker object using the same data values that you used to instantiate
the object created in test 1a. • Display a label identifying the object and then use the toString() method to display the
attribute values of the second object. • Use the equals() method to compare the first and second objects, and display a message
stating whether they are equal or not.
Test 2b: • Instantiate a third Broker object that differs from the first two (by at least 1 field value). • Display a label identifying the object and then use the toString() method to display the
attribute values of the third object. • Use the equals() method to compare the first and third objects, and display a message
stating whether they are equal or not.
Test 2c: • Instantiate a second StockTrade object using the same data values that you used to
instantiate the object in test 1b. • Display a label identifying the object and then use the toString() method to display the
attribute values of the second object. • Use the equals() method to compare to compare the first and second objects, and display a
message stating whether they are equal or not.
Test 2d: • Instantiate a third StockTrade object that differs from the first two (by at least 1 field
value). • Display a label identifying the object and then use the toString() method to display the
attribute values the third object. • Use the equals() method to compare to compare the first and third objects, and display a
message stating whether they are equal or not.
Test Set 3 The third set of tests will test the constructors that have no parameters, and setters and getters, and will also test reading data from the input data file.
Before running the tests, try to open the input data file. • Throw an exception of the data file cannot be found. • The exception handler should display an error message and include the name of the file
that could not be found in the message, and then exit the program with error code 1. If the file opens successfully, in a loop, read data lines until the end of the file. For each line read:
• Use split() to parse the line read into an array of String values.
• If the first item on the data line is the String “BROKER”:
Display whether a broker is being added or deleted.
©Regis University, All Rights Reserved Unauthorized distribution (including uploading to any non-Regis Internet website) violates copyright law p. 6
Instantiate a Broker object, using the constructor with no parameters.
Call the static setBrokerAttributes() method. This main class method will: o Pass in the Broker object and array of String values as parameters o Use the setters to set each attribute value
Now call methods to validate the broker’s license number and department. o If either of these items are invalid, use toString() to display the attributes, along
with an error message explaining what is invalid. o Otherwise, call the static displayBrokerAttributes() method. This main class
method will: • Pass in the Broker object as a parameter. • Use the getters to display each attribute value, one per line.
• If the first item on the data line is the String “TRADE”:
Display whether a stock is being bought or sold.
Instantiate a StockTrade object, using the constructor with no parameters.
Call the static setStockTradeAttributes() method. This main class method will: o Pass in the StockTrade object and array of String values as parameters o Use the setters to set each attribute value
Next, call methods to validate the stock symbol, stock price, and number of shares. o If any of these items are invalid, use toString() to display the attributes, along
with an error message explaining what is invalid. o Otherwise, call the static displayStockTradeAttributes() method. This main
class method will: • Pass in the StockTrade object as a parameter. • Use the getters to display each attribute value, one per line
Close the input data file when you have completed reading the file Test Set 3.
(a Sample Output has been provided later in this file)
Notes on Debugging and Validating your Code
In order to validate your code, you will need to run your test code multiple times, each time using different sets of data. For example:
Run 1: No test data input file in input folder.
Runs 2 through N: In the file data:
For the Broker objects, one broker’s license is invalid and one department number is invalid.
For the StockTrade objects, one stock symbol is invalid, one stock price is invalid, and one number of shares is invalid, and lines contain at least one taxable and one non-taxable trade.
You must supply enough test files to test all possible ways the tests might fail.
©Regis University, All Rights Reserved Unauthorized distribution (including uploading to any non-Regis Internet website) violates copyright law p. 7
Additional Requirements • You can use NetBeans to build comment headers, but you are responsible for completing the
comment headers created. Refer to the NetBeans reader on how to use NetBeans to insert your comment headers.
• Add an input folder. To create a new folder in NetBeans: o Right click on the project name (top level folder). o On the next dialog box, select New. o Another dialog box will pop up. Look for “Folder” (the order of the list varies based on what
the last actions you have done). Select Folder. o When prompted, name the new folder “input”.
Place all test data files that you create to test your program in the input folder of your project. Name them as follows:
assn1input1.txt assn1input2.txt (i.e. number each data file after the filename of assn1input.txt)
Together, all of your test data files should demonstrate that you have tested all possible code paths.
WARNING: Programs that do not compile will not be accepted!! Sample Output Display of Running Program
Running Test 1a: First Broker object: Broker{brokerLicense=AAA11111, firstName=John, lastName=Smith, dept=313- 3333, commissionRate=0.03} Running Test 1b: First StockTrade object: StockTrade{stockSymbol=XXWW, pricePerShare=11.11, wholeShares=33, brokerLicense=RTA33344, taxable=false} Running Test 2a: Second Broker object: Broker{brokerLicense=AAA11111, firstName=John, lastName=Smith, dept=313- 3333, commissionRate=0.03} First and third Broker objects ARE equal
Running Test 2b: Third Broker object: Broker{brokerLicense=AAA22222, firstName=John, lastName=Smith, dept=313- 3333, commissionRate=0.03} First and second Broker objects are NOT equal Running Test 2c: Second StockTrade object: StockTrade{stockSymbol=XXWW, pricePerShare=11.11, wholeShares=33, brokerLicense=RTA33344, taxable=false} First and third StockTrade objects ARE equal
©Regis University, All Rights Reserved Unauthorized distribution (including uploading to any non-Regis Internet website) violates copyright law p. 8
Running Test 2d: Third StockTrade object: StockTrade{stockSymbol=XXWW, pricePerShare=11.11, wholeShares=33, brokerLicense=RTA33344, taxable=true} First and second StockTrade objects NOT are equal Running Test 3: ADDING BROKER XYZ98765 Jose Fuentes 321-111 0.02 DELETING BROKER ABC12345 Amy Johnson 123-222 0.025 DELETING BROKER Broker{brokerLicense=EEE1234, firstName=Harry, lastName=Houdini, dept=555- 666, commissionRate=0.011} ERROR: Invalid broker licence number format: EEE1234 SELLING STOCK StockTrade{stockSymbol=XYZ, pricePerShare=9999.0, wholeShares=450, brokerLicense=ABC12345, taxable=false} ERROR: Invalid share price: 9999.00 BUYING STOCK DGLB 124.40 1022 ABC12345 False
Program Submission This programming assignment is due by midnight of the date listed on the Course Assignments by Week page of the Content. You will submit a zip file containing your project files. To submit your project:
• First export your project from NetBeans. To do this: o Highlight the project name. o Click on File from the top menu, and select Export Project. o Select To ZIP o Name your export file in the following format:
CS310<lastname>Assn<x>.zip
For example: CS310SmithAssn1.zip
NOTE: Save this zip file to some other directory, not your project directory.
©Regis University, All Rights Reserved Unauthorized distribution (including uploading to any non-Regis Internet website) violates copyright law p. 9
• Then submit your .zip file to the Prog Assn 1 Submission Folder (located under Assignments tab in online course).
Warning: Only NetBeans export files will be accepted. Do not use any other kind of archive or zip utility.
Grading This program will be graded using the rubric that is linked under Student Resources page.
WARNING: Programs submitted late will not be accepted,
and will receive a grade of 0.