In: Computer Science
Use JAVA to Design a simple registration system that allows Student to register in a course.
Requirements
The code is clearly explained in the comments of the code itself.
Code--
import java.util.*;
class Student
{
//attributes
private String name;
private String id;
//constructor with parameters
public Student(String name, String id)
{
this.name = name;
this.id = id;
}
//getters and setters
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public String getId()
{
return id;
}
public void setId(String id)
{
this.id = id;
}
}
class Course
{
//attributes
private String name;
private int numberOfStudent;
Student[] students= new Student[10];
//each object of the class is initialized using the
title name
public Course(String name)
{
this.name=name;
this.numberOfStudent=0;
}
public Student[] getStudents()
{
return students;
}
public boolean isFull()
{
if(this.numberOfStudent==10)
{
return
true;
}
else
{
return
false;
}
}
//get methods
public String getName()
{
return name;
}
public int getNumberOfStudent()
{
return numberOfStudent;
}
public void registerStudent(Student student)
{
if(!this.isFull())
{
numberOfStudent++;
students[numberOfStudent-1]=new
Student(student.getName(),student.getId());
}
}
}
public class Tests
{
public static void main(String[] args)
{
Student st=new
Student("Tom","00");
Course phy=new
Course("Physics");
phy.registerStudent(st);
st=new Student("Sam","02");
phy.registerStudent(st);
st=new Student("Ram","03");
phy.registerStudent(st);
st=new Student("Shyam","04");
phy.registerStudent(st);
//display the students
System.out.println("Course: "+
phy.getName());
System.out.println("Number of
students: "+ phy.getNumberOfStudent());
Student[]
physt=phy.getStudents();
System.out.printf("%10s\t%10s\n","NAME","ID");
for(int
i=0;i<phy.getNumberOfStudent();i++)
{
System.out.printf("%10s\t",physt[i].getName());
System.out.printf("%10s\t\n",physt[i].getId());
}
}
}
Output Screenshot--
Note--
Please upvote if you like the effort.