944,179 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 1326
  • Java RSS
Jun 8th, 2006
0

Need Help on some methods

Expand Post »
Hi, I need some help with some method in my classes. I started on some if not all of them. I just cant seem to find what im doing wrong here. Any help would be greatly excepted. Thanks

Here is code:

 

import


java.util.Scanner;

 
 
 

public


class Assignment3

 
{
public static void main(String args[])
{
Scanner scan = new Scanner(System.in);
 
System.out.println("Enter the name of the stock:");
String name = scan.nextLine();
 
System.out.println("Enter the symbol of the stock:");
String symbol = scan.next();
 
Stock stock = new Stock(name, symbol);
 
System.out.println("Enter the quantity of stock purchased (-1 to quit):"); 
int quantity = scan.nextInt();
 
while (quantity > 0)
{
System.out.println("Enter the price of the stock purchased:"); 
double price = scan.nextDouble();
 
Purchase purchase = new Purchase(quantity, price);
stock.addPurchase(purchase);
 
System.out.println("Enter the quantity of stock purchased (-1 to quit):"); 
quantity = scan.nextInt();
}
 
System.out.println("Enter the current stock price:"); 
double currentPrice = scan.nextDouble();
stock.printReport(currentPrice);
}
}
==========================================
 

import


java.math.BigDecimal;

 
 
 
 

public


class Purchase 

 
{
private BigDecimal m_quantity;
private BigDecimal m_price;
 
public Purchase(int quantity, double price)
{
quantity = 0;
price = 0;
}
 
public int getQuantity()
{
int quantity = m_quantity.intValue();
return quantity;
 
}
 
public double getPrice()
{
double price = m_price.doubleValue();
return price;
}
 
public double getCost()
{
BigDecimal c = m_quantity.multiply(m_price);
double cost = c.doubleValue();
return cost;
}
 
 

/* public double getCostAtPrice()

 
{ 
BigDecimal c2 = m_quantity.multiply(******);
double cost2 = c2.doubleValue();
 
return cost2;
}
 
public double getGainAtPrice(double price)
{ 
BigDecimal c = m_quantity.multiply(m_price);
BigDecimal c2 = m_quantity.multiply(******);
double gain = c.doubleValue()- c2.doubleValue();
return gain;
} 
*/
}
=========================================
 

import


java.util.ArrayList;

 
 
 

public


class Stock

 
{
private String m_name = null;
private String m_symbol = null;
private ArrayList<Purchase> m_purchases; 
 
public Stock(String name, String symbol)
{
m_name = name;
m_symbol = symbol;
m_purchases = new ArrayList<Purchase>();
}
public String getName()
{
return m_name;
}
public String getSymbol()
{
return m_symbol;
}
 
public void addPurchase(Purchase p)
{
m_purchases.add(p);
}
public double calculateGain(double price)
{ 
for (Purchase p : m_purchases)
{ 
price = price + price;
 
}
return price;
}
public void printReport(double price)
{
 
System.out.println("");
System.out.printf("-----------------------------------------------------%n");
System.out.printf("Report for %s (%s) @$%s/share %n",m_name, m_symbol,price );
System.out.printf("-----------------------------------------------------%n");
System.out.printf("%-15s%-15s%-15s%-15s%n" ,"Qty","Price","Cost","Gain");
 
System.out.printf("-----------------------------------------------------%n");
System.out.printf("%-45s%-15s%n" ,"Total Gain:","$",calculateGain(price));
} 
}

Im not worry about the output. I know how to do that part but the methods is what i need help on....again thanks
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Bonjava is offline Offline
2 posts
since Jun 2006
Jun 8th, 2006
0

Re: Need Help on some methods

Do what part? What method is wrong? What's it suppose to do and what is it doing instead? Do just say "here's my code, fix it".
Reputation Points: 92
Solved Threads: 51
Practically a Posting Shark
Phaelax is offline Offline
856 posts
since Mar 2004
Jun 8th, 2006
0

Re: Need Help on some methods

The methods I'm talking about are getCostAtPrice, getGainAtPrice and calculateGain. These are the methods that are giving me trouble.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Bonjava is offline Offline
2 posts
since Jun 2006
Jun 16th, 2006
0

Re: Need Help on some methods

Hi, Bonjava,

I looked at your code. There are a few problems there.

1. Assignment3 class is OK.
2. Purchase class.

If you use getters and setters for proerties, the proterty name should match the getters and setters. If you getter is called "getQuantity" then the property name must be "quantity".



In the constructor with arguments you have:

public Purchase(int quantity, double price)
{
quantity = 0;
price = 0;
}

All it does is setting the passed arguments to zeros. This should
set the preprty of a specific instance of Purchase class to passed
values like:

this.quantity = quantity;
this.price = price;

If it confuses you, change the names of the parameters.

Data types.
I believe simple primitive types are sufficient here - double for quantity and int for price.


The changed Purchase class will look like this:
Java Syntax (Toggle Plain Text)
  1. public class Purchase {
  2. private double quantity;
  3. private double price;
  4.  
  5. public Purchase(int quantity, double price)
  6. {
  7. this.quantity = quantity;
  8. this.price = price;
  9. }
  10.  
  11. public double getQuantity() {
  12. return quantity;
  13. }
  14.  
  15. public double getPrice() {
  16. return price;
  17. }
  18.  
  19. public double getCost()
  20. {
  21. double cost = getQuantity() * getPrice();
  22. return cost;
  23. }
  24. }

3. Stock class.

I believe what you want is to print the values for price, cost and
the value, returned from calculateGain() function (I am not sure
what you wanted to achieve though).

What you need to do is to iterate through created Purchase classes
and get the values form each one. This is the modified version of
Stock class:

Java Syntax (Toggle Plain Text)
  1. import java.util.ArrayList;
  2.  
  3. public class Stock {
  4. private String m_name = null;
  5. private String m_symbol = null;
  6. private ArrayList<Purchase> m_purchases;
  7.  
  8. public Stock(String name, String symbol)
  9. {
  10. m_name = name;
  11. m_symbol = symbol;
  12. m_purchases = new ArrayList<Purchase>();
  13. }
  14.  
  15. public String getName() {
  16. return m_name;
  17. }
  18. public String getSymbol() {
  19. return m_symbol;
  20. }
  21.  
  22. public void addPurchase(Purchase p) {
  23. m_purchases.add(p);
  24. }
  25. public double calculateGain(double price) {
  26. for (Purchase p : m_purchases)
  27. {
  28. price = price + price;
  29. }
  30. return price;
  31. }
  32. public void printReport(double price)
  33. {
  34.  
  35. java.util.Iterator it = m_purchases.iterator();
  36.  
  37. System.out.println("");
  38. System.out.printf("-----------------------------------------------------%n");
  39. System.out.printf("Report for %s (%s) @$%s/share %n",m_name, m_symbol,price );
  40. System.out.printf("-----------------------------------------------------%n");
  41. System.out.printf("%-15s%-15s%-15s%-15s%n" ,"Qty","Price","Cost","Gain");
  42. System.out.printf("-----------------------------------------------------%n");
  43.  
  44. while (it.hasNext()) {
  45. Purchase pr = (Purchase)it.next();
  46. System.out.printf("%-15s%-15s%-15s%-15s%n" ,"Qty",pr.getPrice(),pr.getCost(),calculateGain(price));
  47. }
  48. }
  49. }

I hope this helps.
yvs
Reputation Points: 10
Solved Threads: 0
Newbie Poster
yvs is offline Offline
3 posts
since Jun 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: I hava trouble with Microsoft Access database, Pls help me.
Next Thread in Java Forum Timeline: Text Being Written Over The Top Of Each Other





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC