Thursday, August 12, 2021

Design a class named StockTransaction that holds a stock symbol (typically one to four characters), stock name, and price per share.

Complete the following tasks:


a. Design a class named StockTransaction that holds a stock symbol (typically one to four characters), stock name, and price per share. Include methods to set and get values for each data field. Create a class diagram and write the pseudocode that defines the class.


b. Design an application that declares two StockTransaction objects and sets and displays their values.


c. Design an application that declares an array of 10 StockTransactions. Prompt the user for data for each of the objects, and then display all of the values.


Solution
Part A


public class StockTransaction{

   String stock_symbol;

   String stock_name;

   int price_per_share;

  

   private String getStock_symbol(){

       return stock_symbol;

   }


   private void setStock_symbol(String x){

       this.stock_symbol = x;

   }


   private String getStock_name(){

       return stock_name;

   }


   private void setStock_name(String y){

       this.stock_name = y;

   }


   private int getPrice_per_share(){

       return price_per_share;

   }


   private void setPrice_per_share(int z){

       this.price_per_share = z;

   }


}

Solution
Part B

public class StockTransaction{

   String stock_symbol;

   String stock_name;

   int price_per_share;

  

   private String getStock_symbol(){

       return stock_symbol;

   }


   private void setStock_symbol(String x){

       this.stock_symbol = x;

   }


   private String getStock_name(){

       return stock_name;

   }


   private void setStock_name(String y){

       this.stock_name = y;

   }


   private int getPrice_per_share(){

       return price_per_share;

   }


   private void setPrice_per_share(int z){

       this.price_per_share = z;

   }


  


public static void main(String[] args) {

       StockTransaction st1 = new StockTransaction();

       StockTransaction st2 = new StockTransaction();

       st1.setStock_symbol("GOOG");

       st1.setStock_name("Google");

       st1.setPrice_per_share(400);

       st2.setStock_symbol("FB");

       st2.setStock_name("Facebook");

       st2.setPrice_per_share(80);


       System.out.println("Stock 1 symbol = " + st1.getStock_symbol());

       System.out.println("Stock 1 name = " + st2.getStock_symbol());

       System.out.println("Stock 1 price oer share = " + st1.getStock_name());

       System.out.println("Stock 2 symbol = " + st2.getStock_name());

       System.out.println("Stock 3 name = " + st1.getPrice_per_share());

       System.out.println("Stock 2 price oer share = " + st2.getPrice_per_share());


   }   

}


Solution
Part C

import java.io.Console;
public class StockTransaction{
   String stock_symbol;
   String stock_name;
   int price_per_share;
  
   private String getStock_symbol(){
       return stock_symbol;
   }

   private void setStock_symbol(String x){
       this.stock_symbol = x;
   }

   private String getStock_name(){
       return stock_name;
   }

   private void setStock_name(String y){
       this.stock_name = y;
   }

   private int getPrice_per_share(){
       return price_per_share;
   }

   private void setPrice_per_share(int z){
       this.price_per_share = z;
   }
  

   public static void main(String[] args) {
       StockTransaction[] stArray = new StockTransaction[10];
       Console console = System.console();
       for(int i=0; i<10; i++){
           StockTransaction st = new StockTransaction();
           System.out.println("Enter Stock Symbol");
           st.setStock_symbol(console.readLine());

           System.out.println("Enter Stock Name");
           st.setStock_name(console.readLine());

           System.out.println("Enter price per share");
           st.setPrice_per_share(Integer.parseInt(console.readLine()));
          
           stArray[i] = st;

       }

       for(int j=0; j<10; j++){
           System.out.println("Stock Symbol "+ j + " = " + stArray[j].getStock_symbol());
           System.out.println("Stock Name "+ j + " = " + stArray[j].getStock_name());
           System.out.println("Price per share "+ j + " = " + stArray[j].getPrice_per_share());
       }
   }      
}

No comments:

Post a Comment