Question

Design a JAVA class called Course. The class should contain: ○ The data fields courseName (String),...

Design a JAVA class called Course. The class should contain: ○ The data fields courseName (String), numberOfStudents (int) and courseLecturer (String). ○ A constructor that constructs a Course object with the specified courseName, numberOfStudents and courseLecturer. ○ The relevant get and set methods for the data fields. ○ A toString() method that formats that returns a string that represents a course object in the following format: (courseName, courseLecturer, numberOfStudents) ● Create a new ArrayList called courses1, add 5 courses to it and print it out. ● Sort the List according the numberOfStudents and print it out. ● Swap the element at position 1 of the List with the element at position 2 and print it out. ● Create a new ArrayList called courses2. ● Using the addAll method add 5 courses to the courses2 List and print it out. ● Copy all of the courses from courses1 into courses2. ● Add the following two elements to courses2: ○ (Java 101, Dr. P Green, 55) ○ (Advanced Programming, Prof. M Milton, 93) ● Sort the courses in courses2 alphabetically according to the course name and print it out. ● Search for the course “Java 101” in courses2 and print out the index of the course in the List. ● Use the disjoint function to determine whether courses1 and courses2 have any elements in common and print out the result. ● In courses2, find the course with the most students and the course with the least students and print each out. Hi and thanx

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Course.java:

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
class Course{
   String courseName;
   int numOfStudents;
   String courseLecturer;
   Course(String courseName, int numOfStudents, String courseLecturer) {
       this.courseName = courseName;
       this.numOfStudents = numOfStudents;
       this.courseLecturer = courseLecturer;
   }
   public void setCouseName(String courseName) {
       courseName = courseName;
   }
   public String getCourseName() {
       return courseName;
   }
   public void setNumOfStudents(int numOfStudents) {
       numOfStudents = numOfStudents;
   }
   public int getNumOfStudents() {
       return numOfStudents;
   }
   public void setCouseLecturer(String courseLecturer) {
       courseLecturer = courseLecturer;
   }
   public String getcourseLecturer() {
       return courseLecturer;
   }
   public String toString() {
       return "CourseName : "+courseName+" Number of Students : "+numOfStudents+" Course Lecturer: "+courseLecturer;
}
public static Comparator<Course> courseNameComparator = new Comparator<Course>() {

   public int compare(Course c1, Course c2) {
   String cs1 = c1.getCourseName().toUpperCase();
   String cs2 = c2.getCourseName().toUpperCase();

   //ascending order
   return cs1.compareTo(cs2);
}};
public static Comparator<Course> studentsComparator = new Comparator<Course>() {

   public int compare(Course c1, Course c2) {

   int s1 = c1.getNumOfStudents();
   int s2 = c2.getNumOfStudents();

   /*For ascending order*/
   return s1-s2;

}};


public static void main(String[] args) {
       ArrayList<Course> course1 = new ArrayList<Course>();
       course1.add(new Course("C", 10, "Leela"));
       course1.add(new Course("C++", 20, "Usha"));
       course1.add(new Course("Python", 5, "Teja"));
       course1.add(new Course("R", 2, "Ravi"));
       course1.add(new Course("C#", 15, "Kiran"));
       for(Course c: course1) {
           System.out.println(c);
       }
       System.out.println();
       Collections.sort(course1, Course.studentsComparator);
       for(Course c: course1) {
           System.out.println(c);
       }
       System.out.println();
       Collections.swap(course1, 1, 2);
       for(Course c: course1) {
           System.out.println(c);
       }
       System.out.println();
       ArrayList<Course> course2 = new ArrayList<Course>();
       course2.addAll(course1);
       for(Course c: course2) {
           System.out.println(c);
       }
       System.out.println();
       course2.add(new Course("Java 101",55, "Dr. P Green"));
       course2.add(new Course("Advanced Programming", 93, "Prof. M Milton"));
       for(Course c: course2) {
           System.out.println(c);
       }
       System.out.println();
       Collections.sort(course2, Course.courseNameComparator);
       for(Course c: course2) {
           System.out.println(c);
       }
       System.out.println();
       boolean isCommon = Collections.disjoint(course1, course2);
       System.out.println("No Common elements : "+isCommon);
       System.out.println();
       Collections.sort(course2, Course.studentsComparator);
       System.out.println("Course with minimum no of students : ");
       System.out.println(course2.get(0));
       System.out.println("Course with maximum no of students : ");
       System.out.println(course2.get(course2.size()-1));
   }
}

Output:

Add a comment
Know the answer?
Add Answer to:
Design a JAVA class called Course. The class should contain: ○ The data fields courseName (String),...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Java Framework Collection Assign. Create a class called ArrayListExample Create a ArrayList object called languageList which...

    Java Framework Collection Assign. Create a class called ArrayListExample Create a ArrayList object called languageList which accept type of String Add English, French, Italian and Arabic strings into languageList array object Print languageList using Iterator object Sort ArrayList object alphabetically Print languageList using Iterator object Solution will produce following: ArrayListExample class ArrayList object called languageList ArrayListExampleTest class with main method

  • Given a class called Student and a class called Course that contains an ArrayList of Student....

    Given a class called Student and a class called Course that contains an ArrayList of Student. Write a method called getDeansList() as described below. Refer to Student.java below to learn what methods are available. I will paste both of the simple .JAVA codes below COURSE.JAVA import java.util.*; import java.io.*; /****************************************************** * A list of students in a course *****************************************************/ public class Course{     /** collection of Students */     private ArrayList<Student> roster; /***************************************************** Constructor for objects of class Course *****************************************************/...

  • JAVA Create an ArrayList called list of type Character 2. Add the following five elements |...

    JAVA Create an ArrayList called list of type Character 2. Add the following five elements | < \ / > (4 points) 3. Print the elements of the list, with a label (3 points) 4. Delete the last element of the list (4 points) 5. Use the method size to determine how many elements there are in the list (no hard-coded numbers). Print the number of elements. (3 points) 6. Add a plus sign ( + ) to the list...

  • JAVA /** * This class stores information about an instructor. */ public class Instructor { private...

    JAVA /** * This class stores information about an instructor. */ public class Instructor { private String lastName, // Last name firstName, // First name officeNumber; // Office number /** * This constructor accepts arguments for the * last name, first name, and office number. */ public Instructor(String lname, String fname, String office) { lastName = lname; firstName = fname; officeNumber = office; } /** * The set method sets each field. */ public void set(String lname, String fname, String...

  • java Create the following classes: DatabaseType: an interface that contains one method 1. Comparator getComparatorByTrait(String trait)...

    java Create the following classes: DatabaseType: an interface that contains one method 1. Comparator getComparatorByTrait(String trait) where Comparator is an interface in java.util. Database: a class that limits the types it can store to DatabaseTypes. The database will store the data in nodes, just like a linked list. The database will also let the user create an index for the database. An index is a sorted array (or in our case, a sorted ArrayList) of the data so that searches...

  • Design and code a JAVA program called ‘Grades’. ? Your system should store student’s name, and...

    Design and code a JAVA program called ‘Grades’. ? Your system should store student’s name, and his/her course names with grades. ? Your system should allow the user to input, update, delete, list and search students’ grades. ? Each student has a name (assuming no students share the same name) and some course/grade pairs. If an existing name detected, user can choose to quit or to add new courses to that student. ? Each student has 1 to 5 courses,...

  • In C++ Write a program that contains a class called VideoGame. The class should contain the...

    In C++ Write a program that contains a class called VideoGame. The class should contain the member variables: Name price rating Specifications: Dynamically allocate all member variables. Write get/set methods for all member variables. Write a constructor that takes three parameters and initializes the member variables. Write a destructor. Add code to the destructor. In addition to any other code you may put in the destructor you should also add a cout statement that will print the message “Destructor Called”....

  • Create a nested inner class inside a file called LinkedList.java called the ListIterator. ListIterator: This public...

    Create a nested inner class inside a file called LinkedList.java called the ListIterator. ListIterator: This public class is nested within an LinkedList data structure and allows us to traverse the elements in any collection, access any data element and remove any data elements of the collection. It also adds the functionality to move forward and backward, which is new functionality. We are going to build this data structure from scratch. Instance variables (fields) are: mPrevious (Node) – the previous node...

  • Using Java coding, complete the following: This program should implement a LinkedList data structure to handle...

    Using Java coding, complete the following: This program should implement a LinkedList data structure to handle the management of student records. It will need to be able to store any number of students, using a Linked List. LinearNode.java can be used to aid creating this program Student should be a class defined with the following:    • String name    • int year    • A linked list of college classes (college classes are represented using strings)    • An...

  • create a class in Java for a Plane the class will include String Data Fields for...

    create a class in Java for a Plane the class will include String Data Fields for the make, model, and year. (as well as mutators and accessors) Defined constructor and copy constructor. there must be a toString method as well as a copy method. runner program the program will create an array for Plane class. The size of the array will be set to user input. then the user will input the information into each plane object in the array....

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT