5 Replies - 790 Views - Last Post: 11 December 2010 - 03:43 PM Rate Topic: -----

#1 Jamaica1937  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 11-December 10

Help with arrays

Posted 11 December 2010 - 07:37 AM

So I've got an assignment to do but I don't quite get it, I'm supposed to make a program let's say for a company with x number of employees, what the program does is that it stores the name, age and salary of each user by using both arrays and a different class (Object oriented). The thing is I have no idea how to combine arrays with a different class, I made something but it doesn't use another class so I won't get a grade, if anyone can help me use this with another class I'll be grateful

The thing I really need is someone to explain to me how to use the class (Let's say I call it employee) in the main

This is what I did so far

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

package assignment;

import com.sun.org.apache.bcel.internal.generic.ARRAYLENGTH;
import javax.swing.JOptionPane;
import sun.swing.PrintColorUIResource;

/**
 *
 * @author Gimmy
 */
public class Main {

   private static int size;

   public static void main(String[] args) {
        size = Integer.parseInt(JOptionPane.showInputDialog("Enter number of employees"));

        double [] salary = new double[size];
        String [] name = new String[size];
        int [] age = new int[size];

        for (int i=1; i<=size ; i++)

        {
            salary[i] = Double.parseDouble(JOptionPane.showInputDialog("Enter salary of employee number" + i ));
            name [i] = (JOptionPane.showInputDialog("Enter name of employee number" + i));
            age [i] = Integer.parseInt(JOptionPane.showInputDialog("Enter age of employee number" + i));


        }

        for (int r=1; r<=size ; r++)
        {

            System.out.println("Employee number " + r + "Details are");
            System.out.println("Name is " + name[r] + "Age is " + age[r] + "Salary is " +salary[r]);


        }

    }

}



Is This A Good Question/Topic? 0
  • +

Replies To: Help with arrays

#2 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2250
  • View blog
  • Posts: 5,340
  • Joined: 10-September 10

Re: Help with arrays

Posted 11 December 2010 - 07:46 AM

I think what you need is another class that defines Employee, like:

public class Employee
{
    double [] salary = new double[size];
    String [] name = new String[size];
    int [] age = new int[size];
}



Then in your main() you can create an array of the Employee object:

Employee[] employees = new Employee[size];



Was This Post Helpful? 1
  • +
  • -

#3 Jamaica1937  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 11-December 10

Re: Help with arrays

Posted 11 December 2010 - 07:53 AM

I didn't know I can make an array like that in the main, well my problem is solved in this case, thanks a lot I appreciate it
Was This Post Helpful? 0
  • +
  • -

#4 Jamaica1937  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 11-December 10

Re: Help with arrays

Posted 11 December 2010 - 08:27 AM

Sorry to double post but now I have another problem, I can't seem to find a way to enter the variables (Age. name and salary) in the main, here's what I did so far

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

package assignment;

import javax.swing.JOptionPane;

/**
 *
 * @author Gimmy
 */
public class Employee {

    int size = Integer.parseInt(JOptionPane.showInputDialog("Enter number of employees"));

    double [] salary ;
    String [] name ;
    int [] age ;

}



^That is the Employee class


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

package assignment;

import com.sun.org.apache.bcel.internal.generic.ARRAYLENGTH;
import javax.swing.JOptionPane;
import sun.swing.PrintColorUIResource;

/**
 *
 * @author Gimmy
 */
public class Main {

   private static int size;

   public static void main(String[] args) {
        size = Integer.parseInt(JOptionPane.showInputDialog("Enter number of employees"));

         Employee[] employees = new Employee[size];



        for (int i=1; i<=size ; i++)

        {
          
           salary[i] = Double.parseDouble(JOptionPane.showInputDialog("Enter salary of employee number" + i ));
            name [i] = (JOptionPane.showInputDialog("Enter name of employee number" + i));
            age [i] = Integer.parseInt(JOptionPane.showInputDialog("Enter age of employee number" + i));


        }

        for (int r=1; r<=size ; r++)
        {

            System.out.println("Employee number " + r + "Details are");
            System.out.println("Name is " + name[r] + "Age is " + age[r] + "Salary is " +salary[r]);


        }

    }

}



I'm sure I must remove the salary, name and age in the first for loop but I have no idea what to replace/remove it with
Was This Post Helpful? 0
  • +
  • -

#5 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2250
  • View blog
  • Posts: 5,340
  • Joined: 10-September 10

Re: Help with arrays

Posted 11 December 2010 - 03:42 PM

I'm not real sure what you're problem is. Yes, you have some confusion about what goes where, what classes do, how you create and define one, but that'll all come out in the wash eventually.

Typically, a program instantiates classes and assigns values to its fields. In your case, you have an Employee class with 3 fields:

double salary,
String name,
int age

You declared them arrays, but you don't need arrays of those items in each class. Let's start with one instantiation of your Employee class:

    Employee employee = new Employee();



You would then typically assign values to the employee fields using accessor methods, commonly called 'setters.'

Somewhere in your program, perhaps in the class that contains your main() method:

    employee.setEmployeeName("John Smith");



Then, in the Employee class, the method defined:

public void setEmployeeName( String employeeName )
{
    this.name = employeeName;
}



You can do the same thing with arrays of your employee object. An example of how that looks:

    employee[index].setEmployeeName("John Smith");



and similarly for the rest of the snippets, except those that would be in the class.

This isn't likely to make this clear to you. You need to study more examples, preferably starting with single items rather than arrays of objects.

Good luck.
Was This Post Helpful? 1
  • +
  • -

#6 macosxnerd101  Icon User is online

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12149
  • View blog
  • Posts: 45,166
  • Joined: 27-December 08

Re: Help with arrays

Posted 11 December 2010 - 03:43 PM

You might also want to check out my tutorial Moving Away From Parlallel Arrays for more on class design. :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1