Chief Delphi

Chief Delphi (http://www.chiefdelphi.com/forums/index.php)
-   Programming (http://www.chiefdelphi.com/forums/forumdisplay.php?f=51)
-   -   Java headache problem (http://www.chiefdelphi.com/forums/showthread.php?t=36774)

MisterX 30-03-2005 10:28

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

Ryan M. 30-03-2005 10:36

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. :)

MisterX 30-03-2005 10:51

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);
}
}

dm0ney 30-03-2005 10:51

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.

AIBob 30-03-2005 10:52

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.

dm0ney 30-03-2005 10:58

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?

MisterX 30-03-2005 11:02

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.

AIBob 30-03-2005 11:08

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

dm0ney 30-03-2005 11:09

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.


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

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