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");...
Assignment 1 - ITSC 2214 I have to complete the array list for a shopping list...
Assignment 1 - ITSC 2214 I have to complete the array list for a shopping list code and can't figure out how to complete it: Below is the code Complete the three empty methods (remove(), find(), and contains()) in the ShoppingListArrayList.java file. These methods are already implemented in the ShoppingListArray class. /////////////////////////////////////////////////////////////////////////////////////////////////////////// Grocery Class (If this helps) package Shopping; public class Grocery implements Comparable<Grocery> { private String name; private String category; private int aisle; private float price; private int quantity;...
Please solve this problem in java. import java.util.Arrays; public class PriorityQueue { /* This class is...
Please solve this problem in java. import java.util.Arrays; public class PriorityQueue { /* This class is finished for you. */ private static class Customer implements Comparable { private double donation; public Customer(double donation) { this.donation = donation; } public double getDonation() { return donation; } public void donate(double amount) { donation += amount; } public int compareTo(Customer other) { double diff = donation - other.donation; if (diff < 0) { return -1; } else if (diff > 0) { return...
Please explain code 1 and code 2 for each lines code 1 public class MyQueue {...
Please explain code 1 and code 2 for each lines code 1 public class MyQueue {    public static final int DEFAULT_SIZE = 10;    private Object data[];    private int index; code 2 package test; import java.util.*; /* Class Node */ class Node { protected Object data; protected Node link; /* Constructor */ public Node() { link = null; data = 0; } /* Constructor */ public Node(Object d,Node n) { data = d; link = n; } /*...
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...
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...
[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...
public class PalindromeChecker { /** * Method that checks if a phrase or word is *...
public class PalindromeChecker { /** * Method that checks if a phrase or word is * a Palindrome * * @param str * Represents a string input * * @return true * True if the string is a Palindrome, * false otherwise */ public static boolean isPalindrome(String str) { if (str == null) { return false; } str = str.toLowerCase(); String temp = ""; for (int i = 0; i < str.length(); i++) { char ch = str.charAt(i); if ((ch...
Complete the // TODO sections in the EasyRental class. Create a method that implements an iterative...
Complete the // TODO sections in the EasyRental class. Create a method that implements an iterative sort that sorts the vehicle rentals by color in ascending order (smallest to largest) Create a method to binary search for a vehicle based on a color, that should return the index where the vehicle was found or -1 You are comparing Strings in an object not integers. Ex. If the input is: brown red white blue black -1 the output is: Enter the...
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];            ...