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

Objectives: During this homework, students will learm how to 1. Create a static

ID: 3908374 • Letter: O

Question

Objectives: During this homework, students will learm how to 1. Create a static data member 2 Create a static member method 3. Access both a static data member and member method from within Main 4 Create and use the Java toString method, and 5. Use static data members to keep track of the number of objects created Instructions: Task 1: 1. For this homework I would like you to create a Java class based on the following UML (note that in a LUML diagram, you underine static members) Stock symbol: String price: double -numberOfShares: double number?Stocks-int"0" Stock +Stock[Sym String, iPrice double, iShares double) +getSymbol() String +setSymbol(input: String) : void +getPrice): double +setPrice (input double) void rget setNumberOfShares(input double) void toString): String ) : double 2 numberofShares represents the number of shares you bought of this particular stock, where numberOfStock is the number of different stock you own (in other words, the number of objects that your program has created) 3 Notice that you initialize numberOfStocks to zero, you don't do this in the constructor, you do it in the variable declaration line 4 For the to String method, build me a meaningful (pretty) string that displays all of the information about the one stock that is selected (so you won't be using numberOfStocks, since that is pertaining to the entize class and not one individual object) You will be calling this method in Main and feeding it to a printout Task 2

Explanation / Answer

Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts. Thanks

// Stock.java

public class Stock {

                // attributes

                private String symbol;

                private double price;

                private double numberOfShares;

                private static int numberOfStocks = 0;

                /**

                * Default constructor

                */

                public Stock() {

                                symbol = "";

                                price = 0;

                                numberOfShares = 0;

                                numberOfStocks++;// incrementing number of stocks created

                }

                /**

                * constructor with arguments

                */

                public Stock(String iSym, double iPrice, double iShares) {

                                symbol = iSym;

                                price = iPrice;

                                numberOfShares = iShares;

                                numberOfStocks++;// incrementing number of stocks created

                }

               

                //getters and setters

               

                public String getSymbol() {

                                return symbol;

                }

                public void setSymbol(String symbol) {

                                this.symbol = symbol;

                }

                public double getPrice() {

                                return price;

                }

                public void setPrice(double price) {

                                this.price = price;

                }

                public double getNumberOfShares() {

                                return numberOfShares;

                }

                public void setNumberOfShares(double numberOfShares) {

                                this.numberOfShares = numberOfShares;

                }

                /**

                * static method for fetching number of stocks

                */

                public static int getNumberOfStocks() {

                                return numberOfStocks;

                }

                @Override

                public String toString() {

                                //retrning a properly formatted string

                                return "Symbol: " + symbol + ", Price: " + price

                                                                + ", Number of shares: " + numberOfShares;

                }

}

// StockDriver.java

import java.util.Scanner;

public class StockDriver {

                public static void main(String[] args) {

                                /**

                                * creating an array of stocks

                                */

                                Stock stockArray[] = new Stock[100];

                                Scanner scanner = new Scanner(System.in);

                                String choice = "y";

                                /**

                                * looping until user chooses to quit, or the array is full

                                */

                                do {

                                                /**

                                                * getting inputs

                                                */

                                                System.out.print("Enter symbol: ");

                                                String symbol = scanner.nextLine();

                                                System.out.print("Enter price: ");

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

                                                System.out.print("Enter number of shares: ");

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

                                                /**

                                                * creating a stock object

                                                */

                                                Stock stock = new Stock(symbol, price, shares);

                                                /**

                                                * adding to the array in proper position (number of stocks as

                                                * index)

                                                */

                                                stockArray[Stock.getNumberOfStocks() - 1] = stock;

                                                System.out.println("Number of stocks created: "

                                                                                + Stock.getNumberOfStocks());

                                                /**

                                                * prompting the user to add more stock, if the array is not full

                                                */

                                                if (Stock.getNumberOfStocks() < stockArray.length) {

                                                                System.out

                                                                                                .print("Do you want to create another stock? (y/n): ");

                                                                choice = scanner.nextLine();

                                                }

                                } while (Stock.getNumberOfStocks() < stockArray.length

                                                                && choice.equalsIgnoreCase("y"));

                                /**

                                * Displaying all the read data once exited from the loop

                                */

                                System.out.println("Stocks entered: ");

                                for (int i = 0; i < Stock.getNumberOfStocks(); i++) {

                                                System.out.println(stockArray[i]);

                                }

                                System.out.println("Total number of stocks: "

                                                                + Stock.getNumberOfStocks());

                }

}

/*OUTPUT*/

Enter symbol: NASDAQ

Enter price: 2233.55

Enter number of shares: 5

Number of stocks created: 1

Do you want to create another stock? (y/n): y

Enter symbol: NYSE

Enter price: 889

Enter number of shares: 4

Number of stocks created: 2

Do you want to create another stock? (y/n): y

Enter symbol: ABCD

Enter price: 789.66

Enter number of shares: 10

Number of stocks created: 3

Do you want to create another stock? (y/n): y

Enter symbol: XYZ

Enter price: 1

Enter number of shares: 2

Number of stocks created: 4

Do you want to create another stock? (y/n): n

Stocks entered:

Symbol: NASDAQ, Price: 2233.55, Number of shares: 5.0

Symbol: NYSE, Price: 889.0, Number of shares: 4.0

Symbol: ABCD, Price: 789.66, Number of shares: 10.0

Symbol: XYZ, Price: 1.0, Number of shares: 2.0

Total number of stocks: 4