| | |
Need Help on some methods
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Jun 2006
Posts: 2
Reputation:
Solved Threads: 0
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:
Im not worry about the output. I know how to do that part but the methods is what i need help on....again 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
•
•
Join Date: Jun 2006
Posts: 3
Reputation:
Solved Threads: 0
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:
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:
I hope this helps.
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)
public class Purchase { private double quantity; private double price; public Purchase(int quantity, double price) { this.quantity = quantity; this.price = price; } public double getQuantity() { return quantity; } public double getPrice() { return price; } public double getCost() { double cost = getQuantity() * getPrice(); return cost; } }
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)
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) { java.util.Iterator it = m_purchases.iterator(); 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"); while (it.hasNext()) { Purchase pr = (Purchase)it.next(); System.out.printf("%-15s%-15s%-15s%-15s%n" ,"Qty",pr.getPrice(),pr.getCost(),calculateGain(price)); } } }
I hope this helps.
![]() |
Similar Threads
- get/set Methods in java (Java)
- "Are pop-up windows dead, are there alternative methods? (Site Layout and Usability)
- Hacker methods? (Geeks' Lounge)
- Need help Using "get and set methods" (Java)
- calling methods. (Java)
Other Threads in the Java Forum
- Previous Thread: I hava trouble with Microsoft Access database, Pls help me.
- Next Thread: Text Being Written Over The Top Of Each Other
| Thread Tools | Search this Thread |
2dgraphics account android api apple applet application array arrays automation banking binary binarytree bluetooth chat chatprogramusingobjects class classes client code component database derby design draw eclipse encryption error event exception fractal game givemetehcodez graphics gui homework html ide if_statement image inheritance input integer interface j2me java javadesktopapplications javaprojects jlabel jni jpanel jtextfield julia linux list loop map method methods midlethttpconnection mobile monitoring netbeans newbie nullpointerexception open-source oracle print printing problem program programming project property recursion reference ria scanner screen search server set size sms sort sourcelabs splash sql sqlite stop string swing testautomation threads time tree ui unicode validation windows





