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

CIT 239 Java Programming Lab Exercise 9a Stock class Due Date You must demonstra

ID: 3809290 • Letter: C

Question

CIT 239 Java Programming Lab Exercise 9a Stock class Due Date You must demonstrate the solution to this lab exercise to the instructor by Sunday, March 12, 2017, in order to receive credit for this work. Stock Class This lab is adapted from Programming Exercise 9.2, on page 360 of the Liang textbook. Lab Exercise The Stock class: Following the examples of the CircleWithPrivateDataFields class in Section 9.9 and TestCircleWithPrivate DataFieldsjava (also in scction 9.9), design a class named Stock that conlains A string data field named symbol, for the stock's symbol A string data lield named name, for the stock'sname A double data field named previousClosingPrice that stores the stock price for the previous day A double data field named currentPrice that stores the stock price for the current time "Getter" and "Scttcr" methods for each of the data fields get Symbol0, setSymbol0, getName0, setName0, get PreviousClosingPriceo, setPreviousClasingPrice0, get CurrentPrice0, setCurrentPrice A constructor that creates a stock with a specified symbol and name A method named getChangePercent0 that returns the percentage changed from previousClosingPrice to currentPrice. Write a test program that prompus uhe user for a stock symbol. If the user enters "q", then the program should exit. If the user enters anything other than "q", then the program should assume that the input represents a stock symbol After a stock symbol is input, the program should prompt the user for the company name associated with thc stock symbol, and also the company's previous closing price and current price. The program should create an instance of the Stock class, and utilize the appropriate set methods to set the object's internal field values. After the object is created and its member variables are set, the program should call the getChangePercento method to determine how much the stock price has changed. When this processing is finished for one stock, the main loop should ask the user for another stock symbol, and repeat the process. The program exits when the user enters the string "q" in place of a stock symbol Your program should have two classes: a public class named Lab9a, and another (non-public) class named Stock. The Lab9a class contains amain method, which creates an object ofthe Stock class CIT239-SU Lab9a Stock 20170219 docx 2/18/2017 8:42 PM

Explanation / Answer

Stock.java

public class Stock {
   // Declaring variables
   private String symbol;
   private String name;
   private double previousClosingPrice;
   private double currentPrice;

   // Parameterized constructor
   public Stock(String symbol, String name) {
       super();
       this.symbol = symbol;
       this.name = name;
   }

   //Setters and getters
   public String getSymbol() {
       return symbol;
   }

   public void setSymbol(String symbol) {
       this.symbol = symbol;
   }

   public String getName() {
       return name;
   }

   public void setName(String name) {
       this.name = name;
   }

   public double getPreviousClosingPrice() {
       return previousClosingPrice;
   }

   public void setPreviousClosingPrice(double previousClosingPrice) {
       this.previousClosingPrice = previousClosingPrice;
   }

   public double getCurrentPrice() {
       return currentPrice;
   }

   // This method will find the change in percentage
   double getChangePercent() {
       return ((currentPrice - previousClosingPrice) / previousClosingPrice) * 100;
   }

   public void setCurrentPrice(double currentPrice) {
       this.currentPrice = currentPrice;
   }

   @Override
   public String toString() {
       return " symbol=" + symbol + ", name=" + name
               + ", previousClosingPrice=" + previousClosingPrice
               + ", currentPrice=" + currentPrice;
   }

}

_________________

Test.java

import java.util.Scanner;

public class Test {

   public static void main(String[] args) {
       //Declaring variables
       String stockSymbol,companyName;
       double previousClosingPrice,currentPrice;
  
       //Scanner object is used to get the inputs entered by the user
       Scanner sc=new Scanner(System.in);
      
       //Getting the company name entered by the user
       System.out.print("Enter stock Symbol (or 'q' to exit): ");
       stockSymbol=sc.next();
      
       //This loop continues to execute until the user enters 'q'
       while(!stockSymbol.equals("q"))
       {
           sc.nextLine();
          
           //getting the company name
           System.out.print("Enter the Company name :");
           companyName=sc.nextLine();
          
           //getting the previous closing price          
           System.out.print("Enter Previous closing price :");
           previousClosingPrice=sc.nextDouble();
          
           //getting the current price
           System.out.print("Enter Current price :");
           currentPrice=sc.nextDouble();
          
           //Creating the stock class object by passing the inputs as parameters
           Stock s=new Stock(stockSymbol,companyName);
          
           //Setting the prices
           s.setPreviousClosingPrice(previousClosingPrice);
           s.setCurrentPrice(currentPrice);
          
           //Displaying the result
           System.out.print(s.toString());
           System.out.printf(" Percent Change :%.2f ",s.getChangePercent());
          
           System.out.print(" Enter stock Symbol (or 'q' to exit): ");
           stockSymbol=sc.next();
  
       }
      

   }

}

__________________

Output:

Enter stock Symbol (or 'q' to exit): ORCL
Enter the Company name :Oracle Corporation
Enter Previous closing price :100
Enter Current price :90.50

symbol=ORCL, name=Oracle Corporation, previousClosingPrice=100.0, currentPrice=90.5 Percent Change :-9.50
Enter stock Symbol (or 'q' to exit): NFLX
Enter the Company name :Netflix Corporation
Enter Previous closing price :55.23
Enter Current price :58.04

symbol=NFLX, name=Netflix Corporation, previousClosingPrice=55.23, currentPrice=58.04 Percent Change :5.09
Enter stock Symbol (or 'q' to exit): q

_____________Thank You