Need help with this Java. I need help with the "to do" sections. Theres two parts to this and I added the photos with the entire question
Lab14 Part 1:
1) change the XXX to a number in the list, and YYY to a
number
// not in the list
2.code a call to linearSearch with the item number
(XXX)
// that is in the list; store the return in the variable
result
3. change both XXX numbers to the number searched for
4. code a call to linearSearch with the
item number (yyy)
// that is not in the list; store the return in the variable
result
5. change both YYY to the number searched for
6. code to sort the array using the Collections class
7. change the AAA to a number in the list, and BBB to a
number
// not in the list (use two different numbers)
8. code a call to binarySearch with the item number
(AAA)
// that is in the list; store the return in the variable
result
0. change both AAA numbers to the number searched for
10. code a call to binarySearch with the item number
(BBB)
// that is not in the list; store the return in the variable
result
11. change both BBB numbers to the number searched for
All To Do sections of the given code are completed and indicated with their respective To do numbers as comments.
Sample output is added at the end.
Code:
ItemList.java:
import java.util.ArrayList;
import java.util.Collections;
public class ItemList {
public static void main(String[] args)
{
System.out.println("\n\tBegin Item List Demo\n");
System.out.println("Declare an ArrayList to hold Item Objects");
ArrayList<Item> list =new ArrayList<Item>();
try
{
System.out.println("\n Add several Items to the list");
list.add(new Item(123, "Statue"));
list.add(new Item(332, "Painting"));
list.add(new Item(241, "Figurine"));
list.add(new Item(126, "Chair"));
list.add(new Item(411, "Model"));
list.add(new Item(55, "Watch"));
System.out.println("\nDisplay original Items list:");
listItems(list);
int result = -1;
// 1
System.out.println("\nLinear Search list for items 332 and 421:");
//2
result=linearSearch(list,332);
if(result >= 0){
//3
System.out.printf("Item 332 found at pos %d\n",result);
}
else
System.out.printf("Item 332 not found in the list");
//4
result=linearSearch(list,421);
if(result >= 0){
//5
System.out.printf("Item 421 found at pos %d\n",result);
}
else
System.out.printf("Item 421 not found in the list");
System.out.println("\nSort list into Item number sequence");
//6
Collections.sort(list);
System.out.println("\nDisplay the Sorted Items list:");
listItems(list);
//7
System.out.println("Binary Search list for items 126 and 200:");
//8
result=binarySearch(list,126);
if(result >= 0){
//9
System.out.printf("Item 126 found at pos %d\n",result);
}
else {
System.out.printf("Item 126 not found in the list");
System.out.printf("Should be at pos %d\n", -result - 1);
}
//10
result=binarySearch(list,200);
if(result >= 0){
//11
System.out.printf("Item 200 found at pos %d\n",result);
}
else {
System.out.printf("Item 200 not found in the list\n");
System.out.printf("Should be at pos %d\n", -result - 1);
}
} catch (Exception e) {
System.out.println(e.getMessage());
}
System.out.println("\n\tEnd Item List Demo\n");
}
public static int linearSearch(ArrayList<Item> list, int number)
{
for(int i=0; i<list.size();i++)
if(list.get(i).getItemNo() == number)
return i;
return -1;
}
public static void listItems(ArrayList<Item> list)
{
for(Item item : list)
System.out.println(item.toString());
}
public static int binarySearch(ArrayList<Item> list, int key){
int low =0;
int high=list.size() - 1;
int pos = 0;
while(high >= low)
{
pos = (low + high) / 2;
if (key < list.get(pos).getItemNo())
high=pos-1;
else if (key == list.get(pos).getItemNo())
return pos;
else
low = pos + 1;
}
return -low -1;
}
}
Item.java:
public class Item implements Comparable<Item>{
private int itemNo;
private String itemDesc;
public Item()
{
itemNo = 0;
itemDesc = "";
}
public Item(int number, String desc) throws Exception
{
setItemNo(number);
setItemDesc(desc);
this.itemDesc = itemDesc;
}
public int getItemNo() {
return itemNo;
}
public void setItemNo(int number) throws Exception {
if (itemNo == 0) {
if (number > 0)
itemNo = number;
else
throw new Exception("Item number must be greater than (0)");
} else {
throw new Exception("An existing nymber cannot be changed!");
}
}
public String getItemDesc() {
return itemDesc;
}
public void setItemDesc(String desc) throws Exception {
if(desc.trim().length() > 0)
itemDesc=desc;
else
throw new Exception("Item description cannot be blank");
}
public boolean equals(Item other){
return (itemNo == other.itemNo);
}
@Override
public int compareTo(Item other) {
return itemNo - other.itemNo;
}
@Override
public String toString() {
return String.format("%4d %-25s",itemNo,itemDesc);
}
}
Output of ItemList.java:
Need help with this Java. I need help with the "to do" sections. Theres two parts...
I am currently using eclipse to write in java. A snapshot of the output would be greatly appreciated to verify that the program is indeed working. Thanks in advance for both your time and effort. Here is the previous exercise code: /////////////////////////////////////////////////////Main /******************************************* * Week 5 lab - exercise 1 and exercise 2: * * ArrayList class with search algorithms * ********************************************/ import java.util.*; /** * Class to test sequential search, sorted search, and binary search algorithms * implemented in...
Look for some finshing touches java help with this program. I just two more things added to this code. A loop at the end that will ask the user if they want to quit if they do want to quit the program stops if they don't it loads a new number sequence. import java.util.Random; import java.util.ArrayList; import java.util.Scanner; import java.util.Arrays; import java.util.List; import java.util.Collections; public class main { public static void main(String[] args) { List < Sequence > list =...
Can you help me rearrange my code by incorporating switch I'm not really sure how to do it and it makes the code look cleaner. Can you help me do it but still give the same output? Also kindly add an in-line comment on what changes and rearrangement you've done so I can understand what's going on. import java.util.ArrayList; import java.util.Scanner; public class Bank { /** * Add and read bank information to the user. * @param arg A string,...
using Data Structures using Java. Modify it to make the ADT resizable. For this ADT, the MAX_SIZE will not be final, but rather will be increased whenever an new item needs to be added and the array is full (ie. size==MAX_SIZE). Whenever the array needs to grow, add the lesser of: (i) half the current size, or; (ii) 50 additional. Have your ADT report its old and new maximum size whenever the resize operation is called by printing “ADT resized...
Need help with this binary tree program. I will give a thumbs up for anybody who helps. Source Code: import java.util.Random; import java.util.Scanner; import java.util.Arrays; import java.util.Collections; import java.util.ArrayList; public class Trees { public static int[] prepareData(int[] data){ /* Data is prepared by inserting random values between 1 and data.length. Data items may be assumed to be unique. Please refer to lab spec for the problem definiton. */ ArrayList list = new ArrayList(); for (int i=0; i< data.length; i++) {...
Need help with this Java project implementing an interpolation search, bolded the missing parts: /** A class that implements an interpolation search and a binary search of a sorted array of integers. @author Frank M. Carrano @author Timothy M. Henry @version 4.0 */ public class SearchComparer { private int[] a; private int interpolationSearchCounter; private int binarySearchCounter; private static final int CAPACITY = 50; public SearchComparer(int[] array, int n) {...
// I need help with the following questions. Please use java programming ECLIPSE language to solve the questions. YOU ONLY NEED TO DIRECTLY COPY IT IN YOUR ECLIPSE APPLICATION AND RUN IT. I NEED THOSE PART WHICH IS SAYS --> "TO BE COMPLETED" I NEED HELP WITH [GET*] AND [REPLACE ALL] AND [ADD INT DOUBLE] PLEASE. import java.util.ArrayList; public class CustomArrayList { //instance variables public int[] data; //data.length gives the capacity public int nItems; //nItems gives items currently in the...
n JAVA, students will create a linked list structure that will be used to support a role playing game. The linked list will represent the main character inventory. The setting is a main character archeologist that is traveling around the jungle in search of an ancient tomb. The user can add items in inventory by priority as they travel around (pickup, buy, find), drop items when their bag is full, and use items (eat, spend, use), view their inventory as...
Deck of Cards Program I need help printing a flush, which is showing the top 5 cards of the same suite. Below is the code I already have that answers other objectives, such as dealing the cards, and finding pairs. Towards the end I have attempted printing a flush, but I cannot figure it out. public class Shuffler { /** * The number of consecutive shuffle steps to be performed in each call * to each sorting...
I just need to add comment for the code blow. Pleas add comment for each method and class if comments left out. package exercise01; /*************************************************************************** * <p> This program demonstrates the technique of recursion and includes * recursive methods that are defined for a variety of mathematical * functions. * * <br>A recursive method is one that directly or indirectly calls itself * and must include: * <br>(1) end case: stopping condition * which terminates/ends recursion * <br>(2) reduction:...