Created
November 18, 2020 12:36
-
-
Save PrajaktaSathe/aaebb1ed4fe1f323f16fac3584392964 to your computer and use it in GitHub Desktop.
Program to demonstrate array of objects, take user input, display elements of array
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Program to demonstrate array of objects in Java - | |
package array; | |
import java.util.Scanner; // For taking user input | |
class Student { | |
private int rno, marks; | |
private String name; | |
// Function for getting data from user input - | |
public void getData() { | |
Scanner sc = new Scanner(System.in); | |
System.out.println("Enter roll no: "); | |
rno = sc.nextInt(); // To take int input from user, use nextInt() | |
System.out.println("Enter name: "); | |
name = sc.next(); // To take string input from user, use next() | |
System.out.println("Enter marks: "); | |
marks = sc.nextInt(); | |
} | |
// Function to display data taken from user - | |
public void displayData() { | |
System.out.println("Roll No.: " + rno); | |
System.out.println("Name: " + name); | |
System.out.println("Marks: " + marks); | |
} | |
} | |
public class Array { | |
public static void main(String[] args) { | |
// TODO Auto-generated method stub | |
Scanner sc = new Scanner(System.in); | |
int studNum; | |
System.out.println("Enter number of students: "); | |
studNum = sc.nextInt(); | |
// Creating an array of objects - | |
Student[] stud = new Student[10]; | |
int i; | |
for (i = 0; i < studNum; i++) { | |
stud[i] = new Student(); // allocation of memory | |
} | |
for (i = 0; i < studNum; i++) { | |
stud[i].getData(); // Getting data | |
} | |
for (i = 0; i < studNum; i++) { | |
stud[i].displayData(); // Displaying data | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment