Question

The first part is to implement one of the questions from your examination. /* A Range...

The first part is to implement one of the questions from your examination.

/*

A Range objects represents an integer range, such as

1-10 or 50701-50799. The lower and upper bounds of

a Range are given at the time the object is created.

*/

public interface Range {

    public boolean contains( int value );

    // returns true if v is ≥ lower bound and ≤ upper bound,

    // and false otherwise

    public boolean overlaps( Range other );

    // returns true if the receiver contains at least

    // one value in common with other, and false otherwise

    public int size();

    // returns the number of integers in the range

    public boolean isEquals( Range );

    // Returns if the passed in Range is equal to the range in the

    //class

}

public class PriceRange implements Range {

               // your implementations go here

}

write the code in java please and thank you

When you create your classes(es) you should create then under source directories:

            src/main/java

            src/test/java

0 0
Add a comment Improve this question Transcribed image text
Answer #1

package HomeworkLib;

interface Range
{
// int lowerLimit ;
   //int upperLimit ;

   public boolean contains( int value );

public boolean overlaps( Range other );


public int size();

// returns the number of integers in the range

public boolean isEquals( Range other );

// Returns if the passed in Range is equal to the range in the

//class
  
}
  
public class PriceRange implements Range {
  
  
int lowerLimit=0;
int upperLimit=0;
  
  
public PriceRange(int lowerLimit,int upperLimit)
{
this.lowerLimit=lowerLimit;
this.upperLimit=upperLimit;
  
  
}
  
  
  
public boolean contains( int value )
{
  
if(value>=lowerLimit&&value<=upperLimit)
return true;
else
return false;
}
// checks overlapping
public boolean overlaps( Range other )
  
  
{
  

PriceRange others=(PriceRange)other;

if((lowerLimit<=others.lowerLimit && upperLimit>=others.lowerLimit)||(lowerLimit<=others.upperLimit && upperLimit>=others.upperLimit))
return true;
else
return false;
}
  
// returns the number of integers in the range
public int size()
{
  
return upperLimit-lowerLimit+1;
  
}
// Returns if the passed in Range is equal to the range in the

//class
public boolean isEquals( Range other)
{
  
PriceRange others=(PriceRange)other;
if(others.lowerLimit==lowerLimit && others.upperLimit-lowerLimit==upperLimit-lowerLimit)
return true;
else
return false;
}
  
  
// test main method;
  
public static void main(String args[])
{
  
  
PriceRange r1=new PriceRange(2,5);
  
System.out.println(r1.contains(3));
System.out.println(r1.contains(9));
  
PriceRange r2=new PriceRange(3,10);
System.out.println(r1.overlaps(r2));
  
PriceRange r3=new PriceRange(20,30);
System.out.println(r1.overlaps(r3));
  
System.out.println(r1.size());
System.out.println(r1.isEquals(r2));
  
  
}

}

Sample Run:

please upvote me...

for any query plz cooment

