Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 10 of 10

Thread: writing java code help

  1. #1
    Junior Member
    Join Date
    Feb 2011
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default writing java code help

    Create a class called Employee. An Employee should have four pieces of information as instance variables – first name (type String), last name (type String), monthly salary (type double), and dependents(type int). Your class should have a constructor that initializes the four instance variables. Provide a get and set method for each instance variable. In addition, provide a method named getAnnualSalary that determines the annual salary of the employee (i.e. monthly salary multiplied by 12), and returns the annual salary (a double value). Write a test application named EmployeeTest that demonstrates class Employee's capabilities.

    any ideas on where to start this and how to do the test deff need help on this im lost as hell


  2. #2
    Member samfin's Avatar
    Join Date
    Dec 2010
    Location
    Manchester UK
    Posts
    37
    Thanks
    1
    Thanked 5 Times in 4 Posts

    Default Re: writing java code help

    We can't do your homework for you, try to write the code, there are tutorials on this forum and info from Google and if you get stuck, post the code and the problem

  3. #3
    Junior Member
    Join Date
    Feb 2011
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: writing java code help

    You should start by creating a class called Employee. Try googling first to see what a constructor is and how it can be used to initialize your variables. Your test class is relatively simple. Just create another class with a main method that contains various calls to the methods (constructor and get/set) of your Employee class.

  4. #4
    Junior Member
    Join Date
    Feb 2011
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: writing java code help

    o so ive written some code please tell me if im on the right track and if possible offer suggestions

    /*
    * To change this template, choose Tools | Templates
    * and open the template in the editor.
    */

    package employee;

    /**
    *
    * @author JAISAN
    */
    public class employee {

    String _firstname;
    String _lastname;
    double _monthlysalary;
    int _dependents;

    public employee(String initialfirstname,String initiallastname,double initialmonthlysalary, int initialdependents){

    _firstname = initialfirstname;
    _lastname = initiallastname;
    _dependents = initialdependents;
    _monthlysalary = initialmonthlysalary
    }

    public String get_firstname()
    {
    return _firstname;
    }

    public void set_firstname(String new_firstname)
    {
    _firstname = new_firstname;
    }

    public String get_lastname()
    {
    return _lastname;
    }

    public void set_lastname(String new_lastname)
    {
    _lastname = new_lastname;
    }

    public int _dependents()
    {
    return _dependents;
    }

    public void setSong(int new_dependents)
    {
    _dependents = new_dependents;
    }

    }


    and the test i used a previous project so its not complete but am i on the right trak

    /*
    * To change this template, choose Tools | Templates
    * and open the template in the editor.
    */

    package employee;

    /**
    *
    * @author JAISAN
    */
    public class employeetest {


    public static void main(String args[])

    {
    // Construct new employee
    employee myEmployee = new employee();

    // Display IPOD state

    System.out.println("Current Brand : " + myemployee.get_firstname());
    System.out.println("Current Song : " + myemployee.get_lastname());
    System.out.println("Current Volume : " + myemployee.get_dependents());

    System.out.println();

    // Change Brand, Channel, and turn up volume.
    myIpod.setBrand("APPLE 12GIG");
    myIpod.setSong(100);
    myIpod.turnVolumeUp();

    // Display IPOD state
    System.out.println("Power On Status : " + myIpod.isOn());
    System.out.println("Current Brand : " + myIpod.getBrand());
    System.out.println("Current Song : " + myIpod.getSong());
    System.out.println("Current Volume : " + myIpod.getVolume());
    System.out.println();

    // Turn Volume down 2 times and then turn off IPOD
    myIpod.turnVolumeDown();
    myIpod.turnVolumeDown();
    myIpod.turnOff();

    // Display IPOD state
    System.out.println("Power On Status : " + myIpod.isOn());
    System.out.println("Current Brand : " + myIpod.getBrand());
    System.out.println("Current Song : " + myIpod.getSong());
    System.out.println("Current Volume : " + myIpod.getVolume());
    System.out.println();

    // Turn IPOD on
    myIpod.turnOn();

    // Display IPOD state
    System.out.println("Power On Status : " + myIpod.isOn());
    System.out.println("Current Brand : " + myIpod.getBrand());
    System.out.println("Current Song : " + myIpod.getSong());
    System.out.println("Current Volume : " + myIpod.getVolume());
    System.out.println();
    }
    }

  5. #5
    Member samfin's Avatar
    Join Date
    Dec 2010
    Location
    Manchester UK
    Posts
    37
    Thanks
    1
    Thanked 5 Times in 4 Posts

    Default Re: writing java code help

    Quote Originally Posted by jaisan72980 View Post
    Create a class called Employee. An Employee should have four pieces of information as instance variables – first name (type String), last name (type String), monthly salary (type double), and dependents(type int). Your class should have a constructor that initializes the four instance variables. Provide a get and set method for each instance variable. In addition, provide a method named getAnnualSalary that determines the annual salary of the employee (i.e. monthly salary multiplied by 12), and returns the annual salary (a double value). Write a test application named EmployeeTest that demonstrates class Employee's capabilities.
    You're pretty much there, just a few typos stopping it working and you haven't done a method to work out the annual salary.
    _dependents() I'm guessing should be get_dependants as that's what you've called in employeetest and myemployee is spelt two different ways so just choose one and keep it consistent.
    As for the annual salary, you can do a getter method in employee for _monthlysalary and then just multiply it by 12 in your main method.

    I guess the parts about iPods was from a different project.

  6. #6
    Member samfin's Avatar
    Join Date
    Dec 2010
    Location
    Manchester UK
    Posts
    37
    Thanks
    1
    Thanked 5 Times in 4 Posts

    Default Re: writing java code help

    Just a quick one, can you put your code in the [ highlight=Java] [ /highlight] tags, makes it easier to read.
    Also and this is totally up to you, class names are usually written with capital letters for the beginning of each word so Employee or EmployeeTest and method names the same but with the first letter lower case, getFirstName getLastName. The code will work anyway, just makes it even easier to read

  7. #7
    Junior Member
    Join Date
    Feb 2011
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: writing java code help

    ok i am still stuck please help i know im close but im just not getting it only had 2 weeks instruction im in a bind

    this is where im at , im still getting errors and i havent done the monthy salary becaus i dont know how please help

    /*
    * To change this template, choose Tools | Templates
    * and open the template in the editor.
    */

    package Employee;

    /**
    *
    * @author JAISAN
    */
    public class employee {

    String _FirstName;
    String _LastName;
    double _monthlysalary;
    int _Dependents;

    public employee(String initialFirstName,String initialLastName,double initialmonthlysalary, int initialDependents){

    _FirstName = initialFirstName;
    _LastName = initialLastName;
    _Dependents = initialDependents;
    _monthlysalary = initialmonthlysalary;
    }

    public String get_FirstName()
    {
    return _FirstName;
    }

    public void set_FirstName(String new_FirstName)
    {
    _FirstName = new_FirstName;
    }

    public String get_LastName()
    {
    return _LastName;
    }

    public void set_LastName(String new_LastName)
    {
    _LastName = new_LastName;
    }

    public int _getDependents()
    {
    return _Dependents;
    }

    public void set_Dependents(int new_Dependents)
    {
    _Dependents = new_Dependents;
    }

    public int get_monthlysalary()
    {
    return _monthlysalary;
    }

    public void set_monthlysalary(int new_monthlysalary)
    {
    _monthlysalary = new_monthlysalary;
    }



    }

    and the test is

    /*
    * To change this template, choose Tools | Templates
    * and open the template in the editor.
    */

    package employee;

    /**
    *
    * @author JAISAN
    */
    public class employeetest {


    public static void main(String args[])

    {
    // Construct new employee
    employee myEmployee = new employee();

    // Display IPOD state

    System.out.println("Current First Name : " + myEmployee.get_FirstName());
    System.out.println("Current Last Name : " + myEmployee.get_LastName());
    System.out.println("Current Amount of Dependents : " + myEmployee.get_Dependents());

    System.out.println();

    // Change Brand, Channel, and turn up volume.
    myEmployee.set_FirstName();
    myEmployee.set_LastName();
    myEmployee.set_Dependents();

    System.out.println("Current First Name : " + myEmployee.get_FirstName());
    System.out.println("Current Last Name : " + myEmployee.get_LastName());
    System.out.println("Current Amount of Dependents : " + myEmployee.get_Dependents());
    }
    }

  8. #8
    Member samfin's Avatar
    Join Date
    Dec 2010
    Location
    Manchester UK
    Posts
    37
    Thanks
    1
    Thanked 5 Times in 4 Posts

    Default Re: writing java code help

    When you say you're getting errors, can you post the errors you're getting please.

  9. #9
    Junior Member
    Join Date
    Feb 2011
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: writing java code help

    ok first i am using netbeans to write this and one error im getting (Possible loss of precision)

    public int get_monthlysalary()
    {
    return _monthlysalary;

    }


    when i run the program im getting

    Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - cannot find symbol
    symbol: class employee
    location: class employee.employeetest
    at employee.employeetest.main(employeetest.java:19)
    Java Result: 1

  10. #10
    Member samfin's Avatar
    Join Date
    Dec 2010
    Location
    Manchester UK
    Posts
    37
    Thanks
    1
    Thanked 5 Times in 4 Posts

    Default Re: writing java code help

    The loss of precision is because the return type on get_monthlysalary() is int but _monthlysalary is a double value.

    The other exception where it can't find the class is another typo. One package is called Employee and the other is employee. Java is case sensitive so these are different packages to the compiler.

    You've still got a typo for _getDependents(), when you call this method in the test class you've spelled it differently

    Also, your declarations for the methods in the employee class look like this
    public void set_FirstName(String new_FirstName)
    {
    _FirstName = new_FirstName;
    }
     
    public void set_LastName(String new_LastName)
    {
    _LastName = new_LastName;
    }
     
    public void set_Dependents(int new_Dependents)
    {
    _Dependents = new_Dependents;
    }

    but when you call the methods, they look like this

    myEmployee.set_FirstName();
    myEmployee.set_LastName();
    myEmployee.set_Dependents();

    You're missing the arguments when you call the methods.

    Think that's it

Similar Threads

  1. How to start writing Java Apps for Cell Phones?
    By Jchang504 in forum Java ME (Mobile Edition)
    Replies: 7
    Last Post: January 10th, 2011, 05:11 AM
  2. Replies: 2
    Last Post: August 1st, 2010, 06:29 AM
  3. Writing data to the java DefaultTableModel
    By dubois.ford in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 1st, 2010, 08:43 PM
  4. Trouble writing some code...help?
    By bChEos in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: February 7th, 2010, 08:54 PM
  5. Help writing some Scanner code
    By bChEos in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: February 3rd, 2010, 04:27 AM