-
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
-
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
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks