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++ 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.

  • 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...

  • 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...

  • Create a class named BankAccount with data fields for a count number and a balance. Include...

    Create a class named BankAccount with data fields for a count number and a balance. Include a constructor that takes arguments for each field. The constructor sets the balance to 0 if it is below the required $200.00 minimum for an account. Include a method that displays the account details, including an explanation if the balance was reduced to 0. Write the class TestAccount in which you instantiate two BankAccount objects, prompt a user for values of the account number...

  • Design a class named EmployeeRecord that holds an employee’s ID number, name, and payrate. Include mutator...

    Design a class named EmployeeRecord that holds an employee’s ID number, name, and payrate. Include mutator methods to set the values for each data field and output the values for each data field. Create the class diagram and write the code that defines the class and implements the mutator methods. need help please with this question

  • USE C++. Just want to confirm is this right~? Write a class named RetailItem that holds...

    USE C++. Just want to confirm is this right~? Write a class named RetailItem that holds data about an item in a retail store. The class should have the following member variables: description: A string that holds a brief description of the item. unitsOnHand: An int that holds thw number of units currently in inventory. price: A double that holds that item's retail price. Write the following functions: -appropriate mutator functions -appropriate accessor functions - a default constructor that sets:...

  • Design and implement a Java data structure class named BookShelf which has an array to store...

    Design and implement a Java data structure class named BookShelf which has an array to store books and the size data member. The class should have suitable constructors, get/set methods, and the toString method, as well as methods for people to add a book, remove a book, and search for a book. Design and implement a Java class named Book with two data members: title and price. Test the two classes by creating a bookshelf object and five book objects....

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
ADVERTISEMENT