Need some java help


Last Updated:

  1. Sainix

    Sainix Well-Known Member This Topic's Starter

    Joined:
    Aug 27, 2010
    Messages:
    158
    Likes Received:
    2
    Hey guys taking a java class and i am having trouble trying to get this to work.

    need to Creat a class called Employee that includes three instance variabels, a first name (type string), a last name (type string) and a monthly salary (double). provide a constructor that initializes the three instance variables. provide a set and a get method for each instance variable. if the monthly salary is not positive, do not set its value write a test application named EmployeeTest that demonstrates class Employee's capabilities. Creat two employee objects and display each objects yearly salary. Then give each Employee a 10% raise and display each Employee's yearly salary agian.

    So that is the problem and so far i have got

    class Employee{

    // data vector
    String fn;
    String ln;
    double s;

    // constructor
    public Employee(String f, String l, double sal) {
    fn = f;
    ln = l;
    s = sal;
    }

    // set procedure
    void setFirst(String fNew) {
    fn = fNew;
    }
    void setLast(String lNew) {
    ln = lNew;
    }
    void setSal(double sNew) {
    s = sNew;
    }

    //get function
    String getFirst(){
    return fn;
    }
    String getLast(){
    return ln;
    }
    String getSalary(){
    return s;
    }
    }

    and for the test i have

    public class EmployeeTest {
    public static void main(String [] args){
    Employee e1 = new Employee();
    Employee e2 = new Employee("John","Pimpson",1.00);
    System.out.printf("%s\t%s\t%f\n", e2.getFirst(),e2.getLast(),e2.getSalary());
    System.out.printf("%s\t%s\t%f\n", e1.getFirst(),e1.getLast(),e1.getSalary());
    e1.setFirst("John 5");
    e1.setLast("Player");
    e1.setSalary(10.00);
    System.out.printf("%s\t%s\t%f\n", e1.getFirst(),e1.getLast(),e1.getSalary());
    e.raiseSalary(1.1);
    }
    }

    now im just stuck and trying to read threw my book for more info but if someone who knows java could maybe lend me a hand with this i would be greatful since this is only my first week in class.
     

    Advertisement
  2. johnlgalt

    johnlgalt Antidisestablishmentarian VIP Member

    Joined:
    Oct 28, 2009
    Messages:
    9,416
    Likes Received:
    1,919
    What exactly are you stuck on?
     
  3. Sainix

    Sainix Well-Known Member This Topic's Starter

    Joined:
    Aug 27, 2010
    Messages:
    158
    Likes Received:
    2
    Stuck on getting it to run, watched some youtube videos and came up with this

    Public Class Employee{

    // data vector
    Private String firstName;
    Private String lastName;
    Private Double salaryValue;

    // constructor
    public Employee(String firstNameParameter, String lastNameParameter, Double salaryValueParameter)
    {
    firstName = firstNameParameter;
    lastName = lastNameParameter;
    if (doubleSalaryValue < 0)
    salaryValue = 0.0
    else
    salaryValue = salaryValueParameter;
    }

    // set methods
    void setFirstName(String firstNameParameter)
    {
    firstName = firstNameParameter;
    }

    void setLastName(String lastNameParameter)
    {
    lastName = lastNameParameter;
    }

    void setSalaryValue(Double doubleSalaryValue)
    {
    if (doubleSalaryValue < 0)
    salaryValue = 0.0
    else
    salaryValue = doubleSalaryValue;

    }

    //get function
    String getFirstName()
    {
    return firstName;
    }
    String getLastName()
    {
    return lastName;
    }
    Double getSalaryValue()
    {
    return salaryValue;
    }


    } //End Class

    ----------

    class EmployeeTest
    {
    public static void main(String [] args)
    {
    String temp;
    Double temp2;
    Double temp3;
    Employee EmployeeoneObject = new Employee("Bob","Bobson",10.0);
    Employee EmployeetwoObject = new Employee("John","Pimpson",36.0);

    temp = EmployeeoneObject.getFirstName();
    System.out.print(temp);
    temp = EmployeeoneObject.getLastName();
    System.out.print(" ");
    System.out.print(temp);
    System.out.print("'s salary is ");
    temp2 = EmployeeoneObject.getSalaryValue();
    System.out.print(temp2);
    System.out.print("/n");

    temp = EmployeetwoObject.getFirstName();
    System.out.print(temp);
    temp = EmployeetwoObject.getLastName();
    System.out.print(" ");
    System.out.print(temp);
    System.out.print("'s salary is ");
    temp2 = EmployeetwoObject.getSalaryValue();
    System.out.print(temp2);
    System.out.print("/n");

    temp2 = EmployeeoneObject.getSalaryValue();
    temp3 = temp2 * 1.1;
    EmployeeoneObject.setSalaryValue(temp3);

    temp2 = EmployeetwoObject.getSalaryValue();
    temp3 = temp2 * 1.1;
    EmployeetwoObject.setSalaryValue(temp3);

    temp = EmployeeoneObject.getFirstName();
    System.out.print(temp);
    temp = EmployeeoneObject.getLastName();
    System.out.print(" ");
    System.out.print(temp);
    System.out.print("'s salary is ");
    temp2 = EmployeeoneObject.getSalaryValue();
    System.out.print(temp2);
    System.out.print("/n");

    temp = EmployeetwoObject.getFirstName();
    System.out.print(temp);
    temp = EmployeetwoObject.getLastName();
    System.out.print(" ");
    System.out.print(temp);
    System.out.print("'s salary is ");
    temp2 = EmployeetwoObject.getSalaryValue();
    System.out.print(temp2);
    System.out.print("/n");
    }

    }


    When ever i try to javac Employee or javac EmployeeTest in command prompt i get a message saying

    error: Class names, 'Employee', are only accepted if annotations explicitly requested
    1 error

    same thing for employeetest

    Any ideas?
     
  4. johnlgalt

    johnlgalt Antidisestablishmentarian VIP Member

    Joined:
    Oct 28, 2009
    Messages:
    9,416
    Likes Received:
    1,919
    Did you include the .java part of the file names for each file?

    you gotta do javac Employee.java in order for it to compile, IIRC....
     
  5. Sainix

    Sainix Well-Known Member This Topic's Starter

    Joined:
    Aug 27, 2010
    Messages:
    158
    Likes Received:
    2
    ROFL OMG how did i forget that haha must be that sleepy dammm
    Thank you =p

    kk now i get 13 errors....=[

    Microsoft Windows XP [Version 5.1.2600]
    (C) Copyright 1985-2001 Microsoft Corp.

    C:\Documents and Settings\USER>e:





    E:\>javac Employee.java
    Employee.java:1: class, interface, or enum expected
    Public Class Employee{
    ^
    Employee.java:5: class, interface, or enum expected
    Private String lastName;
    ^
    Employee.java:6: class, interface, or enum expected
    Private Double salaryValue;
    ^
    Employee.java:9: class, interface, or enum expected
    public Employee(String firstNameParameter, String lastNameParameter, Dou
    ble salaryValueParameter)
    ^
    Employee.java:12: class, interface, or enum expected
    lastName = lastNameParameter;
    ^
    Employee.java:13: class, interface, or enum expected
    if (doubleSalaryValue < 0)
    ^
    Employee.java:17: class, interface, or enum expected
    }
    ^
    Employee.java:23: class, interface, or enum expected
    }
    ^
    Employee.java:28: class, interface, or enum expected
    }
    ^
    Employee.java:37: class, interface, or enum expected
    }
    ^
    Employee.java:43: class, interface, or enum expected
    }
    ^
    Employee.java:47: class, interface, or enum expected
    }
    ^
    Employee.java:51: class, interface, or enum expected
    }
    ^
    13 errors

    E:\>
     
  6. Hangdog42

    Hangdog42 Well-Known Member

    Joined:
    Jan 24, 2010
    Messages:
    170
    Likes Received:
    25
    You've got a ton of typos in there. For example Private should be private and you're missing EOL delimiters in a lot of places.

    One thing to do that will help TREMENDOUSLY is to get yourself a decent IDE. Have a look at Eclipse or netbeans. They do syntax checking on the fly and will save you a megaton of headaches.
     
    Sainix likes this.
  7. Sainix

    Sainix Well-Known Member This Topic's Starter

    Joined:
    Aug 27, 2010
    Messages:
    158
    Likes Received:
    2
    I am using netbeans and i have eclipse, thank you for letting me know about that i kinda thought that might be what is up.
     

Share This Page

Loading...