Question

Design a class named StockTransaction that holds a stock symbol (typically one to four characters), stock...

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 the values for each data field.

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

Design an application that declares an array of 15 StockTransaction objects. Prompt the user for data for each object, and then display all the values.

Design an application that declares an array of 15 StockTransaction objects. Prompt the user for data for each object, and then pass the array to a method that determines and displays the two stocks with the highest and lowest price per share.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here is the completed code for this problem. I have attached StockTransaction.java and three applications named Driver1.java, Driver2.java and Driver3.java. Please copy and paste these files SEPARATELY. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks

// StockTransaction.java

public class StockTransaction {

    // fields

    private String stockSymbol;

    private String stockName;

    private double price_per_share;

    // constructor taking values for all fields

    public StockTransaction(String stockSymbol, String stockName,

             double price_per_share) {

         this.stockSymbol = stockSymbol;

         this.stockName = stockName;

         this.price_per_share = price_per_share;

    }

    // getters and setters

    public String getStockName() {

         return stockName;

    }

    public String getStockSymbol() {

         return stockSymbol;

    }

    public double getPrice_per_share() {

         return price_per_share;

    }

    public void setStockName(String stockName) {

         this.stockName = stockName;

    }

    public void setStockSymbol(String stockSymbol) {

         this.stockSymbol = stockSymbol;

    }

    public void setPrice_per_share(double price_per_share) {

         this.price_per_share = price_per_share;

    }

    // returns a String that will be printed when this object is printed

    public String toString() {

         return "stock symbol=" + stockSymbol + ", stock name=" + stockName

                 + ", price per share=" + price_per_share;

    }

}

// Driver1.java

public class Driver1 {

    public static void main(String[] args) {

         // creating two StockTransaction objects and displaying them

         StockTransaction stock1 = new StockTransaction("GOOG", "Google", 200);

         StockTransaction stock2 = new StockTransaction("NYSE",

                 "New York Stock Exchange", 120);

         System.out.println(stock1); // toSring() method will be invoked

         System.out.println(stock2);

    }

}

// Driver2.java

import java.util.Scanner;

public class Driver2 {

    public static void main(String[] args) {

         // creating an array of 15 StockTransaction objects

         StockTransaction stocks[] = new StockTransaction[15];

         // scanner to read user input

         Scanner scanner = new Scanner(System.in);

         // looping and reading input for all 15 stock objects and creating &

         // adding object to the array

         for (int i = 0; i < stocks.length; i++) {

             System.out.print("Enter stock symbol for stock #" + (i + 1) + ": ");

             String symbol = scanner.nextLine();

             System.out.print("Enter stock name: ");

             String name = scanner.nextLine();

             System.out.print("Enter stock price per share: ");

             double price = Double.parseDouble(scanner.nextLine());

             stocks[i] = new StockTransaction(symbol, name, price);

         }

        

         //displaying all transactions created

         System.out.println("\nStock Transactions:");

         for (StockTransaction stk : stocks) {

             System.out.println(stk);

         }

    }

}

// Driver3.java

import java.util.Scanner;

public class Driver3 {

    // method to find and display the two stocks with highest and lowest price

    // per shares from an array of StockTransactions, assuming the array has no

    // null elements

    static void displayStocksHighestLowest(StockTransaction[] stocks) {

         // initializing both highest and lowest to null

         StockTransaction highest = null, lowest = null;

         // looping through the array

         for (int i = 0; i < stocks.length; i++) {

             // if this is the first element, setting it as both highest and

             // lowest

             if (i == 0) {

                 highest = stocks[i];

                 lowest = stocks[i];

             } else {

                 // if current stock's price is bigger than highest, updating

                 // highest, if it is lower than lowest, setting it as lowest

                 if (stocks[i].getPrice_per_share() > highest

                          .getPrice_per_share()) {

                      highest = stocks[i];

                 }

                 if (stocks[i].getPrice_per_share() < lowest

                          .getPrice_per_share()) {

                      lowest = stocks[i];

                 }

             }

         }

         // displaying both if they are not null

         if (highest != null) {

             System.out

                      .println("Stock with highest price per share: " + highest);

             System.out.println("Stock with lowest price per share: " + lowest);

         }

    }

    public static void main(String[] args) {

         // creating an array of 15 StockTransaction objects

         StockTransaction stocks[] = new StockTransaction[15];

         // scanner to read user input

         Scanner scanner = new Scanner(System.in);

         // looping and reading input for all 15 stock objects and creating &

         // adding object to the array

         for (int i = 0; i < stocks.length; i++) {

             System.out.print("Enter stock symbol for stock #" + (i + 1) + ": ");

             String symbol = scanner.nextLine();

             System.out.print("Enter stock name: ");

             String name = scanner.nextLine();

             System.out.print("Enter stock price per share: ");

             double price = Double.parseDouble(scanner.nextLine());

             stocks[i] = new StockTransaction(symbol, name, price);

         }

         // displaying two stocks with highest and lowest price per shares

         displayStocksHighestLowest(stocks);

    }

}

