1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
fgiga [73]
3 years ago
5

Suppose you are implementing the Comparable interface in a class representing a Book, where the ordering is based on the number

of pages in the book. Write a compareTo method for this class. You may assume that in the Book class there is an instance variable called numPages and a method called getNumPages.
Computers and Technology
1 answer:
guapka [62]3 years ago
4 0

Answer:

Following are the code to this question:

public class Book implements Comparable<Book> //defining a class that inherits the interface

{

int numPage; //defining integer variable numPage

int getNumPage()//defining a method getNumPage

{

return numPage; //use return keyword to return numPage value

}

int compareTo(Book b)//defining a method compareTo that uses Book instance b

{

   //using multiple conditions

if (getNumPage() > b.getNumPage())//defining if block that checks getNumPage method value is greater then instance of getNumPage  

{

return 1; //return value 1

}

else if (getNumPage() == b.getNumPage())//defining else if block that checks method and its instance value is euqal    

{

return 0;//return 0

}

else //defining else block

{

return -1;//return value -1

}

}

}

Explanation:

In the above-given code, a class book is defined, that inherits the interface that is "Comparable<Book>". Inside the class, an integer variable "numPage" and a method "getNumPages" is defined, that returns the "numPage" value.

In the next step, the "compareTo" method is declared, which uses an instance of the book and use the conditional statement.

  • In the if block, It checks the "getNumPages" method value is greater than the instance of the "getNumPages" method if the value is true, it will return "1", otherwise goto else if block.
  • In this block, it will check both "method and instance of method" values are equal, if the condition is true, it will return "0".
  • In the else block, if the above condition is false, it will return the value that is "-1".  
You might be interested in
QUESTION 11
tino4ka555 [31]

Answer:

x == 32

Explanation:

CODE in Java:

       int x = 32;

       if(x == 32){

           System.out.println("Pass");

       }

       else{

           System.out.println("Fail");

       }

OUTPUT:

Pass

8 0
2 years ago
Is anyone else excited for sephiroth (I think I spelt it wrong) in smash. And if you arn't are you excited for christmas
Fed [463]

Answer: idek what that is but okay ;v;

7 0
3 years ago
What country is associated with the tor relay using the ip address 104. 244. 74. 97?.
Lelu [443]

Answer:

104.28.63.45 is an IP address located in Santos, Sao Paulo.

Explanation:

3 0
2 years ago
im try to search for question the search bar and is giving me a error message is the page having any technical problem?
aalyn [17]
I have been running into the same problem, they haven't said anything but that might be the case.
6 0
3 years ago
You can use the ____ method to search a string to determine whether it contains a specific sequence of characters.
kherson [118]

Answer:

in

Explanation:

Not an explanation, but here's an example:

if "mouth" in "mouthwatering":

   print("It is.")

else:

   pass

Another example:

if "water" in "mouthwatering":

   print("Yes.")

else:

   pass

6 0
2 years ago
Other questions:
  • The purpose of the ________ element is to describe the contents of a table.
    15·1 answer
  • Tab stops are very useful when you want to A. align text into rows. B. add space between paragraphs. C. align text into columns.
    14·2 answers
  • Which one of the following is not possible to view in the debug logs?
    8·1 answer
  • Application software (apps) includes all the programs that allow you to perform specific tasks or applications on a computer. In
    6·1 answer
  • Which of the following actions is an example of "window dressing?" a. Using some of the firm’s cash to reduce long-term debt. b.
    13·1 answer
  • Coment on this if your user starts with dida
    5·1 answer
  • _________________________ are people, places, and materials, either printed or non-printed, that can provide answers to inquirie
    8·1 answer
  • You attempt to telnet to system 192.168.1.240. You receive the following message: "Connecting To 192.168.1.240...Could not open
    5·1 answer
  • takes the perspective that a program describes what the solution to a problem is, not how that problem is solved. Select one: a.
    5·1 answer
  • Subscribe to my you tube channel to get all your questions answered
    8·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!