aspose file tools*
The moose likes Java in General and the fly likes Class declaration problem. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of JavaScript Promises Essentials this week in the JavaScript forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Class declaration problem." Watch "Class declaration problem." New topic
Author

Class declaration problem.

Anu satya
Ranch Hand

Joined: Mar 17, 2005
Posts: 146
Hi,
I am writing one test application where, employee name and salary are displayed. After that, e-sal has to me incremented to 10% on current salary and display the output like this.....
-------------------------------------------
Employ 1:Bob Jon s;Yearly Salary:34500.00 Employ 2:Susan Bak r;Yearly Salary:37809.00
Incr asing employee salari s by 10 Employ 1:Bob Jon s;Yearly Salary:37950.00 Employ 2:Susan Bak r;Yearly Salary:41589.90
-----------------------------------------
I must you another class by name "Employeetest". My code as follows

code
---------------------------------------------
import java.util.*;

public class Employee
{
private String firstname="";
private String lastname="";
private double monthsal=0.0;

Employee(String fnm,String lnm,double sal)
//constructor
{
firstname=fnm;
lastname=lnm;
//validation for negative numbers
if(sal<0)
{
monthsal=0.0;
}
else
monthsal=sal;
}
//end constructor

public void setfname(String fnm)
{
firstname=fnm;
}
public String getfname()
{
return firstname;
}

public void setlstname(String lnm)
{
lastname=lnm;
}
public String getlstname()
{
return lastname;
}

public void setsal(double sal)
{
//validation for negative numbers
if(sal<0)
{monthsal=0.0;}
else
monthsal=sal+((10*sal)/100);
}
public double getsal()
{
return monthsal;
}

public void display()
{
//System.out.println("This is from display method");
System.out.print(getfname());
System.out.print(getlstname());
System.out.print(getsal());
System.out.print("New salary=");
System.out.print(getsal());
}
}//end of class Employee


public class Employeetest
{
public static void main(String[] args)
{
Employee emp1=new Employee("Anu","Radha",2305.00);
Employee emp2=new Employee("Raja","Kumar",4599.05);
System.out.print("Employee1: ");
emp1.display();
System.out.println("Employee2: ");
emp2.display();
}
}
---------------------------------------------

Error message:
----------------------------------------
C:\Ash\javaprms>javac Employeetest.java
Employeetest.java:3: class Employee is public, should be declared in a file name
d Employee.java
public class Employee
^
1 error
---------------------------------------

Can anybody help me in this regard:

1. HOw to use Employee class from Employeetest.
2. How to display incremented salary after displaying current salary ?



With Regards,
Anu
cbare
Greenhorn

Joined: Oct 17, 2005
Posts: 1
Ash,

You should have two separate files. One called EmployeeTest.java containing the class EmployeeTest and the other called Employee.java containing the employee class. That's the way it's done in Java.

There's a rule that you can only have one public class per java file. You can have more than one class, but only one public class. It's usually a good idea to stick to one class per java file.

You compile them together like this:

>javac Employee.java EmployeeTest.java


<a href="http://www.cbare.org" target="_blank" rel="nofollow">www.cbare.org</a>
Bhavna Jharbade
Ranch Hand

Joined: Sep 08, 2005
Posts: 69
Hi Ashvini,
The error you r getting may be because you might have not saved the
Employee class with properly.Save it as Employee.java

1)You have done proper use of Employee in the Employeetest class.

2)To get the new salary make the following changes in your code
a)make the return type of setsal(double sal) method as double
n return monthsal from it


b) Include the extra line in the display() method as shown below



Hope this solves your problem
Gud luck


SCJP1.4 (86%)
Aadi Narayana Reddy
Greenhorn

Joined: Oct 17, 2005
Posts: 13
hi ash,

There should be only one Public Class in a java file. But u have written two public classes in the same java file. So it wont compile first.
So u just delete the public key word of the first class, and compile it.
or u can write the first class in seperate java file. and u can create object of that class from the second class.


G.Adi Narayana Reddy,
Bimal Patel
Ranch Hand

Joined: Aug 29, 2003
Posts: 130
Hi All,

I think in general, One should write whatever he/she wants to test in a class which has default access and not the public say Employee. And in the same file write a class which should have main method having public access, say EmployeeTest.


Work Hard, Expect The Worst...<br /> <br />Bimal R. Patel<br />(SCJP 1.2, SCWCD 1.4)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Class declaration problem.