Question

This is my course class. Can someone please explain what the equals method is for and...

This is my course class. Can someone please explain what the equals method is for and also the compareTo method is for in general and then explain what the methods are doing in this class.

public class Course {
   private boolean isGraduateCourse;
   private int courseNum;
   private String courseDept;
   private int numCredits;
  
   public Course(boolean isGraduateCourse, int courseNum, String courseDept, int numCredits) {
       this.isGraduateCourse=isGraduateCourse;
       this.courseNum=courseNum;
       this.courseDept=courseDept;
       this.numCredits=numCredits;
   }
   public boolean isGraduateCourse() {
       return this.isGraduateCourse;
   }
   public int getCourseNum() {
       return courseNum;
   }
   public String getCourseDept() {
       return courseDept;
   }
   public int getNumCredits() {
       return numCredits;
   }
   public String getCourseName() {
       if (this.isGraduateCourse == true) {
           return "G" + this.courseDept + this.courseNum;
       }
       else {
           return "U" + this.courseDept + this.courseNum;
       }
   }
   public boolean equals(Object obj) {
       Course other=(Course)obj;
       if(this.isGraduateCourse!=other.isGraduateCourse) {
           return false;
       }
       if(this.courseNum!=other.courseNum) {
           return false;
       }
       if(this.courseDept!=other.courseDept) {
           return false;
       }
       if(this.numCredits!=other.numCredits) {
           return false;
       }
       else
           return true;
   }
   public String toString() {
       if(isGraduateCourse) {
           return String.format("Course: %3s-%3d | Number of Credits: %02d | Graduate", courseDept,courseNum, numCredits, isGraduateCourse);
       }
       else {
           return String.format("Course: %3s-%3d | Number of Credits: %02d | Undergraduate", courseDept, courseNum,numCredits,isGraduateCourse);
       }
   }
   public int compareTo(Course c) {
       if(courseNum==c.getCourseNum()) {
           return 0;
       }
       else if(courseNum>=c.getCourseNum()){
           return 1;
       }
       else {
           return -1;
       }
   }
}

Homework Answers

Answer #1

Here equal method is used to check the equality with other Course object
so here are checking if both objects courseNum and courseDept and
numCredits are same than we are returning true means both objectsare same
this will help when we are adding the Course objects to any collections

compareTo():
This is used to compare the 2 objects to check which one is greaters
if both are equal than it will return 0 , if it is less than than it will return -1
else it will return 1
so here we are comparing the 2 objects based on the courseNum

so this will help when we want sort the Course objects in Arrays or Collections


NOTE : PLEASE COMMENT BELOW IF YOU HAVE CONCERNS.
I AM HERE TO HELP YOUIF YOU LIKE MY ANSWER PLEASE RATE AND HELP ME IT IS VERY IMP FOR ME

Know the answer?
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
Similar Questions
Q: Implement an equals method for the ADT list that returns true when the entries in...
Q: Implement an equals method for the ADT list that returns true when the entries in one list equal the entries in a second list. In particular, add this method to the class AList. The following is the method header: public boolean equals (Object other) public class AList<T>{ private T list[]; private int capacity = 100; private int numOfEnteries =0; public AList(){ list = (T[])new Object[capacity + 1]; } public void add(T element){ numOfEnteries++; if (numOfEnteries >capacity) System.out.println ("Exceed limit");...
The AssemblyLine class has a potential problem. Since the only way you can remove an object...
The AssemblyLine class has a potential problem. Since the only way you can remove an object from the AssemblyLine array is when the insert method returns an object from the last element of the AssemblyLine's encapsulated array, what about those ManufacturedProduct objects that are "left hanging" in the array because they never got to the last element position? How do we get them out? So I need to edit my project. Here is my AssemblyLine class: import java.util.Random; public class...
[Java] I'm not sure how to implement the code. Please check my code at the bottom....
[Java] I'm not sure how to implement the code. Please check my code at the bottom. In this problem you will write several static methods to work with arrays and ArrayLists. Remember that a static method does not work on the instance variables of the class. All the data needed is provided in the parameters. Call the class Util. Notice how the methods are invoked in UtilTester. public static int min(int[] array) gets the minimum value in the array public...
Here is a basic Counter class. class Counter { private int num = 0; // Not...
Here is a basic Counter class. class Counter { private int num = 0; // Not needed for this question public void increment() { num++; } public boolean equals(Counter other) { return this.num == other.num; } } Assume we have a program (in another file) that uses this class. As part of the program, we need to write a method with the following header: public static boolean exists(Counter[] counters, int numCounters, Counter counter) The goal of the method is to...
please fix code to delete by studentname import java.util.Scanner; public class COurseCom666 {     private String...
please fix code to delete by studentname import java.util.Scanner; public class COurseCom666 {     private String courseName;     private String[] students = new String[1];     private int numberOfStudents;     public COurseCom666(String courseName) {         this.courseName = courseName;     }     public String[] getStudents() {         return students;     }     public int getNumberOfStudents() {         return numberOfStudents;     }     public void addStudent(String student) {         if (numberOfStudents == students.length) {             String[] a = new String[students.length + 1];            ...
In java create a dice game called sequences also known as straight shooter. Each player in...
In java create a dice game called sequences also known as straight shooter. Each player in turn rolls SIX dice and scores points for any sequence of CONSECUTIVE numbers thrown beginning with 1. In the event of two or more of the same number being rolled only one counts. However, a throw that contains three 1's cancels out player's score and they mst start from 0. A total of scores is kept and the first player to reach 100 points,...
Task 1: You will modify the add method in the LinkedBag class.Add a second parameter to...
Task 1: You will modify the add method in the LinkedBag class.Add a second parameter to the method header that will be a boolean variable: public boolean add(T newEntry, boolean sorted) The modification to the add method will makeit possible toadd new entriesto the beginning of the list, as it does now, but also to add new entries in sorted order. The sorted parameter if set to false will result in the existing functionality being executed (it will add the...
java Consider the following class definition: public class Circle { private double radius; public Circle (double...
java Consider the following class definition: public class Circle { private double radius; public Circle (double r) { radius = r ; } public double getArea(){ return Math.PI * radius * radius; } public double getRadius(){ return radius; } } a) Write a toString method for this class. The method should return a string containing the radius and area of the circle; For example “The area of a circle with radius 2.0 is 12.1.” b) Writeaequalsmethodforthisclass.ThemethodshouldacceptaCircleobjectasan argument. It should return...
Whenever I try to run this program a window appears with Class not Found in Main...
Whenever I try to run this program a window appears with Class not Found in Main project. Thanks in Advance. * * 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 Assignment10; /** * * @author goodf */ public class Assignment10{ public class SimpleGeometricObject { private String color = "white"; private boolean filled; private java.util.Date dateCreated;    /**...
The following program creates a linked list which contains 5 links. Add a method called doubleValue()...
The following program creates a linked list which contains 5 links. Add a method called doubleValue() to the LinkedList class. The doubleValue() method must double the value of the number data field in each link. Add the required code to the main() method to call the doubleValue() method and display the revised list. public class Link { private int number; private Link next;      public Link(int x) { number = x; }    public void displayLink() { System.out.println("The number is:...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT