class Widget implements Comparable
{
// variables for name and price
private String name;
private double price;
/**
* default constructor
*/
public Widget()
{
name = "";
price = 0.0;
}
/**
* @param n
* @param p
* to initialiaze the values for name and prices of the
widget
*/
public Widget(String n, double p)
{
name = n;
price = p;
}
/**
* @return name of the widget
*/
public String getName()
{
return name;
}
/**
* @param sets name of the widget
*/
public void setName(String n)
{
name = n;
}
/**
* @return price of the widget
*/
public double getPrice()
{
return price;
}
/**
* @param sets the price of the widget
*/
public void setPrice(double p)
{
price = p;
}
/* (non-Javadoc)
* @see java.lang.Object#toString()
* returns name and price of the widget
*/
public String toString()
{
return "Name: " + name + ", Price: $" + String.format("%.2f", price);
}
// this method comparing the widgets based on the
name
// so when we put these in collections sorting will be
based on name of the widget
public int compareTo(Object obj)
{
Widget other = (Widget) obj;
if (this.name.compareTo(other.name) < 0)
return -1;
else if (this.name.compareTo(other.name) > 0)
return 1;
else
return 0;
}
// checking the equlaity of widgets based on the
name
public boolean equals(Object obj)
{
Widget other = (Widget) obj;
return this.compareTo(other) == 0;
}
// this is finding the widget in the given array using
the binary search
public static boolean getWidgetMatch(Widget widget,
Widget[] widArr)
{
int first = 0;
int last = widArr.length -
1;
// loop untill first reaches
last
while (first <= last)
{
// findin the
middle position
int middle =
(first + last) / 2;
// if it is
in the middle return true
if
(widArr[middle].equals(widget))
return true;
// if is less
than middle than search only left side of the array
if
(widArr[middle].compareTo(widget) < 0)
first = middle + 1;
// search right
side of the array
else
last = middle - 1;
}
return false;
}
}//end of class
public class WidgetTest
{
public static void main(String[] args)
{
//creating the widget class
array of 5
Widget[] widArr = new
Widget[5];
//creating 5 objects and putting
them in the array
widArr[0] = new Widget("Koti",
253);
widArr[1] = new Widget("Smith", 158);
widArr[2] = new Widget("Warden", 389);
widArr[3] = new Widget("Perl", 219);
widArr[4] = new
Widget("Uttam",454);
//sorting the array of obejcts
for (int i = 0; i < widArr.length - 1; i++) {
int minPos =
i;
//finding the
min element from the array
for (int j = i +
1; j < widArr.length; j++) {
if (widArr[j].compareTo(widArr[minPos]) < 0)
minPos = j;
}
//swapping
with the starting elements
if (minPos != i)
{
Widget temp = widArr[i];
widArr[i] = widArr[minPos];
widArr[minPos] = temp;
}
}
//printing array after
sorting
System.out.println("Array of
widgets:");
for (int i = 0; i < widArr.length; i++)
System.out.println("\t" + widArr[i]);
System.out.println();
// finding the widget from the
array
Widget widget = new Widget("Uday",
159);
if (Widget.getWidgetMatch(widget, widArr))
System.out.println("Widget '" + widget.getName() + "' is found in the array of widgets.");
else
System.out.println("Widget '" + widget.getName() + "' is not found in the array of widgets.");
}
}
![//creating the widget class array of 5 173 174 175 176 Widget widArrnew Widget[5]; //creating 5 objects and putting them in t](http://img.homeworklib.com/questions/444a27c0-bb0d-11ea-82db-bd668540f77a.png?x-oss-process=image/resize,w_560)
1. Submit a Screenshot with a visible operating system data/time with the primary code in your...
Can someone help me with the main class of my code. Here is the assignment notes. Implement project assignment �1� at the end of chapter 18 on page 545 in the textbook. Use the definition shown below for the "jumpSearch" method of the SkipSearch class. Notice that the method is delcared static. Therefore, you do not need to create a new instance of the object before the method is called. Simply use "SkipSearch.jumpSearch(...)" with the appropriate 4 parameter values. When...
Use the definition shown below for the "jumpSearch" method of the SkipSearch class.
Notice that the method is delcared static. Therefore, you do not need to create a new
instance of the object before the method is called. Simply use "SkipSearch.jumpSearch(...)"
with the appropriate 4 parameter values.
When objects are compared, you should use the "compareTo" method as in the following example.
item.compareTo(array[k])
However, not every type of object implements the compareTo method of the Comparable interface.
This is resolved...
What to submit: your answers to exercises 1, 3, and 4 and separate the codes to each question. Using your solution to question 3 of Lab Practice 2, modify it to create a class that contains a static method; the method takes a string as a parameter and returns a boolean value indicating whether the parameter string has repeated characters in it or not. That is, return true if there is at least one character which appears more than once...
Question 1 Consider the following code snippet: public class Box<E> { private E data; public Box() { . . . } public void insert(E value) { . . . } public E getData() { . . . } } What will result from executing the following code? Box<String> box = new Box<>(); . . . box.insert("blue Box"); String b = box.getData(); A. run-time error B. compiler warning C. no error D. compiler error Question 2 What is used as a...
Create a class named Module2. You should submit your source code file (Module2.java). The Module2 class should contain the following data fields and methods (note that all data and methods are for objects unless specified as being for the entire class) Data fields: A String object named firstName A String object named middleName A String object name lastName Methods: A Module2 constructor method that accepts no parameters and initializes the data fields from 1) to empty Strings (e.g., firstName =...
1. Create a class MLL, a singly linked, non-circular list where each node only has one link next and the list has a head and a tail link (think about implementation of node). The MLL class also implements the Iterable interface. The following should be implemented as well: 2. Add the methods to the MLL class: a. iterator() to implement the Iterable interface. This method returns an instance of MLLI initialized appropriately. b. addFirst( value) that adds a value to...
Step 1 Your text has an example of LinkedList class. Convert the class so that the Link objects have a single long instance variable -- call it "data" -- and the Link instance variable.. Step 2 Then add the following capabilities to the class. A constructor that takes an array of longs as an argument. The contents of the array are use to create Link objects that become objects in the LinkedList. A "search" method that takes a long argument...
//Generic interface that describes various searching and sorting //algorithms. Note that the type parameter is unbounded. However, //for these algorithms to work correctly, the data objects must //be compared using the method compareTo and equals. //In other words, the classes implementing the list objects //must implement the interface Comparable. The type parameter T //is unbounded because we would like to use these algorithms to //work on an array of objects as well as on objects of the classes //UnorderedArrayList and...
Using Merge Sort: (In Java) (Please screenshot or copy your output file in the answer) In this project, we combine the concepts of Recursion and Merge Sorting. Please note that the focus of this project is on Merging and don't forget the following constraint: Programming Steps: 1) Create a class called Art that implements Comparable interface. 2) Read part of the file and use Merge Sort to sort the array of Art and then write them to a file. 3)...
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...