Question

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

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\fred.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);

                                    }

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

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();

}

}

Add a comment
Know the answer?
Add Answer to:
In java netbean8.1 or 8.2 please. The class name is Stock. It has 5 private attributes...
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
  • Ask the user for the name of a file and a word. Using the FileStats class,...

    Ask the user for the name of a file and a word. Using the FileStats class, show how many lines the file has and how many lines contain the text. Standard Input                 Files in the same directory romeo-and-juliet.txt the romeo-and-juliet.txt Required Output Enter a filename\n romeo-and-juliet.txt has 5268 lines\n Enter some text\n 1137 line(s) contain "the"\n Your Program's Output Enter a filename\n romeo-and-juliet.txt has 5268 lines\n Enter some text\n 553 line(s) contain "the"\n (Your output is too short.) My...

  • QUESTION The ReadFile class opens and reads a text file. The WriteFile class opens and writes...

    QUESTION The ReadFile class opens and reads a text file. The WriteFile class opens and writes to a file. Compile and run these programs. There are several ways to read/write text files. The examples shown here are just one way of achieving this. Look at the API for the BufferedReader class. The readline() method is a simple way to read the text file line by line. It returns null when the end of the file has been reached. https://docs.oracle.com/javase/8/docs/api/java/io/BufferedReader.html Look...

  • Filename(s): ReformatCode. java Public class: ReformatCode Package-visible class(es): none Write a program that reformats Java source...

    Filename(s): ReformatCode. java Public class: ReformatCode Package-visible class(es): none Write a program that reformats Java source code from the next-line brace style to the end-of-line brace style. The program is invoked from the command line with the input Java source code file as args [0] and the name of the file to save the formatted code in as args [1]. The original file is left untouched. The program makes no other changes the source code, including whitespace. For example, the...

  • import java.util.Scanner; public class Cat { private String name; private Breed breed; private double weight; public...

    import java.util.Scanner; public class Cat { private String name; private Breed breed; private double weight; public Cat(String name, Breed breed, double weigth) { this.name = name; this.breed = breed; this.weight = weight; } public Breed getBreed() { return breed; } public double getWeight() { return weight; } //other accessors and mutators ...... } public class Breed { private String name; private double averageWgt; public Breed(String name,double averageWgt) { this.name = name; this.averageWgt= averageWgt; } public double getWeight() { return averageWgt;...

  • Please help me do the java project For this project you will be reading in a...

    Please help me do the java project For this project you will be reading in a text file and evaluating it in order to create a new file that represents the Class that will represent the properties of the text file. For example, consider the following text file: students.txt ID              Name                              Age                    IsMale           GPA 1                Tom Ryan                       22                       True              3.1 2                Jack Peterson                31                       True              2.7 3                Cindy LuWho                12                       False             3.9 When you read in the header line, you...

  • JAVA help Create a class Individual, which has: Instance variables for name and phone number of...

    JAVA help Create a class Individual, which has: Instance variables for name and phone number of individual. Add required constructors, accessor, mutator, toString() , and equals method. Use array to implement a simple phone book , by making an array of objects that stores the name and corresponding phone number. The program should allow searching in phone book for a record based on name, and displaying the record if the record is found. An appropriate message should be displayed if...

  • composed the following java code to read a string from a text file but receiving compiling...

    composed the following java code to read a string from a text file but receiving compiling errors. The text file is MyNumData.txt. Included the original java script that generated the output file. Shown also in the required output results after running the java program. I can't seem to search for the string and output the results. Any assistance will be greatly appreciated. import java.io.BufferedReader; import java.io.FileReader; import java.util.ArrayList; public class Main {   public static void main(String[] args) {     System.out.print("Enter the...

  • public class Animal {    private String name; //line 1    private int weight; //line 2...

    public class Animal {    private String name; //line 1    private int weight; //line 2    private String getName(){       return name;    } //line 3    public int fetchWeight(){       return weight; } //line 4 } public class Dog extends Animal {    private String food; //line 5    public void mystery(){       //System.out.println("Name = " + name); //line 6            System.out.println("Food = " + food); //line 7    } } I want to know the super...

  • FOR JAVA: Summary: Create a program that adds students to the class list (see below). The...

    FOR JAVA: Summary: Create a program that adds students to the class list (see below). The solution should be named Roster402_v2.java. Allow the user to control the number of students added to the roster. Ask if the user would like to see their new roster to confirm additions. If yes, then display contents of the file, if no, end the program. ------------------------------------------------------------------------------------- List of student names and IDs for class (this will be your separate text file): Jones, Jim,45 Hicks,...

  • 4. Command pattern //class Stock public class Stock { private String name; private double price; public...

    4. Command pattern //class Stock public class Stock { private String name; private double price; public Product(String name, double price) { this.name = name; this.price = price; } public void buy(int quantity){ System.out.println(“BOUGHT: “ + quantity + “x “ + this); } public void sell(int quantity){ System.out.println(“SOLD: “ + quantity + “x “ + this); } public String toString() { return “Product [name=” + name + “, price=” + price + “]”; } } a. Create two command classes that...

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