Need Help on some methods

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jun 2006
Posts: 2
Reputation: Bonjava is an unknown quantity at this point 
Solved Threads: 0
Bonjava Bonjava is offline Offline
Newbie Poster

Need Help on some methods

 
0
  #1
Jun 8th, 2006
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
Reply With Quote Quick reply to this message  
Join Date: Mar 2004
Posts: 765
Reputation: Phaelax is on a distinguished road 
Solved Threads: 38
Phaelax Phaelax is offline Offline
Master Poster

Re: Need Help on some methods

 
0
  #2
Jun 8th, 2006
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".
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 2
Reputation: Bonjava is an unknown quantity at this point 
Solved Threads: 0
Bonjava Bonjava is offline Offline
Newbie Poster

Re: Need Help on some methods

 
0
  #3
Jun 8th, 2006
The methods I'm talking about are getCostAtPrice, getGainAtPrice and calculateGain. These are the methods that are giving me trouble.
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 3
Reputation: yvs is an unknown quantity at this point 
Solved Threads: 0
yvs yvs is offline Offline
Newbie Poster

Re: Need Help on some methods

 
0
  #4
Jun 16th, 2006
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:
  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:

  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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC