How to turn this into an object oriented program

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
antcalAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

suprapto45Commented:
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
Siva Prasanna KumarPrincipal Solutions ArchitectCommented:
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.
Manikandan ThiagarajanSenior consultantCommented:
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;


}

}
Bootstrap 4: Exploring New Features

Learn how to use and navigate the new features included in Bootstrap 4, the most popular HTML, CSS, and JavaScript framework for developing responsive, mobile-first websites.

antcalAuthor Commented:
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
Siva Prasanna KumarPrincipal Solutions ArchitectCommented:
just change line 52 to

               mtgcalc2 mtgcalc = new mortcalc2();

just above

               double balance;
antcalAuthor Commented:
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
antcalAuthor Commented:
I thought I can call the commands without having to rekey them
Siva Prasanna KumarPrincipal Solutions ArchitectCommented:
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.

antcalAuthor Commented:
So remove the second main and keep it under the initial bracket?
Manikandan ThiagarajanSenior consultantCommented:
java is the case sensitive language.

check your class name when creationg object
Mayank SAssociate Director - Product EngineeringCommented:
>> 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.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.