Link to home
Start Free TrialLog in
Avatar of coldjava
coldjavaFlag for United States of America

asked on

public class error

I have entered my Java code in NetBeans -- more answers than Command Prompt -- and I am receiving one error. Not should have to correct this. When I save my Java code in Notepad, how do I incorporate public class into the file? Thanks so much.
ERROR:
class WilliamsWeek2 is public, should be declared in a file named WilliamsWeek2.java

 
Avatar of oferam
oferam
Flag of United States of America image

In Java the name of the class and the name of the file should be the same.
Each class should be in it's own file.
Avatar of coldjava

ASKER

Thanks for your reply. I still do not know what I should do.
Do I save in two locations?
Say, I have a Java code I just finished. I want to save it in my Java file. In the file name blank should I type "WilliamsWeek2.java" and then save it somewhere else?  

I have been trying to follow the same steps I did to execute HelloWorldApp. Everything worked fine then.  
Why save in two locations?
You should save the class in WilliamsWeek2.java like you stated.

It sounds to me that you are coding in notepad, this is bad practice, especially when you are new to Java, try using Eclipse, it's a bit more complicated, but will make your life easier once you get the idea.

http://www.eclipse.org/downloads/download.php?file=/technology/epp/downloads/release/galileo/SR1/eclipse-java-galileo-SR1-win32.zip
I have been saving WilliamsWeek2.java

How do I identify it as public class?

Avatar of Mick Barry
Each .java file should contain one (and only one) public class. And the name of that class must be the same as the filename

so in your example where the filename is WilliamsWeek2.java the public class should be defined as:

public class WilliamsWeek2
Thanks. It sounds like a public class is not being created when I save WilliamsWeek2.java.

I do have a class path. I was able to get HelloWorldApp.java to execute properly.
 
But now there is a problem. How would I go about correctly this?

Thanks again.
please post WilliamsWeek2.java
Avatar of sudhasundaram
sudhasundaram

hi,

there is no need for the name of the java file should be the same as the public class name inside your java code. But once you compile your java code with javac filename.java you will be getting a class file.

Look into the name of the class file, and run the java command,

java classfilename
Thanks. It is when I type javac WilliamsWeek2.java that I receive an error message:  

   public class is public and should be declared in the file name WilliamsWeek2.java

 When I created Hello World, the class showed in the Java directory but now when I save WilliamsWeek2 no class shows in the directory.

 What is really weird is that I used a sample code as a test, and named it Mortgage and it executed. It seems to be a problem with WilliamsWeek2. Yet I am required to use that as part of the grade.  

 This is all so frustrating.
What is the name you have given inside your code after class variable?
If you posted your code here it would be more helpful.
Thanks.

Here is the beginning of my code:

/**
Programming 420 Week 2 Mortgage Calculator by Lynn Williams
*/

//The java.io package provides for system input and output through data streams
import java.io.*;

//A subclass of the NumberFormat used to format numbers in Java programs
import java.text.DecimalFormat;