Add a comment
Know the answer?
Add Answer to:
The first part is to implement one of the questions from your examination. /* A Range...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

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
  • write the following code in java thank you and if you can, could you please write...

    write the following code in java thank you and if you can, could you please write the code in replit and sent me a link to it replit is basically like a online compiler and you can write java code on it thank you The first part is to implement one of the questions from your examination. /* A Range objects represents an integer range, such as 1-10 or 50701-50799. The lower and upper bounds of a Range are given...

  • Requirements:  Your Java class names must follow the names specified above. Note that they are...

    Requirements:  Your Java class names must follow the names specified above. Note that they are case sensitive. See our template below.  BankAccount: an abstract class.  SavingAccount: a concrete class that extends BankAccount. o The constructor takes client's firstname, lastname and social security number. o The constructor should generate a random 6-digit number as the user account number. o The initial balance is 0 and the annual interest rate is fixed at 1.0% (0.01).o The withdraw method signature...

  • In a file named LLBag.java, write a class called LLBag that implements the Bag interface using...

    In a file named LLBag.java, write a class called LLBag that implements the Bag interface using a linked list instead of an array. You may use a linked list with or without a dummy head node. Bag interface code: /* * Bag.java * * Computer Science 112, Boston University */ /* * An interface for a Bag ADT. */ public interface Bag { /*    * adds the specified item to the Bag. Returns true on success    * and...

  • Java: Create the skeleton. Create a new file called ‘BasicJava4.java’ Create a class in the file...

    Java: Create the skeleton. Create a new file called ‘BasicJava4.java’ Create a class in the file with the appropriate name. public class BasicJava4 { Add four methods to the class that return a default value. public static boolean isAlphabetic(char aChar) public static int round(double num) public static boolean useSameChars(String str1, String str2) public static int reverse(int num) Implement the methods. public static boolean isAlphabetic(char aChar): Returns true if the argument is an alphabetic character, return false otherwise. Do NOT use...

  • JAVA - Circular Doubly Linked List Does anybody could help me with this method below(previous)? public...

    JAVA - Circular Doubly Linked List Does anybody could help me with this method below(previous)? public E previous() { // Returns the previous Element return null; } Explanation: We have this class with these two implemented inferfaces: The interfaces are: package edu.ics211.h04; /** * Interface for a List211. * * @author Cam Moore * @param the generic type of the Lists. */ public interface IList211 { /** * Gets the item at the given index. * @param index the index....

  • In java, create class BNode(T), which implements the interface TreeNode(T) interface methods: void setRight(TreeNode(T) right) -...

    In java, create class BNode(T), which implements the interface TreeNode(T) interface methods: void setRight(TreeNode(T) right) - sets right side of node, where right is the node to the right of this node void setRight(TreeNode(T) left) - sets left side of node, where left is the node to the left of this node TreeNode getRight() - gets the right node TreeNode getLeft() - gets the left node T getData() - gets the data within the node THEN, create class BT(E), which...

  • Course.java import java.io.Serializable; /** * Represents a course that might be taken by a student. *...

    Course.java import java.io.Serializable; /** * Represents a course that might be taken by a student. * */ public class Course implements Serializable { private String prefix; private int number; private String title; private String grade; /** * Constructs the course with the specified information. * * @param prefix the prefix of the course designation * @param number the number of the course designation * @param title the title of the course * @param grade the grade received for the course...

  • Question from Object-Oriented Data Structures Using Java 4th Edition Chapter 5 Question 30 Add the following...

    Question from Object-Oriented Data Structures Using Java 4th Edition Chapter 5 Question 30 Add the following methods to the LinkedCollection class, and create a test driver for each to show that they work correctly. Code each of these methods by accessing the internal variables of the LinkedCollection, not by calling the previously defined methods of the class.String toString() creates and returns a string that correctly represents the current collection. 1. Such a method could prove useful for testing and debugging...

  • In java please: TotalCostForTicket interface Create interface that includes one variable taxRate which is .09. It...

    In java please: TotalCostForTicket interface Create interface that includes one variable taxRate which is .09. It also includes an abstract method calculateTotalPrice which has no parameters and returns a value of type double. public abstract class Ticket { //There is a public static instance variable of type double for the basePrice. public static double basePrice; // private int theaterNumber; private int seatNumber; private boolean isTicketSold; private boolean isTicketReserved; public Ticket(int theaterNumber, int seatNumber) { this.theaterNumber = theaterNumber; this.seatNumber = seatNumber;...

  • In java Build a QueueInt class for integers that is compatible with the driver code below. The QueueInt should operate in a FIFO (first in, first out) fashion and implement the variables and methods a...

    In java Build a QueueInt class for integers that is compatible with the driver code below. The QueueInt should operate in a FIFO (first in, first out) fashion and implement the variables and methods also listed below: Data Members: Declare and initialize, as needed, the data item(s) you will need to manage a queue of integers. You may only use arrays and primitives for your instance and/or static variables (I,e You can’t use Java defined Queue / Stack / List...

ADVERTISEMENT
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.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT