saving . . . saved Chaining of a class has been deleted. Chaining of a class has been hidden .
Chaining of a class
Title
Question
Madam/sir,
Can't we make a loop of Class say to save the data of employees in say a Class named EMPDATA no. of times using loop of an object of that class like we do in case of a C++ program like
Class EmpDATA
{
String name;
int age;
.......
...............
}s[5];

for(int i=0;i<5;i++)
{
String a;
int t;
cin>>t;
cin>>a;
s[i].name=a;
s[i].age=t;

}
for(int i=0;i<5;i++)
{
cout<<"name="<<s[i].name;
cout<<"age"=s[i].age;
.......... 
}


Java Strings 04-05 min 0-10 sec 18-06-22, 3:23 p.m. sc256356

Answers:

Simple java class can be defined such as Student.java. Then, you  can define class with main method such as Driver.java. In the Driver class you can create array of objects of Student class. Each Student object can be initialized in the loop.

Student.java
public class Student {
int id;
String name;
}

Driver.java

import java.util.Scanner;

public class Driver {

public static void main (String args[]) {
Student st[];

st = new Student[3];

Scanner sc = new Scanner(System.in);

for(int i= 1; i<3; i++) {

st[i] = new Student();
System.out.println("Enter Id: ");
st[i].id = sc.nextInt();
System.out.println("Enter Name: ");
st[i].name =sc.next();
}

System .out.println("Print the Results");

for(int i=1; i<3; i++) {
System.out.println(st[i].id);
System.out.println(st[i].name);
}
}
}

23-06-22, 12:50 p.m. pankajap@cse.iitb.ac.in


Log-in to answer to this question.