What is the value held by the variable cmp

Assignment Help JAVA Programming
Reference no: EM132430772

Problem: The following code segment will be used for questions 6-10. The code in the class Book is complete, and ExamIITest contains only a partially completed main method (3 points each):

public class Book implements Comparable<Book> {

      String title;

      Map<Integer,String> pages;

      public Book(String title) {

       this.title = title;

       this.pages = new TreeMap<Integer,String>();

      }

      public String getTitle() {

       return this.title;

      }

      public int getNumPages() {

       return this.pages.size();

      }

      public void addPage(int pNum, String page) {

       this.pages.put(pNum,page);

      }

      public int compareTo(Book other) {

       int cmp = 0;

       if (this.getNumPages() < other.getNumPages()) {

         cmp = -1;

       }

       else if (this.getNumPages () > other.getNumPages()) {

         cmp = 1;

       }

       return cmp;

      }

}

public class ExamIITest {

      public static void main(String[] args) {

            Book myFoo1 = new Book("Harry Potter");

            Book myFoo2 = new Book("Lord of the Rings");

            Book myFoo3 = new Book("1984");

            myFoo1.addPage(1,"Hello");       myFoo1.addPage(2,"World");

            myFoo1.addPage(3,"Goodbye");    myFoo2.addPage(1,"Frodo");

            myFoo2.addPage(1,"Baggins");    myFoo3.addPage(1,"Big");

            myFoo3.addPage(2,"Brother");

            int cmp = myFoo1.compareTo(myFoo2);

            System.out.println(cmp);                             (1)

                             PriorityQueue<Book> q = new PriorityQueue<Book>();

            q.add(myFoo1);

            q.add(myFoo2);

            q.add(myFoo3);

            Book b = q.remove();

            System.out.println(b.getTitle());                             (2)

      }

}

1. In the code above, the method getTitle() is an example of (Mark ALL that apply):

a. An accessor method

b. A mutator method

c. A static method

d. A constructor method

e. None of the above

2. In the code above, what is the value held by the variable cmp when the statement marked with line (1) in the main method is reached?

a. 1

b. 0

c. -1

d. You cannot tell from the code provided

3. In the code above, what is the state of the variable named pages contained by the object myFoo1 hold when the statement marked with line (1) is reached?

4. In the code above, what is the state of the variable named pages contained by the object myFoo2 hold when the statement marked with line (1) is reached?

5. In the code above, what will be printed by the line of code when the statement marked with line (2) is reached?

Reference no: EM132430772

Questions Cloud

What shortcomings do see in the bureaucracy : What shortcomings do you see in the bureaucracy?How would you resolve the shortcomings mentioned?How did you come in contact with the bureaucracy?
How much would be inventory turnover : If Sales is $1,000,000, cost of merchandise sold is $750,000, and average inventory is $220,000, how much would be inventory turnover
Cause study - project cost accounting and finance course : A project of cause study (cost estimation) under Project Cost Accounting and Finance Course - Investigate what went right and what went wrong in each parts
For school and workplace-appropriate writing tools : Microsoft Word has several built-in tools to help writers eliminate errors. For this discussion, perform a search for similar tools that are on web and could
What is the value held by the variable cmp : In the code above, what is the value held by the variable cmp when the statement marked with line (1) in the main method is reached?
How will the nursing workforce shortage affect profession : Discuss how issues currently affecting the nursing workforce will affect you as an individual. How will the nursing workforce shortage affect.
What are differences between cohabiting and solo parents : Based on your use of the Urban Institute's Map, name one demographic change in your community or closest city that will be important in the future.
What is helicopter pylori and how does it develop : What is Helicopter pylori and how does it develop? Laxative abuse occurs most often during which age group and how can nurses educate patients regarding this.
Program will print only numbers between 0 and 25 : How to use a java program that ask the user to enter an integer number, then the program will print only numbers between 0 and 25.

Reviews

Write a Review

JAVA Programming Questions & Answers

  Recursive factorial program

Write a class Array that encapsulates an array and provides bounds-checked access. Create a recursive factorial program that prompts the user for an integer N and writes out a series of equations representing the calculation of N!.

  Hunt the wumpus game

Reprot on Hunt the Wumpus Game has Source Code listing, screen captures and UML design here and also, may include Javadoc source here.

  Create a gui interface

Create GUI Interface in java programing with these function: Sort by last name and print all employees info, Sort by job title and print all employees info, Sort by weekly salary and print all employees info, search by job title and print that emp..

  Plot pois on a graph

Write a JAVA program that would get the locations of all the POIs from the file and plot them on a map.

  Write a university grading system in java

University grading system maintains number of tables to store, retrieve and manipulate student marks. Write a JAVA program that would simulate a number of cars.

  Wolves and sheep: design a game

This project is designed a game in java. you choose whether you'd like to write a wolf or a sheep agent. Then, you are assigned to either a "sheep" or a "wolf" team.

  Build a graphical user interface for displaying the image

Build a graphical user interface for displaying the image groups (= cluster) in JMJRST. Design and implement using a Swing interface.

  Determine the day of the week for new year''s day

This assignment contains a java project. Project evaluates the day of the week for New Year's Day.

  Write a java windowed application

Write a Java windowed application to do online quiz on general knowledge and the application also displays the quiz result.

  Input pairs of natural numbers

Java program to input pairs of natural numbers.

  Create classes implement java interface

Interface that contains a generic type. Create two classes that implement this interface.

  Java class, array, link list , generic class

These 14 questions covers java class, Array, link list , generic class.

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd