Question

WRITE JAVA PROGRAM Problem Statement You are required to write a stock price simulator, which simulates...

WRITE JAVA PROGRAM
Problem Statement
You are required to write a stock price simulator, which simulates a price change of a stock.
When the program runs, it should do the following:
 Create a Stock class which must include fields: name, symbol, currentPrice, nextPrice,
priceChange, and priceChangePercentage.
 The Stock class must have two constructor: a no-argument constructor and a constructor
with four parameters that correspond to the four fields.
o For the no-argument constructor, set the default value for each field such as:
 Name: Microsoft
 Symbol: MSFT
 currentPrice: 46.87
 nextPrice: 46.87
o For the argument constructor, set the value of four fields based on the arguments.
 The Stock class must have accessors and mutators for all of its fields.
 The setter methods must protect a user from setting the currentPrice/nextPrice into
negative number. You can set the values to 0 if a user tries to change them to negative
values.
 The Stock class should have a SimulatePrice() method, which increases or decreases the
currentPrice by 0 - 10% randomly, including 0.00% and 2.34%.
 The main program will ask user to enter a name, symbol, current price of a stock. Then, it
simulates the prices for next 30 days. It displays the prices for the next 30 days on the
console. If a user enters “NONE”, “NA”, 0.0 for name, symbol, current price
respectively, then the no-argument constructor is used.

Input
This program requires that you read in the following data values:
 A stock’s name, symbol, and current price.
o E.g., Microsoft Corporation, MSFT, 45.87.
You will use interactive I/O in this program. All of the input must be validated if it is needed.
You can assume that for a numeric input value, the grader will enter a numeric value in the
testing cases.
Output
Your program should display the stock’s name, symbol, current price, next price
priceChange, and priceChangePercentage for each day on the console

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

// for scanner

import java.util.*;

// Stock class

public class Stock{

    // stock name field

    private String name;

    // stock symbol field

    private String symbol;

    // stock current price

    private float currentPrice;

    // stock next price

    private float nextPrice;

    // price change between days

    private float priceChange;

    // price change in percent

    private float priceChangePercentage;

    // empty constructor

    public Stock(){

        this.name="Microsoft";

        this.symbol="MS";

        this.currentPrice=Float.valueOf("46.87");

        this.nextPrice=Float.valueOf("46.87");

    }

    // cinstructot with frist 4 fields

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

        this.name=name;

        this.symbol=symbol;

        this.currentPrice=currentPrice;

        this.nextPrice=nextPrice;

    }

    // getter methods for all the fields

    public String getName(){

        return this.name;

    }

    public String getSymbol(){

        return this.symbol;

    }

    public float getCurrentPrice(){

        return this.currentPrice;

    }

    public float getNextPrice(){

        return this.nextPrice;

    }

    public float getPriceChange(){

        return this.priceChange;

    }

    public float getPriceChangePercentage(){

        return this.priceChangePercentage;

    }

    

    // setter methods

    public void setName(String name){

        this.name=name;

    }

    public void setSymbol(String symbol){

        this.symbol=symbol;

    }

    // if price passed is -ve set current price =0

    public void setCurrentPrice(float currentPrice){

        if(currentPrice<0){

            this.currentPrice=0;

        }else{

            this.currentPrice=currentPrice;

        }

    }

    // if price passed is -ve set next price =0

    public void setNextPrice(float nextPrice){

        if(nextPrice<0){

            this.nextPrice=0;

        }else{

            this.nextPrice=nextPrice;

        }

    }

    public void setPriceChange(float priceChange){

        this.priceChange=priceChange;

    }

    public void setPriceChangePercentage(float priceChangePercentage){

        this.priceChangePercentage=priceChangePercentage;

    }

    // simulatePrice function

    // returns the new price of the stock

    public float simulatePrice(){

        // initialize random nummber generator

        Random rand = new Random();

        // get a random float number between 0-1

        // divide by 10 so that it stays in range of 0-10%

        float randomPercent=rand.nextFloat()/10;

        // random number to increase of decrease current price

        int positive=rand.nextInt(2);

        if(positive!=0){

            // increase next price by randomPercent calculated

            setNextPrice(this.currentPrice+(this.currentPrice*randomPercent));

        }

        else{

             // decrease next price by randomPercent calculated

            setNextPrice(this.currentPrice-(this.currentPrice*randomPercent));

        }

        // set price change to the difference

        setPriceChange(this.nextPrice-this.currentPrice);

        // calculate and set price change percentage

        setPriceChangePercentage((this.priceChange/this.currentPrice)*100);

        // return the new price calculated

        return this.nextPrice;

    }

    // main function   

    public static void main(String[] args){

        // get stock name ,symbol and current price

        System.out.println("\t--Stock Price Simulation--");

        Scanner s=new Scanner(System.in);

        System.out.println("Enter Stock Name");

        String stockName=s.next();

        System.out.println("Enter Stock Symbol");

        String stockSymbol=s.next();

        System.out.println("Enter Current Stock Price");

        float currentStockPrice=s.nextFloat();

        Stock stock;

        // if user enters NONE ,or NA or 0.0 then

        if(stockName.equals("NONE") || stockSymbol.equals("NA") || currentStockPrice==0.0){

            stock=new Stock();

        }

        else{

            stock=new Stock(stockName,stockSymbol,currentStockPrice,currentStockPrice);

        }

        // if Stock Name set to NONE or Stock Symbol set to NA or stock current price set to 0.0

        // exit

        if(stockName.equals("NONE") || stockSymbol.equals("NA") || currentStockPrice==0.0){

            System.out.println("Stock Name set to NONE or Stock Symbol set to NA or stock current price set to 0.0");

            System.out.println("Exiting");

        }

        else{

            // loop for 30 count

            System.out.println("\t--30 Days Simulation--");

            for(int i=1;i<=30;i++){

                // get the new price

                float price=stock.simulatePrice();

                // print all the fields of the stock

                System.out.println("--Day-"+i+"--");

                System.out.println("Stock Name:"+stock.getName());

                System.out.println("Stock Symbol:"+stock.getSymbol());

                System.out.println("Stock Current Price:"+stock.getCurrentPrice());

                System.out.println("Stock Next Price:"+stock.getNextPrice());

                System.out.println("Price Change:"+stock.getPriceChange());

                System.out.println("Price Change Percentage:"+stock.getPriceChangePercentage()+"%");

                // set stock current price to new price

                stock.setCurrentPrice(price);

            }

        }

    }

}

// for scanner
import java.util.*;
// Stock class
public class Stock{
    // stock name field
    private String name;
    // stock symbol field
    private String symbol;
    // stock current price
    private float currentPrice;
    // stock next price
    private float nextPrice;
    // price change between days
    private float priceChange;
    // price change in percent
    private float priceChangePercentage;

    // empty constructor
    public Stock(){
        this.name="Microsoft";
        this.symbol="MS";
        this.currentPrice=Float.valueOf("46.87");
        this.nextPrice=Float.valueOf("46.87");
    }
    // cinstructot with frist 4 fields
    public Stock(String name,String symbol,float currentPrice,float nextPrice){
        this.name=name;
        this.symbol=symbol;
        this.currentPrice=currentPrice;
        this.nextPrice=nextPrice;
    }

    // getter methods for all the fields
    public String getName(){
        return this.name;
    }
    public String getSymbol(){
        return this.symbol;
    }
    public float getCurrentPrice(){
        return this.currentPrice;
    }
    public float getNextPrice(){
        return this.nextPrice;
    }

    public float getPriceChange(){
        return this.priceChange;
    }

    public float getPriceChangePercentage(){
        return this.priceChangePercentage;
    }
    
    // setter methods 
    public void setName(String name){
        this.name=name;
    }
    public void setSymbol(String symbol){
        this.symbol=symbol;
    }
    // if price passed is -ve set current price =0
    public void setCurrentPrice(float currentPrice){
        if(currentPrice<0){
            this.currentPrice=0;
        }else{
            this.currentPrice=currentPrice;
        }
    }
    // if price passed is -ve set next price =0
    public void setNextPrice(float nextPrice){
        if(nextPrice<0){
            this.nextPrice=0;
        }else{
            this.nextPrice=nextPrice;
        }
    }
    public void setPriceChange(float priceChange){
        this.priceChange=priceChange;
    }
    public void setPriceChangePercentage(float priceChangePercentage){
        this.priceChangePercentage=priceChangePercentage;
    }
    // simulatePrice function
    // returns the new price of the stock
    public float simulatePrice(){
        // initialize random nummber generator
        Random rand = new Random();
        // get a random float number between 0-1
        // divide by 10 so that it stays in range of 0-10%
        float randomPercent=rand.nextFloat()/10;
        // random number to increase of decrease current price
        int positive=rand.nextInt(2);
        if(positive!=0){
            // increase next price by randomPercent calculated
            setNextPrice(this.currentPrice+(this.currentPrice*randomPercent));
        }
        else{
             // decrease next price by randomPercent calculated
            setNextPrice(this.currentPrice-(this.currentPrice*randomPercent));
        }
        // set price change to the difference 
        setPriceChange(this.nextPrice-this.currentPrice);
        // calculate and set price change percentage
        setPriceChangePercentage((this.priceChange/this.currentPrice)*100);
        // return the new price calculated
        return this.nextPrice;
    }
    // main function   

    public static void main(String[] args){
        // get stock name ,symbol and current price
        System.out.println("\t--Stock Price Simulation--");
        Scanner s=new Scanner(System.in);
        System.out.println("Enter Stock Name");
        String stockName=s.next();
        System.out.println("Enter Stock Symbol");
        String stockSymbol=s.next();
        System.out.println("Enter Current Stock Price");
        float currentStockPrice=s.nextFloat();
        Stock stock;
        // if user enters NONE ,or NA or 0.0 then 
        if(stockName.equals("NONE") || stockSymbol.equals("NA") || currentStockPrice==0.0){
            stock=new Stock();
        }
        else{
            stock=new Stock(stockName,stockSymbol,currentStockPrice,currentStockPrice);
        }
        // if Stock Name set to NONE or Stock Symbol set to NA or stock current price set to 0.0
        // exit
        if(stockName.equals("NONE") || stockSymbol.equals("NA") || currentStockPrice==0.0){
            System.out.println("Stock Name set to NONE or Stock Symbol set to NA or stock current price set to 0.0");
            System.out.println("Exiting");
        }
        else{
            // loop for 30 count
            System.out.println("\t--30 Days Simulation--");
            for(int i=1;i<=30;i++){
                // get the new price 
                float price=stock.simulatePrice();
                // print all the fields of the stock
                System.out.println("--Day-"+i+"--");
                System.out.println("Stock Name:"+stock.getName());
                System.out.println("Stock Symbol:"+stock.getSymbol());
                System.out.println("Stock Current Price:"+stock.getCurrentPrice());
                System.out.println("Stock Next Price:"+stock.getNextPrice());
                System.out.println("Price Change:"+stock.getPriceChange());
                System.out.println("Price Change Percentage:"+stock.getPriceChangePercentage()+"%");
                // set stock current price to new price
                stock.setCurrentPrice(price);
            }
        }
    }
}

--Stock Price Simulation-- Enter Stock Name Microsoft Enter Stock Symbol MSFT Enter Current Stock Price 45.87 --30 Days Simul

--Day-4-- Stock Name: Microsoft Stock Symbol:MSFT Stock Current Price: 46.78652 Stock Next Price: 48.835407 Price Change:2.04

--Day-9-- Stock Name:Microsoft Stock Symbol:MSFT Stock Current Price:34.185307 Stock Next Price:32.913002 Price Change: -1.27

--Day-14-- Stock Name: Microsoft Stock Symbol:MSFT Stock Current Price:33.198723 Stock Next Price: 31.635479 Price Change: -1

--Day-19- Stock Name: Microsoft Stock Symbol:MSFT Stock Current Price:31.350174 Stock Next Price:31. 185534 Price Change:-0.1

--Day-24-- Stock Name: Microsoft Stock Symbol:MSFT Stock Current Price:28.323288 Stock Next Price:28.29183 Price Change:-0.03

--Day-29- Stock Name:Microsoft Stock Symbol:MSFT Stock Current Price: 30.106163 Stock Next Price:32.36004 Price Change:2.2538

Add a comment
Know the answer?
Add Answer to:
WRITE JAVA PROGRAM Problem Statement You are required to write a stock price simulator, which simulates...
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
  • 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...

  • Write a C++ Program that simulates a basic calculator using functions which performs the operations of...

    Write a C++ Program that simulates a basic calculator using functions which performs the operations of Addition, Subtraction, multiplication, and Division. ( make sure you cover the case to avoid division by a zero) Display a menu for list of operations that can be calculated and get the input from user about his choice of calculation. Based on user choice of operation, take the input of number/numbers from user. Assume all input values are of type double. Calculations must be...

  • Write a C++ Program that simulates a basic calculator using functions which performs the operations of...

    Write a C++ Program that simulates a basic calculator using functions which performs the operations of Addition, Subtraction, multiplication, and Division. ( make sure you cover the case to avoid division by a zero) Display a menu for the list of operations that can be calculated and get the input from the user about his choice of calculation. Based on user choice of operation, take the input of number/numbers from user. Assume all input values are of type double. Calculations...

  • please use c programme Write a program which simulates a calculator using the following specifications. (50...

    please use c programme Write a program which simulates a calculator using the following specifications. (50 points - 10pts for syntax, 10pts for commenting and 30pts for successful execution) • User input must have the following format - value1 operator value2 • Compute value1 operator value2 • Use switch statement • Operators must be ’+’ ,’-’, ’*’, ’/’ and ’%’ • Division by 0 in C leads to an error. Terminate the program before divison occurs in such cases. •...

  • Write a program in C++ that simulates a soft drink machine. The program will need several...

    Write a program in C++ that simulates a soft drink machine. The program will need several classes: DrinkItem, DrinkMachine and Receipt. For each of the classes you must create the constructors and member functions required below. You can, optionally, add additional private member functions that can be used for doing implementation work within the class. DrinkItem class The DrinkItem class will contains the following private data members: name: Drink name (type of drink – read in from a file). Type...

  • This is for Java Programming Please use comments Customer and Employee data (15 pts) Problem Description:...

    This is for Java Programming Please use comments Customer and Employee data (15 pts) Problem Description: Write a program that uses inheritance features of object-oriented programming, including method overriding and polymorphism. Console Welcome to the Person Tester application Create customer or employee? (c/e): c Enter first name: Frank Enter last name: Jones Enter email address: frank44@ hot mail. com Customer number: M10293 You entered: Name: Frank Jones Email: frank44@hot mail . com Customer number: M10293 Continue? (y/n): y Create customer...

  • Written in python using puTTy!! i'm having a lot of trouble with this, will upvote! also...

    Written in python using puTTy!! i'm having a lot of trouble with this, will upvote! also here is the address.csv file Name,Phone,Email,Year_of_Birth Elizi Moe,5208534566,[email protected],1978 Ma Ta,4345667345,[email protected],1988 Diana Cheng,5203456789,[email protected],1970 ACTIVITY I Implement in Python the Subscriber class modeled by the UML diagram provided below. Save in a file called MyClasses.py Subscriber name: string yearOfBirth: int phone: string email: string getName() getAge() getPhone() getEmail() Write a test program that does the following: Read the file addresses.csv. For each record, create an object...

  • In this assignment, you will implement Address and Residence classes. Create a new java project. Part...

    In this assignment, you will implement Address and Residence classes. Create a new java project. Part A Implementation details of Address class: Add and implement a class named Address according to specifications in the UML class diagram. Data fields: street, city, province and zipCode. Constructors: A no-arg constructor that creates a default Address. A constructor that creates an address with the specified street, city, state, and zipCode Getters and setters for all the class fields. toString() to print out all...

  • . Use what you learned from our First Python Program to write a Python program that...

    . Use what you learned from our First Python Program to write a Python program that will carry out the following tasks. Ask user provide the following information: unit price (for example: 2.5) quantity (for example: 4) Use the following formula to calculate the revenue: revenue = unit price x quantity Print the result in the following format on the console: The revenue is $___. (for example: The revenue is $10.0.) . . Use the following formula to calculate the...

  • ( Object array + input) Write a Java program to meet the following requirements: 1. Define...

    ( Object array + input) Write a Java program to meet the following requirements: 1. Define a class called Student which contains: 1.1 data fields: a. An integer data field contains student id b. Two String data fields named firstname and lastname c. A String data field contains student’s email address 1.2 methods: a. A no-arg constructor that will create a default student object. b. A constructor that creates a student with the specified student id, firstname, lastname and email_address...

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