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?
 
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.
0
 
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
0
 
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.
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
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;


}

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

               mtgcalc2 mtgcalc = new mortcalc2();

just above

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

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

check your class name when creationg object
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.