Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

stock class/ i don\'t know what to do please help . String data field named symb

ID: 665300 • Letter: S

Question

stock class/ i don't know what to do please help

. String data field named symbol for the stock's3-charater symbol, a string data field named name stock's name/A double data field named prevoiusColsingPrice that stores the stores stock price for the previous day./ double data field named closingPrice that stores the stock price for the current time/ A constructor method that creates a stock with the specified symbol and name/ A method named getChangePercent() that returns the percentage changed from the previousClosingPrice to closingPrice

Implement the class and write a test program that creates a Stock object with the symbol OCL, the name Oracle Corporation and previous closing price of 55.5. Create another Stock object with the symbol MSF, the name Microsoft Corporation and previous closing price of 52.9

The test program will take the stocks through a 5-day week, calculating the percentage change and copying the closingPrice to the previousClosingPrice for each day.

"Loop" Display the weekday(Monday-Friday)/ prompt the user to enter the closing price for today for each stock on your "exchange".Be sure to validate your input./Calculate the percentage from the previous day's closing price for each stock on your "exchange"/ for each stock display the symbol, the previousClosingPrice, the closingPrice and the percentage,the closingPrice and the percentageChange. These four values will be on a single line of output/ copy the closingPrice to the previousClosingPrice for each stock in preparation for the next day's "action"

Explanation / Answer

/*The stock class contains the constructor that sets then symbol
* and name. The class have methods for accessors and mutators
* functions to set symbol, name, previouse closing price and
* closing price . The class also contains method to calculate
* the percent change.
* */
//Stock.java
public class Stock
{
   private String symbol;
   private String name;
   private double previousClosingPrice;
   private double closingPrice;

   //constructor of the Stock class
   public Stock(String symbol,String name)
   {
       this.symbol=symbol;
       this.name=name;
   }
  
  
   //Set symbol
   public void setSymbol(String symbol)
   {
       this.symbol=symbol;
   }
   //Returns symbol of string type
   public String getSymbol()
   {
       return symbol;
   }
  
   //set Name
   public String getName()
   {
       return name;
   }
  
   //set previous price
   public void setPreviousClosingPrice(double previousClosingPrice)
   {
       this.previousClosingPrice=previousClosingPrice;
   }
   //Returns previous price
   public double getPreviousClosingPrice()
   {
       return previousClosingPrice;
   }
  
   //set closing price
   public void setClosingPrice(double closingPrice)
   {
       this.closingPrice=closingPrice;
   }
  
   //Returns closing price
   public double getClosingPrice()
   {
       return closingPrice;
   }
  
  
   //calculate the percent change
   public double getChangePercent()
   {
       return (closingPrice-previousClosingPrice)/previousClosingPrice;
   }
  
   //Override the toString method that returns the string representaion
   //of Stock object
   @Override
   public String toString()
   {      
       return "Name : "+name+"Symbol : "+symbol+"Previous price : "+
               previousClosingPrice+"Current Price : "+closingPrice;
              
   }
  
  
}//end of the Stock class


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

/**The java driver program that creates two objects of Stock class
* and prompts closing price for each day . Then calculates
* the percent change and display symbol, name, previous closing price,
* closing price ,percentage change .
* Then set closing price to previous price for all week days for both
* the objects
* */
//StockDriver.java
import java.util.Scanner;
public class StockDriver
{
   public static void main(String[] args)
   {
       //create a Scanner class object
       Scanner scanner=new Scanner(System.in);
       double closingPrice;
       //string array for week day names
       String weekDays[]={"Monday","Tuesday","Wednesday",
               "Thursday","Friday"};


       //for stock1 object
       //Create an object of Stock class with symbol and name
       Stock stock1=new Stock("OCL", "Oracle Corporation");
       stock1.setPreviousClosingPrice(55.5);
       //Loop through the week days and prompt for closing price for each day
       for (int day = 0; day < weekDays.length; day++)
       {          

           //Input validataion :accept only positive closing price
           do
           {
               System.out.println("Enter closing price of "+weekDays[day]);
               closingPrice=scanner.nextDouble();

               if(closingPrice<0)
                   System.out.println("Enter valid closing price.");

           }while(closingPrice<0);

           stock1.setClosingPrice(closingPrice);

           //print name, symbool, previous closing price, closing price
           //change percent
           System.out.printf("%-20s%-10s%-15.2f%-15.2f%-15.2f ",
                   stock1.getName(),
                   stock1.getSymbol(),
                   stock1.getPreviousClosingPrice(),
                   stock1.getClosingPrice(),
                   stock1.getChangePercent());

           //copy the closing price to the previousclosig pirce
           stock1.setPreviousClosingPrice(stock1.getClosingPrice());
       }


       //For stock2 object.
       Stock stock2=new Stock("MSF", "Microsoft Corporation");
       stock2.setPreviousClosingPrice(52.9);

      
       //Loop through the week days and prompt for closing price for each day
       for (int day = 0; day < weekDays.length; day++)
       {          

           //Input validataion :accept only positive closing price
           do
           {
               System.out.println("Enter closing price of "+weekDays[day]);
               closingPrice=scanner.nextDouble();

               if(closingPrice<0)
                   System.out.println("Enter valid closing price.");

           }while(closingPrice<0);

           stock2.setClosingPrice(closingPrice);

           //print name, symbool, previous closing price, closing price
           //change percent
           System.out.printf("%-20s%-10s%-15.2f%-15.2f%-15.2f ",
                   stock2.getName(),
                   stock2.getSymbol(),
                   stock2.getPreviousClosingPrice(),
                   stock2.getClosingPrice(),
                   stock2.getChangePercent());

           //copy the closing price to the previousclosig pirce
           stock2.setPreviousClosingPrice(stock2.getClosingPrice());
       }

   }
}


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

Sample output:

Enter closing price of Monday
58.9
Oracle Corporation OCL       55.50          58.90          0.06         
Enter closing price of Tuesday
60.2
Oracle Corporation OCL       58.90          60.20          0.02         
Enter closing price of Wednesday
70.5
Oracle Corporation OCL       60.20          70.50          0.17         
Enter closing price of Thursday
72.6
Oracle Corporation OCL       70.50          72.60          0.03         
Enter closing price of Friday
74.6
Oracle Corporation OCL       72.60          74.60          0.03         
Enter closing price of Monday
54.2
Microsoft CorporationMSF       52.90          54.20          0.02         
Enter closing price of Tuesday
56.3
Microsoft CorporationMSF       54.20          56.30          0.04         
Enter closing price of Wednesday
58.9
Microsoft CorporationMSF       56.30          58.90          0.05         
Enter closing price of Thursday
60.2
Microsoft CorporationMSF       58.90          60.20          0.02         
Enter closing price of Friday
70.6
Microsoft CorporationMSF       60.20          70.60          0.17         

Hope this helps you