Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Implement toString() 1. Implement the toString() method for the provided Account Class. public class Account { private String account Number; public String getAccountNumber() { return

image text in transcribedimage text in transcribed

Implement toString() 1. Implement the toString() method for the provided Account Class. public class Account { private String account Number; public String getAccountNumber() { return account Number; } public void setAccountNumber(String accountNumber) { this.accountNumber = account Number; } } Implement equals() 2. Implement the equals() method for the provided Account Class. Implement hashCode() 3. Implement the hashCode() method for the provided Account Class. Implement the Comparable Interface and compare To() method 4. We already implemented the overrides of the toString, equals and hashCode methods. The equals method would tell us whether two Objects are equal or not. But in some cases we need to determine the order of an object with respect to another. For example if we are comparing two strings "AA" and "AB" the string AA precedes, or comes before the string "AB", because lexicographically the orders of characters in the first string precede the ones in the second String. In some situations, for example when objects need to be ordered (sorted), two objects need to be compared so they need to support a comparison method that determines whether two given objects are either: Equal if they have equal predefined data One object precedes (comes before) the other object Or One object comes after the other object You can try the example: String s = "AA"; System.out.println(s.compareTo("BB")); And check the compare to method definition. In Java in order to compare two objects the object's class must implement the comparable Interface and use the compare to method for comparison. The compareTo method returns: Zero if the two objects are equal Negative if the first object precedes the second object (parameter to compareTo) in order Positive if the first object comes after the second object (parameter to compareTo) in order Moreover, the definition of compare To for String: public int compare To(String anotherString) Parameters: anotherString - the String to be compared. Returns: the value 0 if the argument string is equal to this string; a value less than 0 if this string is lexicographically less than the string argument; and a value greater than 0 if this string is lexicographically greater than the string argument

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Beyond Big Data Using Social MDM To Drive Deep Customer Insight

Authors: Martin Oberhofer, Eberhard Hechler

1st Edition

0133509796, 9780133509793

More Books

Students also viewed these Databases questions