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

In java netbean8.1 or 8.2 please. The class name is Stock . It has 5 private att

ID: 3855166 • Letter: I

Question

In java netbean8.1 or 8.2 please.

The class name is Stock.

It has 5 private attributes (name, symbol, numberOfShares, currentPrice and boughtPrice)and one static private attribute (numberOfStocks). All attributes must be initialized (name and symbol to “No name/ symbol yet” and numberOfShares, currentPrice and boughtPrice to 0.

Create two constructors, one with no arguments and the other with all the attributes. (However, remember to increase the numberOfStocks in both constructors.

Write the necessary mutators and accessors (getters and setters) for the private attributes.

Write four methods in the class: calcValue, display, calcPercentReturn and toString.

calcValuewill return the value of the stock (price * shares)

calcPercentReturnwill return the percent calculated by the (currentPrice- boughtPrice)/boughtPrice.

The displaymethod has no parameter and no return but will display the attributes and the calcValue and calcPercentReturn methods in a series of formatted print lines.

toStringwill return a string with the stock name, symbol and percent return.

The client program will have main method and create 4 stock objects using both constructors at least once (in other words, you will have to use a series of mutators for at least one stock to set the attributes). Test all the object methods. Calculate and display the sum of the entire portfolio current value in a formatted output. The variable sum will be a class variable in the main.

Test data:

IBM                IBM                            100 146.22 150.50

Humana HUM                           200 180.22 130.25

Google GOOGL                      100      763.16 500.78

Apple              AAPL                         100      106.09 112.90

Load into a Word doc the class Stock, the client and the output then upload in CANVAS.

Notes::

Search for “java read file line by line“ to find how to read a file.

Search for “java split string” to find how to split a string

Search for “java split string multiple spaces” to split lines with many spaces between tokens

The first double is the actual current price as of 3/17/16 the other double is the price from 2 years ago when we did this!!..... I should have bought Google!!

Put the test data in a file in a specific directory. I put the input file into the folder “C:C201”, had to create that folder first.

No blank lines in the input file, please.

Line up the data in columns, number of spaces between columns are immaterial.

Notice the reading code is inside a try, this code is attempted and if it fails with the exception detailed in the catch (below) then the catch is executed. This is part of the java exception technology and keeps a program from putting red ink on the screen.

This code can read the input file line by line:

                        String filename = "c:C201 red.data";

                        try (BufferedReader br = new BufferedReader(new FileReader(filename)))

                        {

                                    String line;

                                    while ((line = br.readLine()) != null)

                                    {

                                                String ss = line;

                                                System.out.println(ss);

                                    }

                        }  

                        catch (IOException e) {System.out.println(e);}

This code will split a string in java:

                                    for (String str: ss.split(" +"))

                                    {

                                    System.out.println(str);

                                    }

Explanation / Answer

public class Stock {

private String name;

private String symbol;

static int numberOfShare;

private float currentPrice;

private float brougtPrice;

public Stock(String name, String symbol, float currentPrice, float brougtPrice) {

numberOfShare+=1;

this.name = name;

this.symbol = symbol;

this.currentPrice = currentPrice;

this.brougtPrice = brougtPrice;

}

public Stock() {

numberOfShare+=1;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public String getSymbol() {

return symbol;

}

public void setSymbol(String symbol) {

this.symbol = symbol;

}

public static int getNumberOfShare() {

return numberOfShare;

}

public static void setNumberOfShare(int numberOfShare) {

Stock.numberOfShare = numberOfShare;

}

public float getCurrentPrice() {

return currentPrice;

}

public void setCurrentPrice(float currentPrice) {

this.currentPrice = currentPrice;

}

public float getBrougtPrice() {

return brougtPrice;

}

public void setBrougtPrice(float brougtPrice) {

this.brougtPrice = brougtPrice;

}

public float calcValue()

{

return currentPrice*numberOfShare;

}

public float calcPercentReturn() {

return (currentPrice-brougtPrice)/brougtPrice;

}

@Override

public String toString() {

return "Stock [name=" + name + ", symbol=" + symbol + ", currentPrice=" + currentPrice + ", brougtPrice="

+ brougtPrice + "]";

}

}

package thread;

public class MainClass {

public static void main(String[] args) {

// TODO Auto-generated method stub

Stock stock1=new Stock("IBM","IBM",146.22f,150.50f);

Stock stock2=new Stock("Humana","HUM",180.22f,130.25f);

Stock stock3=new Stock("Google","GOOGL",763.16f,500.78f);

Stock stock4=new Stock();

stock4.setName("Apple");

stock4.setSymbol("AAPL");

stock4.setCurrentPrice(112.90f);

stock4.setBrougtPrice(106.09f);

stock1.calcPercentReturn();

stock1.calcValue();

stock1.toString();

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote