Question

In: Computer Science

PUT IN JAVA PROGRAMMING The Stock class: Design a class named Stock that contains: • A...

PUT IN JAVA PROGRAMMING

The Stock class: Design a class named Stock that contains:
• A string data field named symbol1 for the stock’s symbol.
• A string data field named name for the stock’s name.
• A double data field named previousClosingPrice that stores the stock price for the previous day.
• A double data field named currentPrice that stores the stock price for the current time.
• A constructor that creates a stock with the specified symbol and name.
• A method named getChangePercent() that returns the percentage changed from previousClosingPrice to currentPrice.
• Write a test program that creates a Stock object with the stock symbol ORCL, the name Oracle Corporation, and the previous closing price of 34.5. Set a new current price to 34.35 and display the price-change percentage.

Solutions

Expert Solution

Below is your code:

Stock.java

public class Stock {
        // • A string data field named symbol1 for the stock’s symbol.
        private String symbol1;
        // • A string data field named name for the stock’s name.
        private String name;
        // • A double data field named previousClosingPrice that stores the stock
        // price for the previous day.
        private double previousClosingPrice;
        // • A double data field named currentPrice that stores the stock price for
        // the current time.
        private double currentPrice;

        // • A constructor that creates a stock with the specified symbol and name.
        public Stock(String symbol1, String name) {
                this.symbol1 = symbol1;
                this.name = name;
        }

        // A method named getChangePercent() that returns the percentage changed
        // from previousClosingPrice to currentPrice.
        public double getChangePercent() {
                // calculate change
                double change = this.currentPrice - this.previousClosingPrice;
                // calculate percent by dividing change with current price and
                // multiplying with 100
                double percentage = (change / this.currentPrice) * 100;
                return percentage;
        }

        // getter and setters for instance variables
        public String getSymbol1() {
                return symbol1;
        }

        public void setSymbol1(String symbol1) {
                this.symbol1 = symbol1;
        }

        public String getName() {
                return name;
        }

        public void setName(String name) {
                this.name = name;
        }

        public double getPreviousClosingPrice() {
                return previousClosingPrice;
        }

        public void setPreviousClosingPrice(double previousClosingPrice) {
                this.previousClosingPrice = previousClosingPrice;
        }

        public double getCurrentPrice() {
                return currentPrice;
        }

        public void setCurrentPrice(double currentPrice) {
                this.currentPrice = currentPrice;
        }

}

StockDriver.java

public class StockDriver {
        // main method declaration
        public static void main(String[] args) {
                // create stock object
                Stock stock = new Stock("ORCL", "Oracle Corporation");
                // set previous closing price
                stock.setPreviousClosingPrice(34.5);
                // set current price
                stock.setCurrentPrice(34.35);
                // get percentage
                double percentage = stock.getChangePercent();
                // print result
                if (percentage < 0) {
                        System.out.println("Price decreased by " + (-percentage) + "%.");
                } else {
                        System.out.println("Price increased by " + (percentage) + "%.");
                }
        }
}

Output

Price decreased by 0.4366812227074194%.


Related Solutions

Put In Java Programming The TicketMachine class: Design a class named TicketMachine that contains: • A...
Put In Java Programming The TicketMachine class: Design a class named TicketMachine that contains: • A double data field named price (for the price of a ticket from this machine). • A double data field named balance (for the amount of money entered by a customer). • A double data field named total (for total amount of money collected by the machine). • A constructor that creates a TicketMachine with all the three fields initialized to some values. • A...
PUT IN JAVA PROGRAMMING The StockB class: Design a class named StockB that contains the following...
PUT IN JAVA PROGRAMMING The StockB class: Design a class named StockB that contains the following properties: • Name of company • Number of shares owned • Value of each share • Total value of all shares and the following operations: • Acquire stock in a company • Buy more shares of the same stock • Sell stock • Update the per-share value of a stock • Display information about the holdings • The StockB class should have the proper...
PUT IN JAVA PROGRAMMING The Rectangle class: Design a class named Rectangle to represent a rectangle....
PUT IN JAVA PROGRAMMING The Rectangle class: Design a class named Rectangle to represent a rectangle. The class contains: • Two double data fields named width and height that specify the width and height of a rectangle. The default values are 1 for both width and height. • A no-arg (default) constructor that creates a default rectangle. • A constructor that creates a rectangle with the specified width and height. • A method named findArea() that finds the area of...
THIS IS JAVA PROGRAMMING Design a class named Account (that contains 1. A private String data...
THIS IS JAVA PROGRAMMING Design a class named Account (that contains 1. A private String data field named id for the account (default 0). 2. A private double data field named balance for the account (default 0). 3. A private double data field named annualInterestRate that stores the current interest rate (default 0). 4. A private Date data field named dateCreated that stores the date when the account was created. 5. A no-arg constructor that creates a default account. 6....
THIS IS JAVA PROGRAMMING 1. Create a class named Name that contains the following: • A...
THIS IS JAVA PROGRAMMING 1. Create a class named Name that contains the following: • A private String to represent the first name. • A private String to represent the last name. • A public constructor that accepts two values and assigns them to the above properties. • Public methods named getProperty (e.g. getFirstName) to return the value of the property. • Public methods named setProperty ( e.g. setFirstName)to assign values to each property by using a single argument passed...
In Java, design a class named MyInteger. The class contains: An int data field named value...
In Java, design a class named MyInteger. The class contains: An int data field named value that stores the int value represented by this object. A constructor that creates a MyInteger object for the specified int A get method that returns the int Methods isEven(), isOdd(), and isPrime() that return true if the value is even, odd, or prime, respectively. Static methods isEven(int), isOdd(int), and isPrime(int) that return true if the specified value is even, odd, or prime, respectively. Static...
Java - Design a class named Account that contains: A private String data field named accountNumber...
Java - Design a class named Account that contains: A private String data field named accountNumber for the account (default AC000). A private double data field named balance for the account (default 0). A private double data field named annualIntRate that stores the current interest rate (default 0). Assume all accounts have the same interest rate. A private Date data field named dateCreated that stores the date when the account was created. A no-arg constructor that creates a default account....
JAVA PROGRAMMING. In this assignment, you are to create a class named Payroll. In the class,...
JAVA PROGRAMMING. In this assignment, you are to create a class named Payroll. In the class, you are to have the following data members: name: String (5 pts) id: String   (5 pts) hours: int   (5 pts) rate: double (5 pts) private members (5 pts) You are to create no-arg and parameterized constructors and the appropriate setters(accessors) and getters (mutators). (20 pts) The class definition should also handle the following exceptions: An employee name should not be empty, otherwise an exception...
Design a class named Fan to represent a fan. The class contains: ■ Three constants named...
Design a class named Fan to represent a fan. The class contains: ■ Three constants named SLOW, MEDIUM, and FAST with the values 1, 2, and 3 to denote the fan speed. ■ A private int data field named speed that specifies the speed of the fan (the default is SLOW). ■ A private boolean data field named on that specifies whether the fan is on (the default is false). ■ A private double data field named radius that specifies...
c++ E2b: Design a class named Rectangle to represent a rectangle. The class contains:
using c++E2b: Design a class named Rectangle to represent a rectangle. The class contains:(1) Two double data members named width and height which specifies the width and height of the rectangle .(2) A no-arg constructor that creates a rectangle with width 1 and height 1.(3) A constructor that creates a rectangle with the specified width and height .(4) A function named getArea() that returns the area of this rectangle .(5) A function named getPerimeter() that returns the perimeter of this...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT