[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

How to turn this into an object oriented program

Posted on 2006-04-17
11
Medium Priority
?
199 Views
Last Modified: 2013-11-23
I have modified this script a thousand times, but I still haven't figured it out on how to make this a true object oriented program. Please help here is the code that actually compiles and runs ok.

import java.io.*;
import java.text.*;

public class MtgCalc
       {
             public static void main(String[] args) throws IOException
                  {
                        DecimalFormat decimalPlaces=new DecimalFormat("0.00");

                        double monthlyPayment;
                        double principal;
                        double balance;
                        double monthlyInterestPayment;
                        double monthlyPrincipalPayment;
                        double interestRateYears;
                        double interestRateMonths;
                        int termYears;
                        int termMonths = 0;
                        int linecount;


            //assignment of fixed values
                  principal = 200000;
                  interestRateYears = 5.75;
                  interestRateMonths = (interestRateYears / 12) / 100;
                  termYears = 30;
                  termMonths = termYears * 12;
                  balance = principal;                  // set current balance = principal
                  monthlyInterestPayment = 0;            // set current interest payment = 0
                  monthlyPrincipalPayment = 0;            // set current principal payment = 0
                  linecount = 30;                  // display 5 lines of results

            // calculation of monthly payment
                  interestRateMonths = (interestRateYears / 12) / 100;
                  monthlyPayment = (principal * interestRateMonths) / (1 - Math.pow(1 + interestRateMonths, - termMonths));

            // output
                  System.out.println();
                  System.out.println("\tYour monthly payment of a loan amount of $200,000.00");
                  System.out.println("\t@ 5.75% for 30 years is $ " + decimalPlaces.format(monthlyPayment));
                  System.out.println();

            // formating used to set column headers for results
                              System.out.println("\n\n\nMonths\t\tPrincipal\tInterest\tBalance");
                              System.out.println("Remaining\tPayment\t\tPayment\t\tRemaining");
                              System.out.println("---------\t---------\t---------\t---------");


            /// start while loop
            while (termMonths > 0)
            {
            // calculate interest and principal payments

                   monthlyInterestPayment = (balance * interestRateMonths);
                   monthlyPrincipalPayment = (monthlyPayment - monthlyInterestPayment);
                   balance = (balance - monthlyPrincipalPayment);


            // format column headers for results to be displayed
                              System.out.println(termMonths + "\t\t$" + decimalPlaces.format(monthlyPrincipalPayment) +
                              "\t\t$" + decimalPlaces.format(monthlyInterestPayment) +
                              "\t\t$" + decimalPlaces.format(balance));

            // decrement months
                   termMonths--;


            // these conditional statements cause the results to pause
                    if(linecount == 30)
                    {
                              linecount = 0;
                  try
                  {
                        Thread.sleep(3000);      // pause to last three seconds

            // formating used to set column headers for results
                        System.out.println("\n\n\nMonths\t\tPrincipal\tInterest\tBalance");
                        System.out.println("Remaining\tPayment\t\tPayment\t\tRemaining");
                        System.out.println("---------\t---------\t---------\t---------");
                        }

                  catch (InterruptedException e)
                  {
                  }
                  }      // end if
                  else
                  {
                  linecount++;
                  }      // end else
        }          // end while
    }              // end main
}                  // end MtgCalc
0
Comment
Question by:antcal
  • 4
  • 3
  • 2
  • +2
11 Comments
 
LVL 16

Expert Comment

by:suprapto45
ID: 16474993
Just a hint ;)

                    double monthlyPayment;
                    double principal;
                    double balance;
                    double monthlyInterestPayment;
                    double monthlyPrincipalPayment;
                    double interestRateYears;
                    double interestRateMonths;
                    int termYears;
                    int termMonths = 0;

You should create a new class for those attributes. Then in this class, you also need to have the method to calculate the necessary variables. Finally, you should have one more class that has public static void main(String[] args) to create this new class and call the necessary method

David
0
 
LVL 23

Expert Comment

by:Siva Prasanna Kumar
ID: 16475056
First write a Class(Bean Class) with all these variables along with their getter & setter methords and then write another class for impleneting the bussiness logic
( your claculations) with in a method along with instance of the bean class. in the same class have the main method to create and call the method implementing the business logic.
0
 
LVL 9

Expert Comment

by:Manikandan Thiagarajan
ID: 16475162
class calc

{

variables and method here

}

class calc1
{


public static void main(String s[])

{

calc q = new calc(),

create no of calc objects

call that method and variable using object name

like

q.method();
q.variable;


}

}
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:antcal
ID: 16480625
Unfortunately I am new to Java and I get the concept that you all have explained (ruffly) but I am lost! What if I just separate the mort calculations ? Will this work? Obviously this section is not compiling. Here's the info.
Thanks,
Tony

import java.io.*;
import java.text.*;

              public class MtgCalc2
              {
                  public static void main(String[] args) throws IOException
                  {
                        DecimalFormat decimalPlaces=new DecimalFormat("0.00");

                        double monthlyPayment;
                        double principal;
                        double interestRateYears;
                        double interestRateMonths;
                        int termYears;
                        int termMonths = 0;
                        int linecount;


            //assignment of fixed values
                  principal = 200000;
                  interestRateYears = 5.75;
                  interestRateMonths = (interestRateYears / 12) / 100;
                  termYears = 30;
                  termMonths = termYears * 12;


            // calculation of monthly payment
                  interestRateMonths = (interestRateYears / 12) / 100;
                  monthlyPayment = (principal * interestRateMonths) / (1 - Math.pow(1 + interestRateMonths, - termMonths));

            // output
                  System.out.println();
                  System.out.println("\tYour monthly payment of a loan amount of $200,000.00");
                  System.out.println("\t@ 5.75% for 30 years is $ " + decimalPlaces.format(monthlyPayment));
                  System.out.println();
                  }

}
            public class mort
            {public static void main(String s[])

            {
                  mtgcalc2 mtgcalc = new mortcalc2
                  double balance;
                  double monthlyInterestPayment;
                  double monthlyPrincipalPayment;

                  //assignment of mort values
                  balance = principal;                  // set current balance = principal
                  monthlyInterestPayment = 0;            // set current interest payment = 0
                  monthlyPrincipalPayment = 0;      // set current principal payment = 0
                  linecount = 25;                              // display 25 lines of results



                  DecimalFormat decimalPlaces=new DecimalFormat("0.00");


            // formating used to set column headers for results
                              System.out.println("\n\n\nMonths\t\tPrincipal\tInterest\tBalance");
                              System.out.println("Remaining\tPayment\t\tPayment\t\tRemaining");
                              System.out.println("---------\t---------\t---------\t---------");

                        // start while loop
                              while (termMonths > 0)
                              {
                              // information to display

                              System.out.println(termMonths + "\t\t$" + decimalPlaces.format(monthlyPrincipalPayment) +
                                          "\t\t$" + decimalPlaces.format(monthlyInterestPayment) +
                                          "\t\t$" + decimalPlaces.format(balance));

                              // decreases month by 1
                                     termMonths--;

                              // calculate interest and principal payments
                                     monthlyInterestPayment = (balance * interestRateMonths);
                                     monthlyPrincipalPayment = (monthlyPayment - monthlyInterestPayment);
                                     balance = (balance - monthlyPrincipalPayment);


                              // statement cause the results to pause
                                      if(linecount == 20)

                                      {
                                                linecount = 0;
                                          try
                                          {
                                                Thread.sleep(4000);            // pause to last four seconds
                                                System.out.println("\n\n\nMonths\t\tPrincipal\tInterest\tBalance");
                                                System.out.println("Remaining\tPayment\t\tPayment\t\tRemaining");
                                                System.out.println("---------\t---------\t---------\t---------");
                                          }
                                          catch (InterruptedException e)
                                          {
                                                }
                                          }      // end if
                                    else
                                    {
                                    linecount++;
                                    }
                              }
                        }
                  }


I get compiling error

MtgCalc2.java:53: '(' or '[' expected
                  double balance;
                        ^
1 error
0
 
LVL 23

Assisted Solution

