Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 17 of 17

Thread: Object help

  1. #1
    Member
    Join Date
    Feb 2013
    Posts
    30
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Object help

    I'm making a program and I can't get it to display the results properly in the displayInterest method in the InterestCalculatorTest class.

    InterestCalculator7 Class

    public class InterestCalculator7
    {
    		// Class Level Variables (Data Fields / Properties)
    	public double principalAmount;
    	public double interestRate;
    	public int term1;
     
     
    		  // ** Constructors **
    		  // No-Argument constructor
    	  public InterestCalculator7()
    	  {
    		  this.principalAmount = 0.0;
    		  this.interestRate = -1.0;
    		  this.term1 = 0;
    	  }
     
     
    	  public InterestCalculator7(double principalAmount, double interestRate, int term1)
    	  {
    	    this.principalAmount = principalAmount;
    	    this.interestRate = interestRate;
    	    this.term1 = term1;
    	  }
     
    		  // ** Methods **
    		   public double calculateSimpleInterest()
    		  {
    			  double calculateSimpleInterest = ((principalAmount)*(interestRate/100.0)*(term1/12.0));
    				return calculateSimpleInterest;
    		  }
     
    		   public  double calculateCompoundInterest(double term1)
    		   {
    			     double calculateCompoundInterest = (principalAmount*Math.pow((1.0+((interestRate/100.0)/term1)),(term1*(term1/12.0))))-principalAmount;
    				return calculateCompoundInterest;
    			 }
     
     
    }


    InterestCalculatorTest class

    public class InterestCalculatorTester
    {
    	public static void main(String[] args)
    	{
    		double principal;
    	    double interest;
    	    int term;
     
    	   do{
    		   principal = getPrincipalAmount();
    			interest = getInterestRate();
    			term = getTerm();
    			double calculationType = getCalculationType(principal,interest,term);
     
    			InterestCalculator7 interestCalculator = new InterestCalculator7(principal, interest, term);
     
    			if (calculationType == 1)
    			{
    				calculationType = interestCalculator.calculateSimpleInterest();
    				displayInterest(interestCalculator, "Simple", calculationType);
    			}
    			else if (calculationType == 2)
    			{
    				calculationType = interestCalculator.calculateCompoundInterest(12.0);
    				displayInterest(interestCalculator, "Monthly", calculationType);
    			}
    			else if (calculationType == 3)
    			{
    				calculationType = interestCalculator.calculateCompoundInterest(365.0);
    				displayInterest(interestCalculator, "Daily", calculationType);
    			}
    			else 
    			{
    				calculationType = interestCalculator.calculateCompoundInterest(52.0);
    				displayInterest(interestCalculator, "Weekly", calculationType);
    			}
     
     
    			} while (askYesNo("Calculate another loan interest (Yes/No)"));
     
    			System.out.print("Thanks for using Interest Calculator!");
    	  }
     
     
     
    	/** Round **/
    	  public  double round(double numb1, double numb2) {
    	    double round = ((double) Math.round(numb1*(Math.pow(10, numb2)))/(Math.pow(10, numb2)));;
    		return round;
    	  }
     
     
    	    /** display interest **/
    	  public static void displayInterest(InterestCalculator7 object, String type, double calculationType) {
     
     
    		System.out.println("Principal Amount    Interest Rate    Term    Calculation Type    Calculated Interest    Total Repayment");
    		System.out.println("----------------    -------------    ----    ----------------    -------------------    ---------------");
    		System.out.print(getPrincipalAmount()+"                ");
    		System.out.print(+getInterestRate()+"           ");
    		System.out.print(+getTerm()+"           ");
    		System.out.print(+type+"            ");
    		System.out.print(+calculationType+"              ");
    		System.out.println(+(getPrincipalAmount() + calculationType));
    	  }
     
    	  /** Get principal amount **/
    	  public static double getPrincipalAmount() {
    		  Scanner input = new Scanner(System.in);
    		  double numb2 = 1;
    		 do{System.out.print("Enter Loan Amount: ");
    		   numb2 = input.nextDouble();
    		   if(numb2 > 0);
     
    		   		else{	
    			    		System.out.println("Data Error: Loan amount must be greater than zero. You entered " +numb2);
    		   		}		
    			   }while (numb2 < 0);
    		return numb2;
    	  }
     
    	  /** Get interest rate **/
    	  public static double getInterestRate() {
    		  Scanner input = new Scanner(System.in);
    		  double numb2=1;
    		  do{System.out.print("Enter Yearly Interest Rate (1 to 100 percent): ");
    		    numb2 = input.nextDouble(); 
    		  double getInterestRate = 0;
    		 if (numb2 >= 0 && numb2 <= 100)
    		  getInterestRate = numb2;
    		   		else{	
    			    		System.out.println("Data Error: Interest rate must be greater than or equal to zero and less than or equal to 100. You entered " +numb2);
    		   		}		
    			   }while (numb2 <= 0 || numb2 >= 100);
    		return numb2;
    	  }
     
    	  /** Get term **/
    	  public static int getTerm() {
    		  Scanner input = new Scanner(System.in);
    		  int numb2=1;
    		  do{System.out.print("Enter the Term (in months): ");
    		    numb2 = input.nextInt();
    		  double getTerm = 0;
    		  if (numb2 > 0)
    		  getTerm = numb2;
    		   		else{	
    			    		System.out.println("Data Error: Loan amount must be greater than zero. You entered " +numb2);
    		   		}		
    			   }while (numb2 <= 0);
    		return numb2;
    	  }
     
    	  /** Get calculation type **/
    	  public static double getCalculationType(double numb1, double numb2, double numb3) {
    		  Scanner input = new Scanner(System.in);
    		  int type;
    		 do{
    			  System.out.print("Enter Interest Calculation Type (1 – Simple, 2 – Monthly Compounded, 3 – Daily Compounded, 4 - Weekly Compounded):");
    		      type = input.nextInt();
    		      double principle =  numb1;
    			  double interest= numb2;
    			  double term= numb3;
     
     
    		 } while (type !=1 && type !=2 && type !=3 && type !=4);
     
    	return type;
    	  }
     
    	  /**ask yes or no **/
    	  public static boolean askYesNo(String question) {
    		  Scanner input = new Scanner(System.in);
    		  String enteredText;
    		  boolean isAnswerValid;
     
    		  do{
    			  isAnswerValid = false;
    			  System.out.println(question);
    			  enteredText = input.nextLine();
     
    			  if (enteredText.length() > 0)
    			  {
    				  enteredText = enteredText.toUpperCase();
     
    				  if(enteredText.equals("YES") || enteredText.equals("Y") || enteredText.equals("NO") || enteredText.equals("N"))
    				  {
    					  isAnswerValid = true;
    				  }
    			  }
     
    			  if(isAnswerValid == false)
    			  {
    				  System.out.println("Please enter 'Yes' or 'No'?");
    			  }
     
    		  } while(isAnswerValid == false);
     
    		  if(enteredText.equals("YES") || enteredText.equals("Y"))
    		  {
    			  return true;
    		  }
     
    		  return false;
    	  }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Object help

    I can't get it to display the results properly
    Post what is currently displayed and post what it should look like. Put the output in code tags to preserve spaces and formatting.
    Describe what the differences are.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Feb 2013
    Posts
    30
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Object help

    This is the correct output is supposed to be

    Enter loan amount: 1375
    Enter yearly interest rate (0 to 100 percent): 1.175
    Enter the term in number of months: 7
    Enter Interest Calculation Type (1 – Simple, 2 – Monthly Compounded, 3 – Daily Compounded, 4 – Weekly Compounded): 1
    Principal Amount Interest Rate Term Calculation Type Calculated Interest Total Repayment Amount
    ---------------- ------------- ---- ---------------- ------------------- ----------------------
    $1375.0 1.175 7 Simple $9.42448 $1384.42
    Calculate another loan interest (Yes/No)? n
    Thank you for using the Interest Calculator!

    I need to take the principal amount, Interest rate, and term from the interestCalculator7. I'm just confused on how to call the values of the principal amount, interest rate, and term and use them in the displayInterest method

    public static void displayInterest(InterestCalculator7 object, String type, int calculationType) {
     
     
    		System.out.println("Principal Amount    Interest Rate    Term    Calculation Type    Calculated Interest    Total Repayment");
    		System.out.println("----------------    -------------    ----    ----------------    -------------------    ---------------");
    		System.out.print(getPrincipalAmount()+"                ");
    		System.out.print(+getInterestRate()+"           ");
    		System.out.print(+getTerm()+"           ");
    		System.out.print(word1+"            ");
    		System.out.print(+calculationType+"              ");
    		System.out.println(+(getPrincipalAmount() + calculationType));

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Object help

    What does the program's current output look like?

    What is wrong with the current program's output?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Feb 2013
    Posts
    30
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Object help

    The current output looks like this

    Enter Loan Amount: 10
    Enter Yearly Interest Rate (1 to 100 percent): 10
    Enter the Term (in months): 10
    Enter Interest Calculation Type (1 – Simple, 2 – Monthly Compounded, 3 – Daily Compounded, 4 - Weekly Compounded):1
    Principal Amount Interest Rate Term Calculation Type Calculated Interest Total Repayment
    ---------------- ------------- ---- ---------------- ------------------- ---------------
    Enter Loan Amount:

    It doesn't input any of the values that the user inputs when asked and doesn't do the calculations and the Do while loop doesn't prompt the user to "enter another loan? (yes/no)

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Object help

    The code has syntax errors that keep me from being able to execute it.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Member
    Join Date
    Feb 2013
    Posts
    30
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Object help

    Here's what I have, hopefully this works.

    InterestCalculator class
    public class InterestCalculator7
    {
    		// Class Level Variables (Data Fields / Properties)
    	public double principalAmount;
    	public double interestRate;
    	public int term1;
     
     
    		  // ** Constructors **
    		  // No-Argument constructor
    	  public InterestCalculator7()
    	  {
    		  this.principalAmount = 0.0;
    		  this.interestRate = -1.0;
    		  this.term1 = 0;
    	  }
     
     
    	  public InterestCalculator7(double principalAmount, double interestRate, int term1)
    	  {
    	    this.principalAmount = principalAmount;
    	    this.interestRate = interestRate;
    	    this.term1 = term1;
    	  }
     
    		  // ** Methods **
    		   public double calculateSimpleInterest()
    		  {
    			  double calculateSimpleInterest = ((principalAmount)*(interestRate/100.0)*(term1/12.0));
    				return calculateSimpleInterest;
    		  }
     
    		   public  double calculateCompoundInterest(double term1)
    		   {
    			     double calculateCompoundInterest = (principalAmount*Math.pow((1.0+((interestRate/100.0)/term1)),(term1*(term1/12.0))))-principalAmount;
    				return calculateCompoundInterest;
    			 }
     
     
    }

    InterestCalculatortester class
    import java.util.Scanner;
    public class InterestCalculatorTester
    {
    	public static void main(String[] args)
    	{
    		double principal;
    	    double interest;
    	    int term;
     
    	   do{
     
    		   principal = getPrincipalAmount();
    		   interest = getInterestRate();
    		   term = getTerm();
    		   int type = getCalculationType();
    		   InterestCalculator7 interestCalculator = new InterestCalculator7(principal, interest, term);
     
     
    			double totalInterest;
    			if (type == 1)
    			{
    				totalInterest = interestCalculator.calculateSimpleInterest();
    				displayInterest(interestCalculator, "Simple", type);
    			}
    			else if (type == 2)
    			{
    				totalInterest = interestCalculator.calculateCompoundInterest(12.0);
    				displayInterest(interestCalculator, "Monthly", type);
    			}
    			else if (type == 3)
    			{
    				totalInterest = interestCalculator.calculateCompoundInterest(365.0);
    				displayInterest(interestCalculator, "Daily", type);
    			}
    			else 
    			{
    				totalInterest = interestCalculator.calculateCompoundInterest(52.0);
    				displayInterest(interestCalculator, "Weekly", type);
    			}
     
     
    			} while (askYesNo("Calculate another loan interest (Yes/No)"));
     
    			System.out.print("Thanks for using Interest Calculator!");
    	  }
     
     
     
    	/** Round **/
    	  public  double round(double numb1, double numb2) {
    	    double round = ((double) Math.round(numb1*(Math.pow(10, numb2)))/(Math.pow(10, numb2)));;
    		return round;
    	  }
     
     
    	    /** display interest **/
    	  public static void displayInterest(InterestCalculator7 object, String word1, int calculationType) {
     
     
    		System.out.println("Principal Amount    Interest Rate    Term    Calculation Type    Calculated Interest    Total Repayment");
    		System.out.println("----------------    -------------    ----    ----------------    -------------------    ---------------");
    		System.out.print(+getPrincipalAmount()+"                ");
    		System.out.print(+getInterestRate()+"           ");
    		System.out.print(+getTerm()+"           ");
    		System.out.print(word1+"            ");
    		System.out.print(+calculationType+"              ");
    		System.out.println(+(getPrincipalAmount() + calculationType));
    	  }
     
    	  /** Get principal amount **/
    	  public static double getPrincipalAmount() {
    		  Scanner input = new Scanner(System.in);
    		  double numb2 = 1;
    		 do{System.out.print("Enter Loan Amount: ");
    		   numb2 = input.nextDouble();
    		   if(numb2 > 0);
     
    		   		else{	
    			    		System.out.println("Data Error: Loan amount must be greater than zero. You entered " +numb2);
    		   		}		
    			   }while (numb2 < 0);
    		return numb2;
    	  }
     
    	  /** Get interest rate **/
    	  public static double getInterestRate() {
    		  Scanner input = new Scanner(System.in);
    		  double numb2=1;
    		  do{System.out.print("Enter Yearly Interest Rate (1 to 100 percent): ");
    		    numb2 = input.nextDouble(); 
    		  double getInterestRate = 0;
    		 if (numb2 >= 0 && numb2 <= 100)
    		  getInterestRate = numb2;
    		   		else{	
    			    		System.out.println("Data Error: Interest rate must be greater than or equal to zero and less than or equal to 100. You entered " +numb2);
    		   		}		
    			   }while (numb2 <= 0 || numb2 >= 100);
    		return numb2;
    	  }
     
    	  /** Get term **/
    	  public static int getTerm() {
    		  Scanner input = new Scanner(System.in);
    		  int numb2=1;
    		  do{System.out.print("Enter the Term (in months): ");
    		    numb2 = input.nextInt();
    		  double getTerm = 0;
    		  if (numb2 > 0)
    		  getTerm = numb2;
    		   		else{	
    			    		System.out.println("Data Error: Loan amount must be greater than zero. You entered " +numb2);
    		   		}		
    			   }while (numb2 <= 0);
    		return numb2;
    	  }
     
    	  /** Get calculation type **/
    	  public static int getCalculationType() {
    		  Scanner input = new Scanner(System.in);
    		  int type;
    		 do{
    			  System.out.print("Enter Interest Calculation Type (1 – Simple, 2 – Monthly Compounded, 3 – Daily Compounded, 4 - Weekly Compounded):");
    		      type = input.nextInt();
    		 } while (type !=1 && type !=2 && type !=3 && type !=4);
     
    	return type;
    	  }
     
    	  /**ask yes or no **/
    	  public static boolean askYesNo(String question) {
    		  Scanner input = new Scanner(System.in);
    		  String enteredText;
    		  boolean isAnswerValid;
     
    		  do{
    			  isAnswerValid = false;
    			  System.out.println(question);
    			  enteredText = input.nextLine();
     
    			  if (enteredText.length() > 0)
    			  {
    				  enteredText = enteredText.toUpperCase();
     
    				  if(enteredText.equals("YES") || enteredText.equals("Y") || enteredText.equals("NO") || enteredText.equals("N"))
    				  {
    					  isAnswerValid = true;
    				  }
    			  }
     
    			  if(isAnswerValid == false)
    			  {
    				  System.out.println("Please enter 'Yes' or 'No'?");
    			  }
     
    		  } while(isAnswerValid == false);
     
    		  if(enteredText.equals("YES") || enteredText.equals("Y"))
    		  {
    			  return true;
    		  }
     
    		  return false;
    	  }
    }

  8. #8
    Junior Member
    Join Date
    Apr 2013
    Posts
    17
    My Mood
    Happy
    Thanks
    0
    Thanked 9 Times in 9 Posts

    Default Re: Object help

    By studying your program I see that there are lots of things you didn't do quite well.
    When you display you information, you are not displaying any data there, rather you are asking the user to enter the data again.
    What you need to do is. After collecting all the necessary information from the user, pass the information to the InterestCalculator7 class through its constructor.
    write severall getmethod in interestcalculator7 class to call these saved information.
    And in your displaying code, call the getter methods of the class.

  9. The Following User Says Thank You to myjava For This Useful Post:

    ThePrince (April 20th, 2013)

  10. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Object help

    The last line in the current output:
    Enter Loan Amount:
    Looks like the program is asking for input and will wait for you to enter some input.

    Is the program waiting for you to enter data? Type in some numbers and see what happens.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #10
    Member
    Join Date
    Feb 2013
    Posts
    30
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Object help

    I think I have it, except when the program compiles I am just getting 0.0 for the output. The current output is doing

    Enter Loan Amount: 12
    Enter Yearly Interest Rate (1 to 100 percent): 23
    Enter the Term (in months): 24
    Enter Interest Calculation Type (1 – Simple, 2 – Monthly Compounded, 3 – Daily Compounded, 4 - Weekly Compounded):1
    Principal Amount Interest Rate Term Calculation Type Calculated Interest Total Repayment
    ---------------- ------------- ---- ---------------- ------------------- ---------------
    0.0 0.0 0.0 Simple 1 1.0
    Calculate another loan interest (Yes/No)
    no
    Thanks for using Interest Calculator!

    My new InterestCalculatorTester class is
    import java.util.Scanner;
    public class InterestCalculatorTester
    {
    	public static void main(String[] args)
    	{
    		double principal;
    	    double interest;
    	    int term;
     
    	   do{
     
    		   principal = getPrincipalAmount();
    		   interest = getInterestRate();
    		   term = getTerm();
    		   int type = getCalculationType();
    		   InterestCalculator7 interestCalculator = new InterestCalculator7(principal, interest, term);
     
     
    			double totalInterest;
    			if (type == 1)
    			{
    				totalInterest = interestCalculator.calculateSimpleInterest();
    				displayInterest(interestCalculator, "Simple", type);
    			}
    			else if (type == 2)
    			{
    				totalInterest = interestCalculator.calculateCompoundInterest(12.0);
    				displayInterest(interestCalculator, "Monthly", type);
    			}
    			else if (type == 3)
    			{
    				totalInterest = interestCalculator.calculateCompoundInterest(365.0);
    				displayInterest(interestCalculator, "Daily", type);
    			}
    			else 
    			{
    				totalInterest = interestCalculator.calculateCompoundInterest(52.0);
    				displayInterest(interestCalculator, "Weekly", type);
    			}
     
     
    			} while (askYesNo("Calculate another loan interest (Yes/No)"));
     
    			System.out.print("Thanks for using Interest Calculator!");
    	  }
     
     
     
    	/** Round **/
    	  public  double round(double numb1, double numb2) {
    	    double round = ((double) Math.round(numb1*(Math.pow(10, numb2)))/(Math.pow(10, numb2)));;
    		return round;
    	  }
     
     
    	    /** display interest **/
    	  public static void displayInterest(InterestCalculator7 object, String word1, int calculationType) {
     
     
    		System.out.println("Principal Amount    Interest Rate    Term    Calculation Type    Calculated Interest    Total Repayment");
    		System.out.println("----------------    -------------    ----    ----------------    -------------------    ---------------");
    		System.out.print(+InterestCalculator7.getPrincipal()+"                ");
    		System.out.print(+InterestCalculator7.getInterest()+"           ");
    		System.out.print(+InterestCalculator7.getTerm()+"           ");
    		System.out.print(word1+"            ");
    		System.out.print(+calculationType+"              ");
    		System.out.println(+(InterestCalculator7.getPrincipal() + calculationType));
    	  }
     
    	  /** Get principal amount **/
    	  public static double getPrincipalAmount() {
    		  Scanner input = new Scanner(System.in);
    		  double numb2 = 1;
    		 do{System.out.print("Enter Loan Amount: ");
    		   numb2 = input.nextDouble();
    		   if(numb2 > 0);
     
    		   		else{	
    			    		System.out.println("Data Error: Loan amount must be greater than zero. You entered " +numb2);
    		   		}		
    			   }while (numb2 < 0);
    		return numb2;
    	  }
     
    	  /** Get interest rate **/
    	  public static double getInterestRate() {
    		  Scanner input = new Scanner(System.in);
    		  double numb2=1;
    		  do{System.out.print("Enter Yearly Interest Rate (1 to 100 percent): ");
    		    numb2 = input.nextDouble(); 
    		  double getInterestRate = 0;
    		 if (numb2 >= 0 && numb2 <= 100)
    		  getInterestRate = numb2;
    		   		else{	
    			    		System.out.println("Data Error: Interest rate must be greater than or equal to zero and less than or equal to 100. You entered " +numb2);
    		   		}		
    			   }while (numb2 <= 0 || numb2 >= 100);
    		return numb2;
    	  }
     
    	  /** Get term **/
    	  public static int getTerm() {
    		  Scanner input = new Scanner(System.in);
    		  int numb2=1;
    		  do{System.out.print("Enter the Term (in months): ");
    		    numb2 = input.nextInt();
    		  double getTerm = 0;
    		  if (numb2 > 0)
    		  getTerm = numb2;
    		   		else{	
    			    		System.out.println("Data Error: Loan amount must be greater than zero. You entered " +numb2);
    		   		}		
    			   }while (numb2 <= 0);
    		return numb2;
    	  }
     
    	  /** Get calculation type **/
    	  public static int getCalculationType() {
    		  Scanner input = new Scanner(System.in);
    		  int type;
    		 do{
    			  System.out.print("Enter Interest Calculation Type (1 – Simple, 2 – Monthly Compounded, 3 – Daily Compounded, 4 - Weekly Compounded):");
    		      type = input.nextInt();
    		 } while (type !=1 && type !=2 && type !=3 && type !=4);
     
    	return type;
    	  }
     
    	  /**ask yes or no **/
    	  public static boolean askYesNo(String question) {
    		  Scanner input = new Scanner(System.in);
    		  String enteredText;
    		  boolean isAnswerValid;
     
    		  do{
    			  isAnswerValid = false;
    			  System.out.println(question);
    			  enteredText = input.nextLine();
     
    			  if (enteredText.length() > 0)
    			  {
    				  enteredText = enteredText.toUpperCase();
     
    				  if(enteredText.equals("YES") || enteredText.equals("Y") || enteredText.equals("NO") || enteredText.equals("N"))
    				  {
    					  isAnswerValid = true;
    				  }
    			  }
     
    			  if(isAnswerValid == false)
    			  {
    				  System.out.println("Please enter 'Yes' or 'No'?");
    			  }
     
    		  } while(isAnswerValid == false);
     
    		  if(enteredText.equals("YES") || enteredText.equals("Y"))
    		  {
    			  return true;
    		  }
     
    		  return false;
    	  }
    }

    My new InterestCalculator7 class is
     
    public class InterestCalculator7
    {
    		// Class Level Variables (Data Fields / Properties)
    	public double principalAmount;
    	public double interestRate;
    	public int term1;
     
     
    		  // ** Constructors **
    		  // No-Argument constructor
    	  public InterestCalculator7()
    	  {
    		  double principal = 0.0;
    		  double interest = -1.0;
    		  double term1 = 0;
    	  }
     
     
    	  public InterestCalculator7(double principalAmount, double interestRate, int term1)
    	  {
    	    double principal = principalAmount;
    	    double interest= interestRate;
    	    int term = term1;
    	  }
     
    	    public static double getPrincipal(){
    	    	double principal = 0;
    			return principal;
    	    }
     
    	    public static double getInterest(){
    	    	double interest = 0;
    			return interest;
    	    }
     
    	    public static double getTerm(){
    	    	double term = 0;
    			return term;
    	    }
     
     
     
    		  // ** Methods **
    		   public double calculateSimpleInterest()
    		  {
    			  double calculateSimpleInterest = ((principalAmount)*(interestRate/100.0)*(term1/12.0));
    				return calculateSimpleInterest;
    		  }
     
    		   public  double calculateCompoundInterest(double term1)
    		   {
    			     double calculateCompoundInterest = (principalAmount*Math.pow((1.0+((interestRate/100.0)/term1)),(term1*(term1/12.0))))-principalAmount;
    				return calculateCompoundInterest;
    			 }
     
     
    }

    I feel like I did something wrong when I was setting them?

  12. #11
    Junior Member
    Join Date
    Apr 2013
    Posts
    17
    My Mood
    Happy
    Thanks
    0
    Thanked 9 Times in 9 Posts

    Default Re: Object help

    I see you have already passed the information to the InterestCalculator7 class. Simply write a getter method in the InterestCalculator7 class. user object to call those data.

    --- Update ---

    you are using InterestCalculator7, write object not InterestCalculator in the displayinterest method

  13. The Following User Says Thank You to myjava For This Useful Post:

    ThePrince (April 20th, 2013)

  14. #12
    Member
    Join Date
    Feb 2013
    Posts
    30
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Object help

    I'm just confused on how to write the getter method..

  15. #13
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Object help

    Define what you want the getter method to do?
    1) get a value from a user
    or
    2) return the value of a variable

    Maybe you need two different methods.
    If you don't understand my answer, don't ignore it, ask a question.

  16. #14
    Junior Member
    Join Date
    Apr 2013
    Posts
    17
    My Mood
    Happy
    Thanks
    0
    Thanked 9 Times in 9 Posts

    Default Re: Object help

    Also I think there is something wrong with you interest calculator. When you enter 25000 in principal amount and 15% APR, the interest paid is way too much. That doesnt happen in real world.

    --- Update ---

    To write getter method, in your InterestCalculator7 class, write a new method just like calculatesimpleinterest with a return statement for principal. It just a 2 line code. The moderator doesn't allow me to write code here.

    --- Update ---

    public double getprincipal()
    {return principalAmount;}

  17. The Following User Says Thank You to myjava For This Useful Post:

    ThePrince (April 20th, 2013)

  18. #15
    Member
    Join Date
    Feb 2013
    Posts
    30
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Object help

    I tried that and it's still not pulling the user input

    {
    		// Class Level Variables (Data Fields / Properties)
    	public static double principalAmount;
    	public static double interestRate;
    	public static int term1;
     
     
    		  // ** Constructors **
    		  // No-Argument constructor
    	  public InterestCalculator7()
    	  {
    		  this.principalAmount = 0.0;
    		  this.interestRate = -1.0;
    		  this.term1 = 0;
    	  }
     
     
    	  public InterestCalculator7(double principalAmount, double interestRate, int term1)
    	  {
    		  double principal = principalAmount;
    		double interest= interestRate;
    	    int term = term1;
    	  }
     
    	  public static double getPrincipal()
    	  {return principalAmount;}
     
    	  public static double getInterestRate()
    	  {return interestRate;}
     
    	  public static double getTerm()
    	  {return term1;}
     
     
     
    		  // ** Methods **
    		   public double calculateSimpleInterest()
    		  {
    			  double calculateSimpleInterest = ((principalAmount)*(interestRate/100.0)*(term1/12.0));
    				return calculateSimpleInterest;
    		  }
     
    		   public  double calculateCompoundInterest(double term1)
    		   {
    			     double calculateCompoundInterest = (principalAmount*Math.pow((1.0+((interestRate/100.0)/term1)),(term1*(term1/12.0))))-principalAmount;
    				return calculateCompoundInterest;
    			 }
     
     
    }

  19. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Object help

    it's still not pulling the user input
    What is the contents of the command prompt console now when you execute the program?
    If you don't understand my answer, don't ignore it, ask a question.

  20. #17
    Junior Member
    Join Date
    Apr 2013
    Posts
    17
    My Mood
    Happy
    Thanks
    0
    Thanked 9 Times in 9 Posts

    Default Re: Object help

    did you change your displayInterest methods?
    Replace that InterestCalculator7 with just "object".
    like object.getPincipal()
    Replace all in this methods and it should work.

  21. The Following User Says Thank You to myjava For This Useful Post:

    ThePrince (April 20th, 2013)

Similar Threads

  1. static variable accessible in object b, not in object a?
    By Pajaro in forum What's Wrong With My Code?
    Replies: 4
    Last Post: March 30th, 2013, 02:41 PM
  2. Convert File Object to class<?> object
    By CEO in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: June 27th, 2012, 06:55 PM
  3. Creating object everytime object is called
    By aandcmedia in forum What's Wrong With My Code?
    Replies: 6
    Last Post: March 12th, 2012, 04:18 PM
  4. testing weather one object is greater than another object
    By u-will-neva-no in forum Object Oriented Programming
    Replies: 1
    Last Post: February 19th, 2012, 07:02 PM
  5. Reading from ResultSet to Object and from object Object Array
    By anmaston in forum What's Wrong With My Code?
    Replies: 4
    Last Post: April 7th, 2011, 06:11 AM