CSCI 1103 Lab08: Stocks as Objects
- Due: 11:59pm Friday 11/3/2017
- Approximately 0.83% of total grade
- Submit to Canvas
- You may work with one partner on this lab but both partners must submit files and be physically present for Check-offs
- Lab exercises are open resource/open collaboration. You may freely discuss lab topics with other members of the class.
CODE DISTRIBUTION: lab08-code.zip
- Download the code distribution every lab
- See further setup instructions below
CHANGELOG:
1 Rationale
Understanding how to define and operate on objects, records, or structures is the first step towards creating proper programming modules with information hiding and abstraction. Before those latter topics make sense, though, one must first appreciate how simple objects with public fields operate. This lab defines a simple class and some static methods associated with it to practice these ideas. Completing it will help to give a feel of how objects behave when passed as method arguments.
Associated Reading
Material from Eck Ch 5.1-2 is covered in this lab.
2 Download Lab Code and Setup
As in Lab01, download the code pack linked at the top of the page. Unzip this which will create a folder and create your files in that folder.
File | State | Notes |
---|---|---|
PARTNERS.txt | Edit | Fill in your name and the name of your partner in this text file |
TextIO.java | Provided | Allows easy input calls like TextIO.getInt() |
KTests.java | Testing | Utility routines for all tests |
junit-1103.jar | Testing | For testing, don't try to edit this one |
Problem1Tests.java | Testing | Tests for problem 1 |
Problem2Tests.java | Testing | Tests for problem 2 |
Problem3Tests.java | Testing | Tests for problem 3 |
StocksMain.java | Provided | main() method to complete the lab |
Stock.java | Create | Create this file and fill it with methods for all problems |
3 Overview
The main purpose of this lab is to define the Stock
class so that
the provided StockMain
class can run its main()
method. This will
require the following steps
- Problem 1: create the
Stock.java
file, add fields to it and theprintInfo()
method. - Problem 2: add the
buyMore()
andsellOff()
methods. - Problem 3: ensure that
StockMain
functions correctly
The stock class itself is a simple class that represents information on a stock in a stock market. If stocks are unfamiliar, pay careful attention to the description in Problem 1 and know that our treatment here will be very simple.
3.1 Write your code in Stock.java
for all Problems
Create only one file for this lab, Stock.java
. It will contain
methods that solve each problem.
Do not modify any of the files provided. Your goal is to write create
a Stock.java
file that is compatible with the provided files like
StockMain.java
.
There are a series of testing files with names such as
Problem1Tests.java
which have tests for methods required in each
problem. This will allow you to work on each problem separately.
3.2 Structure of the Stock
Class
Below is the structure the Stock.java
file which you should
create. Each problem corresponds to one method in the file though
later problems make use of earlier methods.
public class Stock{ public static void printInfo(Stock stock); // PROBLEM 1 // Print information on the given stock to the screen. The format should be // Microsoft Corporation (MSFT): price: $79.12 shares: 350 // ^^^ ^^^ ^^ ^^ // Name Symbol Price Number of shares // // Note that the price is preceded by a $ and has 2 decimal places // of accuracy. This makes printf() a good choice to produce the // output. public static double buyMore(Stock stock, int moreShares); // PROBLEM 2 // Buy more of the specified stock. The number of additional shares // to buy is the parameter moreShares. Modify stock.numShares to be // this amount more. Calculate the cost of the purchase which is the // // cost = -(moreShares * stock.price) // // Note that costs are negative while profits are positive. Return // the cost. public static double sellOff(Stock stock, int lessShares); // PROBLEM 2 // Sell off shares of the specified stock. The number of shares to // sell is the parameter lessShares. If the numShares field of stock // is smaller than lessShares, the transaction is error and return // 0.0 without changing anything about stock. Otherwise, reduce // stock.numShares by lessShares. Calculate the profit which is // // profit = stock.price * lessShares // // Return the profit. }
4 Problem 1: The Stock Class
A stock is an abstract financial concept that represents value in an existing company such as Google, Apple, IBM, or General Mills. If a person owns stock in a company, that person essentially owns part of the company which entitles them to potential control over the company and profits within it. Stock is often measured in shares and there are usually a large but finite number of shares available for a company. If a single person owns many shares of a company, that person can exert more control of the company in some contexts and stands to gain more or lose more based on the performance of the company.
Stocks have a price per share associated with them which fluctuates. When more people want to own the stock, the price usually increases while everyone selling the stock tends to decrease its value. The changes in price won't be the subject of this set of problems, however.
All stocks are associated with a specific company so the name of the company is an important attribute. When stocks are traded, a short alphabetic abbreviation called the ticker symbol is often used for it.
This problem centers around setting up a Java class to model the amount of stock owned by an individual person. Having a class represent the stock will allow multiple stocks with different names, prices, and shares owned to be tracked.
4.1 Create Fields
In Stock.java
create 4 public fields for the stock class.
name
for the name of the company. It is aString
price
for the price per share. It is adouble
numShares
for the number of shared owned. It is anint
symbol
for the ticker symbol of the stock. It is aString
Each field should be public
and non-static.
Successfully defining the fields and compiling the Stock
class
should allow the following kind of code to run.
Stock stock = new Stock(); stock.symbol = "GOOGL"; stock.price = 1000.62; stock.numShares = 500; stock.name = "Alphabet Inc Class A";
4.2 printInfo()
method
public static void printInfo(Stock stock)
Printing information on a stock is a basic operation that is done by
the printInfo()
method. It should produce output that looks like
the following
Welcome to DrJava. > Stock stock = new Stock(); > stock.symbol = "GOOGL"; > stock.price = 1000.62; > stock.numShares = 500; > stock.name = "Alphabet Inc Class A"; > Stock.printInfo(stock) Alphabet Inc Class A (GOOGL): price: $1000.62 shares: 500
The format of the message is broken down as follows.
> Stock.printInfo(stock) Alphabet Inc Class A (GOOGL): price: $1000.62 shares: 500 ^ ^ ^ ^ stock.name stock. symbol stock.price stock.numshares
printf()
is useful here to format the price
output to 2 decimal
digits of accuracy. Use format specifies appropriate to the types of
the other fields.
5 Problem 2: Buying and Selling Stocks
Buying and selling stocks is essential to managing them.. This problem defines two methods for carrying out these activities.
5.1 Buying more stock
public static double buyMore(Stock stock, int moreShares)
This method takes two parameters, a stock
to purchase and the number
of moreShares
desired. The method should change the numShares
field of stock
to be larger by moreShares
. It should then return
the total cost of the purchase which is the stock price multiplied by
moreShares
. Since this is a cost rather than a profit, the value
returned should be negative.
In the examples below, notice how both stocks have their own
numShares
which changes as buyMore()
is invoked on them.
Welcome to DrJava. > Stock stock = new Stock(); > stock.symbol = "GOOGL"; > stock.price = 1000.62; > stock.numShares = 500; > stock.name = "Alphabet Inc Class A"; > double cost = Stock.buyMore(stock, 1); > cost -1000.62 > stock.numShares 501 > Stock other = new Stock(); > other.symbol = "KMON"; > other.price = 25.00; > other.numShares = 100; > other.name = "Kauffmoney Inc"; > cost = Stock.buyMore(other, 10); > cost -250.0 > cost = Stock.buyMore(other, 10); > cost -250.0 > other.numShares 120 > Stock.printInfo(stock) Alphabet Inc Class A (GOOGL): price: $1000.62 shares: 501 > Stock.printInfo(other) Kauffmoney Inc (KMON): price: $25.00 shares: 120
5.2 Selling off Stock
public static double sellOff(Stock stock, int lessShares)
As some folks buy stock, others must sell it leading to the
sellOff()
operation. This method should alter the numShares
field
to decrease it by the argument lessShares
. The profit made from
selling the stock is returned which is the stock's price
multiplied
by the lessShares
sold.
There is one exceptional case. In some cases, stock traders mistype
orders which can lead to erroneous sales of too much stock at
once. One such case that is easy to check is to ensure that if
lessShares
is larger than the numShares
available for the given
stock
. In this case, ignore the request: do not change numShares
and return a profit of 0.0.
Examples are below:
Welcome to DrJava. > Stock stock = new Stock(); > stock.symbol = "GOOGL"; > stock.price = 1000.62; > stock.numShares = 500; > stock.name = "Alphabet Inc Class A"; > double profit = Stock.sellOff(stock, 1); > profit 1000.62 > stock.numShares 499 > profit = Stock.sellOff(stock, 99); > profit 99061.38 > stock.numShares 400 > Stock other = new Stock(); > other.symbol = "KMON"; > other.price = 25.00; > other.numShares = 100; > other.name = "Kauffmoney Inc"; > profit = Stock.sellOff(other, 50); > profit 1250.0 > other.numShares 50 // Attempt to sell of more stock than availble: no effect > profit = Stock.sellOff(other, 200); > profit 0.0 > other.numShares 50 > Stock.printInfo(stock) Alphabet Inc Class A (GOOGL): price: $1000.62 shares: 400 > Stock.printInfo(other) Kauffmoney Inc (KMON): price: $25.00 shares: 50
6 Problem 3: Ensure StockMain
Functions
If all fields and methods for the Stock
class are complete and
correct, the StockMain
class should be compilable and its main()
method runnable. The expected output for Problem3Tests
is below.
> javac Stock.java > javac StockMain.java > java StockMain Initial position: funds=$100000.00 Alphabet Inc Class A (GOOGL): price: $1000.62 shares: 500 Microsoft Corporation (MSFT): price: $79.12 shares: 350 Transaction 1 Buying 300 shares of MSFT: funds=$76264.00 Alphabet Inc Class A (GOOGL): price: $1000.62 shares: 500 Microsoft Corporation (MSFT): price: $79.12 shares: 650 Transaction 2 Selling 400 shares of GOOGL: funds=$476512.00 Alphabet Inc Class A (GOOGL): price: $1000.62 shares: 100 Microsoft Corporation (MSFT): price: $79.12 shares: 650 Transaction 3 Selling 200 shares of GOOGL: funds=$476512.00 Alphabet Inc Class A (GOOGL): price: $1000.62 shares: 100 Microsoft Corporation (MSFT): price: $79.12 shares: 650
7 Getting Credit for this Lab
7.1 Demonstrate your code to Lab Staff (40%)
You should feel free at any time to get help from lab staff as you get stuck on the exercises.
By the end of the lab, make sure to demonstrate your code to a staff member. This will ensure that you receive full credit on the lab. Staff might ask you to
- Change what is printed
- Compile and run on the command line
- Run tests in DrJava or on the command line
- Show how to zip the folder with your programs in it
Be ready to do any or all of these.
7.2 Zip and Submit (60%)
Ensure that file PARTNERS.txt
has your name and the name of your
partner in it. This fill is located with the other Java files for the
lab and can be edited with DrJava.
Once your are confident your code is working, you are ready to submit. Ensure your folder has all of the required files. Create a zip archive of your lab folder and submit it to blackboard.
On Canvas:
- Click on the Assignments section
- Click on the appropriate link for this lab
- Scroll down to "Attach a File"
- Click "Browse My Computer"
- Select you Zip file and press OK