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:
Stream Selection Table
void print() – Display student name, age, mks and stream allocated.
Call all the above methods in main method using an object.
Student Stream Allocation Program in Java
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:
| Marks | 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.
Answer:
Student Stream Allocation Program in Java
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();
}
}