Implement a Stock class that has the following attributes: symbol (String), pric
ID: 3783044 • Letter: I
Question
Implement a Stock class that has the following attributes: symbol (String), price (double), number of shares (int) and the following methods: Constructor (that sets the symbol, price and number of shares to user defined values). Get and set methods. toString method returns the symbol, price and number of shares method public int compareTo(Stock s) compares the share price of this stock with another stock and returns: -1 if the value of this stock is lower than the other stock. 1 if the value of this stock is higher than the other stock. 0 if both the values are the same. (Value of the stock = price * number of shares) For example, let’s say IBM has a price of $105.23 and you have bought 45 shares and MOS has a price of $89.88 and you have bought 60 shares. Then the value of IBM in your portfolio is 105.23*45= $4735.35 and the value of MOS in your portfolio is 89.88*60 = $5392.80. Comparing this stock(IBM) with another stock(MOS) will return a -1 since the value of MOS is greater in your portfolio. Test the Stock class with a client program that asks the user to enter the symbols, share prices, and number of shares for two stocks, prints their values, determines which stock is higher than the other and by how much and prints the total value. You must use the methods in the Stock class wherever appropriate. A portion of the client program is given below for your programming convenience.
Exercise 3: Implement a Stock class that has the following attributes symbol (String), price (double), number of shares int) and the following methods Constructor (that sets the symbol, price and number of shares to user defined values) Get and set methods. toString method returns the symbol, price and number of shares method public int compareTo (Stock s) compares the share price of this stock with another stock and returns: -1 if the value of this stock is lower than the other stock. l if the value of this stock is higher than the other stock 0 if both the values are the same. alue of the stock -price number of shares For example, let's say IBM has a price of $105.23 and you have bought 45 shares and MOS has a price of $89.88 and you have bought 60 shares. Then the value of IBM in your portfolio is 105.23*45 $4735.35 and the value of MOS in your portfolio is 89.88*60 $5392.80. Comparing this stock(IBM) with another stock(MOS) will return a -1 since the value of MOS is greater in your portfolio Test the Stock class with a client program that asks the user to enter the symbols, share prices, and number of shares for two stocks, prints their values, determines which stock is higher than the other and by how much and prints the total value. You must use the methods in the Stock class wherever appropriate A portion of the client program is given below for your programming convenience. import ava util. Scanner public class StockDemo public static void main (String args) Scanner keyboard. new Scanner (System in) String syml sym2 i double prcl prc2Explanation / Answer
import java.util.Scanner;
class Stock
{
private String symbol; //class variables
private double price;
int shares;
public Stock(String symbol,double price,int shares) //parameterized constructor
{
this.symbol = symbol;
this.price = price;
this.shares = shares;
}
public String getSymbol() //get and set methods for symbol.price and number of shares
{
return symbol;
}
public void setSymbol(String symbol)
{
this.symbol = symbol;
}
public double getPrice()
{
return price;
}
public void setPrice(double price)
{
this.price = price;
}
public int getShares()
{
return shares;
}
public void setShares(int shares)
{
this.shares = shares;
}
public int compareTo(Stock s) //method to compare two stock values
{
if(this.price * this.shares > s.price * s.shares)
return 1;
else if(this.price * this.shares < s.price * s.shares)
return -1;
else
return 0;
}
public String toString() //overriding toString() method
{
return " Stock :" + getSymbol() +" Price :"+getPrice() +" Shares :"+getShares();
}
}
class StockDemo
{
public static void main (String[] args)
{
Scanner keyboard = new Scanner(System.in);
String sym1,sym2;
double prc1,prc2;
int sh1,sh2;
//get the values for the two stocks
System.out.print(" Enter the symbols for the two stocks: ");
sym1 = keyboard.next();
sym2 = keyboard.next();
System.out.print(" Enter their prices: ");
prc1 = keyboard.nextDouble();
prc2 = keyboard.nextDouble();
System.out.print(" Enter the number of shares for the two stocks: ");
sh1 = keyboard.nextInt();
sh2 = keyboard.nextInt();
//create the first stock
Stock s1 = new Stock(sym1,prc1,sh1);
//create the first stock
Stock s2 = new Stock(sym2,prc2,sh2);
System.out.println(" I have the following stocks");
System.out.println(" "+s1.toString());
System.out.println(" "+s2.toString());
if(s1.compareTo(s2) == 1)
{
System.out.println(" The value of " + s1.getSymbol() +" is higher than "+s2.getSymbol());
System.out.println(" The total value of my portfolio is $"+s1.getPrice()*s1.getShares());
}
else if(s1.compareTo(s2) == -1)
{
System.out.println(" The value of " + s2.getSymbol() +" is higher than "+s1.getSymbol());
System.out.println(" The total value of my portfolio is $"+s2.getPrice()*s2.getShares());
}
else
System.out.println(" The value of " + s1.getSymbol() +" is equal to "+s2.getSymbol());
}
}
output:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.