?
Solved

public class error

Posted on 2009-12-17
22
Medium Priority
?
352 Views
Last Modified: 2013-11-23
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

 
0
Comment
Question by:coldjava
  • 11
  • 5
  • 3
  • +1
22 Comments
 
LVL 6

Expert Comment

by:oferam
ID: 26076202
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.
0
 

Author Comment

by:coldjava
ID: 26076249
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.  
0
 
LVL 6

Expert Comment

by:oferam
ID: 26076459
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
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:coldjava
ID: 26077059
I have been saving WilliamsWeek2.java

How do I identify it as public class?

0
 
LVL 92

Expert Comment

by:objects
ID: 26077327
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
0
 

Author Comment

by:coldjava
ID: 26077506
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.
0
 
LVL 92

Expert Comment

by:objects
ID: 26077509
please post WilliamsWeek2.java
0
 

Expert Comment

by:sudhasundaram
ID: 26079593
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
0
 

Author Comment

by:coldjava
ID: 26080714
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.
0
 

Expert Comment

by:sudhasundaram
ID: 26082252
What is the name you have given inside your code after class variable?
If you posted your code here it would be more helpful.
0
 

Author Comment

by:coldjava
ID: 26082704
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");
0
 
LVL 92

Expert Comment

by:objects
ID: 26084474
that looks ok, do you have any other classes defined in that .java file?
0
 
LVL 6

Expert Comment

by:oferam
ID: 26084805
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
0
 

Author Comment

by:coldjava
ID: 26085010
Objects --

Thanks. Yes. I have HelloWorldApp.java
0
 

Author Comment

by:coldjava
ID: 26085045
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
0
 

Accepted Solution

by:
sudhasundaram earned 500 total points
ID: 26086000
The problem is in your for loop.

Modify it as

for (p = 0; p <= 2; p++)
0
 

Author Comment

by:coldjava
ID: 26087406
Sudhasundaram --

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

0
 

Author Closing Comment

by:coldjava
ID: 31667564
Provided a good solution.
0
 

Expert Comment

by:sudhasundaram
ID: 26093659
yes this is the only error in the code. If you rectify your code will compile.
0
 

Author Comment

by:coldjava
ID: 26093733
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
0
 

Expert Comment

by:sudhasundaram
ID: 26094101
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.
0
 

Author Comment

by:coldjava
ID: 26096285
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?



 
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
The purpose of this article is to demonstrate how we can use conditional statements using Python.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses
Course of the Month14 days, 5 hours left to enroll

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question