by:Siva Prasanna Kumar
Siva Prasanna Kumar earned 500 total points
ID: 16480694
just change line 52 to

               mtgcalc2 mtgcalc = new mortcalc2();

just above

               double balance;
0
 

Author Comment

by:antcal
ID: 16481155
shivaspk,
Thanks for your help. I still don't get it to compile. It is not pulling from above.
the list of error looks like this.

\Desktop\POS406\MtgCalc2.java:48: class mort is public, should be declared in a file named mort.java
            public class mort
                       ^
\Desktop\POS406\MtgCalc2.java:52: cannot find symbol
symbol  : class mtgcalc2
location: class mort
                  mtgcalc2 mtgcalc = new mortcalc2();
\Desktop\POS406\MtgCalc2.java:52: cannot find symbol
symbol  : class mortcalc2
location: class mort
                  mtgcalc2 mtgcalc = new mortcalc2();
                                               ^
\Desktop\POS406\MtgCalc2.java:58: cannot find symbol
symbol  : variable principal
location: class mort
                  balance = principal;                  // set current balance = principal
                                  ^
\Desktop\POS406\MtgCalc2.java:61: cannot find symbol
symbol  : variable linecount
location: class mort
                  linecount = 25;                              // display 25 lines of results
                        ^
\Desktop\POS406\MtgCalc2.java:74: cannot find symbol
symbol  : variable termMonths
location: class mort
                              while (termMonths > 0)
                                               ^
\Desktop\POS406\MtgCalc2.java:78: cannot find symbol
symbol  : variable termMonths
location: class mort
                              System.out.println(termMonths + "\t\t$" + decimalPlaces.format(monthlyPrincipalPayment) +
                                                           ^
\Desktop\POS406\MtgCalc2.java:83: cannot find symbol
symbol  : variable termMonths
location: class mort
                                     termMonths--;
                                               ^
\Desktop\POS406\MtgCalc2.java:86: cannot find symbol
symbol  : variable interestRateMonths
location: class mort
                                     monthlyInterestPayment = (balance * interestRateMonths);
                                                                                   ^
\Desktop\POS406\MtgCalc2.java:87: cannot find symbol
symbol  : variable monthlyPayment
location: class mort
                                     monthlyPrincipalPayment = (monthlyPayment - monthlyInterestPayment);
                                                                          ^
\Desktop\POS406\MtgCalc2.java:92: cannot find symbol
symbol  : variable linecount
location: class mort
                                      if(linecount == 20)
                                                   ^
\Desktop\POS406\MtgCalc2.java:95: cannot find symbol
symbol  : variable linecount
location: class mort
                                                linecount = 0;
                                                                ^
\Desktop\POS406\MtgCalc2.java:109: cannot find symbol
symbol  : variable linecount
location: class mort
                                    linecount++;
                                                ^
13 errors

Tool completed with exit code 1
0
 

Author Comment

by:antcal
ID: 16481165
I thought I can call the commands without having to rekey them
0
 
LVL 23

Expert Comment

by:Siva Prasanna Kumar
ID: 16481197
Oh i am sorry i didn't notice two main methods cannot exit in the same file and more on when you define a class as public then it must & should be stored in same class name .java file.

0
 

Author Comment

by:antcal
ID: 16481500
So remove the second main and keep it under the initial bracket?
0
 
LVL 9

Expert Comment

by:Manikandan Thiagarajan
ID: 16484511
java is the case sensitive language.

check your class name when creationg object
0
 
LVL 30

Accepted Solution

by:
Mayank S earned 500 total points
ID: 16484573
>> class mort is public, should be declared in a file named mort.java

You cannot have 2 public classes at the same level in the same .java file. You need to cut out the mort class and put it in another file called mort.java.

>> mtgcalc2 mtgcalc = new mortcalc2

MtgCalc2 mtgcalc = new mortcalc2 () ;

>> balance = principal;

principal is a local variable in the main () method in the MtgCalc2 class. You cannot access it here directly. Which main () method do you actually want to run?

>> linecount = 25;            

Declare those variables as int, double wherever required.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses
Course of the Month18 days, 23 hours left to enroll

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question