Add a comment
Know the answer?
Add Answer to:
Design a class named StockTransaction that holds a stock symbol (typically one to four characters), stock...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Design an application that declares an array of 20 AutomobileLoan objects. Prompt the user for data...

    Design an application that declares an array of 20 AutomobileLoan objects. Prompt the user for data for each object, and then pass the array to a method that determines the sum of the balances.

  • a. Create a class named Lease with fields that hold an apartment tenant's name, apartment Number,...

    a. Create a class named Lease with fields that hold an apartment tenant's name, apartment Number, monthly rent amount, and term of the lease in months. Include a constructor that initializes the name. "XXX", the apartment number to O, the rent to 1000, and the term to 12. Also include methods to get and set each of the fields. Include a nonstatic method named addPetFee() that adds $10 to the monthly rent value and calls a static method named explainPetPolicy...

  • C++ Program 1. Create a class named BaseballGame that has fields for two team names and...

    C++ Program 1. Create a class named BaseballGame that has fields for two team names and a final score for each team. Include methods to set and get the values for each data field. Your application declares an array of 12 BaseballGame objects. Prompt the user for data for each object, and display all the values. Then pass each object to a method that displays the name of the winning team or “Tie” if the score is a tie. (main.cpp,...

  • C++ Visul Studio Create a class named Vehicle. The class has the following five member variables:...

    C++ Visul Studio Create a class named Vehicle. The class has the following five member variables: • Vehicle Name • Vehicle number • Sale Tax • Unit price • Total price Include set (mutator) and get (accessor) functions for each field except the total price field. The set function prompt the user for values for each field. This class also needs a function named computePrice() to compute the total price (quantity times unit price + salesTax) and a function to...

  • Design a class named AutomobileLoan that holds a loan number, make and model of automobile, and...

    Design a class named AutomobileLoan that holds a loan number, make and model of automobile, and balance. Include methods to set values for each data field and a method that displays all the loan information. -Complete the flowchart and pseudocode that defines the class. Make a working version of this program in Python.

  • C# Create an application named JobDemo that declares and uses Job objects. The Job class holds...

    C# Create an application named JobDemo that declares and uses Job objects. The Job class holds job information for a home repair service. The class has five properties that include: JobNumber - The job number Customer - The customer name Description - The job description Hours - The estimated hours price - The price for the job Create a constructor that requires parameters for all the data except price. Include auto-implemented properties for the job number, customer name, and job...

  • Java Programming Exercise 9-7 In the exercises in Chapter 6, you created a class named Purchase....

    Java Programming Exercise 9-7 In the exercises in Chapter 6, you created a class named Purchase. Each Purchase contains an invoice number, amount of sale, amount of sales tax, and several methods. Add get methods for the invoice number and sale amount fields so their values can be used in comparisons. Next, write a program that declares an array of five Purchase objects and prompt a user for their values. Then, in a loop that continues until a user inputs...

  • 4. RetailItem Class Write a class named RetailItem that holds data about an item in a...

    4. RetailItem Class Write a class named RetailItem that holds data about an item in a retail store. The class should have the following fields: • description. The description field references a String object that holds a brief description of the item. • unitsOnHand. The unitsOnHand field is an int variable that holds the number of units currently in inventory. • price. The price field is a double that holds the item’s retail price. Write a constructor that accepts arguments...

  • in python Write a class named RetaiI_Item that holds data about an item in a retail...

    in python Write a class named RetaiI_Item that holds data about an item in a retail store. The class should store the following data in attributes: • Item Number • Item Description • Units in Inventory • Price Create another class named Cash_Register that can be used with the Retail_Item class. The Cash_Register class should be able to internally keep a list of Retail_Item objects. The class should include the following methods: • A method named purchase_item that accepts a...

  • PLEASE DO BOTH OF THESE IN PYTHON 1) Exercise #1: Design and implement class Rectangle to...

    PLEASE DO BOTH OF THESE IN PYTHON 1) Exercise #1: Design and implement class Rectangle to represent a rectangle object. The class defines the following attributes (variables) and methods: Two Class variables of type double named height and width to represent the height and width of the rectangle. Set their default values to 1.0 in the default constructor. A non-argument constructor method to create a default rectangle. Another constructor method to create a rectangle with user-specified height and width. Python...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT