Question

In: Computer Science

Look at Stock and StockDriver. Get these programs to run. We will be keeping track 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.

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
   public String stockName;
   public int custID;
   public int numShares;
   public double costPaid;
   public double currentValue;
       // create an empty and full constructor
  
   public Stock()
   {
   }
  
   public Stock(String s, int c, int n, double co, double cv)
   {
       stockName = s;
       custID = c;
       numShares = n;
       costPaid = co;
       currentValue = cv;
   }
   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)
}

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

      
   }
         
}

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

Solutions

Expert Solution

Stock.java

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
public String stockName;
public int custID;
public int numShares;
public double costPaid;
public double currentValue;
// create an empty and full constructor

public Stock()
{
}

public Stock(String s, int c, int n, double co, double cv)
{
stockName = s;
custID = c;
numShares = n;
costPaid = co;
currentValue = cv;
}
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(){
return costPaid * numShares;
}

// write a method named calcCurrentValue that takes no parameters and returns a double
// that is the current value of the stock.
public double calcCurrentValue(){
return currentValue * numShares;
}

// 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(){

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
public String stockName;
public int custID;
public int numShares;
public double costPaid;
public double currentValue;
// create an empty and full constructor

public Stock()
{
}

public Stock(String s, int c, int n, double co, double cv)
{
stockName = s;
custID = c;
numShares = n;
costPaid = co;
currentValue = cv;
}
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(){
return costPaid * numShares;
}

// write a method named calcCurrentValue that takes no parameters and returns a double
// that is the current value of the stock.
public double calcCurrentValue(){
return currentValue * numShares;
}

// 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 int getCustID(){
return custID;
}

public int getNumShares(){
return numShares;
}

public double getCostPaid(){
return costPaid;
}

public double getCurrentValue(){
return currentValue;
}


public void setStockName(String stockName){
this.stockName = stockName;
}

public void setCustID(int custID){
this.custID = custID;
}

public void setNumShares(int numShares){
this.numShares = numShares;
}

public void setCostPaid(double costPaid){
this.costPaid = costPaid;
}

public void setCurrentValue(double currentValue){
this.currentValue = currentValue;
}

}

return stockName;
}

public int getCustID(){
return custID;
}

public int getNumShares(){
return numShares;
}

public double getCostPaid(){
return costPaid;
}

public double getCurrentValue(){
return currentValue;
}


public void setStockName(String stockName){
this.stockName = stockName;
}

public void setCustID(int custID){
this.custID = custID;
}

public void setNumShares(int numShares){
this.numShares = numShares;
}

public void setCostPaid(double costPaid){
this.costPaid = costPaid;
}

public void setCurrentValue(double currentValue){
this.currentValue = currentValue;
}

}

StockDriver.java

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
System.out.print("Customer id: ");
int custID = scan.nextInt();
//extra scan.nextLine() added to read the remaining line of the cutomer ID.
scan.nextLine();
// ask for the name of the stock and store the answer in the variable named type
System.out.print("Name of stock: ");
String stockName = scan.nextLine();
// ask for the number of shares and store the answer in the variable named num
System.out.print("Number of shares: ");
int numShares = scan.nextInt();
// ask for how much was paid per share and store the answer in the variable named paid
System.out.print("Price paid per share: ");
double costPaid = scan.nextDouble();
// ask for the current value per share and store the answer in the variable named current
System.out.print("Current value per share: ");
double currentValue = scan.nextDouble();


// create a Stock instance named stock by calling the full constructor
Stock stock = new Stock(stockName, custID, numShares, costPaid, currentValue);
// call the calcCost method on the stock instance (-- NOTE - this is in the Stock class!!)
// and store the answer in the double variable grossAmt
double grossAmt = stock.calcCost();
// call the calcCurrentValue method on the stock instance and store the answer in the double
// variable named currentAmt
double currentAmt = stock.calcCurrentValue();


// print out the toString() method on the stock instance
System.out.println(stock);

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

}


}

}

-----------------------------------------------------------------------------------------------------------------------

The above is the complete code to the problem. It consists of two files Stock.java and StockDriver.java which should be kept int the same directory.

Output of the above code:

note: In my case it is showing currency as rupees because I am from India. However it will show the currency according to the region\country where you live.

-----------------------------------------------------------------------------------------------------

Hope this helps. If you like the answer, please upvote. Cheers!!


Related Solutions

The concept of GDP is used by policy makers for manypurposes that include keeping track...
The concept of GDP is used by policy makers for many purposes that include keeping track of the growth of our economy and forecasting a recession; but it is also abused by some. Discuss some of the applications of GDP which may be considered improper use or abuse of the concept."Even though the Great Recession officially ended, the unemployment rate is still considered high." Discuss.Discuss how the distribution of income among various groups of income earners have changed in this...
The basic document for keeping track of costs in a job order costing system is a...
The basic document for keeping track of costs in a job order costing system is a a. process cost report. b. job order cost card. c. labor time card. d. materials requisition form. Which of the following products probably would be manufactured using a job order costing system? a. Computer monitors b. Baseball bats c. Company business cards d. Paper Which of the following is not a characteristic of a process costing system? a. Manufacturing costs are grouped by processes,...
There are several methods used by people to organize their lives in terms of keeping track...
There are several methods used by people to organize their lives in terms of keeping track of appointments, meetings, and deadlines. Some of these include using a desk calendar, using informal notes of scrap paper, keeping them “in your head,” using a day planner, and keeping a formal “to do” list. Suppose a business researcher wants to test the hypothesis that a greater proportion of marketing managers keep track of such obligations “in their head” than do accountants. To test...
Government Incentive Programs There are other programs offered by the government that look to incentivize healthcare...
Government Incentive Programs There are other programs offered by the government that look to incentivize healthcare providers through demonstrating the delivery of quality healthcare and a commitment to quality patient outcomes. To encourage participation, incentives programs which, support quality outcomes, have been established. Instructions: Identify and describe the four incentive programs used to encourage providers to meet quality healthcare expectations. Which incentive program do you feel offers the most value in terms of monetary and quality return. Support your selection...
Smith is a weld inspector at a shipyard. He knows from keeping track of good and...
Smith is a weld inspector at a shipyard. He knows from keeping track of good and substandard welds that for the afternoon shift 5% of all welds done will be substandard. If Smith checks 300 welds completed that shift, what is the probability that he will find more than 25 substandard welds?
Smith is a weld inspector at a shipyard. He knows from keeping track of good and...
Smith is a weld inspector at a shipyard. He knows from keeping track of good and substandard welds that for the afternoon shift 5% of all welds done will be substandard. If Smith checks 300 of the 7500 welds completed that shift, what is the probability that he will find less than 20 substandard welds? 0.9066 0.0934 0.4066 0.5934
Compare telehealth in Ontario to Successful Telehealth Programs? -look at a couple of comparable telehealth programs...
Compare telehealth in Ontario to Successful Telehealth Programs? -look at a couple of comparable telehealth programs that are begin shown to be successful -what did changes they implement to make it work for users
4)Smith is a weld inspector at a shipyard. He knows from keeping track of good and...
4)Smith is a weld inspector at a shipyard. He knows from keeping track of good and substandard welds that for the afternoon shift 5% of all welds done will be substandard. If Smith checks 300 of the 7500 welds completed that shift, what is the probability that he will find between 10 and 20substandard welds? A)0.2033 B)0.6377 C)0.8132 D)0.4066
Oxidation states are important for keeping track of electrons in oxidation-reduction reactions. Here are some general...
Oxidation states are important for keeping track of electrons in oxidation-reduction reactions. Here are some general rules to remember: In most cases, oxygen has an oxidation state of −2. Group 1 and group 2 elements on the periodic table have +1 and +2 oxidation states, respectively. In most cases, hydrogen has an oxidation state of +1. Many elements can have more than one oxidation state. In such cases, use the other elements in the compound whose oxidation states are known...
Using a regression to estimate beta, we run a regression with returns on the stock in...
Using a regression to estimate beta, we run a regression with returns on the stock in question plotted on the Y axis and returns on the market portfolio plotted on the X axis. The intercept of the regression line, which measures relative volatility, is defined as the stock’s beta coefficient, or b. True. FALSE A 10-year corporate bond has an annual coupon payment of 2.8%. The bond is currently selling at par ($1,000). Which of the following statement is NOT...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT