Results 1 to 10 of 10
Thread: The Stock Class
- 07-12-2011, 07:30 AM #1
Member
- Join Date
- Jun 2011
- Posts
- 6
- Rep Power
- 0
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; } } }
- 07-12-2011, 08:41 AM #2
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
- 07-12-2011, 09:02 AM #3
Member
- Join Date
- Jun 2011
- Posts
- 6
- Rep Power
- 0
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
- 07-12-2011, 10:14 AM #4
What's on line 6?
db
- 07-12-2011, 10:28 AM #5
Member
- Join Date
- Jun 2011
- Posts
- 6
- Rep Power
- 0
This is line 6 -
System.out.println("Percentage change in price: " + stock.getchangePercent());
- 07-12-2011, 10:55 AM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,375
- Blog Entries
- 7
- Rep Power
- 17
Look at the previous line (see above); it doesn't make sense, i.e. you need to call a constructor for that class.Java Code:Stock stock = new Stock System.out.println("Percentage change in price: " + stock.getchangePercent()); } }
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 07-12-2011, 11:05 AM #7
Member
- Join Date
- Jul 2011
- Posts
- 26
- Rep Power
- 0
@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());
}
}
- 07-12-2011, 01:18 PM #8
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.
- 07-12-2011, 01:32 PM #9
Member
- Join Date
- Jul 2011
- Posts
- 26
- Rep Power
- 0
@Dark : sure will take ur suggestion.
- 07-13-2011, 04:40 AM #10
Member
- Join Date
- Jun 2011
- Posts
- 6
- Rep Power
- 0
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
-
Webcrawling www.stock-forecasting.com
By devasia1000 in forum NetworkingReplies: 0Last Post: 02-12-2011, 11:25 AM -
Unable to retrieve stock quotes from yahoo
By the reporter in forum AWT / SwingReplies: 4Last Post: 06-03-2010, 04:10 PM -
Need a Main for a Stock Managing program !!
By Sary in forum New To JavaReplies: 3Last Post: 05-30-2010, 07:57 PM -
Stock exchange program
By askinne2 in forum New To JavaReplies: 2Last Post: 05-02-2010, 11:25 PM -
Stock Quotes
By BartDR in forum Advanced JavaReplies: 0Last Post: 09-15-2009, 09:15 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks