Results 1 to 3 of 3
Thread: constructor messes up?
- 08-08-2011, 08:18 PM #1
Member
- Join Date
- Jun 2011
- Posts
- 2
- Rep Power
- 0
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); } }
- 08-08-2011, 08:40 PM #2
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
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.
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.Java Code:public class Test{ private int x; //Notice it's not initialized, just declared public Test(int x){ this.x = x; } //setters //getters }
If you want default values for your instance variables, you should do that in the default constructor.
- 08-09-2011, 10:12 AM #3
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
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.
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:
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).Java Code:private int x; //Notice it's not initialized, just declared
As for the OP itself, there's a lot of strangeness going on...just to pick one:
What is this supposed to be doing?Java Code:new Investment(principal,interestRate);
Similar Threads
-
Constructor
By theoneroo in forum New To JavaReplies: 5Last Post: 01-28-2011, 09:37 AM -
What does this Constructor Do ?
By Ciwan in forum New To JavaReplies: 1Last Post: 10-02-2010, 06:24 PM -
Constructor
By Sarinam in forum AWT / SwingReplies: 1Last Post: 06-19-2008, 08:03 AM -
Calling constructor of parent class from a constructor
By Java Tip in forum Java TipReplies: 0Last Post: 12-19-2007, 09:10 AM -
Calling constructor of same class from a constructor
By Java Tip in forum Java TipReplies: 0Last Post: 12-19-2007, 09:01 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks