Hi!
I'm new to Java programming. I have a program due for class next week
and I'm having some difficulties.
I've done a little programming before, but not very good at it! LOL
One of my biggest problems (and there are many!) is that I'm not
familiar with Java. I downloaded the latest version of Java from SUN.
I'm just not real sure how to use it. The teacher gave an example but
mine doesn't seem to be working.
First, I typed to program (based on a sample from my book) into notepad
and saved it as LoanKEG.java into the bin directory on my java program.
When I go to the command line and bring up the directory and type the
commands that the teacher gave (C:\Documents and Settings\Administrator>cd..
C:\Documents and Settings>cd..
C:\>f:
F:\>path=c:\Program Files\Java\jdk1.5.0\bin
F:\>javac NewFeatures.java (the name of the program is actually LoanKEG)
F:\>java NewFeatures
...
If I'm in that directory and bring up the command line, then the command
line brings me to: C:\Program Files\Java\jdk1.5.0_01\bin.
When I type javac LoanKEG.java it comes back and says "Exception in
thread "main" java.lang.NoclassDefFoundError: LoanKEG
I'm not sure what to do. Here is the code that I wrote for the program:
Any help is appreciated!!!
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// Author: Kathy Gibson
// Course: ITSE 2417
// Program No: 1
// Due Date: 2/4/2005
//
// Program Name: LoanKEG.java
// Program Description:
// This program will compute the number of months it will take to
// pay off a loan based on a beginning principal balance of $1000,
// with an interest rate of 18% per year.
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
import java.text.NumberFormat;
// Computes the number of months and total interest paid on a
$1000.0 loan
// at 18% per year.
public class LoanKEG
{
// Amount of the loan
public static final double LOAN_AMOUNT = 1000.0;
// Annual interest rate, as a percentage
public static final double ANNUAL_RATE = 18.0;
// Amount of monthly payment
public static final double MONTHLY_PAYMENT = 50.0;
public static void main(String[] args)
{
// Compute the monthly interest rate, as a fraction
double rate = (ANNUAL_RATE/100.0)/12.0;
// principalRemaining will contain the amount of the loan that
// is not yet paid off.
double principalRemaining = LOAN_AMOUNT;
// totalInterest will contain the total amount of interest
paid over
// the life of the loan.
double totalInterest = 0.0;
// Make a counter for the number of months
int months = 0;
// Loop as long as the remaining principal is greater than 0
while (principalRemaining > 0.0)
{
months++;
}
NumberFormat moneyFormat = NumberFormat.getCurrencyInstance();
System.out.println("It will take " + months +
" months to pay off a loan of " +
moneyFormat.format(LOAN_AMOUNT));
System.out.println(
"at an annual interest rate of " + ANNUAL_RATE + "%.");
System.out.println("You will pay a total of " +
moneyFormat.format(totalInterest) + " in interest.");
}
}
klynn47@comcast.net - 28 Jan 2005 17:33 GMT
What is your CLASSPATH?
DrNoose - 28 Jan 2005 17:55 GMT
> What is your CLASSPATH?
I'm not sure if this right, but "public class LoanKEG
{"
Kathy
Marc Poulin - 28 Jan 2005 22:58 GMT
>> What is your CLASSPATH?
>>
> I'm not sure if this right, but "public class LoanKEG
> {"
>
> Kathy
Kathy:
I teach Java, and my students often run into the same issues
you are dealing with.
Java is an object oriented language, and Java programs
are organized into "classes".
Rule #1: Put each class into its own file.
In your case, you have a class named "LoanKEG" located
in a file named "LoanKEG.java".
Rule #2: Java is a case sensitive language. This means
the SPELLING and CAPITALIZATION of the file and
the name of the class within that file must match EXACTLY.
If you changed your file name to "loanKEG.java" you would
have problems because "loanKEG" is not exactly the same as
"LoanKEG".
Rule #3: The "javac" compiler expects to be given the
name of a FILE to compile. The name of the file
includes the FILE EXTENTION.
You would issue the command "javac LoanKEG.java".
Pay attention to the ".java" file extension.
Rule #4: The "java" virtual machine (the program that
actually runs your program) expects to be given a
CLASS name, not a FILE name.
You would issue the command "java LoanKEG".
"LoanKEG" is the name of the class, "LoanKEG.java"
and "LoanKEG.class" are the names of files.
Rule #5: Keep all your .java and .class files in the
same directory and you won't have to worry about the
classpath.
The classpath tells the "javac" and "java" programs
where to look for additional .class files if they are
not found in your current directory. Life is simpler
if you just keep all your files in one directory.
Finally, on a Windows system the "Program Files" directory
is where programs like Word, Powerpoint, etc. are installed.
Files created by you should never go there. Put them in
"My Documents" or some other folder that is set aside
for your personal use.
Hope this helps.
Marc
Mike B - 29 Jan 2005 01:43 GMT
>>> What is your CLASSPATH?
>>>
[quoted text clipped - 53 lines]
> "My Documents" or some other folder that is set aside
> for your personal use.
What everyone also seemed to have missed is that she "cd"-ed to the
directory where her java compiler was located. She should "cd" into the
directory where the java source is located and then type the full path name
to the java compiler.
eg.
C:\my documents\Java> C:\Program files\java\bin\javac LoanKEG

Signature
Mike B
Alex Molochnikov - 28 Jan 2005 17:59 GMT
> ...the command
> line brings me to: C:\Program Files\Java\jdk1.5.0_01\bin.
If your current directory is other than the one where the Java source code
is, you will have to provide a fully qualified file name to the Java
compiler:
C:\Program Files\Java\jdk1.5.0_01\bin> javac C:\temp\LoanKEG.java
Then you will have to run the compiled program, specifying the classpath:
C:\Program Files\Java\jdk1.5.0_01\bin> java -cp C:\temp LoanKEG
Alternatively, you can make the directory of your source code current, and
enter the fully qualified java compiler and interpreter names, like this:
C:\temp> C:\Program Files\Java\jdk1.5.0_01\bin\javac LoanKEG.java
C:\temp> C:\Program Files\Java\jdk1.5.0_01\bin\java LoanKEG
Either way, your program will compile, but it will not give you the expected
answer because of the infinite loop that it will run into. To find and fix
it should be part of your homework.
Alex Molochnikov
Gestalt Corporation
Starshine Moonbeam - 28 Jan 2005 18:31 GMT
> Hi!
>
[quoted text clipped - 24 lines]
> When I type javac LoanKEG.java it comes back and says "Exception in
> thread "main" java.lang.NoclassDefFoundError: LoanKEG
You have to be in the same folder as your class. Navigate to the folder
your class is in and try it again.

Signature
Starshine Moonbeam
mhm31x9 Smeeter#29 WSD#30
sTaRShInE_mOOnBeAm aT HoTmAil dOt CoM
DrNoose - 28 Jan 2005 18:55 GMT
>>Hi!
>>
[quoted text clipped - 27 lines]
> You have to be in the same folder as your class. Navigate to the folder
> your class is in and try it again.
Okay, I apologize for being so dumb!
I go into Windows Explorer and go do the Java directory in program
files. I then go to the jdk1.5.0_01 directory and then lastly to bin. I
then go to "Start" and click on "Command" That brings me to the command
line. It says C:\Program Files\Java\jdk1.5.0_01\bin. Isn't that where I
need to be? When I type in javac LoanKEG.java it comes back and says
"Exception in thread "main" java.lang.NoclassDefFoundError: LoanKEG.
I've also redone the program to see if this will end the indefinate
loop. Did I mention that I'm not very good at this programming stuff,
but have to have the class to graduate!!! LOL
import java.text.NumberFormat;
// Computes the number of months and total interest paid on a
$1000.0 loan
// at 18% per year.
public class LoanKEG
{
// Amount of the loan
public static final double LOAN_AMOUNT = 1000.0;
// Annual interest rate, as a percentage
public static final double ANNUAL_RATE = 18.0;
// Amount of monthly payment
public static final double MONTHLY_PAYMENT = 50.0;
public static void main(String[] args)
{
// Compute the monthly interest rate, as a fraction
double rate = (ANNUAL_RATE/100.0)/12.0;
// principalRemaining will contain the amount of the loan that
// is not yet paid off.
double principalRemaining = LOAN_AMOUNT;
// totalInterest will contain the total amount of interest
paid over
// the life of the loan.
double totalInterest = 0.0;
// Make a counter for the number of months
int months = 0;
double interestAccrued = 0;
double totalInterestPaid = 0;
double diff = 0;
// Loop as long as the remaining principal is greater than 0
while (principalRemaining > 0.0)
{
interestAccrued = principalRemaining * rate;
totalInterestPaid = totalInterestPaid + interestAccrued;
diff = MONTHLY_PAYMENT - interestAccrued;
principalRemaining = principalRemaining - diff;
months++;
}
NumberFormat moneyFormat = NumberFormat.getCurrencyInstance();
System.out.println("It will take " + months +
" months to pay off a loan of " +
moneyFormat.format(LOAN_AMOUNT));
System.out.println(
"at an annual interest rate of " + ANNUAL_RATE + "%.");
System.out.println("You will pay a total of " +
moneyFormat.format(totalInterest) + " in interest.");
}
}
Starshine Moonbeam - 28 Jan 2005 19:03 GMT
> >>Hi!
> >>
[quoted text clipped - 35 lines]
> line. It says C:\Program Files\Java\jdk1.5.0_01\bin. Isn't that where I
> need to be?
No. You have to navigate to the folder the LoanKEG.java is in. Or use the
full path as another poster explained. (I forgot about that one, my bad)
> When I type in javac LoanKEG.java it comes back and says
> "Exception in thread "main" java.lang.NoclassDefFoundError: LoanKEG.
Yeah, because it can't find the source code. It's looking for the class
definition and there isn't one there.
> I've also redone the program to see if this will end the indefinate
> loop. Did I mention that I'm not very good at this programming stuff,
> but have to have the class to graduate!!! LOL
You will be after this class. Java is nothing if not educational (and
evil)

Signature
Starshine Moonbeam
mhm31x9 Smeeter#29 WSD#30
sTaRShInE_mOOnBeAm aT HoTmAil dOt CoM
Carl - 28 Jan 2005 18:44 GMT
> Hi!
>
[quoted text clipped - 28 lines]
> I'm not sure what to do. Here is the code that I wrote for the program:
> Any help is appreciated!!!
<--snip-->
read entire reply please...
1. Make sure you are in the directory that contains the file LoanKEG.java.
2. Make sure the java bin directory is in your path.
Type 'java -version' at the command line, and it should print the
version.
3. Compile the .java file. (i.e. javac LoanKEG.java). You should now
have 2 files in your current directory; LoanKEG.java and LoanKEG.class.
verify this by typing 'dir Loan*'
4. fix the endless while loop in your source file, then do step 3 again.
5. run.
DrNoose - 28 Jan 2005 19:39 GMT
>> Hi!
>>
[quoted text clipped - 46 lines]
>
> 5. run.
Carl,
Hi!
I made sure that I was in the directory that contains the file LoanKEG.java.
I also checked the version and it told me I was using version
"1.5.0_01". I do have the two files - .java and .class.
I just reran the file and did get input out. The only thing that looks
like it isn't right is the total amount of interest paid.
Thanks!
Kathy
When I type in javac LoanKEG.java it comes up with a bunch of help
files. I can't see what it started out with because it scrolls so
quickly. I tried doing the /p, but that didn't help. Th
DrNoose - 28 Jan 2005 19:49 GMT
>>> Hi!
>>>
[quoted text clipped - 68 lines]
> files. I can't see what it started out with because it scrolls so
> quickly. I tried doing the /p, but that didn't help. Th
I think It is working now! It tells me that it will take 24 months to
pay off the loan of $1,000.00 at an annual interst rate of 18.0%. You
will pay a total of $197.83 in interest.
I've got verify my results now.
I appreciate everyone's help!
If you see anything wrong in my calculations, please let me know!
Kathy
DrNoose - 28 Jan 2005 21:05 GMT
>>>> Hi!
>>>>
[quoted text clipped - 81 lines]
>
> Kathy
I do have one other question. I've changed the code to import
"java.text.DecimalFormat" Would this class allow my figures to be
rounded up ie: 47.99 * 1.5% would be 0.71985, which would be rounded up
to 0.72 cents. I know those cents can add up when they are rounded. I
think this will make a difference in my final outcome.
I hope this makes sense!
Kathy
Carl - 28 Jan 2005 23:03 GMT
> I do have one other question. I've changed the code to import
> "java.text.DecimalFormat" Would this class allow my figures to be
[quoted text clipped - 5 lines]
>
> Kathy
Yeah, you're on the right track. the DecimalFormat class implements half
even rounding, as explained here:
http://java.sun.com/j2se/1.4.2/docs/api/java/math/BigDecimal.html#ROUND_HALF_EVEN
Starshine Moonbeam - 29 Jan 2005 00:16 GMT
> >>>> Hi!
> >>>>
[quoted text clipped - 86 lines]
> to 0.72 cents. I know those cents can add up when they are rounded. I
> think this will make a difference in my final outcome.
I made a test case and ran it. I didn't do any calculations, just picked
a number I knew should round because I would do any calculations first
before I formatted anyway.
<sscce>
import java.text.*;
public class TestRounding {
double x = 10.359;
public void maybeRound() {
DecimalFormat df = new DecimalFormat("$##00.00");
System.out.print(df.format(x));
}
public static void main(String[] args) {
TestRounding drive = new TestRounding();
drive.maybeRound();
}
} //end class TestRounding
</sscce>

Signature
Starshine Moonbeam
mhm31x9 Smeeter#29 WSD#30
sTaRShInE_mOOnBeAm aT HoTmAil dOt CoM