Write the tester class that demonstrates how the Stock class

Write the tester class that demonstrates how the Stock class works by creating a stock object with a given string symbol and initial price, sets a current price for the stock, create a copy of the stock object and outputs whether the stock has increased or decreased in value and the percentage change.

Solution

class Stock{

public String symbol;
public Double initialPrice;
public Double currentPrice;
   public Stock(){
   }
   public Stock(String symbol,Double initialPrice){
   this.symbol=symbol;
   this.initialPrice=initialPrice;
   }
   Stock(Stock S){
   symbol=S.symbol;
   initialPrice=S.initialPrice;
   }
   public void setSymbol(String symbol) {
   this.symbol = symbol;
   }
   public String getSymbol() {
   return symbol;
   }
   public void setInitialPrice(double initialPrice){
   this.initialPrice=initialPrice;
   }
   public double getInitialPrice(){
   return initialPrice;
   }
   public void setCurrentPrice(double currentPrice){
   this.currentPrice=currentPrice;
   }
   public double getCurrentPrice(){
   return currentPrice;
   }
   void display(){
   System.out.println(symbol+\" \"+initialPrice);
   }
   void increasedecrease(){
if(currentPrice>initialPrice){
System.out.println(\"Stock value increases\");
}else if(currentPrice==initialPrice){
System.out.println(\"stock value remain same\");
}else{
System.out.println(\"stock value decreases\");
}   
   }
void percentageChange(){
   double perchg=((currentPrice-initialPrice)/initialPrice)*100;
System.out.println(\"Percentage change is:\"+perchg);
}

}
public class Tester{
public static void main(String args[]){
Stock st=new Stock(\"MCGRAW\",250.50);//st is stock object,here MCGRAW is symbol and 250.50 is initial price
System.out.println(\"Intial symbol and price of stock:\");
st.display();
st.setCurrentPrice(350);//Here sets current price,350 is current price
System.out.println(\"Stock current value is:\"+st.getCurrentPrice());
Stock st1=new Stock(st);//st1 is a copy of stock object
System.out.println(\"Value of the copy object:\");
st1.display();
st.increasedecrease();//test stock value increase or decrease
st.percentageChange();//finding out percentage change
}
}

 Write the tester class that demonstrates how the Stock class works by creating a stock object with a given string symbol and initial price, sets a current pric

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site