Question
To do in Java: Look at Stock and StockDriver . Get these programs to run. We will be keeping track of both our cost and
To do in Java: Look at Stock and StockDriver. Get these programs to run. We will be keeping track of both our cost and the fair market value of our shares of stock. There is sample output for this program at the bottom of this document.
Stock:
import java.text.*;
public class Stock {
// this line is used to output money but this is NOT considered a field
NumberFormat money = NumberFormat.getCurrencyInstance();
// create five fields - a String called stockName, a integer called custID
// a integer called numShares, a double named costPaid,and a double called currentValue
// Do not forget the access modifier
// create an empty and full constructor
public String toString() {
return custID + " owns " + numShares + " shares of " + stockName
+ " worth " + money.format(currentValue) + " per share. You paid "+
money.format(costPaid) + " per share.";
}
// write a calcCost method that takes no parameters and returns a double
// Have it return the total cost you paid for the stock
// write a method named calcCurrentValue that takes no parameters and returns a double
// that is the current value of the stock.
// write your getters and setters. You should at least once try to create
// these by hand so you understand them
// (even though Eclipse will write these for you)
// (DO NOT create one for money - it really is not a field)
}
StockDriver:
import java.text.*;
import java.util.*;
public class StockDriver {
public static void main(String[] args) {
boolean more = true;
Scanner scan = new Scanner(System.in);
NumberFormat money = NumberFormat.getCurrencyInstance();
while (more)
{
/* NOTE : in the next four comments, you will need two lines per comment
one for the prompt and one for the answer. Make certain your choice
of data types for each is the same as the associated field in Stock*/
// ask for the Customer ID and store the answer in the variable named custNum
// ask for the name of the stock and store the answer in the variable named type
// ask for the number of shares and store the answer in the variable named num
// ask for how much was paid per share and store the answer in the variable named paid
// ask for the current value per share and store the answer in the variable named current
// create a Stock instance named stock by calling the full constructor
// call the calcCost method on the stock instance (-- NOTE - this is in the Stock class!!)
// and store the answer in the double variable grossAmt
// call the calcCurrentValue method on the stock instance and store the answer in the double
// variable named currentAmt
// print out the toString() method on the stock instance
System.out.println("The total cost of the stock is " + money.format(grossAmt) +
" and the current value is " + money.format(currentAmt));
System.out.println ("More stocks to calculate? (true or false)");
more = scan.nextBoolean();
}
}
}
StockDriver sample Output:
Customer id: 1234
Name of stock: IBM
Number of shares: 200
Price paid per share: 60.00
Current value per share: 65.00
1234 owns 200 shares of IBM worth $65.00 per share. You paid $60.00 per share.
The total cost of the stock is $12,000.00 and the current value is $13,000.00
More stocks to calculate? (true or false)
true
Customer id: 3434
Name of stock: GOOG
Number of shares: 120
Price paid per share: 45.00
Current value per share: 120.00
3434 owns 120 shares of GOOG worth $120.00 per share. You paid $45.00 per share.
The total cost of the stock is $5,400.00 and the current value is $14,400.00
More stocks to calculate? (true or false)
false
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started