Question

Question 6) A “Golomb ruler” is a collection of integers such that no two pairs of...

Question 6)

A “Golomb ruler” is a collection of integers such that no two pairs of values are the same distance apart. Eg: If the current ruler is {1, 2, 4}, then either 7 or 9 could be added to the ruler but not both.

(Since 7 − 2 = 5 and 9 − 4 = 5).
You might want to use Math.abs(n) to compute the absolute value of n and the sort(null) method on List to sort a list.

public class Ruler {
/**
* Creates a ruler containing no values
*/
public Ruler();
/**
* @return The values of in the ruler (in increasing order), comma separated.
* This method should not rearrange/reorder any internal data.
*/
public String toString();
/**
* @return true if x could be added to the ruler without breaking the invariant,
* false otherwise
*/
public boolean isLegal(int x);
/**
* Add x to the ruler if doing so wouldn’t break the invariant, do nothing otherwise.
*/
public void add(int x);
}

A) Which Javadoc tag should be used on add(int x) to describe the invariant?

B) Declare the member variables you think are necessary.

C) Implement toString() for this class.

D) Implement isLegal() for this class.

E) Implement add(int x) for this class.

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

Thanks for posting the question, we are glad to help you. Below is the Java class - Ruler.java where all the methods has been implemented as asked in the question.

The class also has the main() method which is used to run the program and its works as expected : )

Thats It !!! Refer the screenshot for the output the program generated when i ran the program : )

Here is the class -

--------------------------------------------------------------------------------------------------------------------------------

import java.util.ArrayList;

public class Ruler {

// B) Declare the member variables you think are necessary.

private ArrayList<Integer> scale;

private ArrayList<Integer> differences;

/**

* Creates a ruler containing no values

*/

public Ruler() {

scale = new ArrayList<Integer>();

differences = new ArrayList<Integer>();

}

/**

* @return The values of in the ruler (in increasing order), comma separated.

*

* This method should not rearrange/reorder any internal data.

*/

// C) Implement toString() for this class.

public String toString() {

String ruler = "{";

for (int item : scale) {

ruler += item + ", ";

}

ruler = ruler.substring(0, ruler.length() - 2) + "}";

return ruler;

}

/**

* @return true if x could be added to the ruler without breaking the invariant,

* false otherwise

*/

// D) Implement isLegal() for this class.

public boolean isLegal(int x) {

boolean isTheDifferenceExists = false;

for (int item : scale) {

int difference = Math.abs(item - x);

int index = differences.indexOf(difference);

if (index >= 0) {

isTheDifferenceExists = true;

break;

}

}

return !isTheDifferenceExists;

}

/**

* Add x to the ruler if doing so wouldn’t break the invariant, do nothing

* otherwise.

*/

// E) Implement add(int x) for this class.

public void add(int x) {

boolean isTheDifferenceExists = false;

for (int item : scale) {

int difference = Math.abs(item - x);

int index = differences.indexOf(difference);

if (index >= 0) {

isTheDifferenceExists = true;

break;

}

}

if (!isTheDifferenceExists) {

for (int item : scale) {

differences.add(Math.abs(item - x));

}

scale.add(x);

}

}

public static void main(String[] args) {

Ruler ruler = new Ruler();

ruler.add(1);

System.out.println(ruler);

ruler.add(2);

System.out.println(ruler);

ruler.add(4);

System.out.println(ruler);

ruler.add(5);

System.out.println(ruler);

ruler.add(9);

System.out.println(ruler);

}

}

--------------------------------------------------------------------------------------------------------------------------------

Image Screenshot

thank You !!!!

plz a thumbs up : )

