Results 1 to 10 of 10

Thread: The Stock Class

  1. #1
    luckyirish is offline Member
    Join Date
    Jun 2011
    Posts
    6
    Rep Power
    0

    Default The Stock Class

    I need to design a class in java named Stock that contains:
    -A string data field named symbol for the stock’s symbol
    -A string data field named name for the stock’s name
    -A double data field name 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 specified symbol and name
    -The access method for all data fields
    -The mutator method for a previousClosingPrice and currentPrice
    -A method named changePercent() that returns the percentage changed from previousClosingPrice to currentPrice.

    Write a test program that creates a Stock class object with the stock symbol ABC, the name Name Inc., and the previous closing price of 60. Set a new current price to 65 and display the price-change percentage.

    So far this is what I have but I keep getting errors that i don't understand.
    Thank you for any and all help.

    Java Code:
    public class StockProgram
    {
    public static void main (String[] args)
    {
    Stock stock = new Stock
    System.out.println("Percentage change in price: " + stock.getchangePercent());
    }
    }
    
    class Stock
    {
    private String symbol;
    private String name;
    private double previousClosingPrice;
    private double currentPrice;
    
    public Stock()
    {
    }
    
    public Stock(String symbol, String name, double previousClosingPrice, double currentPrice)
    {
    this.symbol = symbol;
    this.name = name;
    this.previousClosingPrice = previousClosingPrice;
    this.currentPrice = currentPrice;
    }
    
    public String getSymbol()
    {
    return this.symbol;
    }
    
    public String getName()
    {
    return this.name;
    }
    
    public double getPreviousClosingPrice()
    {
    return this.previousClosingPrice;
    }
    
    public double getCurrentPrice()
    {
    return this.currentPrice;
    }
    
    public void setSymbol(string symbol)
    {
    this.symbol = symbol;
    }
    
    public void setName(string name)
    {
    this.name = name;
    }
    
    public void setPreviousClosingPrice(double price)
    {
    this.previousClosingPrice = previousClosingPrice;
    }
    
    public void setCurrentPrice(double price)
    {
    this.currentPrice = 60;
    }
    
    public void changePercent( double newPercent)
    {
    return (currentPrice - previousClosingPrice) / previousClosingPrice;
    }
    }
    }

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default

    1. Learn how to properly format code. Unindented code is very difficult to read.
    Code Conventions for the Java(TM) Programming Language: Contents

    2. Copy and paste the errors. Most of us aren't interested in playing guessing games.

    db

  3. #3
    luckyirish is offline Member
    Join Date
    Jun 2011
    Posts
    6
    Rep Power
    0

    Default

    First I apologize for the lack of indention I will do better next time Thank You.

    The errors are as follows -

    Error:[line: 6] '(' or '[' expected
    Error:[line: 6] illegal start of expression
    Error:[line: 6] ';' expected

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default

    What's on line 6?

    db

  5. #5
    luckyirish is offline Member
    Join Date
    Jun 2011
    Posts
    6
    Rep Power
    0

    Default

    This is line 6 -

    System.out.println("Percentage change in price: " + stock.getchangePercent());

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,003
    Blog Entries
    7
    Rep Power
    20

    Default

    Java Code:
    Stock stock = new Stock
    System.out.println("Percentage change in price: " + stock.getchangePercent());
    }
    }
    Look at the previous line (see above); it doesn't make sense, i.e. you need to call a constructor for that class.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    acmohan is offline Member
    Join Date
    Jul 2011
    Posts
    26
    Rep Power
    0

    Default

    @luckyirish
    here are your errors
    1.Stock stock = new Stock ==>Stock stock = new Stock()
    2.stock.getchangePercent() ==>in your program there s no method by this name. it should have been stock.changePercent();

    below is the code which will set symbol=abc,name=NAME ,previousClosingPrice=60 and currentPrice =65 and gives changepercentage

    class Stock
    {
    private String symbol;
    private String name;
    private double previousClosingPrice;
    private double currentPrice;

    public Stock(String symbol, String name, double previousClosingPrice, double currentPrice)
    {
    this.symbol = symbol;
    this.name = name;
    this.previousClosingPrice = previousClosingPrice;
    this.currentPrice = currentPrice;
    }


    public double changePercent()
    {
    return ((currentPrice - previousClosingPrice) / previousClosingPrice)*100;
    }
    }


    public class StockProgram {
    public static void main (String[] args)
    {
    Stock stock = new Stock("ABC","NAME",60,65);
    System.out.println("Percentage change in price: " + stock.changePercent());
    }
    }

  8. #8
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    Quote Originally Posted by acmohan View Post
    @luckyirish
    here are your errors
    1.Stock stock = new Stock ==>Stock stock = new Stock()
    2.stock.getchangePercent() ==>in your program there s no method by this name. it should have been stock.changePercent();

    below is the code which will set symbol=abc,name=NAME ,previousClosingPrice=60 and currentPrice =65 and gives changepercentage

    class Stock
    {
    private String symbol;
    private String name;
    private double previousClosingPrice;
    private double currentPrice;

    public Stock(String symbol, String name, double previousClosingPrice, double currentPrice)
    {
    this.symbol = symbol;
    this.name = name;
    this.previousClosingPrice = previousClosingPrice;
    this.currentPrice = currentPrice;
    }


    public double changePercent()
    {
    return ((currentPrice - previousClosingPrice) / previousClosingPrice)*100;
    }
    }


    public class StockProgram {
    public static void main (String[] args)
    {
    Stock stock = new Stock("ABC","NAME",60,65);
    System.out.println("Percentage change in price: " + stock.changePercent());
    }
    }
    You're spoon feeding, it is better to point out the mistakes in a way that they will find it themselves. This way the OP learns, fixing the code for them doesn't really do much even if you explain it because the OP will normally take the fixed code and run. A lot of the problems we see on the site are from people who are in basic coding classes, then they decide they just want to pass instead of learn so they come here looking for answers in the form of spoon feeding.
    • Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
    • +Rep people for helpful posts.

  9. #9
    acmohan is offline Member
    Join Date
    Jul 2011
    Posts
    26
    Rep Power
    0

    Default

    @Dark : sure will take ur suggestion.

  10. #10
    luckyirish is offline Member
    Join Date
    Jun 2011
    Posts
    6
    Rep Power
    0

    Default

    Thank you all for the help. I will take your suggestions and re visit my code.
    @acmohan - Thank You very much, your explanation was very very helpful. I'm new to java and its not coming to me as easy as Python or VB did.
    @ Dark - I don't want to take the answer and run, I actually want to understand my mistakes so I don't make them again. You can point out my mistakes all you want but helping me understand the mistake and where i went wrong will do more than just pointing it out. I'd like to one day be able to help others the way I was helped here.

Similar Threads

  1. Webcrawling www.stock-forecasting.com
    By devasia1000 in forum Networking
    Replies: 0
    Last Post: 02-12-2011, 11:25 AM
  2. Unable to retrieve stock quotes from yahoo
    By the reporter in forum AWT / Swing
    Replies: 4
    Last Post: 06-03-2010, 04:10 PM
  3. Need a Main for a Stock Managing program !!
    By Sary in forum New To Java
    Replies: 3
    Last Post: 05-30-2010, 07:57 PM
  4. Stock exchange program
    By askinne2 in forum New To Java
    Replies: 2
    Last Post: 05-02-2010, 11:25 PM
  5. Stock Quotes
    By BartDR in forum Advanced Java
    Replies: 0
    Last Post: 09-15-2009, 09:15 AM

Tags for this Thread

Posting Permissions

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