Results 1 to 3 of 3
  1. #1
    Maverick83 is offline Member
    Join Date
    Jun 2011
    Posts
    2
    Rep Power
    0

    Default constructor messes up?

    /*

    there is a problem in this code guys i know what the problem is but i dont know how to fix it .as you see there are 2 case i get inputs and i call constructors and initialize and set inputs.
    but in case 3 ( cl=new Investment() and cl.getFutureValueMontly()) this lines mess up everything the constructor initialize them as i initialized first.i need to make some calculations in getFutureValueMontly() i just print them and see if they are correct or not.as you see they say 0.0 and 1 etc.
    my problem is there are 2 different case i need to get data from user.i dont know how to allocate them how i can put them together and call that getFutureValueMontly() in case 3 ?



    */



    Java Code:
    import java.util.Scanner;
    
    
    public class InterestTester 
    
    {
    	
    	
    	
    	
    	public static void main(String[] args)
    	
    	{
    		
    		InterestTester it=new InterestTester();
    		
    		it.menu();
    		
    		
    		
    	}
    	
    	public void menu()
    	{
    		int choice=1;
    		double principal=0.0;
    		double interestRate=0.0;
    		int    numberOfYears=1;
    		int    numberOfTimes=1;
    		
    		
    		Investment cl;
    		Scanner sr=new Scanner(System.in);
    		System.out.println("1  Enter starting balance and interest rate ");
    		System.out.println("2  Enter the number of years and the number of times ");
    		System.out.println("3  Display the balance schedule  ");
    		
    		choice=sr.nextInt();
    		
    		switch(choice)
    		{
    		
    		
    		case 1:
    			
    		System.out.print("Please enter the starting balance : ");
    		principal=sr.nextDouble();
    		System.out.print("Please enter the interest rate : ");
    		interestRate=sr.nextDouble();
    
    		new Investment(principal,interestRate);
    		
    		
    		
    		System.out.print("\n");
    		menu();
    		
    		
    		 
    		break;
    		case 2:
    			
    			System.out.print("Please enter the number of years : ");
    			numberOfYears=sr.nextInt();
    			System.out.print("Please enter the number of times : ");
    			numberOfTimes=sr.nextInt();
    			
    				cl=new Investment(numberOfYears,numberOfTimes);
    				
    					
    			menu();
    			}
    			
    			break;
    		
    		case 3:
    			
    			
    			cl=new Investment();
    			cl.getFutureValueMontly();
    			
    		
    		break;
    		
    		}
    		
    	}
    	
    
    	
    	
    }
    
    
    
    //Investment Class start here
    
    
    
    public class Investment
    {
    	
    	private double principal=0.0;
        private double interestRate=0.0;
        private int numberOfYears=1;
      	private int numberOfTimes=1;
      	
      	 public Investment()
      	  
       	 {
       		 
       		
       	 }
       
        public Investment(double principal,double interestRate)
     	 {
     		
        	setPrincipal(principal);
        	setInterestRate(interestRate);
     	 }
       
          
       	 
       	 
       	 
       	   public Investment(int numberOfYears,int numberOfTimes)
       	  
       	 {
       		 
       		setNumberOfYears(numberOfYears);
       		setNumberOfTimes(numberOfTimes);
       	 }
       	
       
       	 public void setPrincipal(double principal)
       	 {
       		this.principal=principal;
       		
       		
       	 }
       	 
       	public double getPrinciple()
      	 {
      		
      		return this.principal;
      		
          	
      		
      	 }
       	 public void setInterestRate(double interestRate)
       	 {
       		 
       		this.interestRate=interestRate;
       		
       	 }
       	public double getinterestRate()
      	 {
      		 
      		 return interestRate;
      	 }
       	 
       	
       	public void setNumberOfYears(int numberOfYears)
      	 {
      		this.numberOfYears=numberOfYears;
      		
      		
      	 }
       	public int getNumberOfYears()
     	 {
     		return this.numberOfYears;
     		
     		
     	 }
    
       	
    
       	public void setNumberOfTimes(int numberOfTimes)
     	 {
     		this.numberOfTimes=numberOfTimes;
     		
     		
     	 }
      	public int getNumberOfTimes()
    	 {
    		return this.numberOfTimes;
    		
    		
    	 }
       	 
             public void getFutureValueMontly()
            
    	     {
            	  
            	   
            	 
            	 System.out.println(getNumberOfYears());
               	System.out.println(getinterestRate());
            	 System.out.println(getnumberOfYears);
            	System.out.println(getnumberOfTimes);
             	
        		
        	 	 
        	 	 }
    			  
                
             
    }

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Why are you using setters in the constructor? This is bad practice since the setters can be overridden in a sub class which can break your code. You should avoid using methods that can be overridden in a constructor. You also shouldn't initialize instance variables like you have, the initialization should be in the constructor.
    Java Code:
    public class Test{
      private int x;  //Notice it's not initialized, just declared
      public Test(int x){
        this.x = x;
      }
      //setters
      //getters
    }
    Why do you think it's necessary to use setters inside the constructor? It's fine to access instance variables inside the class, you provide setters for access to the classes innards to outside classes.

    If you want default values for your instance variables, you should do that in the default constructor.

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,002
    Rep Power
    20

    Default

    Quote Originally Posted by sunde887 View Post
    Why are you using setters in the constructor? This is bad practice since the setters can be overridden in a sub class which can break your code. You should avoid using methods that can be overridden in a constructor.
    Just to clarify, the last senetence is the important bit. It's not the use of setters as such, but the use of overrideable methods. There are occasions possibly, but they are rare and should be analysed to ensure that it makes sense.

    Quote Originally Posted by sunde887 View Post
    You also shouldn't initialize instance variables like you have, the initialization should be in the constructor.
    Java Code:
    public class Test{
      private int x;  //Notice it's not initialized, just declared
      public Test(int x){
        this.x = x;
      }
      //setters
      //getters
    }
    Why do you think it's necessary to use setters inside the constructor? It's fine to access instance variables inside the class, you provide setters for access to the classes innards to outside classes.

    If you want default values for your instance variables, you should do that in the default constructor.
    This is actually one of those cases where there is no hard and fast rule.
    If you have a load of constructors, and not all attributes are assigned from constructors, it makes perfect sense to assign a default to them, rather than assigning that default in each and every constructor.

    And just to be picky but:
    Java Code:
      private int x;  //Notice it's not initialized, just declared
    is not actually true. It's initialised to 0. All attributes are initialised to a default value (0 for int and long, 0.0 for float and double, false for boolean and null for objects).

    As for the OP itself, there's a lot of strangeness going on...just to pick one:
    Java Code:
    		new Investment(principal,interestRate);
    What is this supposed to be doing?

Similar Threads

  1. Constructor
    By theoneroo in forum New To Java
    Replies: 5
    Last Post: 01-28-2011, 09:37 AM
  2. What does this Constructor Do ?
    By Ciwan in forum New To Java
    Replies: 1
    Last Post: 10-02-2010, 06:24 PM
  3. Constructor
    By Sarinam in forum AWT / Swing
    Replies: 1
    Last Post: 06-19-2008, 08:03 AM
  4. Replies: 0
    Last Post: 12-19-2007, 09:10 AM
  5. Replies: 0
    Last Post: 12-19-2007, 09:01 AM

Posting Permissions

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