Question

In: Computer Science

Write a Class called Module with the following attributes: module code, module name, list of lecturers...

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

Solutions

Expert Solution

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!


Related Solutions

Write a class Car that contains the following attributes: The name of car The direction of...
Write a class Car that contains the following attributes: The name of car The direction of car (E, W, N, S) The position of car (from imaginary zero point) The class has the following member functions: A constructor to initialize the attributes. Turn function to change the direction of car to one step right side (e.g. if the direction is to E,it should be changed to S and so on.) Overload the Turn function to change the direction to any...
#Write a class called "Burrito". A Burrito should have the #following attributes (instance variables): # #...
#Write a class called "Burrito". A Burrito should have the #following attributes (instance variables): # # - meat # - to_go # - rice # - beans # - extra_meat (default: False) # - guacamole (default: False) # - cheese (default: False) # - pico (default: False) # - corn (default: False) # #The constructor should let any of these attributes be #changed when the object is instantiated. The attributes #with a default value should be optional. Both positional #and...
Using python class: Design a class called Account CLASS NAME:    Account ATTRIBUTES: -nextAcctID: int    #NOTE-...
Using python class: Design a class called Account CLASS NAME:    Account ATTRIBUTES: -nextAcctID: int    #NOTE- class-level attribute initialized to 1000 -acctID: int -bank: String -acctType: String (ex: checking, savings) -balance: Float METHODS: <<Constructor>>Account(id:int, bank: String, type:String, bal:float) +getAcctID(void): int                        NOTE: retrieving a CLASS-LEVEL attribute! -setAcctID(newID: int): void           NOTE: PRIVATE method +getBank(void): String +setBank(newBank: String): void +getBalance(void): float +setBalance(newBal: float): void +str(void): String NOTE: Description: prints the information for the account one item per line. For example: Account #:        ...
Write a class named Person with data attributes for a person’s first name, last name, and...
Write a class named Person with data attributes for a person’s first name, last name, and telephone number. Next, write a class named Customer that is a subclass of the Person class. The Customer class should have a data attribute for a customer number and a Boolean data attribute indicating whether the customer wishes to be on a calling list. Demonstrate an instance of the Customer class in a simple program. Using python
Write a class called Time that represents the time of the day. It has attributes for...
Write a class called Time that represents the time of the day. It has attributes for the hour and minute. The hour value ranges from 0 to 23, where the range 0 to 11 represents a time before noon. The minute value ranges from 0 to 59. Write a default constructor that initializes the time to 0 hours and 0 minutes. Write a private method isValid(hour, minute) that returns true if the given hour and minute values are in the...
Write a class called Time that represents the time of the day. It has attributes for...
Write a class called Time that represents the time of the day. It has attributes for the hour and minute. The hour value ranges from 0 to 23, where the range 0 to 11 represents a time before noon. The minute value ranges from 0 to 59. Write a default constructor that initializes the time to 0 hours and 0 minutes. Write a private method isValid(hour, minute) that returns true if the given hour and minute values are in the...
1. Write a class called Rectangle that maintains two attributes to represent the length and width...
1. Write a class called Rectangle that maintains two attributes to represent the length and width of a rectangle. Provide suitable get and set methods plus two methods that return the perimeter and area of the rectangle. Include two constructors for this class. One a parameterless constructor that initializes both the length and width to 0, and the second one that takes two parameters to initialize the length and width. 2. Write a java program (a driver application) that tests...
Write a class called Name. A tester program is provided in Codecheck, but there is no...
Write a class called Name. A tester program is provided in Codecheck, but there is no starting code for the Name class. The constructor takes a String parameter representing a person's full name. A name can have multiple words, separated by single spaces. The only non-letter characters in a name will be spaces or -, but not ending with either of them. The class has the following method: • public String getName() Gets the name string. • public int consonants()...
Write a class called Name. A tester program is provided in Codecheck, but there is no...
Write a class called Name. A tester program is provided in Codecheck, but there is no starting code for the Name class. The constructor takes a String parameter representing a person's full name. A name can have multiple words, separated by single spaces. The only non-letter characters in a name will be spaces or -, but not ending with either of them. The class has the following methods. • public String getName() Gets the name string. • public int consonants()...
Implement a class Student, including the following attributes and methods: Two public attributes name(String) and score...
Implement a class Student, including the following attributes and methods: Two public attributes name(String) and score (int). A constructor expects a name as a parameter. A method getLevel to get the level(char) of the student. score level table: A: score >= 90 B: score >= 80 and < 90 C: score >= 60 and < 80 D: score < 60 Example:          Student student = new Student("Zack"); student.score = 10; student.getLevel(); // should be 'D'. student.score = 60; student.getLevel(); //...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT