New Programmer Seeks Help...


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: New Programmer Seeks Help...

  1. #1
    Join Date
    Dec 2004
    Location
    Canada
    Posts
    1

    New Programmer Seeks Help...

    Simple program in retrospect....

    i have to make a class list program making use of the arraylist function.

    Consists of two classes: Cs30 & Student

    Cs30 utilizes Student to add Students to the Cs30 Arraylist.

    Now, i can add students easy enough to the array list itself. The problem i am having is displaying all the students. I tried using a simple system.out.println(Cs30.get(x)), but it prints the code for the object.

    What i really need is to gain access to a specific student's attributes, assign them to temporary variables, and display them.

    So something like:

    name = Cs30(x).StudentName

    println (name)

    OR something like:

    Student Test;

    Test = Cs30(x)

    *at this point, i could use Test's attributes of name name, number, mark etc. to display them*

    I tried this earlier but got a "could not resolve symbol" error over and over again.

    thanks for any help you can give me.
    - Overconfidence Breeds Carelessness

  2. #2
    Join Date
    Aug 2004
    Location
    On the net
    Posts
    38
    Hi,

    Maybe you need to write the Student class and then perform the operations in the Cs30 class as shown below.


    ==============================================
    import java.util.*;

    public class Cs30
    {

    private Student list;
    private ArrayList studentList;

    public Cs30()
    {
    studentList = new ArrayList();
    }

    public void addStudent(String name,int number,double marks)
    {
    list = new Student(name,number,marks);
    studentList.add(list);
    }

    public void getStudent(int number)
    {
    Student temp = (Student) studentList.get(number);

    System.out.println("Name :"+temp.getName());
    /*Place all the out statements needed.*/
    }

    public static void main(String arg[])
    {
    Cs30 obj = new Cs30();

    obj.addStudent("ABC",1,30);
    obj.addStudent("DEF",2,30);
    obj.addStudent("GHI",3,30);
    obj.addStudent("JKL",4,30);

    obj.getStudent(1);
    }


    }


    ==============================================
    public class Student{

    private String name;
    private int number;
    private double marks;

    public Student(String name,int number,double marks)
    {

    this.name = name;
    this.number = number;
    this.marks = marks;
    }

    public String getName()
    {
    return name;
    }

    public int getNumber()
    {
    return number;
    }

    public double getMarks()
    {
    return marks;
    }
    }

    Still if you have any doubts can mail me. Also, I just made an assumption and penned down the above program and this is not the completely implemented logic. You need to incorporate your own logic here.

    Good Luck :-)

    Narayana

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center