Homework Help Question & Answers

public class Item implements Comparable { private String name; private double price; /** * Constructor for...

public class Item implements Comparable
{
private String name;
private double price;
/**
* Constructor for objects of class Item
* @param theName name of item
* @param thePrice price of item
*/
public Item(String theName, double thePrice)
{
name = theName;
price = thePrice;
}
  
/**
* gets the name
* @return name name of item
*/
public String getName()
{
return name;
}
  
/**
* gets price of item
* @return price price of item
*/
public double getPrice()
{
return price;
}
  
/**
* sets new name of item
* @param newName new name of item
*/
public void setName(String newName)
{
name = newName;
}
  
/**
* sets price of item
* @param newPrice new price of item
*/
public void setPrice(Double newPrice)
{
price = newPrice;
}
  
public int compareTo(Object otherObject)
{
Item other = (Item) otherObject;
if (this.getPrice() > other.getPrice())
{
return 1;
}
else if (this.getPrice() < other.getPrice())
{
return -1;
}
else if (this.getPrice() == other.getPrice())
{
return -1;
}
return 0;
  
}
}

In the compareTo method, what exactly does "this" refer to.

  

0 0
Add a comment
Answer #1

The code define a class named Item

Item has two class variables / attributes : a String name and a double price

In the method lines

  
public int compareTo(Object otherObject)
{
Item other = (Item) otherObject;
if (this.getPrice() > other.getPrice())

this refers to the object that will call this method. Let us make an object of class Item

Example

object1 = Item("pencil", 2) // this is a pencil of 2

Lets us make another object

object2 = Item("eraser", 5) // this is eraser of price 5

When we call object1.compareTo(object2) , this , will refer to object 1 having pencil and 2 .

In the following line :

if (this.getPrice() > other.getPrice())

this.getPrice() will yield 2. Similarly other will refer to object 2 and other.getPrice() will yield value of 5

Conversely if object2.compareTo(object1) was called, then this would have referred to object 2

Short Answer : "this" refers to the object that is calling the method. "other" is the object being passed.

Add a comment
Know the answer?
Add Answer to:
public class Item implements Comparable { private String name; private double price; /** * Constructor for...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coin

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
  • How to solve this problem? Consider the following class : public class Store Public final double...

    How to solve this problem? Consider the following class : public class Store Public final double SALES TAX_RATE = 0.063B private String name; public Store(String newName) setName ( newName); public String getName () { return name; public void setName (String newName) { name = newName; public String toString() return "name: “ + name; Write a class encapsulating a web store, which inherits from Store. A web store has the following additional attributes: an Internet Address and the programming language in...

  • import java.util.Scanner; public class StudentClient {       public static void main(String[] args)    {   ...

    import java.util.Scanner; public class StudentClient {       public static void main(String[] args)    {        Student s1 = new Student();         Student s2 = new Student("Smith", "123-45-6789", 3.2);         Student s3 = new Student("Jones", "987-65-4321", 3.7);         System.out.println("The name of student #1 is ");         System.out.println("The social security number of student #1 is " + s1.toString());         System.out.println("Student #2 is " + s2);         System.out.println("the name of student #3 is " + s3.getName());         System.out.println("The social security number...

  • In Java, Write a class encapsulating a restaurant,which inherits from Store. A restaurant has the following...

    In Java, Write a class encapsulating a restaurant,which inherits from Store. A restaurant has the following additional attributes: how many people are served every year and the average price per person. code the constructor, accessors, mutators, toString and equals method of the new subclass; also code a method returning the average taxes per year. You also need to include a client class to test your code for both the parent class and the subclass. Code for Store below(Super class aka...

  • Purchase Class Java help

    Using the PurchaseDemo program and output as your guide, write a program that uses the Purchase class to set the following prices, and buy the number of items asindicated. Calculate the subtotals and total bill called total. Using the writeOutput() method display the subtotals as they are generated as in the PurchaseDemoprogram. Then print the total bill at the endUse the readInput() method for the following input dataOranges: 10 for 2.99 buy 2 dozen orangesEggs: 12 for 1.69 buy 3...

  • This code is in java. Create a Java class that has the private double data of length, width, and price. Create the gets...

    This code is in java. Create a Java class that has the private double data of length, width, and price. Create the gets and sets methods for the variables. Create a No-Arg constructor and a constructor that accepts all three values. At the end of the class add a main method that accepts variables from the user as input to represent the length and width of a room in feet and the price of carpeting per square foot in dollars...

  • I need help for JAVA. Retail Item Class

    DESCRIPTION: Write a class named RetailItem that holds data about an item in a retail store. The class should have the following fields:• description. The description field references a String object that holds a brief description of the item.• unitsOnHand. The unitsOnHand field is an int variable that holds the number of units currently in inventory.• price. The price field is a double that holds the item’s retail price.Write a constructor that accepts arguments for each field, appropriate mutator methods...

  • import java.util.Scanner; public class Cat { private String name; private Breed breed; private double weight; public...

    import java.util.Scanner; public class Cat { private String name; private Breed breed; private double weight; public Cat(String name, Breed breed, double weigth) { this.name = name; this.breed = breed; this.weight = weight; } public Breed getBreed() { return breed; } public double getWeight() { return weight; } //other accessors and mutators ...... } public class Breed { private String name; private double averageWgt; public Breed(String name,double averageWgt) { this.name = name; this.averageWgt= averageWgt; } public double getWeight() { return averageWgt;...

  • public class Date { private int month; private int day; private int year; /** default constructor...

    public class Date { private int month; private int day; private int year; /** default constructor * sets month to 1, day to 1 and year to 2000 */ public Date( ) { setDate( 1, 1, 2000 ); } /** overloaded constructor * @param mm initial value for month * @param dd initial value for day * @param yyyy initial value for year * * passes parameters to setDate method */ public Date( int mm, int dd, int yyyy )...

  • (JAVA NetBeans) Write programs in java Example 9.13~9.15 //Book.Java public class Book { private String title; private...

    (JAVA NetBeans) Write programs in java Example 9.13~9.15 //Book.Java public class Book { private String title; private String author; private double price; /** * default constructor */ public Book() { title = ""; author = ""; price = 0.0; } /** * overloaded constructor * * @param newTitle the value to assign to title * @param newAuthor the value to assign to author * @param newPrice the value to assign to price */ public Book(String newTitle, String newAuthor, double newPrice)...

  • GIVEN CODES *****Boat.java***** import java.util.HashSet; import java.util.Set; public class Boat { private String name; //private instance...

    GIVEN CODES *****Boat.java***** import java.util.HashSet; import java.util.Set; public class Boat { private String name; //private instance variable name of type String private String boatClass; //private instance variable boatClass of type String private int regNum; //private instance variable regNum of type int private Set<String> crew = new HashSet<String>(); public void setName(String name) { this.name = name; } public void setBoastClass(String boatClass) { this.boatClass = boatClass; } public void setRegNum(int regNum) { this.regNum = regNum; } public String getName() { return name;...

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.