Results 1 to 5 of 5

Thread: Stocks.

  1. #1
    Alex89 is offline Member
    Join Date
    Nov 2007
    Posts
    4
    Rep Power
    0

    Smile Stocks.

    I am pretty new to java and have just started getting back into it. I have to create a program that tests stocks, but I do not know how to even begin starting. Any help would be greatly appreciated.

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    We'll need some more information about this. What is a stock and how do you want to test it?

  3. #3
    Alex89 is offline Member
    Join Date
    Nov 2007
    Posts
    4
    Rep Power
    0

    Default

    I hav to create a class named Stock to model a stock on the stock market. The properties and methods of the class are shown below. The method changePercent computes the percentage of the change between the current price and the previous closing price.

    2. Write a client program to test the stock class. In the client program, create a Stock object and test it.


    Stock UML

    -symbol: string
    -name: string
    -previousClosingPrice: double
    -currentPrice: double

    +Stock()
    +Stock(symbol:String, name:String)
    +getSymbol():String
    +getName() :String
    +getPreviousClosingPrice(): double
    +getCurrentPrice(): Double
    +setSymbol(symbol: String): void
    +setName(name: String): void
    +setPreviousClosingPrice(price: double):void
    +setCurrentPrice(price: double): void
    +changePercentage(): double

    I hope this helps.

  4. #4
    Alex89 is offline Member
    Join Date
    Nov 2007
    Posts
    4
    Rep Power
    0

    Default

    I have created the class for my stock now I need to create a client program to test my class. I need help with the client program.



    This is my class for the stock.

    public class Stock {

    private string symbol;
    private string name;
    private double previousClosingPrice;
    private double currentPrice;

    public stock ()
    {

    symbol = "";
    name = "";
    previousClosingPrice = 0.0;
    currentPrice = 0.0;
    }
    public stock (String symbola, String namea)
    {
    this.symbol = symbola;
    this.name = namea;
    previousClosingPrice = 0.0;
    currentPrice = 0.0;
    }
    public String getSymbol ()
    {
    return symbol;
    }
    public String getName ()
    {
    return name;
    }
    public double getPreviousClosingPrice () { return previousClosingPrice;}
    public double getCurrentPrice () { return currentPrice;}

    public void setSymbol (String symbol)
    {
    this.symbol = Symbol;
    }
    public void setName (String name) { this.name = Name;}
    public void setPreviousClosingPrice (double cost)
    {
    this.previousClosingPrice = cost;
    }
    public void setCurrentPrice (double morecost ) {this.currentPrice = morecost}

    public double changePercentage ()
    {
    return currentPrice/previousClosingPrice;
    }
    }

  5. #5
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Java Code:
    public class StockTest {
        public static void main(String[] args) {
            Stock stock = new Stock();
            // Call methods in the stock instance
            // to test/exercise your Stock class.
            stock.setPreviousClosingPrice(5.00);
            stock.setCurrentPrice(3.00);
            // ...
            Stock stock2 = new Stock("GV", "Google");
            // Test stock2...
        }
    }
    
    class Stock {
        private String symbol;
        private String name;
        private double previousClosingPrice;
        private double currentPrice;
    
        public Stock() {
            this("", "");
        }
    
        public Stock(String symbola, String namea) {
            this.symbol = symbola;
            this.name = namea;
            previousClosingPrice = 0.0;
            currentPrice = 0.0;
        }
    
        public String getSymbol() { return symbol; }
    
        public String getName() { return name; }
    
        public double getPreviousClosingPrice() { return previousClosingPrice; }
    
        public double getCurrentPrice() { return currentPrice; }
    
        public void setSymbol(String symbol) {
            // this.memberVariable = localVariable
            // "this" refers to the enclosing class, ie, Stock
            this.symbol = symbol;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public void setPreviousClosingPrice(double price) {
            this.previousClosingPrice = price;
        }
    
        public void setCurrentPrice(double price) {
            this.currentPrice = price;
        }
    
        public double changePercentage() {
            return currentPrice/previousClosingPrice;
        }
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •