How to use an array of objects and call them in methods in Java
- class Student
- {
- private String name;
- private int roll;
- public void setName(String name)
- {
- this.name=name;
- }
- public void setRoll(int roll)
- {
- this.roll=roll;
- }
- public String getName()
- {
- return name;
- }
- public int getRoll()
- {
- return roll;
- }
- }
- -------------------------------------------------------------
- import java.io.*;
- class Department
- {
- public static void main(String args[])
- {
- BufferedReader br=new BufferefReader(new InputStreamReader(System.in));
- System.out.println("How many elements you want to create in the array?");
- int n=Integer.parseInt(br.readLine());
- Student students[]=new Student[n]; //Creating array of object of Student class
- for(int i=0;i
- {
- students[i]=new Student(); //Instantiation of object
- System.out.println("Enter name");
- String na=br.readLine();
- System.out.println("Enter roll");
- int r=Integer.parseInt(br.readLine());
- students[i].setName(na);
- students[i].setRoll(r);
- }
- Department obj=new Department();
- obj.displayDetails(students);
- }
- public void displayDetails(Student[] s)
- {
- for(Student stu:s)
- {
- System.out.println("Name: "+stu.getName());
- System.out.println("Roll: "+stu.getRoll());
- }
- }
- }
Articoli simili
- Qual è la differenza tra call tracking, call metrics e call analytics?
- What are the best dubbed (English) completed animes, and where do I find them? I am seeking for a list and links.
- Come possiamo convertire un array 1-dimensionale in un array 2-dimensionale in Python?
- How to hide the address bar and the tabs on Google Chrome so that when I hover over them, they display