In: Computer Science
Write a Class called Module with the following attributes: module code, module name, list of lecturers for the module (some modules may have more than one lecturer – we only want to store their names), number of lecture hours, and module description. Create a parameterised (with parameters for all of the class attributes) and a non-parameterised constructor, and have the accessor and mutator methods for each attribute including a toString method. Write a class Student with the following attributes: student name, student home address, student id, degree programme enrolled in, year of study, and a list of modules (of type Module) the student is taking in the current year. Create a parameterised (with parameters for all of the attributes except the list of modules) and a non-parameterised constructor, and have the accessor and mutator methods for each attribute including a toString method. Finally write a testClass which creates student objects and adds list of modules chosen by the student.
JAVA language
Hello!
I have written the code what you have asked for and tested the objects of each class
I have limeted the number of modules and students, as this is for testing only
I have added comments to the code please comment below if you find any difficulty in the code , as this is only creation of methods and objects I am not explainig as you would know.
CODE:
Module.java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package javaapplication12;
/**
*
* @author DELL
*/
public class Module {
// instance variables of Module
int module_code;
String module_name;
String lecture_names[] = new String[10];
double lecture_hours;
String module_description;
// parameterized constructor
public Module(int module_code, String module_name, double lecture_hours, String module_description,String lect_names[]) {
this.module_code = module_code;
this.module_name = module_name;
this.lecture_hours = lecture_hours;
this.module_description = module_description;
System.arraycopy(lect_names,0,lecture_names,0,lect_names.length);
}
// non // parameterized constructor
public Module() {
this.module_code = 0;
this.module_name = null;
this.lecture_hours = 0;
this.module_description = null;
}
// all setters and getter of instance variable
public int getModule_code() {
return module_code;
}
public void setModule_code(int module_code) {
this.module_code = module_code;
}
public String getModule_name() {
return module_name;
}
public void setModule_name(String module_name) {
this.module_name = module_name;
}
public String[] getLecture_names() {
return lecture_names;
}
public void setLecture_names(String[] lecture_names) {
this.lecture_names = lecture_names;
}
public double getLecture_hours() {
return lecture_hours;
}
public void setLecture_hours(double lecture_hours) {
this.lecture_hours = lecture_hours;
}
public String getModule_description() {
return module_description;
}
public void setModule_description(String module_description) {
this.module_description = module_description;
}
// to string for the object
@Override
public String toString() {
String lecture_names_string = "[";
for (String value :lecture_names){
if(value != null )
lecture_names_string = lecture_names_string + value + ",";
}
lecture_names_string += "]";
return "Module{" + "module_code=" + module_code + ", module_name=" + module_name + ", lecture_names=" + lecture_names_string + ", lecture_hours=" + lecture_hours + ", module_description=" + module_description + '}';
}
}
Student.java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package javaapplication12;
/**
*
* @author DELL
*/
public class Student {
// instance variables of studenet
String student_name;
String student_home_address;
int student_id;
String degree_program;
int year_of_study;
Module modules[] = new Module[10];
// parameterized constructor
public Student(String student_name, String student_home_address, int student_id, String degree_program, int year_of_study, Module modules[]) {
this.student_name = student_name;
this.student_home_address = student_home_address;
this.student_id = student_id;
this.degree_program = degree_program;
this.year_of_study = year_of_study;
System.arraycopy(modules, 0, this.modules, 0, modules.length);
}
// non parameterized constructor
public Student() {
this.student_name = null;
this.student_home_address = null;
this.student_id = 0;
this.degree_program = null;
this.year_of_study = 0;
}
// all setters and getter of instance variable
public String getStudent_name() {
return student_name;
}
public void setStudent_hame(String student_name) {
this.student_name = student_name;
}
public String getStudent_home_address() {
return student_home_address;
}
public void setStudent_home_address(String student_home_address) {
this.student_home_address = student_home_address;
}
public int getStudent_id() {
return student_id;
}
public void setStudent_id(int student_id) {
this.student_id = student_id;
}
public String getDegree_program() {
return degree_program;
}
public void setDegree_program(String degree_program) {
this.degree_program = degree_program;
}
public int getYear_of_study() {
return year_of_study;
}
public void setYear_of_study(int year_of_study) {
this.year_of_study = year_of_study;
}
public Module[] getModules() {
return modules;
}
public void setModules(Module[] modules) {
this.modules = modules;
}
// to string for the object
@Override
public String toString() {
String modules_string = "[\n";
for(int i=0;i<10;i++){
if(modules[i]!= null )
modules_string = modules_string + modules[i].toString() + "\n";
}
modules_string = "]";
return "Student{" + "student_name=" + student_name + ", student_home_address=" + student_home_address + ", student_id=" + student_id + ", degree_program=" + degree_program + ", year_of_study=" + year_of_study + ", modules=" + modules_string + '}';
}
}
Test.java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package javaapplication12;
import java.util.*;
/**
*
* @author DELL
*/
public class Test {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Module m[] = new Module[5];
Student s[] = new Student[5];
// some sample module objects
// some sample data inserted into module objects
String lecture_names1[] = {"Basics of java", "Java servelets"};
m[0] = new Module(1545, "Java Backend development", 14.5, "Complete java backend", lecture_names1);
String lecture_names2[] = {"HTML", "CSS", "JS"};
m[1] = new Module(9132, "Front end development", 14.5, "UI and UX", lecture_names2);
String lecture_names3[] = {"Ruby", "ERB","Rails"};
m[2] = new Module(1545, "Ruby on Rails", 14.5, "Full stack ruby on rails", lecture_names1);
// printing all the modules data
System.out.println("**************Available modules**********");
for(int i=0;i<2;i++){
System.out.println(m[i].toString());
}
// students objects and entering data
System.out.println("\n**************Enter students data**********");
for (int i = 0; i < 2; i++) {
// variables for student object creation
System.out.println("\n******Student "+ (i+1) + " data**********");
String student_name;
String student_home_address;
int student_id;
String degree_program;
int year_of_study;
Module modules[] = new Module[5];
// getting input for studnts data
System.out.println("Enter the student name:");
student_name = sc.nextLine();
System.out.println("Enter the student address:");
student_home_address = sc.nextLine();
System.out.println("Enter the studend id:");
student_id = Integer.parseInt(sc.nextLine());
System.out.println("Enter the year of study:");
year_of_study = Integer.parseInt(sc.nextLine());
System.out.println("Enter the student degree program:");
degree_program = sc.nextLine();
System.out.println("Enter module numbers:");
int module = 0;
modules[module++] = m[Integer.parseInt(sc.nextLine())];
modules[module++] = m[Integer.parseInt(sc.nextLine())];
// passing all the data using parameterized constructor
s[i] = new Student(student_name,student_home_address, student_id,degree_program,year_of_study,modules);
}
// printing all the students data
System.out.println("**************Students enrolled**********");
for(int i=0;i<2;i++){
System.out.println(s[i].toString());
}
}
}
OUTPUT:
Hope this helps and clear.
I strive to provide the best of my knowledge so please upvote if you like the content.
Thank you!