Add a comment
Know the answer?
Add Answer to:
Question 6) A “Golomb ruler” is a collection of integers such that no two pairs of...
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
  • I need help with these Java programming assignements. public class Die { //here you declare your...

    I need help with these Java programming assignements. public class Die { //here you declare your attributes private int faceValue; //operations //constructor - public Die() { //body of constructor faceValue=(int)(Math.random()*6)+1;//instead of 1, use random approach to generate it } //roll operation public void roll() { faceValue=(int)(Math.random()*6)+1; }    //add a getter method public int getFaceValue() { return faceValue; }    //add a setter method public void setFaceValue(int value) { faceValue=value; } //add a toString() method public String toString() { String...

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

  • JAVA (implementing a collection class) write a completed program (must include MAIN) and straight to the output based on 4. Based on the implementation of ArrayIntlist or ArrayList, write a class Sor...

    JAVA (implementing a collection class) write a completed program (must include MAIN) and straight to the output based on 4. Based on the implementation of ArrayIntlist or ArrayList, write a class SortedIntList or SortedList that provides most of the same operations but maintains its elements in sorted order. When a new value is added to the sorted list rather than appending it to the end of the list, it is placed in the appropriate index to maintain sorted order of...

  • Doubly Linked List Java Help Details: First, read the DoublyLinkedList.java code and try to under...

    Doubly Linked List Java Help Details: First, read the DoublyLinkedList.java code and try to understand what each field stores and what each method is doing. Modify and complete the class as described below •The field size was defined in the class but was never maintained. Set the current default value and modify it whenever it is needed in the existing methods and other methods you implement as it is needed. It should always include the number of Nodes inside the...

  • Need Help finishing up MyCalendar Program: Here are the methods: Modifier and Type Method Desc...

    Need Help finishing up MyCalendar Program: Here are the methods: Modifier and Type Method Description boolean add​(Event evt) Add an event to the calendar Event get​(int i) Fetch the ith Event added to the calendar Event get​(java.lang.String name) Fetch the first Event in the calendar whose eventName is equal to the given name java.util.ArrayList<Event> list() The list of all Events in the order that they were inserted into the calendar int size() The number of events in the calendar java.util.ArrayList<Event>...

  • In a project named 'DogApplication', create a class called 'Dog' 1. declare two instance variables in...

    In a project named 'DogApplication', create a class called 'Dog' 1. declare two instance variables in 'Dog' : name (String type) age (int type) 2. declare the constructor for the 'Dog' class that takes two input parameters and uses these input parameters to initialize the instance variables 3. create another class called 'Driver' and inside the main method, declare two variables of type 'Dog' and call them 'myDog' and 'yourDog', then assign two variables to two instance of 'Dog' with...

  • Assignment: A set is a collection of items without repetitions. The goal of this assignment is...

    Assignment: A set is a collection of items without repetitions. The goal of this assignment is to implement a set as a data structure. Do the following. 1. (30 points) Starting with the template attached, implement a generic class Set(T) that maintains a set of items of generic type T using the class ArrayList(T) in the Java API. Your Set class must provide the following instance methods: add: that adds a new item. (Ignore if already in the set.) remove:...

  • Please explain clearly with steps. Its a very small code . thanks I would rate positively....

    Please explain clearly with steps. Its a very small code . thanks I would rate positively. Write a class pixel in java with following properties. A Pixel has x, y coordinates, which are both integer values. A Pixel also has the following behaviors: It can return the value of its x and y coordinates. Use the follwoing method signatures to implement these behaviors. public int getX( ) public int getY( ) It can change the value of its x and...

  • Need help completing my instance method for deleteFront() as specified below To the class IntegerLinkedList, add...

    Need help completing my instance method for deleteFront() as specified below To the class IntegerLinkedList, add an instance method deleteFront such that … Method deleteFront has no input. Method deleteFront returns … an empty Optional instance if the list is empty an Optional instance whose value is the integer that was deleted from the front of the list, otherwise Hints https://docs.oracle.com/javase/10/docs/api/java/util/Optional.html import java.util.Optional; public class IntegerLinkedList { private IntegerNode head ; private int numberOfItems ; public int getNumberOfItems() { return...

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