As a banker of course I gravitated toward a banking related example. This problem asks that given a loan term, apr, and principal what the monthly payment would be. I was more interested in the construction of the code than the underlying math so I lifted the equation from the book.
((loanamount x monthlyinterestrate) / 1 )
/ (1-(1/(1+monthlyinterestrate)^(number of years x 12)))
Yeah...
It's really exciting to have a gui based prompt and not be confined to a terminal. I'm still not really familiar with the JOptionPane. There was another surprise. I thought that having an exponent could be expressed with a carrot aka "^" character. Such is not the case. After a quick Google search I found that you have to use the method Math.pow(number, exponent). Easy enough but it was still a surprise.
package interestcalculation;
/**
* @author Neil
*/
//Pull in Javax class
import javax.swing.JOptionPane;
public class Main {
public static void main(String[] args) {
//Prompt user for annual interest rate
String rateString = JOptionPane.showInputDialog(null, "Enter the Annual" + " Percentage Rate", "Interest Calculator", JOptionPane.QUESTION_MESSAGE);
//Convert rateString into a double variable
double rate = Double.parseDouble(rateString);
double monthlyrate = rate / 1200;
//Prompt user for term of loan in years
String termString = JOptionPane.showInputDialog(null, "Enter the term of" + " the loan in years", "Interest Calculator",
JOptionPane.QUESTION_MESSAGE);
//Convert termString into an double variable;
double term = Double.parseDouble(termString);
//Prompt user for loan amount
String amountString = JOptionPane.showInputDialog(null,
"Enter the full" + " loan amount.", "Interest Calculator",
JOptionPane.QUESTION_MESSAGE);
//Convert amountString into a double variable;
double amount = Double.parseDouble(amountString);
//Calculate Payment
double payment = amount * monthlyrate / (1-1 / Math.pow(1 + monthlyrate, term *12));
//Display Payment
JOptionPane.showMessageDialog(null, "The monthly payment is $"
+ payment);
}
}
A variable is a data container that well, varies. You have to define it and then indicate what type of data it will contain. int, double, and char are the first examples in the book.
int x; //declare integer variable
/* Occupies 32 bits or 4 bytes, which is: -231 to 231-1 or -2,147,483,648 to 2,147,483,647. */
double x; //declare double variable
/* Occupies 64 bits or 8 bytes, with 14 or 15 significant digits. */
char a; //character variable
/* Occupies 16 bits or 2 bytes, stores a - z, A - Z, 0 - 9, as well as +, - , etc. */
Several variables can be declared in a single statement.
int var1, var2, var3; // three variables are declared in one statement
Variables can also be given a value as they are declared.
int var1=3; //variable declared and given value of 3
int var2=var1+3; //variable declared and given value of an expression
Division of two integers yields an integer. The book gives the example of 5/9. The integer value of this would be 0. 5.0/9 forces a float, double, or something a little bit more accurate.
It's 11:11 pm. Goodnight for now!
After one or two compilation errors I eventually got a clean build. I love the way /* comments look as opposed to //. It's an aesthetic, but you have to enjoy the simple things.
/*
* This is a simple demo displaying "Hello World".
*/
package helloworld;
/**
* @author Neil
*/
public class Main {
public static void main(String[] args) {
System.out.println("Hello World");
}
}
The author suggested pulling in an additional class that enables dialog boxes. This was done by "importing" it. I don't know how people remember these long class names. Maybe it comes with time. Sometimes the IDE will pop up suggestions. It suggested System.out.println but didn't do anything with JOptionPane.showMessageDialog.
/*
* This is a simple demo displaying "Hello World".
*/
package helloworld;
/*Pull in a class that enables a pop up msg box.*/
import javax.swing.JOptionPane;
/**
* @author Neil
*/
public class Main {
public static void main(String[] args) {
/*Output window.*/
JOptionPane.showMessageDialog(null, "Hello World!");
}
}
Yep. The dialog box popped up just like it should have. Huzzah!
|
Posted in
Hello World
,
Java
|
Posted on
11:09 AM
And so it begins.
Nearly every programmer begins with the simple task of creating and executing a program to output a single line of text. "Hello World" has often been that line. It's been a long while since I've cracked open a text editor and even longer since firing up a compiler. The basics have escaped me and I've really forgotten how to craft something even so basic as a "Hello World". It's sad; I'm sad!
Tonight I'll be opening the book, launching my favorite IDE, and churning out some code.
Until then... Auf Wiedersehen.
Benvenuto. Willkommen. Aloha.
Welcome to the kickoff of a brand spanking new blog. It feels like a toast in order, drinks should be shared, and a party to celebrate its launch. So here's a virtual toast. A toast to the success of this blog and its usefulness in my education. Cheers!