QuestionsJavaJava AssignmentWrite a java program to create a class for student and initiate...

Write a java program to create a class for student and initiate the object values using method: create and display for 10 students. (use scanner class)

Write a program to create a class for student and initiate the object values using method: create and display for 10 students. (use scanner class) Using JAVA, java assignment questions and answers, java assignment questions for beginners.

Code:

Java
import java.util.Scanner;

public class Student {
    private String name;
    private int rollNumber;
    private int age;

    public void create() {
        Scanner scanner = new Scanner(System.in);

        System.out.print("Enter name: ");
        this.name = scanner.nextLine();

        System.out.print("Enter roll number: ");
        this.rollNumber = scanner.nextInt();

        System.out.print("Enter age: ");
        this.age = scanner.nextInt();
    }

    public void display() {
        System.out.println("Name: " + this.name);
        System.out.println("Roll number: " + this.rollNumber);
        System.out.println("Age: " + this.age);
    }

    public static void main(String[] args) {
        Student[] students = new Student[10];

        for (int i = 0; i < students.length; i++) {
            students[i] = new Student();
            students[i].create();
        }

        System.out.println("\nStudent Details:");

        for (int i = 0; i < students.length; i++) {
            System.out.println("Student " + (i+1) + ":");
            students[i].display();
            System.out.println();
        }
    }
}

In this program, the Student class has three instance variables – name, rollNumber, and age – and two methods – create() and display(). The create() method uses a Scanner object to read input from the user and initialize the instance variables with the input values. The display() method simply prints the values of the instance variables to the console.

In the main() method, we create an array of 10 Student objects and use a loop to call the create() method for each object. Then, we loop through the array again and call the display() method for each object to print their details to the console.

Output


Enter name: Anubhav Anand
Enter roll number: 2022012
Enter age: 23
.
.
.
.
  1. Java programming language
  2. Java assignments
  3. Java programming assignment help
  4. Java homework help
  5. Java project help
  6. Java online tutoring
  7. Java programming solutions
  8. Java coding challenges
  9. Java programming practice exercises
  10. Java programming examples.

Share with Friends

Recommended

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Must Checkout