Home / Programs / Design a class with the following specifications:
Programming Example

Design a class with the following specifications:

👁 94 Views
💻 Practical Program
📘 Step by Step Learning

Design a class with the following specifications:

Class name: Student

Member variables:
name — name of student
age — age of student
mks — marks obtained
stream — stream allocated

(Declare the variables using appropriate data types)

Member methods:
void accept() — Accept name, age and marks using methods of Scanner class.
void allocation() — Allocate the stream as per following criteria:

mks stream
>= 300 Science and Computer
>= 200 and < 300 Commerce and Computer
>= 75 and < 200 Arts and Animation
< 75 Try Again

void print() – Display student name, age, mks and stream allocated.

Call all the above methods in main method using an object.

Information & Algorithm

Given Input & Expected Output:

Enter student name: Rumman Ansari
Enter age: 25
Enter marks: 89
Name: Rumman Ansari
Age: 25
Marks: 89.0
Stream allocated: Arts and Animation
Press any key to continue . . .

Program Code

import java.util.Scanner;

public class Student
{
    private String name;
    private int age;
    private double mks;
    private String stream;
    
    public void accept() 
    {
        Scanner in = new Scanner(System.in);
        System.out.print("Enter student name: ");
        name = in.nextLine();
        System.out.print("Enter age: ");
        age = in.nextInt();
        System.out.print("Enter marks: ");
        mks = in.nextDouble();
    }
    
    public void allocation() 
    {
        if (mks < 75)
            stream = "Try again";
        else if (mks < 200)
            stream = "Arts and Animation";
        else if (mks < 300)
            stream = "Commerce and Computer";
        else
            stream = "Science and Computer";
    }
    
    public void print() 
    {
        System.out.println("Name: " + name);
        System.out.println("Age: " + age);
        System.out.println("Marks: " + mks);
        System.out.println("Stream allocated: " + stream);
    }
    
    public static void main(String args[]) {
        Student obj = new Student();
        obj.accept();
        obj.allocation();
        obj.print();
    }
}

Output

Enter student name: Rumman Ansari
Enter age: 25
Enter marks: 89
Name: Rumman Ansari
Age: 25
Marks: 89.0
Stream allocated: Arts and Animation
Press any key to continue . . .

How to learn from this program

First read the algorithm, then study the program code line by line. After that, compare the code with the output and finally go through the explanation. This approach helps learners understand both the logic and the implementation properly.