For this lab, you will be writing search algorithms. I have provided method signatures for you to follow. The code I am providing already compiles, so as you write more code you can continually test it as you go. Write all of the methods in order as they appear in the code. DO NOT move on to another method until the previous method is fully tested.
1. The CandyBar class defines an object of type CandyBar. This object has three pieces of information: a name, a weight and a wrapper color. Please DO NOT modify this code. Use this class as it is.
2. You will write your new code in Lab16.java. The main() method includes questions you must find the answers to using methods you will write.
3. Write the method definitions one by one for all four methods, testing after each one. DO NOT write a second method before the first one works perfectly. In doing this you must ask yourself the following questions for each:
a. What information does the method provide (input parameters)?
b. What information does the method return?
d. What should the method do?
Lab16.java -
import java.util.ArrayList;
/**
* This lab practices writing and using search algorithms.
*
* @author
* @version 2018
*/
public class Lab16
{
public static void main(String[] args)
{
ArrayList<CandyBar> bars = new
ArrayList<CandyBar>();
addCandyBarsToList(bars);
/**
* Use the methods you wrote below to answer all of
* the following questions.
*/
/**
* Part A:
*/
/**
* Is Twix in the list?
* If so, print all information about it.
* If not, print a message to the user.
*/
/**
* Is Mounds in the list?
* If so, print all information about it.
* If not, print a message to the user.
*/
/**
* Part B:
*/
/**
* Print all the names of candy bars with yellow wrappers.
*/
/**
* Part C:
*/
/**
* Count how many candy bars are 1.75 oz or more.
*/
/**
* Part D:
*/
/**
* Is there a candy bar that is 1.86 oz?
* If so, print the information.
* If not, print a message to the user.
* Write a binary search method to get the answer.
*/
}
/**
* This method searches a list to find a candy bar by name.
*
* @param list the list to search
* @param n a name to find
* @return the
index of the candy bar
*/
public static int findCandyBar(ArrayList<CandyBar> list,
String n)
{
return -1;
}
/**
* This method prints the names of the candy bars that have a
certain
* wrapper color.
*
* @param list the list to search
* @param c the color wrapper to
find
*/
public static void findByColor(ArrayList<CandyBar> list,
CandyBar.Color c)
{
}
/**
* This method counts the number of candy bars that weigh greater
than
* or equal to the weight input parameter.
*
* @param list the list to search
* @param w the weight to compare
to
* @return the
count of candy bars
*/
public static int countByWeight(ArrayList<CandyBar> list,
double weight)
{
return 0;
}
/**
* This method searches a list using binary search.
*
* @param list the list to search
* @param first the first index
* @param last the last index
* @param w the value to
search for
* @return the
index of the candy bar
*/
public static int binaryFind(ArrayList<CandyBar> list, int
first, int last, double w)
{
return -1;
}
/**
* This method adds candy bars to a list.
*
* @param list the list to add to
*/
public static void addCandyBarsToList(ArrayList<CandyBar>
list)
{
CandyBar kitkat = new CandyBar("KitKat", CandyBar.Color.RED,
1.5);
list.add(kitkat);
CandyBar grand = new CandyBar("One-hundred Grand",
CandyBar.Color.RED, 1.5);
list.add(grand);
CandyBar crunch = new CandyBar("Crunch", CandyBar.Color.BLUE,
1.55);
list.add(crunch);
CandyBar hershey = new CandyBar("Hershey", CandyBar.Color.BROWN,
1.55);
list.add(hershey);
CandyBar krackel = new CandyBar("Krackel", CandyBar.Color.RED,
1.55);
list.add(krackel);
CandyBar caramello = new CandyBar("Caramello",
CandyBar.Color.PURPLE, 1.6);
list.add(caramello);
CandyBar what = new CandyBar("Whatchamacallit",
CandyBar.Color.YELLOW, 1.6);
list.add(what);
CandyBar almond = new CandyBar("Almond Joy", CandyBar.Color.BLUE,
1.61);
list.add(almond);
CandyBar goodbar = new CandyBar("Mr. Goodbar",
CandyBar.Color.YELLOW, 1.75);
list.add(goodbar);
CandyBar twix = new CandyBar("Twix", CandyBar.Color.GOLD,
1.79);
list.add(twix);
CandyBar henry = new CandyBar("Oh Henry", CandyBar.Color.YELLOW,
1.8);
list.add(henry);
CandyBar milkyWay = new CandyBar("Milky Way", CandyBar.Color.GREEN,
1.84);
list.add(milkyWay);
CandyBar payDay = new CandyBar("PayDay", CandyBar.Color.ORANGE,
1.85);
list.add(payDay);
CandyBar snickers = new CandyBar("Snickers", CandyBar.Color.BLUE,
1.86);
list.add(snickers);
CandyBar butterfinger = new CandyBar("Butterfinger",
CandyBar.Color.YELLOW, 1.9);
list.add(butterfinger);
CandyBar musketeers = new CandyBar("Three Musketeers",
CandyBar.Color.SILVER, 1.92);
list.add(musketeers);
CandyBar reeses = new CandyBar("Reese's FastBreak",
CandyBar.Color.ORANGE, 2);
list.add(reeses);
CandyBar babyRuth = new CandyBar("Baby Ruth",
CandyBar.Color.SILVER, 2.1);
list.add(babyRuth);
}
}
CandyBar.java -
public class CandyBar
{
public enum Color { BLUE, BROWN, GOLD, GREEN, ORANGE, PURPLE, RED,
SILVER, WHITE, YELLOW }
// instance variables
private String name;
private double weight;
private Color wrapper;
/**
* Constructor for objects of class CandyBar
*/
public CandyBar(String n, Color c, double w)
{
this.name = n;
this.weight = w;
this.wrapper = c;
}
public String getName()
{
return this.name;
}
public double getWeight()
{
return this.weight;
}
public Color getWrapperColor()
{
return this.wrapper;
}
public void printInfo()
{
System.out.println(this.name);
System.out.printf("%.1f oz with a ", this.weight);
System.out.println(this.wrapper + " wrapper");
}
}
Hi,
Please find the java code below:
Please test the code.
////////////////////
package candybar;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
/**
* This lab practices writing and using search algorithms.
*
* @author
* @version 2018
*/
public class Lab16
{
public static void main(String[] args)
{
ArrayList<CandyBar> bars =
new ArrayList<CandyBar>();
addCandyBarsToList(bars);
/**
* Use the methods you wrote below
to answer all of
* the following questions.
*/
/**
* Part A:
*/
/**
* Is Twix in the list?
* If so, print all information
about it.
* If not, print a message to the
user.
*/
/**
* Is Mounds in the list?
* If so, print all information
about it.
* If not, print a message to the
user.
*/
/**
* Part B:
*/
/**
* Print all the names of candy bars
with yellow wrappers.
*/
/**
* Part C:
*/
/**
* Count how many candy bars are
1.75 oz or more.
*/
/**
* Part D:
*/
/**
* Is there a candy bar that is 1.86
oz?
* If so, print the
information.
* If not, print a message to the
user.
* Write a binary search method to
get the answer.
*/
}
/**
* This method searches a list to find a candy bar by
name.
*
* @param list the list to
search
* @param n a name
to find
*
@return
the index of the candy bar
*/
public static int
findCandyBar(ArrayList<CandyBar> list, String n)
{
for (int i=0; i<= list.size();
i++){
if
(list.get(i).getName().equals(n)){
return i;
}
}
return -1;
}
/**
* This method prints the names of the candy bars that
have a certain
* wrapper color.
*
* @param list the list to
search
* @param c the
color wrapper to find
*/
public static void
findByColor(ArrayList<CandyBar> list, CandyBar.Color c)
{
System.out.println("Candy bars that
have " + c + " color are:");
for (int i=0; i<= list.size();
i++){
if
(list.get(i).getWrapperColor().equals(c)){
System.out.println(list.get(i).getName());
}
}
}
/**
* This method counts the number of candy bars that
weigh greater than
* or equal to the weight input parameter.
*
* @param list the list to
search
* @param w the
weight to compare to
*
@return
the count of candy bars
*/
public static int
countByWeight(ArrayList<CandyBar> list, double weight)
{
int count=0;
for (int i=0; i<= list.size();
i++){
if
(list.get(i).getWeight() >= weight){
count++;
}
}
return count;
}
/**
* This method searches a list using binary
search.
*
* @param list the list to
search
* @param first the first index
* @param last the last
index
* @param
w the value to search for
*
@return
the index of the candy bar
*/
public static int binaryFind(ArrayList<CandyBar>
list, int first, int last, double w)
{
//Sort the list
Collections.sort(list, new
Comparator<CandyBar>() {
@Override
public int
compare(CandyBar o1, CandyBar o2) {
return
Double.valueOf(o1.getWeight()).compareTo(Double.valueOf(o2.getWeight()));
}
});
while(last >= first) {
int mid = (last
+ first)/2;
if(list.get(mid).getWeight() == w){
return mid;
}
else
if(list.get(mid).getWeight() < w) {
first = mid + 1;
}
else {
last = mid - 1;
}
}
return -1;
}
/**
* This method adds candy bars to a list.
*
* @param list the list to add
to
*/
public static void
addCandyBarsToList(ArrayList<CandyBar> list)
{
CandyBar kitkat = new
CandyBar("KitKat", CandyBar.Color.RED, 1.5);
list.add(kitkat);
CandyBar grand = new
CandyBar("One-hundred Grand", CandyBar.Color.RED, 1.5);
list.add(grand);
CandyBar crunch = new
CandyBar("Crunch", CandyBar.Color.BLUE, 1.55);
list.add(crunch);
CandyBar hershey = new
CandyBar("Hershey", CandyBar.Color.BROWN, 1.55);
list.add(hershey);
CandyBar krackel = new
CandyBar("Krackel", CandyBar.Color.RED, 1.55);
list.add(krackel);
CandyBar caramello = new
CandyBar("Caramello", CandyBar.Color.PURPLE, 1.6);
list.add(caramello);
CandyBar what = new
CandyBar("Whatchamacallit", CandyBar.Color.YELLOW, 1.6);
list.add(what);
CandyBar almond = new
CandyBar("Almond Joy", CandyBar.Color.BLUE, 1.61);
list.add(almond);
CandyBar goodbar = new
CandyBar("Mr. Goodbar", CandyBar.Color.YELLOW, 1.75);
list.add(goodbar);
CandyBar twix = new
CandyBar("Twix", CandyBar.Color.GOLD, 1.79);
list.add(twix);
CandyBar henry = new CandyBar("Oh
Henry", CandyBar.Color.YELLOW, 1.8);
list.add(henry);
CandyBar milkyWay = new
CandyBar("Milky Way", CandyBar.Color.GREEN, 1.84);
list.add(milkyWay);
CandyBar payDay = new
CandyBar("PayDay", CandyBar.Color.ORANGE, 1.85);
list.add(payDay);
CandyBar snickers = new
CandyBar("Snickers", CandyBar.Color.BLUE, 1.86);
list.add(snickers);
CandyBar butterfinger = new
CandyBar("Butterfinger", CandyBar.Color.YELLOW, 1.9);
list.add(butterfinger);
CandyBar musketeers = new
CandyBar("Three Musketeers", CandyBar.Color.SILVER, 1.92);
list.add(musketeers);
CandyBar reeses = new
CandyBar("Reese's FastBreak", CandyBar.Color.ORANGE, 2);
list.add(reeses);
CandyBar babyRuth = new
CandyBar("Baby Ruth", CandyBar.Color.SILVER, 2.1);
list.add(babyRuth);
}
}
////////////////////
Let me know if you need more help on this.
Hope this helps,
Thanks.
For this lab, you will be writing search algorithms. I have provided method signatures for you...
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...
JAVA Algorithms course need help completing this method using binary search thank you in advance. ------------------------------------------------------------------------------------------------- /** A class for executing binary searches in an array. */ public class BinarySearcher { /** Finds a value in a range of a sorted array, using the binary search algorithm. @param a the array in which to search @param low the low index of the range @param high the high index of the range @param value the value to find @return the index...
I have one method {search()} that search for a particular file in a directory that the filename start by "B" and it will return the files. I have 2 file that start by B [Bcc.txt, Ba.txt]. But in main method I want to count a word "light" how many times it appeares in the each files [Bcc.txt, Ba.txt]. However, I really don't know how to do it. I need help on that. public class CountWord { public static void main(String[]...
Complete the following Java class. In this class, inside the main() method, user first enters the name of the students in a while loop. Then, in an enhanced for loop, and by using the method getScores(), user enters the grades of each student. Finally, the list of the students and their final scores will be printed out using a for loop. Using the comments provided, complete the code in methods finalScore(), minimum(), and sum(). import java.util.Scanner; import java.util.ArrayList; public class...
LAB PROMPT: Lab 06 For this lab you are going to practice writing method signatures, implementing if statements, and improve your skills with for loops. The methods you will be writing have nothing to do with each other but allow you to use the skills you have gained so far to solve a series of small problems. Power For this method you will practice using methods from the Math class. The first step you must take is to write the...
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...
Please use Java programming: Modify both ArrayList and LinkedList classes and add the following method to both classes: public void reverseThisList(), This method will reverse the lists. When testing the method: print out the original list, call the new method, then print out the list again ------------------------------------------------------------------------- //ARRAY LIST class: public class ArrayList<E> implements List<E> { /** Array of elements in this List. */ private E[] data; /** Number of elements currently in this List. */ private int size; /**...
Lab Assignment : In this lab, you are going to implement QueueADT interface that defines a queue. Then you are going to use the queue to store animals. 1. Write a LinkedQueue class that implements the QueueADT.java interface using links. 2. Textbook implementation uses a count variable to keep track of the elements in the queue. Don't use variable count in your implementation (points will be deducted if you use instance variable count). You may use a local integer variable...
I need help writing my main method**** Computer Science 111 Introduction to Algorithms and Programming: Java Programming Project #4 – Classes and Objects (20 Points) You will create 3 new classes for this project, two will be chosen from the list below and one will be an entirely new class you invent.Here is the list: Shirt Shoe Wine Book Song Bicycle VideoGame Plant Car FootBall Boat Computer WebSite Movie Beer Pants TVShow MotorCycle Design First Create three (3) UML diagrams...
BELOW IS THE CODE I ALREADY HAVE
Linked List - Delete Modify Lab 5 and include the method to delete a node from the Linked List. In summary: 1) Add the method Delete 2) Method call to delete, with a number that IS in the list 3) Method call to delete, with a number that is NOT in the list - Be sure to include comments - Use meaningful identifier names (constants where appropriate) import java.io.*; 1/ Java program to...