Java Programming | Arrays of Class

Introduction

This is a tutorial for creating array of class in java. The program is given below that creates arrays of class. The program is extendable. Go enjoy the program. Lets begin……..

Before you begin.Do this

  1. Create a separate java file for other class.
  2. Create a java file for main class.
  3. Import the created java class in main class file.

Program for creating arrays of class in java.

1.Create a class

//declare the package name if the class file is
//created in other package.
package Myclasses;
 
//import scanner
import java.util.Scanner;
 
//declare class
public class Student {
 
    //declare variables.
    private int rno,s1,s2,s3,tot,per;
 
    //functions should be public if needed to access from other class
    public void getdata()
    {
        //print message to enter numbers
        System.out.println("Enter Roll No:");
        //Take input
        Scanner input = new Scanner(System.in);
        rno = input.nextInt();
        System.out.println("Enter 1st Subject Marks:");
        s1 = input.nextInt();
        System.out.println("Enter 2st Subject Marks:");
        s2 = input.nextInt();
        System.out.println("Enter 3st Subject Marks:");
        s3 = input.nextInt();
    }
 
    public void calculate()
    {
        tot = s1+s2+s3;
        per = tot/3;
    }
 
    public void print()
    {
        System.out.print(rno+"\t"+s1+"\t"+s2+"\t"+s3+"\t"+tot+"\t"+per+"\n");
    }
}

2. Create main class

//import our class
import Myclasses.Student;
 
// the name of our class its public
public class ClassArray {
    //void main
        public static void main (String[] args)
        {
            //declare class
            Student[] s = new Student[3];
            s[0] = new Student();
            s[1] = new Student();
            s[2] = new Student();
            //call functions
            s[0].getdata();
            s[1].getdata();
            s[2].getdata();
            s[0].calculate();
            s[1].calculate();
            s[2].calculate();
            System.out.print("Rno\tS1\tS2\tS3\tTotal\tPer \n");
            s[0].print();
            s[1].print();
            s[2].print();
        }
}

Output

Enter Roll No:
1
Enter 1st Subject Marks:
90
Enter 2st Subject Marks:
90
Enter 3st Subject Marks:
90
Enter Roll No:
2
Enter 1st Subject Marks:
99
Enter 2st Subject Marks:
99
Enter 3st Subject Marks:
99
Enter Roll No:
3
Enter 1st Subject Marks:
90
Enter 2st Subject Marks:
80
Enter 3st Subject Marks:
90
Rno S1 S2 S3 Total Per
1 90 90 90 270 90
2 99 99 99 297 99
3 90 80 90 260 86

How it Works

  1. Array of class is created.
  2. The getdata function is called from main class.
  3. The calculate function is called from main class.
  4. The print function is called from main class.

Extending it

The program can be extended by using the concept in any program. Like for ex. you can create a same program for gross salary of employees,etc. Go ahead extend it……

  • Remember this is a basic part of java which can be used in any program….

Explanation.

Your class

  1. Create class.
  2. Import the Scanner.
  3. Declare the class as public
  4. Declare variables.
  5. Create functions.

Main class

  1. Create other class
  2. Import your class.
  3. Declare the class as public.
  4. Add the void main function.
  5. Declare class.
  6. Declare arrays
  7. Call functions.

At the end.

You learnt creating the Java program for Creating array of class . So now enjoy the program.

Please comment on the post and share it.

Sharing options: