Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

newbie simple java program

 
ajay garg
Greenhorn
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi
i m a newbie in programmig and making a simple program
i made a class

class student
{
String name;
int id;
int age ;
}
public static void main()
{
student o1 = new student();
o1.add (???
}

i want my program in this way:
here output must be like this
do u want to add student? say (y/n)
if i write 'y'
than it asked
please write: name, id, age .
ajay , 001 , 18


than it must show the output:
ajay ,001 ,18
now again it must asked
do u want to add student? say (y/n)
here i want to make a list of student

i wan to print list of student uptill user write y
please help me in any way so that
i can go through this problem
[ August 24, 2005: Message edited by: ajay garg ]
 
Michael Dunn
Ranch Hand
Posts: 4632
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you're adding students, so you want to separate the student from the register.
create the following classes
class StudentRegister
class Student

You need to have (in the register)
your code for main() - to start the program
a collection class - to hold the student objects

the collection class could be
an array - but you need to know/set a size
an arraylist
a hashmap - where (say) student id is the key

now, if the user answers yes to add student, you get the 3 data items
and use the constructor to add the data to the student object
arraylist.add(new Student("Mary",12345,21));
 
ajay garg
Greenhorn
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


import java.util.*;
class student
{

static String name;
static int age;
static int id;

public student(String name , int age , int id)
{


student.name= name;
student.age=age;
student.id = id;
}


}


public class candidate

{

public static void main(String args[])

{

Map map = new HashMap(); // hash table
map.put("a", new student(" ajay" , 25 , 1));
map.put("b", new student(" ram " , 23 , 2));
map.put("c", new student(" shyam" , 20, 3) );
// Iterator it = map.keySet().iterator();
System.out.println(map);
}



}





i have done up till now
now how to iterate and according to myproblem
it must asked do u want to add y/n

pleeeese help anyone
 
Ashok George
Ranch Hand
Posts: 87
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
that reply!! was gud!!
lot of info in that!!!
thanx..... for the help!!!
really great cheers to the moderators of this grou[p...... really great work guys!!!
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic