Question

Question 1 (c) (4 marks] For each of the methods equals and hashCode in the following class, explain whether or not they are

Java Programming.
please give me answer and explain in details.

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

In first screen shot with output have made changes only in equals method and in second code screen shot i have made changes in both hashcode and equals method please have a look

2 public class Term{ private int coeff; private int exp; 40 public Term (int coeff, int exp) { this.coeff=coeff; this.exp=expOutput:

Problems @ Javadoc Declaration Console x Debug <terminated Term [Java Application] C:\Program FilesVava\jdk1.8.0_192\bin\java

in above output: hascode for t[0] and t[1] is same because they both are referencing to the same object.

hashcode for t[1] and t[2] should be same but its not because we did not made changes in hashcode method  in code of above screenshot.

hashcode of t[3] and string object will be diff because these are different objects

Second screen shot with changes in both equals and hashcode method:

2 public class Term{ private int coeff; private int exp; 40 public Term (int coeff, int exp) { this.coeff=coeff; this.exp=expOutput:

Problems @ Javadoc Declaration Console X Debug <terminated Term (Java Application) C:\Program Files\Java\jdk1.8.0_192\bin\jav

Now we can see that the hascode for t[0] and t[1] and t[2] is same as t[0] and t[1] are pointing to the same object and t[1] and t[2] have same values for coeff and exp.

CODE with changes in both method:

public class Term{
   private int coeff;
  
   private int exp;
  
   public Term (int coeff,int exp){
       this.coeff=coeff;
       this.exp=exp;
       }
  
   public int getCoeff(){
       return coeff;
       }
   public int getExp(){
       return exp;
       }
  
   @Override //we can put some more conditions in equals method given by you ..changes made are given below
   public boolean equals(Object obj){
       if(obj==this){ // this condition is to check whether the object refer to the same location like t[0] and t[1] refer to the same object
           return true;
           }
       if(!(obj instanceof Term)){
           return false; // if we pass any other object other than Term will return false
       }
       Term term=(Term) obj;
       return ((term.coeff==this.coeff)&&(term.exp==this.exp)); // here we are checking the value of the object if these values are equal it will return true
   }
  
   @Override
   public int hashCode(){
       int result= this.coeff * this.exp *999; // after making changes in hashcode method
       return result; // now for Term object having same values will have same hashcode value
   }
  
   public static void main(String[] args){
       Term[] t=new Term[4];
       t[0] =new Term(1,2);
       t[1] =t[0]; // t[0] and t[1] refers to the same location so will give us same hashcode value
       t[2] =new Term(1,2);
       t[3] =new Term(3,4);
       String str=new String("abc");
      
       System.out.println("checking equals method :"+t[0].equals(t[2]));//here we are checking equals method it should return true and now hashcode for both will be same
       // because we have made changes in hashcode method now.
       for(int i=0;i<4;i++){
       System.out.println("Hashcode for object t["+i+"] : "+t[i].hashCode()); // printing hashcode values for Term objects
       }
       System.out.println("Hashcode for string object str : "+str.hashCode()); //printinh hashcode value for string
      
   }
}

Add a comment
Know the answer?
Add Answer to:
Java Programming. please give me answer and explain in details. Question 1 (c) (4 marks] For...
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
  • 17. Your job is to choose a data s 17x 4x30x2 6x12x tructure to implement polynomials...

    17. Your job is to choose a data s 17x 4x30x2 6x12x tructure to implement polynomials whose coefficients are positive integers, such as Here are three possible implementations. As a two-dimensional array boolean values: coeff [cl ti1 is true if and only if c x is a tem of the polynomial. As a tree map with Integer keys and values. If c * x? is a term of the polynomial, then we added the following correspondence to the map: coeff.put...

  • Polynomial Using LinkedList class of Java Language Description: Implement a polynomial class using a LinkedList defined...

    Polynomial Using LinkedList class of Java Language Description: Implement a polynomial class using a LinkedList defined in Java (1) Define a polynomial that has the following methods for Polynomial a. public Polynomial() POSTCONDITION: Creates a polynomial represents 0 b. public Polynomial(double a0) POSTCONDITION: Creates a polynomial has a single x^0 term with coefficient a0 c. public Polynomial(Polynomial p) POSTCONDITION: Creates a polynomial is the copy of p d. public void add_to_coef(double amount, int exponent) POSTCONDITION: Adds the given amount to...

  • In java please: TotalCostForTicket interface Create interface that includes one variable taxRate which is .09. It...

    In java please: TotalCostForTicket interface Create interface that includes one variable taxRate which is .09. It also includes an abstract method calculateTotalPrice which has no parameters and returns a value of type double. public abstract class Ticket { //There is a public static instance variable of type double for the basePrice. public static double basePrice; // private int theaterNumber; private int seatNumber; private boolean isTicketSold; private boolean isTicketReserved; public Ticket(int theaterNumber, int seatNumber) { this.theaterNumber = theaterNumber; this.seatNumber = seatNumber;...

  • The following is for java programming. the classes money date and array list are so I are are pre...

    The following is for java programming. the classes money date and array list are so I are are pre made to help with the coding so you can resuse them where applicable Question 3. (10 marks) Here are three incomplete Java classes that model students, staff, and faculty members at a university class Student [ private String lastName; private String firstName; private Address address; private String degreeProgram; private IDNumber studentNumber; // Constructors and methods omitted. class Staff private String lastName;...

  • Please help me do the java project For this project you will be reading in a...

    Please help me do the java project For this project you will be reading in a text file and evaluating it in order to create a new file that represents the Class that will represent the properties of the text file. For example, consider the following text file: students.txt ID              Name                              Age                    IsMale           GPA 1                Tom Ryan                       22                       True              3.1 2                Jack Peterson                31                       True              2.7 3                Cindy LuWho                12                       False             3.9 When you read in the header line, you...

  • Please answer the following question in java: 1. Why do we need to override the method...

    Please answer the following question in java: 1. Why do we need to override the method public boolean equals(Object ob) in any class? 2. Is public boolean equals(Object ob) is same as public boolean equals(Circle ob)?

  • Create a class to represent a term in an algebraic expression. As defined here, a term...

    Create a class to represent a term in an algebraic expression. As defined here, a term consists of an integer coefficient and a nonnegative integer exponent. E.g. in the term 4x2, the coefficient is 4 and the exponent 2 in -6x8, the coefficient is -6 and the exponent 8 Your class will have a constructor that creates a Term object with a coefficient and exponent passed as parameters, and accessor methods that return the coefficient and the exponent. Your class...

  • Can anyone add the 1) add_to_coef, add, multiply, coefficient, eval and equals method in the main...

    Can anyone add the 1) add_to_coef, add, multiply, coefficient, eval and equals method in the main function..so can take it from text file.... or 2) make main function with user input for all methods mentioned in the program. import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.util.Scanner; public class Polynomial { //array of doubles to store the coefficients so that the coefficient for x^k is stored in the location [k] of the array. double coefficients[]; static int size; //fube the following...

  • JAVA - Circular Doubly Linked List Does anybody could help me with this method below(previous)? public...

    JAVA - Circular Doubly Linked List Does anybody could help me with this method below(previous)? public E previous() { // Returns the previous Element return null; } Explanation: We have this class with these two implemented inferfaces: The interfaces are: package edu.ics211.h04; /** * Interface for a List211. * * @author Cam Moore * @param the generic type of the Lists. */ public interface IList211 { /** * Gets the item at the given index. * @param index the index....

  • I need Help PLZ ( Java Code ). Tomorrow 05.06.2019 I have to hand in my homework. reachability Actually, you find f...

    I need Help PLZ ( Java Code ). Tomorrow 05.06.2019 I have to hand in my homework. reachability Actually, you find flying very good, but you do not trust the whole new-fangled flying stuff and the infrastructure it has built up. As a diehard medieval metal fan you prefer to travel from A to B but rather the good old catapult. Since one can not easily take the favorite cat on vacation with it (cats do not get drafts, which...

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