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

Design a class with the following specifications:

👁 94 Views
💻 Practical Program
📘 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();
    }
}
                        

🖥 Program 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 . . .

                            
📚 Learning Subject

Master Programming Through Practical Examples

Improve your coding logic, problem-solving skills and programming confidence by practicing real-world examples with explanations.

🎯 How to learn from this example

First understand the algorithm carefully. Then study the program line-by-line and compare it with the output. Finally, review the explanation section to strengthen your logic and programming understanding.

🔥 Practice suggestion

Rewrite the program without looking at the code. Modify values, conditions or logic and run it again. This helps improve confidence and strengthens coding skills much faster.