Please help a Java noob with some advice

A Place For Programmers Of All Levels To Discuss Programming & Web Building.

Moderator: Community Moderator

User avatar
Wadini
Soldier First Class
Soldier First Class
Posts: 17
Joined: 10 Jan 2010, 02:42
Location: Precariously perched upon a precipice
Has thanked: 6 times

Please help a Java noob with some advice

Post by Wadini »

Hi everyone!

I am new to the world of Java (been learning for almost seven weeks now). Every time I start thinking I might be getting the hang of this object-oriented world, I am brought back down to Earth. :crying: I am learning how to build a GUI now, and I am confused about an assignment. :book: It is the "standard" create a mortgage calculator type of program. Is it better to approach this type of application from a particular perspective? For example, should I build my GUI first (i.e. construct all of my javax.swing & java.awt components first)? Then build any classes to cover the mathematics? Or, should I build my math (logic) classes, and then my GUI? Getting started on this application has me a bit confused. I feel a bit like I am trying to answer the "which came first, the chicken or the egg" question on this assignment. Could be that I am over-thinking and making it harder than it needs to be. I have been known to do that! :blush: One other question, are there any "best resources" for Java that anyone could recommend for me? :confused: Thanks for any insight and/or advice, it is greatly appreciated!
User avatar
MicroLife
Uploader
Uploader
Posts: 438
Joined: 31 Oct 2010, 14:20
Location: Omnipresent
Has thanked: 2 times
Been thanked: 1 time
Contact:

Re: Please help a Java noob with some advice

Post by MicroLife »

try Herbert Schildt or Sams materials.
User avatar
ChattChitto
Site Admin
Site Admin
Posts: 13868
Joined: 20 Aug 2009, 06:20
Status: The Don
Location: The Fourth Dimension
Has thanked: 11 times
Been thanked: 38 times
Contact:

Re: Please help a Java noob with some advice

Post by ChattChitto »

Start with the GUI, check what inputs should be made, what outputs should be displayed.
Once you figure these out create the GUI (don't forget the calculate buttons etc..). now comes the functionality:
Place the functionality inside each specified button. this way you can test each function by itself.

Use NetBeans in this process.
If you need more help you must share more information about your project (the requirements and functionality)
To err is human, to forgive is divine
We can't all be heroes, because somebody has to sit on the curb and applaud when they go by.

ChattChitto RG - Apps Upload List: http://chattchitto.com/forum/viewtopic.php?f=29&t=104
ChattChitto RG - Movies Upload List: http://chattchitto.com/forum/viewtopic.php?f=29&t=5092
ChattChitto RG - Music Upload List: http://chattchitto.com/forum/viewtopic.php?f=29&t=34836
User avatar
ChattChitto
Site Admin
Site Admin
Posts: 13868
Joined: 20 Aug 2009, 06:20
Status: The Don
Location: The Fourth Dimension
Has thanked: 11 times
Been thanked: 38 times
Contact:

Re: Please help a Java noob with some advice

Post by ChattChitto »

I've done for you the GUI. I don't know if these are the exact required fields but you'll have an Idea:
Image
To err is human, to forgive is divine
We can't all be heroes, because somebody has to sit on the curb and applaud when they go by.

ChattChitto RG - Apps Upload List: http://chattchitto.com/forum/viewtopic.php?f=29&t=104
ChattChitto RG - Movies Upload List: http://chattchitto.com/forum/viewtopic.php?f=29&t=5092
ChattChitto RG - Music Upload List: http://chattchitto.com/forum/viewtopic.php?f=29&t=34836
User avatar
MicroLife
Uploader
Uploader
Posts: 438
Joined: 31 Oct 2010, 14:20
Location: Omnipresent
Has thanked: 2 times
Been thanked: 1 time
Contact:

Re: Please help a Java noob with some advice

Post by MicroLife »

ChattChitto wrote:I've done for you the GUI. I don't know if these are the exact required fields but you'll have an Idea:
if i ever hav any ques abt programming i'll consult u before asking my profs :D
User avatar
Wadini
Soldier First Class
Soldier First Class
Posts: 17
Joined: 10 Jan 2010, 02:42
Location: Precariously perched upon a precipice
Has thanked: 6 times

Re: Please help a Java noob with some advice

Post by Wadini »

Thank you ChattChitto, that is a GREAT GUI!!! :appls: Placing the functionality in the component after the GUI is built makes perfect sense after seeing what you are saying. For my assignment, I actually only need the loan amount, the interest rate, and the term. The application is to calculate the monthly payments. The actual assignment states:
Spoiler: show
Write the program in Java (with a graphical user interface) and have it calculate and display the mortgage payment amount from user input of the amount of the mortgage, the term of the mortgage, and the interest rate of the mortgage. Allow the user to loop back and enter new data or quit. Please insert comments in the program to document the program.
I will have to add functionality to the application over the coming weeks. I will work on this program today, and I do have NetBeans. Do you use the GUI builder in NetBeans, and do you import swing.BorderFactory for those boxes and borders? Thanks, you're a great help! :thumbsup:
User avatar
ChattChitto
Site Admin
Site Admin
Posts: 13868
Joined: 20 Aug 2009, 06:20
Status: The Don
Location: The Fourth Dimension
Has thanked: 11 times
Been thanked: 38 times
Contact:

Re: Please help a Java noob with some advice

Post by ChattChitto »

Then please let me know the exact boxes for Input and the ones for output :)
I consider now that input boxes should be: Loan Amount | Annual Interest Rate | Term
and the output results should stay as is?
you can provide me with the formula of calculation if you wish too :)
To err is human, to forgive is divine
We can't all be heroes, because somebody has to sit on the curb and applaud when they go by.

ChattChitto RG - Apps Upload List: http://chattchitto.com/forum/viewtopic.php?f=29&t=104
ChattChitto RG - Movies Upload List: http://chattchitto.com/forum/viewtopic.php?f=29&t=5092
ChattChitto RG - Music Upload List: http://chattchitto.com/forum/viewtopic.php?f=29&t=34836
User avatar
Wadini
Soldier First Class
Soldier First Class
Posts: 17
Joined: 10 Jan 2010, 02:42
Location: Precariously perched upon a precipice
Has thanked: 6 times

Re: Please help a Java noob with some advice

Post by Wadini »

I am definitely having some problems with this code. :confused: The inputs and outputs should be just as you said in your last post. I was hoping to have two buttons, Calculate and Reset (for user to clear information and start over). I have not been able to make nearly as much progress as I was hoping for ...maybe it is the addition of the GUI components that is confounding me (I don't know). I also can't get a GUI to look nearly as good as yours ChattChitto. The formula for calculation that I have determined is as follows:

Code: Select all

 public void CalculateMonthPayment() 
{
           //For a standard loan with interest
        if (interestRate > 0) 
        {
            monthInterest = interestRate / (100.00 * 12.00);
            monthPayment = principalBalance * ((monthInterest * (Math.pow((1 + monthInterest), loanTerm)))
                    / (Math.pow((1 + monthInterest), loanTerm) - 1));
        } else 
           {

            monthPayment = principalBalance / loanTerm;    //For an interest free loan
        }
    }
Any guidance that you could give is very appreciated. I cannot thank you enough for your help, but please know that I really appreciate it. All of your assistance and advice helps me to learn, and hopefully be able to help others when I become able.
User avatar
ChattChitto
Site Admin
Site Admin
Posts: 13868
Joined: 20 Aug 2009, 06:20
Status: The Don
Location: The Fourth Dimension
Has thanked: 11 times
Been thanked: 38 times
Contact:

Re: Please help a Java noob with some advice

Post by ChattChitto »

I've done it. but I think something might be wrong with what you game me.
If it was:

Code: Select all

            monthPayment = principalBalance / loanTerm;    //For an interest free loan
and you placed 1000$ for 1 Year. it will give you 1000 which is wrong.

it should be:

Code: Select all

            monthPayment = principalBalance / (loanTerm*12);    //For an interest free loan
Please check everything and let me know. I don't like keep fixing small mistakes all the time as it would be too confusing :)
To err is human, to forgive is divine
We can't all be heroes, because somebody has to sit on the curb and applaud when they go by.

ChattChitto RG - Apps Upload List: http://chattchitto.com/forum/viewtopic.php?f=29&t=104
ChattChitto RG - Movies Upload List: http://chattchitto.com/forum/viewtopic.php?f=29&t=5092
ChattChitto RG - Music Upload List: http://chattchitto.com/forum/viewtopic.php?f=29&t=34836
User avatar
Wadini
Soldier First Class
Soldier First Class
Posts: 17
Joined: 10 Jan 2010, 02:42
Location: Precariously perched upon a precipice
Has thanked: 6 times

Re: Please help a Java noob with some advice

Post by Wadini »

Mine is certainly not pretty, but all functionality is working now. :D I was so tired by the time I finished, that I left off the "Total Payments" function. The line of code for interest free:

Code: Select all

 monthPayment = principalBalance / loanTerm;    //For an interest free loan
has already been converted to months earlier in the program (I should have mentioned that :blush: ):

Code: Select all

double loanTerm = Double.parseDouble(termText.getText()) * 12;
        AssignTerm(loanTerm);
Could you take a look at the "finished" code for me, and maybe give me some help to get it looking more like your GUI? :pray: To save space, I will collapse the code in a spoiler box.
Spoiler: show

Code: Select all

/**
 *@author Administrator
 *
 * This application is a program for calculating loan payments.
 * The user inputs the loan amount, the interest rate, and the loan term.
 ** The program returns the monthly payment for the user's variables.*/

//Imports for application
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DecimalFormat;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

// Creates the mortgage calculator class
public class MortgageCalculator extends JFrame {
    
    //Define the instance variables
    private double amount;  
    private double rate;      
    private double term;          
    private double monthRate;     
    private double monthPay;      
    DecimalFormat Dollar = new DecimalFormat("$#,###.00");
    
    // Create GUI components
    private final JLabel loanLabel = new JLabel("Loan Amount");
    private JTextField loanText = new JTextField(12);
    private final JLabel intLabel = new JLabel("Interest Rate");
    private JTextField intText = new JTextField(12);
    private final JLabel termLabel = new JLabel("Loan Term");
    private JTextField termText = new JTextField(12);
    private final JLabel monthPayLabel = new JLabel("Monthly Payment");
    private JTextField result = new JTextField(12);

    public MortgageCalculator() {

        //Define the calculate button
        final JButton button = new JButton("Calculate");
        button.addActionListener(new ActionListener() {

            public void actionPerformed(final ActionEvent e) {
                calcButton();
            }
        });
        
        //Define the reset button
        final JButton buttonreset = new JButton("Reset");
        buttonreset.addActionListener(new ActionListener() {

            public void actionPerformed(final ActionEvent e) {
                resetButton();
            }
        });
        
        //Creates and defines the panel, panel components, and layout
        final JPanel panel = new JPanel();        
        setLayout(new BorderLayout());
        add(panel, BorderLayout.SOUTH);
        panel.setLayout(new GridLayout(6, 3, 20, 15)); 
        panel.add(loanLabel);
        panel.add(loanText);
        panel.add(intLabel);
        panel.add(intText);
        panel.add(termLabel);
        panel.add(termText);
        panel.add(monthPayLabel);
        panel.add(result);
        panel.add(button);
        panel.add(buttonreset);
        
        setTitle("Mortgage Calculator");
        setResizable(true);
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        pack(); //Set frame to size needed
    }

    public static void main(String[] args) {
        MortgageCalculator calc = new MortgageCalculator();
    }

        //Set information for calulate button (amount,rate,term, and montly payment)  
    public void calcButton() {
            
        double loanAmt = Double.parseDouble(loanText.getText());
        AssignLoanAmount(loanAmt);
             
        double loanRate = Double.parseDouble(intText.getText());
        AssignLoanRate(loanRate);
             
        double loanTerm = Double.parseDouble(termText.getText()) * 12;
        AssignTerm(loanTerm);
             
        CalculateMonthPay();
        result.setText(Dollar.format(monthPay));
    }

        //Set information for reset button
    public void resetButton() {
        loanText.setText("");
        intText.setText("");
        termText.setText("");
        result.setText("");
    }

        //Methods for value assignments (amount, rate, term)
    public void AssignLoanAmount(double loanAmount) {
        amount = loanAmount;
    }

    public void AssignLoanRate(double interestAmount) {
        rate = interestAmount;
    }

    public void AssignTerm(double termAmount) {
        term = termAmount;
    }

    public void CalculateMonthPay() {
            //For a standard loan with interest
        if (rate > 0) {
            monthRate = rate / (100.00 * 12.00);
            monthPay = amount * ((monthRate * (Math.pow((1 + monthRate), term)))
                    / (Math.pow((1 + monthRate), term) - 1));
        } else {
            //If no interest rate is applicable
            monthPay = amount / term;

        }
    }
}

If you see anything (syntax, structure, format, anything) that I should change for better programming, please let me know. I want, very much, to learn these concepts the best way possible. Thank you * 1,000,000! :appl: :appls: :thumbsup:
Locked

Return to “Programming/Web Building Chat & Support”