Implement the following classes class Department that includ
Implement the following classes:
class Department that includes three instance variables: private String deptName;
private Course CRS[];
private int noOfCourses;
Your class should have the following:
a constructor that initializes the department name and creates the
array.
boolean exist(int) that checks whether the course object with id
passed as parameter exists in the array or not.
void addCourse(int, String) creates and adds the course if the
course with id passed as parameter does not exist in the array.
void deleteCourse(int) to delete a course if it exists in the array
and displays a proper message.
void printCourses() that prints the department name and all the
courses in the array.
4
3. Write a test application named Lab1Test. In the main method, do the following:
Input the department name and maximum number of courses then create a department object.
Input the number of courses.
Input the id and name of each course and add it to the department
(Hint: Use a loop).
Print the courses.
Input the id of a course to be deleted. Delete the course.
Print the courses.
Input the id of a course to check if it exists and print a proper
message
Input the id and name of an additional set of courses and add them
to the department (Hint: Use a loop).
Print the courses.
Sample Output
Please enter the maximum number of courses in the department:
20
Enter a course id to be deleted: 201
The course with the ID = 201 was deleted. Courses in Computer Engineering Department are: 1 - 200 C++ Programming
2 - 207 Data Structures
Enter a course id to search for: 201
The course with the ID = 201 does not exist in the department list
How many additional courses do you have? 4 Enter course 1 id: 210
Enter course 1 name: Computer Ethics
Enter course 2 id: 262
Courses in Computer Engineering Department are: 1 - 200 C++ Programming
2 - 210 Computer Ethics
3 - 207 Data Structures
Solution
Course.class
public class Course {
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Department.java
public class Department {
private String deptName;
private Course CRS[];
private int noOfCourses;
public String getDeptName() {
return deptName;
}
public void setDeptName(String deptName) {
this.deptName = deptName;
}
public Course[] getCRS() {
return CRS;
}
public void setCRS(Course[] cRS) {
CRS = cRS;
}
public int getNoOfCourses() {
return noOfCourses;
}
public void setNoOfCourses(int noOfCourses) {
this.noOfCourses = noOfCourses;
}
Department(String name,int max,int noc)
{
deptName=name;
CRS=new Course[max];
noOfCourses=0;
}
boolean exist(int id)
{
if(noOfCourses>0)
{
for(int i=0;i<noOfCourses;i++)
{
if(CRS[i].getId()==id)
{
return true;
}
}
}
return false;
}
void addCourse(int id, String name)
{
if(noOfCourses<=CRS.length)
{
if(!exist(id))
{
CRS[noOfCourses]=new Course();
CRS[noOfCourses].setId(id);
CRS[noOfCourses].setName(name);
noOfCourses++;
}
else
{
System.out.println(\"Course already exist\");
}
}
else
{
System.out.println(\"Course excced its max limit\");
}
}
void deleteCourse(int id)
{
int i=0;
boolean flag=false;
if(exist(id))
{
for(i=0;i<noOfCourses;i++)
{
if(CRS[i].getId()==id)
{
flag=true;
System.out.println(\"The course with the ID = \" +CRS[i].getId() + \"was deleted\");
break;
}
}
if(flag==true)
{
for(int j=i,k=j+1;j<noOfCourses-1&&k<noOfCourses-1;j++,k++)
{
CRS[j].setId(CRS[k].getId());
CRS[j].setName(CRS[k].getName());
}
noOfCourses--;
}
}
else
{
System.out.println(\"Course\");
}
}
void printCourses()
{
System.out.println(\" Courses in \"+ getDeptName() +\"are:\");
//System.out.println(CRS.length);
for(int i=0;i<noOfCourses;i++)
{
System.out.println(\"\"+(i+1)+\" -\"+ CRS[i].getId() +\" \"+CRS[i].getName());
}
}
}
Lab1Test.java
import java.util.Scanner;
public class Lab1Test {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println(\"Please enter the department name:\");
String name=sc.nextLine();
System.out.println(\"Please enter the maximum number of courses in the department:\");
int max=sc.nextInt();
System.out.println(\"How many courses do you have?\");
int noc=sc.nextInt();
Department department=new Department(name, max, noc);
for(int i=0;i<noc;i++)
{
System.out.println(\"Enter course \"+ (i+1) +\"id: \");
int id=sc.nextInt();
System.out.println(\"Enter course \"+(i+1) +\"name:\");
String cname=sc.next();
sc.nextLine();
department.addCourse(id, cname);
}
department.printCourses();
System.out.println(\"Enter the id to delete:\");
int id=sc.nextInt();
department.deleteCourse(id);
department.printCourses();
department.deleteCourse(id);
System.out.println(\"Enter course \"+ department.getNoOfCourses()+1 +\"id: \");
int id1=sc.nextInt();
System.out.println(\"Enter course \"+department.getNoOfCourses()+1 +\"name:\");
String cname=sc.next();
sc.nextLine();
department.addCourse(id1, cname);
department.printCourses();
}
}
Output:
Please enter the department name:
CSE
Please enter the maximum number of courses in the department:
20
How many courses do you have?
3
Enter course 1id:
200
Enter course 1name:
c++
Enter course 2id:
201
Enter course 2name:
java
Enter course 3id:
202
Enter course 3name:
network
Courses in CSEare:
1 -200 c++
2 -201 java
3 -202 network
Enter the id to delete:
202
The course with the ID = 202was deleted
Courses in CSEare:
1 -200 c++
2 -201 java
Course
Enter course 21id:
203
Enter course 21name:
xyz
Courses in CSEare:
1 -200 c++
2 -201 java
3 -203 xyz
![Implement the following classes: class Department that includes three instance variables: private String deptName; private Course CRS[]; private int noOfCourses Implement the following classes: class Department that includes three instance variables: private String deptName; private Course CRS[]; private int noOfCourses](/WebImages/2/implement-the-following-classes-class-department-that-includ-974555-1761497855-0.webp)
![Implement the following classes: class Department that includes three instance variables: private String deptName; private Course CRS[]; private int noOfCourses Implement the following classes: class Department that includes three instance variables: private String deptName; private Course CRS[]; private int noOfCourses](/WebImages/2/implement-the-following-classes-class-department-that-includ-974555-1761497855-1.webp)
![Implement the following classes: class Department that includes three instance variables: private String deptName; private Course CRS[]; private int noOfCourses Implement the following classes: class Department that includes three instance variables: private String deptName; private Course CRS[]; private int noOfCourses](/WebImages/2/implement-the-following-classes-class-department-that-includ-974555-1761497855-2.webp)
![Implement the following classes: class Department that includes three instance variables: private String deptName; private Course CRS[]; private int noOfCourses Implement the following classes: class Department that includes three instance variables: private String deptName; private Course CRS[]; private int noOfCourses](/WebImages/2/implement-the-following-classes-class-department-that-includ-974555-1761497855-3.webp)
![Implement the following classes: class Department that includes three instance variables: private String deptName; private Course CRS[]; private int noOfCourses Implement the following classes: class Department that includes three instance variables: private String deptName; private Course CRS[]; private int noOfCourses](/WebImages/2/implement-the-following-classes-class-department-that-includ-974555-1761497855-4.webp)