Go to Post Without compromise, engineering would be boring. - Mike Betts [more]
Home
Go Back   Chief Delphi > Technical > Programming
CD-Media   CD-Spy  
portal register members calendar search Today's Posts Mark Forums Read FAQ rules

 
Closed Thread
Thread Tools Rate Thread Display Modes
  #1   Spotlight this post!  
Unread 30-03-2005, 10:28
MisterX's Avatar
MisterX MisterX is offline
Alumni
AKA: Mr. X
FRC #0521
Team Role: College Student
 
Join Date: Feb 2003
Rookie Year: 2000
Location: Around
Posts: 486
MisterX is a splendid one to beholdMisterX is a splendid one to beholdMisterX is a splendid one to beholdMisterX is a splendid one to beholdMisterX is a splendid one to beholdMisterX is a splendid one to behold
Java headache problem

I am having problems with this program any help would be much appreciated:

Write a program named SellStocks that calculates the value of a stock sale. The user will be asked to enter the stock price, the number of shares to be sold, and the commission rate. The program will calculate the value of the shares by multiplying the stock price by the number of shares. It will also calculate the commission (the value of the shares multiplied by the commission rate) and the net proceeds (the value of the shares minus the commission). The following example shows what the user will see on the output command prompt.
This program calculates the net proceeds from a sale of stock.
Enter stock price: 10.125
Enter number of shares: .11
Value of shares: $111.38
Enter commission rate (as a percentage): 1.5
Commission: $1.67
Net proceeds: $109.71

I would post my program for you to decipher but at last compilation it had 31 errors so it is best to just treat it as a blank slate :0
  #2   Spotlight this post!  
Unread 30-03-2005, 10:36
Ryan M. Ryan M. is offline
Programming User
FRC #1317 (Digital Fusion)
Team Role: Programmer
 
Join Date: Jan 2004
Rookie Year: 2004
Location: Ohio
Posts: 1,508
Ryan M. has much to be proud ofRyan M. has much to be proud ofRyan M. has much to be proud ofRyan M. has much to be proud ofRyan M. has much to be proud ofRyan M. has much to be proud ofRyan M. has much to be proud ofRyan M. has much to be proud ofRyan M. has much to be proud of
Re: Java headache problem

Well... dern. You want it in Java? Ya sure about that? I could do it in seconds in C, C++, or PHP...

So, I'll just say "Could I see the source code please?" and hope I can spot the error(s) there.
__________________

  #3   Spotlight this post!  
Unread 30-03-2005, 10:51
MisterX's Avatar
MisterX MisterX is offline
Alumni
AKA: Mr. X
FRC #0521
Team Role: College Student
 
Join Date: Feb 2003
Rookie Year: 2000
Location: Around
Posts: 486
MisterX is a splendid one to beholdMisterX is a splendid one to beholdMisterX is a splendid one to beholdMisterX is a splendid one to beholdMisterX is a splendid one to beholdMisterX is a splendid one to behold
Re: Java headache problem

feast your eyes on 31 errors of pure Java!
File 1:
public class SellStock
{
public SellStock()
{
price = 0;
numbers = 0;
value = 0;
commission = 0;
rate = 0;
proceeds = 0;
}
public void addPrice(double count)
{
price = price + count;
}
public void addNumbers(int count)
{
numbers = numbers + count;
}
public void addRate(double count)
{
rate = rate + count;
}
public void addValue()
{
value = price * numbers
}
public void addCommission (double rate)
{
commission = value * (rate/100);
}
public double getProceeds()
{
return value - commission;
}
}

File 2:

public class SellStockTest
{
public static void main(String[] args)
{
SellStock iSellStock = new SellStock();
iSellStock.addPrice(10.125);
iSellStock.addNumbers(11);
iSellStock.addCommission(1.5);
double totalValue = iSellStock.getProceeds();
System.out.print("This program calculates the net proceeds from a sale of stock to be:");
System.out.println(getProceeds);
}
}

Last edited by MisterX : 30-03-2005 at 11:01.
  #4   Spotlight this post!  
Unread 30-03-2005, 10:51
dm0ney's Avatar
dm0ney dm0ney is offline
Will Code For Food (Food Optional)
AKA: Deepak Mishra
None #0217 (The ThunderChickens)
Team Role: Alumni
 
Join Date: Jan 2005
Rookie Year: 2004
Location: Shelby Twp., MI
Posts: 48
dm0ney will become famous soon enough
Send a message via AIM to dm0ney
Re: Java headache problem

Quote:
Originally Posted by Ryan M.
Well... dern. You want it in Java? Ya sure about that? I could do it in seconds in C, C++, or PHP...

So, I'll just say "Could I see the source code please?" and hope I can spot the error(s) there.
If you can do it in seconds with C++, theres NO reason you cant do it equally as fast in Java.

I'll say the same thing: source code would be tres easier for us.
__________________

Alumni
Team #217, The ThunderChickens



Student, Class of 2009
California Institute of Technology


  #5   Spotlight this post!  
Unread 30-03-2005, 10:52
AIBob's Avatar
AIBob AIBob is offline
AI Programmer
AKA: Bob Frank DOT org
FRC #0358 (Hauppauge Robotic Eagles)
Team Role: Alumni
 
Join Date: Jan 2005
Rookie Year: 2003
Location: Long Island, NY (in Binghamton now)
Posts: 297
AIBob is a splendid one to beholdAIBob is a splendid one to beholdAIBob is a splendid one to beholdAIBob is a splendid one to beholdAIBob is a splendid one to beholdAIBob is a splendid one to beholdAIBob is a splendid one to behold
Send a message via ICQ to AIBob Send a message via AIM to AIBob Send a message via MSN to AIBob Send a message via Yahoo to AIBob
Re: Java headache problem

Quote:
Originally Posted by MisterX
I am having problems with this program any help would be much appreciated:
...
I would post my program for you to decipher but at last compilation it had 31 errors so it is best to just treat it as a blank slate :0
Don't just treat it as a blank slate because you have 31 errors, have had programs that had many errors that have been caused by small little errors, like forgetting to define a constant or including a library.
It is also a good idea to learn from your mistakes, so you do not make the same mistakes again..
If you post the code, I am sure someone would be able to spot the errors within just a small amount of time.
EDIT: I see you posted the code while I was typing.
__________________
- from B B frank

  #6   Spotlight this post!  
Unread 30-03-2005, 10:58
dm0ney's Avatar
dm0ney dm0ney is offline
Will Code For Food (Food Optional)
AKA: Deepak Mishra
None #0217 (The ThunderChickens)
Team Role: Alumni
 
Join Date: Jan 2005
Rookie Year: 2004
Location: Shelby Twp., MI
Posts: 48
dm0ney will become famous soon enough
Send a message via AIM to dm0ney
Re: Java headache problem

Quote:
Originally Posted by MisterX
feast your eyes on 31 errors of pure Java!

public class SellStock
{
public SellStock()
{
price = 0;
numbers = 0;
value = 0;
commission = 0;
rate = 0;
proceeds = 0;
}
public void addPrice(double count)
{
price = price + count;
}
public void addNumbers(int count)
{
numbers = numbers + count;
}
public void addRate(double count)
{
rate = rate + count;
}
public void addValue()
{
value = price * numbers
}
public void addCommission (double rate)
{
commission = value * (rate/100);
}
public double getProceeds()
{
return value - commission;
}
}

public class SellStockTest
{
public static void main(String[] args)
{
SellStock iSellStock = new SellStock();
iSellStock.addPrice(10.125);
iSellStock.addNumbers(11);
iSellStock.addCommission(1.5);
double totalValue = iSellStock.getProceeds();
System.out.print("This program calculates the net proceeds from a sale of stock to be:");
System.out.println(getProceeds);
}
}
SellStock should not be a public class, only class. You cannot have two public classes declared in the same file.

Next, missing a semicolon on in the addValue() function.

Finally, getProceeds in println is a function so getProceeds()

I'll check for logic errors next, but those are the syntax.

Hope that helps.


Going on to some logic / style issues.

Firstly, dont always name variables count. Good practices mean using descriptive variables, also in Java, set and get are the terms used in code, not usually add.

The program was meant to take input from a terminal window according to the description, are you just testing the class itself before implementing a user input?
__________________

Alumni
Team #217, The ThunderChickens



Student, Class of 2009
California Institute of Technology



Last edited by dm0ney : 30-03-2005 at 11:03.
  #7   Spotlight this post!  
Unread 30-03-2005, 11:02
MisterX's Avatar
MisterX MisterX is offline
Alumni
AKA: Mr. X
FRC #0521
Team Role: College Student
 
Join Date: Feb 2003
Rookie Year: 2000
Location: Around
Posts: 486
MisterX is a splendid one to beholdMisterX is a splendid one to beholdMisterX is a splendid one to beholdMisterX is a splendid one to beholdMisterX is a splendid one to beholdMisterX is a splendid one to behold
Re: Java headache problem

Quote:
Originally Posted by dm0ney
SellStock should not be a public class, only class. You cannot have two public classes declared in the same file.

Next, missing a semicolon on in the addValue() function.

Finally, getProceeds in println is a function so getProceeds()

I'll check for logic errors next, but those are the syntax.

Hope that helps.
yeah thanks alreayd better off then when I started. Sorry I didnt make my self clear before that is two seperate files, 1 is for the actual calculations then the second is where I put in the data to compute and print out.
  #8   Spotlight this post!  
Unread 30-03-2005, 11:08
AIBob's Avatar
AIBob AIBob is offline
AI Programmer
AKA: Bob Frank DOT org
FRC #0358 (Hauppauge Robotic Eagles)
Team Role: Alumni
 
Join Date: Jan 2005
Rookie Year: 2003
Location: Long Island, NY (in Binghamton now)
Posts: 297
AIBob is a splendid one to beholdAIBob is a splendid one to beholdAIBob is a splendid one to beholdAIBob is a splendid one to beholdAIBob is a splendid one to beholdAIBob is a splendid one to beholdAIBob is a splendid one to behold
Send a message via ICQ to AIBob Send a message via AIM to AIBob Send a message via MSN to AIBob Send a message via Yahoo to AIBob
Re: Java headache problem

Quote:
Originally Posted by MisterX - edited in RED
feast your eyes on 31 errors of pure Java!

public class SellStock
{
double price;
double numbers;
double value;
double commission;
double rate;
double proceeds;

public SellStock()
{
//these variables need to be.
price = 0;
numbers = 0;
value = 0;
commission = 0;
rate = 0;
proceeds = 0;

}
public void addPrice(double count)
{
price = price + count;
}
public void addNumbers(int count)
{
numbers = numbers + count;
}
public void addRate(double count)
{
rate = rate + count;
}
public void addValue()
{
value = price * numbers; //don't miss semi-colons
}
public void addCommission (double rate)
{
commission = value * (rate/100);
}
public double getProceeds()
{
return value - commission;
}
}
/*
public
*/
class SellStockTest
{
public static void main(String[] args)
{
SellStock iSellStock = new SellStock();
iSellStock.addPrice(10.125);
iSellStock.addNumbers(11);
iSellStock.addCommission(1.5);
double totalValue = iSellStock.getProceeds();
System.out.print("This program calculates the net proceeds from a sale of stock to be:");
System.out.println(totalValue/*getProceeds*/);
}
}
Those are the only errors that were found with the compiler I used...
Hope its better
__________________
- from B B frank

  #9   Spotlight this post!  
Unread 30-03-2005, 11:09
dm0ney's Avatar
dm0ney dm0ney is offline
Will Code For Food (Food Optional)
AKA: Deepak Mishra
None #0217 (The ThunderChickens)
Team Role: Alumni
 
Join Date: Jan 2005
Rookie Year: 2004
Location: Shelby Twp., MI
Posts: 48
dm0ney will become famous soon enough
Send a message via AIM to dm0ney
Re: Java headache problem

Code:
public class SellStockTest
{
public static void main(String[] args)
{
//Declares, Instantiates new SellStock class
    SellStock iSellStock = new SellStock();
//Price of stock
    iSellStock.addPrice(10.125);
//Number of stock
    iSellStock.addNumbers(11);
//Calculate Value of Stock!
    iSellStock.addValue();
//Add commission owed from stock sales, Calculates
    iSellStock.addCommission(1.5);

//Not sure what you wanted here?
    double totalValue = iSellStock.getProceeds();
//Print out statements
System.out.print("This program calculates the net proceeds from a sale of stock to be:");
//This could either be as follows or you could printout totalValue since you declared it above.
System.out.println(iSellStock.getProceeds());
}
EDIT: must declare variables like AIBOB above said. I did that and forgot to forward that on to you.

This is an edited version of the last class. You never calculated the value of the stock, so it would output zero. I added your function call to calculate Value (addValue()) and to my knowledge it works in that implementation.

Might I suggest using setValue, setXXXX, getXXXX for variables. The addXXXX gets confusing. Also change the counts to something even iXXXX where X is the variable and set price = iPrice.

Hope that helps, Good Luck.
__________________

Alumni
Team #217, The ThunderChickens



Student, Class of 2009
California Institute of Technology


Closed Thread


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Strange Encoder Problem AIBob Electrical 3 20-02-2005 22:20
Programming Problem: Extremely Frustrating chantilly_team Programming 19 12-02-2005 23:00
Java Books Yan Wang Programming 1 27-12-2002 16:26
The problem with scouting... archiver 2001 10 23-06-2002 23:49
Major problem with chipphua motors aka Scott White Motors 18 19-03-2002 19:44


All times are GMT -5. The time now is 11:16.

The Chief Delphi Forums are sponsored by Innovation First International, Inc.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi