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 8 of 8

Thread: Java Class Help Please!

  1. #1
    Junior Member
    Join Date
    Jan 2014
    Posts
    11
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Java Class Help Please!

    How do I structure this??
    public static void main(String[] args) {
    // First we create a variable of type Employee and set it to a new
    // instance of an Employee, passing some test data to the constructor. Employee e = new Employee("John", "Fey", 1962, 12.59);
    // Print out the data with nice titles System.out.println("Name: " + e.firstName + " " + e.lastName); System.out.println("Emp. Id: " + e.employeeId); System.out.println("Hourly Rate: $" + e.hourlyRate); System.out.println(); // This will simply create a blank line
    // We're done with this employee, so now we'll re-use the Employee variable // called "e" by once again setting it to a new instance of an Employee,
    // passing different test data to the constructor.
    e = new Employee("Jamie", "Poehler", 5643, 14.25);
    // Print out the data with nice titles System.out.println("Name: " + e.firstName + " " + e.lastName); System.out.println("Emp. Id: " + e.employeeId); System.out.println("Hourly Rate: $" + e.hourlyRate); System.out.println(); // This will simply create a blank line
    }


  2. #2
    Member Ganeprog's Avatar
    Join Date
    Jan 2014
    Location
    Chennai,India
    Posts
    80
    My Mood
    Love
    Thanks
    3
    Thanked 6 Times in 6 Posts

    Cool Re: Java Class Help Please!

    Hi,

    You can structure like this,

     
    class Employee
    {
    private String firstName;
    private String lastName;
    private int employeeId;
    private double hourlyRate;
     
    public Employee(String firstName,String lastName,int employeeId,double hourlyRate)
    {
    	this.firstName=firstName;
    	this.lastName=lastName;
    	this.employeeId=employeeId;
    	this.hourlyRate=hourlyRate;
    }
     
    public void printEmpDetails()
    {
    	System.out.println("Name: " + firstName + " " + lastName);
    	System.out.println("Emp. Id: " + employeeId);
    	System.out.println("Hourly Rate: $" + hourlyRate);
    }
    }
     
    public class EmployeeTest {
     
    	public static void main(String[] args) {
    		Employee emp1 = new Employee("Tina", "Fey", 2092, 12.59);
    		Employee emp2= new Employee("Amy", "Poehler", 6283, 14.25);
    		emp1.printEmpDetails();
    		emp2.printEmpDetails();
     
    	}
     
    }

    Don't call the one class variables to another class its not a good practice. You can call the variables via methods.

  3. The Following User Says Thank You to Ganeprog For This Useful Post:

    zng690 (January 21st, 2014)

  4. #3
    Junior Member
    Join Date
    Jan 2014
    Posts
    3
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Java Class Help Please!

    class Example//starting with capital letter is best
    {
    void display()
    {
    Outer o = new Outer();
    o.test();
    System.out.println("welcome to java");
    }
    class Outer
    {
    void test()
    {
    System.out.println("oops");
    }
    }
    }
    class ExampleTwo
    {
    public static void main(String arg[])
    {
    Example to = new Example();
    to.display();

    }
    }

  5. The Following User Says Thank You to vinothkumar For This Useful Post:

    zng690 (January 21st, 2014)

  6. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,043
    My Mood
    Mellow
    Thanks
    184
    Thanked 648 Times in 635 Posts

    Default Re: Java Class Help Please!

    @AllThosePostingUnformattedCode: Welcome to the Forum! Please read this topic to learn how to post your code correctly along with other useful info for newcomers.

  7. #5
    Junior Member
    Join Date
    Jan 2014
    Posts
    11
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Java Class Help Please!

    Thanks for the help, Ok it seems though I am having a problem with:

    public class EmployeeTest {

    Should that be there?

  8. #6
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,043
    My Mood
    Mellow
    Thanks
    184
    Thanked 648 Times in 635 Posts

    Default Re: Java Class Help Please!

    What problem? Post error messages, describe the problem, ask questions. Give us something to work with.

  9. #7
    Member Ganeprog's Avatar
    Join Date
    Jan 2014
    Location
    Chennai,India
    Posts
    80
    My Mood
    Love
    Thanks
    3
    Thanked 6 Times in 6 Posts

    Default Re: Java Class Help Please!

    Hi,

    Previously i created two classes one is "Employee" and "EmployeeTest". From "EmployeeTest" class i created object then call the methods tat is proper oops program. You can reuse the methods. It reduces the code lines.

    But you given program can be structured like this,

    class Employee
    {
        // instance variables
     
        // constructor
     
        // a main() method to create an use instances
        public static void main(String[] args) {
     
        }
     
    }

    Thanks,
    Last edited by GregBrannon; January 22nd, 2014 at 04:11 AM. Reason: Removed code

  10. #8
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,043
    My Mood
    Mellow
    Thanks
    184
    Thanked 648 Times in 635 Posts

    Default Re: Java Class Help Please!

    @Ganeprog: Please do not provide solutions or code that with minor modifications could be used as a solution. Instead, show possible constructions and relevant examples that guide the OP to an answer rather than presenting the answer. You've already seen some of the confusion caused by providing code without explanation and/or more advanced code than the OP is prepared to handle. Please read The Problem With Spoon-feeding. In the end, thanks for helping.

Similar Threads

  1. Java and Flickr - Help with Inheritance between Main Class and sub-class
    By thientanchuong in forum What's Wrong With My Code?
    Replies: 10
    Last Post: December 14th, 2012, 06:29 PM
  2. Java and Flickr - Help with Inheritance between Main Class and sub-class
    By thientanchuong in forum What's Wrong With My Code?
    Replies: 0
    Last Post: December 12th, 2012, 07:10 PM
  3. Java, calling another public class from within the main class giving problems.
    By RandomGaisha in forum What's Wrong With My Code?
    Replies: 9
    Last Post: November 26th, 2012, 01:30 PM
  4. create a test class (main method) to start(run) the class in Java
    By curious725 in forum Java Theory & Questions
    Replies: 5
    Last Post: August 1st, 2012, 03:21 AM
  5. Replies: 3
    Last Post: June 17th, 2012, 06:22 PM