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
_________ are the special effects that you see when one slide changes to another in slide show view​
lisabon 2012 [21]

Answer:

Transition effects

Explanation:

Transition effects are the special effects you see when one slide changes to another in slide show view.

8 0
3 years ago
16.
dimaraw [331]
  • Answer:

<em>r = 15 cm</em>

  • Explanation:

<em>formula</em>

<em>V = πr²×h/3</em>

<em>replace</em>

<em>4950 = 22/7×r²×21/3</em>

<em>4950 = 22/7×r²×7</em>

<em>4950 = 22×r²</em>

<em>r² = 4950/22</em>

<em>r² = 225</em>

<em>r = √225</em>

<em>r = √15²</em>

<em>r = 15 cm</em>

3 0
3 years ago
Wedding photographers must be skilled at producing photographs that capture the
vampirchik [111]

Answer

True, BUT they can be trained.

Explanation:

4 0
3 years ago
I still need help, thank you! Will give Brainliest ​
wlad13 [49]

Answer: 1 what do u think to help u

6 0
3 years ago
What does a linear bus topology used to connect all network devices?
pentagon [3]
<span>A linear bus topology uses a common background cable to connect all network devices.</span>
5 0
3 years ago
Other questions:
  • What is &lt;html&gt;
    9·2 answers
  • A complete traversal of an n node binary tree is a(n)____ "operation if visiting a node is O(1)for the iterative implementation
    5·1 answer
  • A drowsy cat spots a flowerpot that sails first up and then down past an open window. The pot is in view for a total of 0.50 sec
    13·1 answer
  • From the standpoint of the governing bodies of .com, why is it important that owners of individual domains maintain authoritativ
    5·1 answer
  • What occurs when you call a method on a reference variable that contains null? Select one: a. An ArrayIndexOutOfBoundsException
    11·1 answer
  • Siapa mahapatih brainly.com atau yg disebut Brainly Amerika Serikat ?​
    5·1 answer
  • In QBasic, create a number guessing challenge. Your program should generate a random number from 1-
    9·1 answer
  • Alfred works in the human resources department, and he uses a management information system to find applicants' résumés on the w
    7·1 answer
  • The sequence of Figures shows a pattern. if the pattern repeats, how many triangles will the figure 5 have ?​
    7·1 answer
  • I need some help-
    15·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!