public class WilliamsWeek2
{
//The main function for the mortgage calculator
public static void main(String[] args) throws IOException
{
//Declaring and constructing variables
int [] iTerm = {84, 180, 360};
double [] dInterest = {5.35, 5.5, 5.75};
double dPayment, dRate, dAmount = 200000, dMonthlyInterest,dMonthlyPrinciple, dMonthlyBalance;
DecimalFormat twoDigits = new DecimalFormat("$#,000.00");
that looks ok, do you have any other classes defined in that .java file?
Each public class should be defined in it's own file and the file name should be as the public class name.
So "public class WilliamsWeek2" should be saved as WilliamsWeek2.java and other public classes with the same concept.

You can attach the whole file (Using the File Link bellow) and then I can check exactly what is your problem
Objects --

Thanks. Yes. I have HelloWorldApp.java
Oferam --

 Thanks. I will attach my file code. Also, to help you see what I am doing I am sending my assignment criteria.

Since I cannot get passed the public class I am not sue if my code compiles properly.
Here is my assignment.

Java mortgage calculator
Write the program in Java (without a graphical user interface) and have it calculate the payment amount for 3 mortgage loans.
7 years at 5.35%
15 years at 5.5%
30 years at 5.75%
Use an array for the different loans. Display the mortgage payment amount for each loan and then list the loan balance and interest paid for each payment over the term of the loan. Use loops to prevent lists from scrolling off the screen.

Name your program file XXXWeek2.java, XXX is your last name. It is important to use your last name so that each students program file is unique. When your team evaluates the members program, the team will know whom the program belong to. Not naming the file will cause point deduction. Submit your .java file into your individual forum under the ASSIGNMENT THREAD.

Note:
1.      The program should require two user input values.
2.      The first input value is the loan amount.
3.      The second input value is an indicator for displaying the balance and interest paid list.
4.      Error checking should be done on the input loan amount to make sure that it is a positive number and is not zero.
5.      If the input loan amount is wrong, show an error message and exit the program.
6.      If the input loan amount is correct, proceed to compute.
7.      If the indicator is not to show the list, then only show the mortgage payments. Below is an example:
Loan amount $200000
Loan Term      Interest Rate      Mortgage Payment
7 years      5.35%      $2859.79
15 years      5.5%      $1634.17
30 years      5.75%      $1167.15

8.      If the indicator is to show the list, the program should show the mortgage payments first as in step 7. Following the mortgage payments, the list should be shown in 7 columns. The column are:
Column 1 - payment number
Column 2  the first loan balance
Column 3  the first loan interest paid
Column 4  the second loan balance
Column 5  the second loan interest paid
Column 6  the third loan balance
Column 7  the third loan interest paid
 
See example below on a $200000 loan:
1      198031.88      891.67      199282.50      916.67      199791.19      958.33
2      196054.98      882.89      198561.71      913.38      199581.38      957.33
3      194069.27      874.08      197837.62      910.07      199370.56      956.33


WilliamsWeek2.java
ASKER CERTIFIED SOLUTION
Avatar of sudhasundaram
sudhasundaram

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Sudhasundaram --

Thanks. So it will compile and display properly if I correct the above error?

Provided a good solution.
yes this is the only error in the code. If you rectify your code will compile.
Sudhasundaram

Thanks. Only tonight have I been able to get the Command Prompt to execute to code. I found it is not making 7 columns as outlined in the assignment criteria.

 Could you look the code over and see what is wrong?

 Thanks again.

Here is the assignment criteria:

Here is my assignment.

Java mortgage calculator
Write the program in Java (without a graphical user interface) and have it calculate the payment amount for 3 mortgage loans.
7 years at 5.35%
15 years at 5.5%
30 years at 5.75%
Use an array for the different loans. Display the mortgage payment amount for each loan and then list the loan balance and interest paid for each payment over the term of the loan. Use loops to prevent lists from scrolling off the screen.

Name your program file XXXWeek2.java, XXX is your last name. It is important to use your last name so that each students program file is unique. When your team evaluates the members program, the team will know whom the program belong to. Not naming the file will cause point deduction. Submit your .java file into your individual forum under the ASSIGNMENT THREAD.

Note:
1.      The program should require two user input values.
2.      The first input value is the loan amount.
3.      The second input value is an indicator for displaying the balance and interest paid list.
4.      Error checking should be done on the input loan amount to make sure that it is a positive number and is not zero.
5.      If the input loan amount is wrong, show an error message and exit the program.
6.      If the input loan amount is correct, proceed to compute.
7.      If the indicator is not to show the list, then only show the mortgage payments. Below is an example:
Loan amount $200000
Loan Term      Interest Rate      Mortgage Payment
7 years      5.35%      $2859.79
15 years      5.5%      $1634.17
30 years      5.75%      $1167.15

8.      If the indicator is to show the list, the program should show the mortgage payments first as in step 7. Following the mortgage payments, the list should be shown in 7 columns. The column are:
Column 1 - payment number
Column 2  the first loan balance
Column 3  the first loan interest paid
Column 4  the second loan balance
Column 5  the second loan interest paid
Column 6  the third loan balance
Column 7  the third loan interest paid
 
See example below on a $200000 loan:
1      198031.88      891.67      199282.50      916.67      199791.19      958.33
2      196054.98      882.89      198561.71      913.38      199581.38      957.33
3      194069.27      874.08      197837.62      910.07      199370.56      956.33
declare a temporary string as global.

Then append your values to that string inside the for loop. You will get the values printed as you need.
Sudhasundaram --

Thanks. Could you give me an example of the string? Would I need to append the values for one loop or do I need a loop for each column of numbers?