Create two different stocks and two different preferred stocks. Use the setters
ID: 3568374 • Letter: C
Question
Create two different stocks and two different preferred stocks. Use the setters methods to put new prices in each stock and preferredstock. some prices going higher and some lower.Store them in a single arraylist that has the appropriate type to store only these types of classes.Display the contents of the list.(this is to test to see if the methods work properly) Make a loop to go through the array list and determine the toal value and total dividend if you had 100 shares of each stock and preferredstock.(for each stock and each preferredstock multiply stock with 100 and add the results to get the total value.foreach preferredstock multiply the dividend by 100 and add the results to get the total dividend. Use the instanceof operator to examine each items in the list to see which ones are preferredstock before computing the dividend value.You may need to do some casting. Get a COPY of an item from the list that is a preferredstock and change the Symbol of that cp[ied item to "ZZZZ'. Display the list and the copied item. Create a new preferredstock item that has the same name as one of the item in your list and use the ArrayList indexof method to display its posistion in the list.This will test the method .
This program does not interact with the use it is just used to test the mthods in the class. if one of my methods need's to be edited for it to work better/properly that's no problem.
public class stock {
class Stock {
private String symbol;
private String name;
private double previousClosingPrice;
private double currentPrice;
private double changeInPrice;
public Stock() {
}
public Stock(String symbol, String name) {
this.symbol = symbol;
this.name = name;
}
public String getSymbol() {
return symbol;
}
public String getName() {
return name;
}
public double getPreviousClosingPrice() {
return previousClosingPrice;
}
public double getCurrentPrice() {
return currentPrice;
}
public void setSymbol(String symbol) {
this.symbol = symbol;
}
public void setName(String name) {
this.name = name;
}
public void setPreviousClosingPrice(double price) {
this.previousClosingPrice = price;
}
public void setCurrentPrice(double price) {
this.currentPrice = price;
}
public void changePercent() {
changeInPrice = (currentPrice - previousClosingPrice) /previousClosingPrice;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Stock other = (Stock) obj;
if (!getOuterType().equals(other.getOuterType()))
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
private stock getOuterType() {
return stock.this;
}
public String toString() {
return "Stock [symbol=" + symbol + ", name=" + name
+ ", previousClosingPrice=" + previousClosingPrice
+ ", currentPrice=" + currentPrice + ", changeInPrice="
+ changeInPrice + "]";
}
public stock copy(){
return new stock();
}
}
class PreferredStock extends Stock
{
private double dividendPerShare;
public double getDividendPerShare()
{
return dividendPerShare;
}
public void setividendPerShare(double a)
{
dividendPerShare = a;
}
}
}
Explanation / Answer
SOLUTION:
import java.util.*;
public class Stock
{
private String name;
private double pricePerShare;
private double changePerShare;
public Stock(String name, double pricePerShare, double changePerShare)
{
this.name = name;
this.pricePerShare = pricePerShare;
this.changePerShare = changePerShare;
}
public Stock(Stock st)
{
name = st.name;
pricePerShare = st.pricePerShare;
changePerShare = st.changePerShare;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public double getPricePerShare()
{
return pricePerShare;
}
public void setPricePerShare(double pricePerShare)
{
this.pricePerShare = pricePerShare;
}
public double getChangePerShare()
{
return changePerShare;
}
public void setChangePerShare(double changePerShare)
{
this.changePerShare = changePerShare;
}
public boolean equals(Stock st)
{
if(st.name.equals(name))
{
return true;
}
return false;
}
public String toString()
{
String temp="";
temp+="name: "+name+" ";
temp+="price per share: "+pricePerShare+" ";
temp+="change per share : "+changePerShare+" ";
return temp;
}
}
class PreferredStock extends Stock
{
private double dividendPerShare;
public PreferredStock(String name, double pricePerShare,double changePerShare,double dividendPerShare)
{
super(name, pricePerShare, changePerShare);
// TODO Auto-generated constructor stub
this.dividendPerShare = dividendPerShare;
}
public String toString()
{
String temp=super.toString();
temp+="dividend per share: "+dividendPerShare;
return temp;
}
public double getDividendPerShare()
{
return this.dividendPerShare;
}
}
class Driver1
{
public static void main(String[] args)
{
// TODO Auto-generated method stub
ArrayList<Stock> list = new ArrayList<Stock>();
Stock st1=new Stock("Charan",23.898,4566.8);//Create New Stock Object
list.add(st1);//Add to ArrayList
Stock st2=new Stock("Karan",24.898,4966.8);//Create New Stock Object
list.add(st2);//Add to ArrayList
PreferredStock ps1=new PreferredStock("vishnu", 365.12, 2.1302, 32.0);//Create New PreferredStock Object
list.add(ps1);//Add to ArrayList
PreferredStock ps2=new PreferredStock("kishnu", 355.12, 1.1302, 52.0);//Create New PreferredStock Object
list.add(ps2);//Add to ArrayList
//Now Printing the Arraylist
for(Stock i :list)
System.out.println(i);
//Now proceeding to Calculate Stock Value & Dividend
double value=0,dividend=0;
for(Stock i :list)
{
// Calculate Value;
value+=((i.getPricePerShare()*100));//Adding to get value after multiplying by 100
if(i instanceof PreferredStock)//Checking if instance of PreferredStock
{
// Claculate Dividend for PreferedStock only;
PreferredStock ii = (PreferredStock)i;// Type Casting
dividend+=(ii.getDividendPerShare()*100);//Adding to get dividend after multiplying by 100
}
}
System.out.println();
System.out.println("Value:"+value);
System.out.println("Dividend:"+dividend);
System.out.println();
//Getting a COPY of PREFERRED STOCK & modifying it
for(Stock i :list)
{
if(i instanceof PreferredStock)//Checking if instance of PreferredStock
{
Stock copy = new Stock(i);//Note Here 'i' is an instance of PreferedStock
copy.setName("ZZZZ");//Changing Name
System.out.println();
System.out.println("PRINTING AFTER MODIFYING THE COPY");
System.out.println("Original PreferedStock:");
System.out.println(i);
System.out.println();
System.out.println("COPY Stock:");
System.out.println(copy);
System.out.println();
break;
}
}
/*Create a new PreferredStock itemthat has the same name as one of the items in your list and use the ArrayList indexOf method to display its position in the list. This will test your equals method*/
for(Stock i :list)
{
if(i instanceof PreferredStock)//Checking if instance of PreferredStock
{
PreferredStock copy2 = new PreferredStock("kishnu", 355.12, 5.1302, 72.0);//Same Name as One Previous PreferedStock
if(copy2.equals(i))
{// If Name Matches and they are Equal
System.out.println("Printing Index of Matched Name:");
System.out.println(list.indexOf(i));
}
}
}
}
}
please comment for any doubts
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.