Results 1 to 4 of 4
Thread: Problem with constructors
- 02-07-2013, 08:45 PM #1
Member
- Join Date
- Dec 2012
- Posts
- 24
- Rep Power
- 0
Problem with constructors
Hey my problem is I need to use constructors to assign values to variables from 1 class to another. So i have 3 different constructors 1 with no args 1 with 2 args and 1 with 4 (all args). I know its simple but how do iset values to variables in other classes use "set" methods if there is no args. I can't just reference it by using an object then calling the method.
Main class
Java Code:public class Stock { // instance variables private String symbol; private String name; private double previousClosingPrice; private double currentPrice; //*********************** WRITE YOUR CONSTRUCTORS HERE ******************************** // constructors public Stock(){ symbol = null; name = null; previousClosingPrice = 0; currentPrice = 0; } public Stock(String newName,String newSymbol){ name = newName; symbol = newSymbol; } public Stock(String newSymbol,String newName,Double newPreviousClosingPrice,Double newCurrentPrice){ symbol = newSymbol; name = newName; previousClosingPrice = newPreviousClosingPrice; currentPrice = newCurrentPrice; } //************************************************************************************* // accessors (get methods) public String getSymbol() { return symbol; } public String getName() { return name; } public double getPreviousClosingPrice() { return previousClosingPrice; } public double getCurrentPrice() { return currentPrice; } // mutators (set methods) public void setSymbol( String newSymbol ) { symbol = newSymbol; } public void setName( String newName ) { name = newName; } public void setPreviousClosingPrice( double newPreviousPrice ) { previousClosingPrice = newPreviousPrice; } public void setCurrentPrice( double newCurrentPrice ) { currentPrice = newCurrentPrice; } // toString method public String toString() { return "Stock name: " + name + "\n" + "Symbol: " + symbol + "\n" + "Previous price: " + previousClosingPrice + "\n" + "Current price: " + currentPrice; } // domain specific method public double computePercentChange() { return (currentPrice - previousClosingPrice)/previousClosingPrice*100; } }
Tester class
All of the times I use an object with a method from the other class I get an error of course. Any help is appreciated thanks.Java Code:public class TestStock { Stock stock1 = new Stock(); stock1.setName("SUN"); Stock stock2 = new Stock(); Stock stock3 = new Stock("Mic","Microphone"); stock3.setPreviousClosingPrice(50.0); stock3.setCurrentPrice(75.0); Stock stock4 = new Stock("Win","Windows"); stock4.setPreviousClosingPrice(100.0); stock4.setCurrentPrice(200.0); Stock stock5 = new Stock(); Stock stock6 = new Stock();
- 02-07-2013, 08:51 PM #2
Senior Member
- Join Date
- Oct 2011
- Location
- Sweden
- Posts
- 123
- Rep Power
- 0
Re: Problem with constructors
I don't really understand what your question is. You seem to answer it yourself:
Ask yourself then why you try to use a method in a class, with an object that isn't of the same class?All of the times I use an object with a method from the other class I get an error of course.
- 02-07-2013, 08:54 PM #3
Member
- Join Date
- Dec 2012
- Posts
- 24
- Rep Power
- 0
Re: Problem with constructors
I know its wrong so what do I do how do I assign values to those variables (using constructors). I just need to know how to do it right basically
- 02-07-2013, 09:11 PM #4
Senior Member
- Join Date
- Oct 2011
- Location
- Sweden
- Posts
- 123
- Rep Power
- 0
Re: Problem with constructors
Well, aren't you already doing it?
You have the following variables:
They are being assigned when you use the contructor:Java Code:private String symbol; private String name; private double previousClosingPrice; private double currentPrice;
Have you tried it?Java Code:public Stock(String newSymbol,String newName,Double newPreviousClosingPrice,Double newCurrentPrice){ symbol = newSymbol; name = newName; previousClosingPrice = newPreviousClosingPrice; currentPrice = newCurrentPrice; }
Java Code:public static void main(String args[]){ Stock stockOne = new Stock("testSymbol", "testName", 9.5, 8.5); System.out.println(stockOne.getCurrentPrice()); //This will print out 8.5 }
Similar Threads
-
Constructors
By cups in forum New To JavaReplies: 1Last Post: 02-15-2012, 11:55 AM -
problem with constructors
By digital bath in forum EclipseReplies: 1Last Post: 10-14-2009, 03:49 AM -
constructors?
By shroomiin in forum New To JavaReplies: 4Last Post: 10-13-2009, 02:14 PM -
Constructors
By new2java2009 in forum New To JavaReplies: 5Last Post: 08-18-2009, 06:46 AM -
constructors
By khamuruddeen in forum New To JavaReplies: 2Last Post: 12-01-2007, 03:15 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks