1

I have these two classes:

class Student
{
String name;
String age ;
}

class Person
{

String name;
String age ;
String grade ;
}

In the code below, I am creating Student objects and setting them inside the ArrayList. Once the data is set in the ArrayList, I need to parse that ArrayList and set the values in another object:

public class Work {

    public static void main(String args[]) {

        List StudentItems = new ArrayList();

        Student stud1 = new Student();
        Student stud2 = new Student();

        stud1.name = "ABC";
        stud1.age = "28";
        stud2.name = "XYZ";
        stud2.age = "38";

        StudentItems.add(stud1);
        StudentItems.add(stud2);

        Person[] pers = new Person[StudentItems.size()];

        for (int i = 0; i < StudentItems.size(); i++) {
            pers[i] = new Person();

// I am confused here , could anyone please help 

        }

    }
}
2
  • What is Person class here and what do you want to do with it?? Commented Mar 5, 2012 at 5:58
  • Why has Person a grade, and Student not? Didn't you mix that up? Why doesn't Student extend Person? Commented Mar 6, 2012 at 1:49

6 Answers 6

3

Try it out. This will do the work

Your Person class should be something like this:

package com.student.person.work;

/**
 *
 * @author sarath_sivan
 */
public class Person {

    private String name;
    private int age;
    private String grade;

    public String getName() {
       return this.name;
    }

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

    public int getAge() {
        return this.age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getGrade() {
        return this.grade;
    }

    public void setGrade(String grade) {
        this.grade = grade;
    }

}

Your Student class should be something like this:

package com.student.person.work;

/**
 *
 * @author sarath_sivan
 */
public class Student {

    private String name;
    private int age;

    public String getName() {
       return this.name;
    }

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

    public int getAge() {
        return this.age;
    }

    public void setAge(int age) {
        this.age = age;
    }

}

And finally, the Work class:

package com.student.person.work;

import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author sarath_sivan
 */
public class Work {

    public static String calculateGrade() {
        String grade = "";
        // Your code to find the grade.
        //............
        return grade;
    }

    public static void doWork() {
        List<Student> studentList = new ArrayList<Student>();

        Student student = new Student();
        student.setName("ABC");
        student.setAge(24);
        studentList.add(student);

        student = new Student();
        student.setName("DEF");
        student.setAge(28);
        studentList.add(student);

        student = new Student();
        student.setName("GHI");
        student.setAge(21);
        studentList.add(student);

        List<Person> personList = new ArrayList<Person>();

        for(Student students : studentList) {
            Person person = new Person();
            person.setName(students.getName());
            person.setAge(students.getAge());
            person.setGrade(Work.calculateGrade());// Setting the grade
        }
    }

    public static void main(String[] args) {
        Work.doWork();
    }

}

Hope this will be helpful.

Thank you!

Sign up to request clarification or add additional context in comments.

Comments

3

Something like this:

    List<Student> studentItems = new ArrayList<Student>();
    Student stud1 = new Student();
    Student stud2 = new Student();

    stud1.name = "ABC";
    stud1.age = "28";
    stud2.name = "XYZ";
    stud2.age = "38";

    studentItems.add(stud1);
    studentItems.add(stud2);

    for (int i = 0; i < studentItems.size(); i++) {
        Student student = studentItems.get(i);
        Person person = new Person();
        person.name = student.name;
        person.age = student.age;
        // person.grade = something - set grade here
        pers[i] = person;
    }

But be avare that you shouldn't use public fields... so it should look like this:

for (int i = 0; i < studentItems.size(); i++) {
    Student student = studentItems.get(i);
    Person person = new Person();
    person.setName(student.getName());
    person.setAge(student.getAge());
    // person.setGrade(computeGradeSomehow()); - set grade here
    persons[i] = person;
}

Comments

1

If you are frequently converting from student object to Person object, add following like constructor and setter/getter method

class Person {

        String name;

        String age;

        String grade;

        public Person() {

        }

        Person(Student student) {
            this.name = student.getName();
            this.age = student.getAge();
        }

        public String getGrade() {
            return grade;
        }

        public void setGrade(String grade) {
            this.grade = grade;
        }
    }

    class Student {
        private String name;

        private String age;

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

        public String getName() {
            return name;
        }

        public String getAge() {
            return age;
        }

        public void setAge(String age) {
            this.age = age;
        }
    }

And in Work class, create Person object like,

List<Student> studentItems = new ArrayList<Student>();
 List<Person> personItems = new ArrayList<Person>();

for (int i = 0; i < studentItems.size(); i++) {
        Student student = studentItems.get(i);
        Person person = new Person(student);
        person.setGrade(your_formula_for grade);  
        personItems.add(person); 
}

Comments

0

You can do it as follow:

pers[i].name = StudentItems.get(i).name;

Comments

0

Grade should be in the Student class. If you to this, your classes should look like this:

class Person {
private String name; 
private int age; 

//getters and setters
}

class Student extends Person { // here you have name and age from Person
private String grade;

//getters and setters
}

Now, you want the list of persons from the list of students? You can do this:

for (int i = 0; i < listOfStudents.size(); i++){
arrayOfPersons[i] = (Person)listOfStudents.get(i);
}

Comments

0

Given, that your classes are really properly layouted (see my comment), you could write a constructor for person which takes an Student as input:

        pers[i] = new Person (StudentItems[i]);

note, that I would rename the variables:

        persons [i] = new Person (students[i]);

Your Person with the new CTor would look like this:

class Person
{
  String name;
  String age ;
  String grade ;

  public Person () {}
  public Person (s Student) {
    name = s.name;
    age = s.age;
  }
}

More probably, you want to change the name of Student and Person, and derive Student from Person. Then, every Student is a person, and in your loop, it is justs:

        persons [i] = students[i];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.