Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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. I am having trouble with calling the constructor and methods

private String stockName; private int custID; private int numShares; private double costPaid; private double currentValue; // create an empty and full constructor public Stock() { } public Stock(String s, int c, int n, double p, double v) { stockName = s; custID = c; numShares = n; costPaid = p; currentValue = v; } 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 public double calcCost(){ double grossAmt; grossAmt = numShares * costPaid; return grossAmt; } // write a method named calcCurrentValue that takes no parameters and returns a double // that is the current value of the stock. public double calcCurrentValue(){ double currentAmt; currentAmt = numShares * currentValue; return currentAmt; } // 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) public String getStockName() { return stockName; } public void setStockName(String stockName) { this.stockName = stockName; } public int getCustID() { return custID; } public void setCustID(int custID) { this.custID = custID; } public int getNumShares() { return numShares; } public void setNumShares(int numShares) { this.numShares = numShares; } public double getCostPaid() { return costPaid; } public void setCostPaid(double costPaid) { this.costPaid = costPaid; } public double getCurrentValue() { return currentValue; } public void setCurrentValue(double currentValue) { this.currentValue = currentValue; } }

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 System.out.println("What is the customer's id?"); int custNum = scan.nextInt(); scan.nextLine(); // ask for the name of the stock and store the answer in the variable named type System.out.println("What is the name of the stock?"); String type = scan.nextLine(); // ask for the number of shares and store the answer in the variable named num System.out.println("How many shares did you purchase?"); int num = scan.nextInt(); scan.nextLine(); // ask for how much was paid per share and store the answer in the variable named paid System.out.println("What was the cost of each share?"); double paid = scan.nextDouble(); scan.nextLine(); // ask for the current value per share and store the answer in the variable named current System.out.println("What is the current value of each share?"); double current = scan.nextDouble(); This is the part i need help with

// 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(); } } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions