Results 1 to 4 of 4
  1. #1
    codyjava is offline Member
    Join Date
    Dec 2012
    Posts
    42
    Rep Power
    0

    Default 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
    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();
    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.

  2. #2
    Zyril is offline Senior Member
    Join Date
    Oct 2011
    Location
    Sweden
    Posts
    124
    Rep Power
    0

    Default Re: Problem with constructors

    I don't really understand what your question is. You seem to answer it yourself:

    All of the times I use an object with a method from the other class I get an error of course.
    Ask yourself then why you try to use a method in a class, with an object that isn't of the same class?

  3. #3
    codyjava is offline Member
    Join Date
    Dec 2012
    Posts
    42
    Rep Power
    0

    Default 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

  4. #4
    Zyril is offline Senior Member
    Join Date
    Oct 2011
    Location
    Sweden
    Posts
    124
    Rep Power
    0

    Default Re: Problem with constructors

    Well, aren't you already doing it?

    You have the following variables:

    Java Code:
     private String symbol;
          private String name;
          private double previousClosingPrice;
          private double currentPrice;
    They are being assigned when you use the contructor:

    Java Code:
    public Stock(String newSymbol,String newName,Double newPreviousClosingPrice,Double newCurrentPrice){
        symbol = newSymbol;
        name = newName;
        previousClosingPrice = newPreviousClosingPrice;
        currentPrice = newCurrentPrice;
    }
    Have you tried it?

    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

  1. Constructors
    By cups in forum New To Java
    Replies: 1
    Last Post: 02-15-2012, 11:55 AM
  2. problem with constructors
    By digital bath in forum Eclipse
    Replies: 1
    Last Post: 10-14-2009, 03:49 AM
  3. constructors?
    By shroomiin in forum New To Java
    Replies: 4
    Last Post: 10-13-2009, 02:14 PM
  4. Constructors
    By new2java2009 in forum New To Java
    Replies: 5
    Last Post: 08-18-2009, 06:46 AM
  5. constructors
    By khamuruddeen in forum New To Java
    Replies: 2
    Last Post: 12-01-2007, 03:15 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •