Question

All those points need to be in the code

Project 3 Iterative Linear Search, Recursive Binary Search, and Recursive Selection Sort Class River describes rivers name a

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

Working code implemented in Java and appropriate comments provided for better understanding.

Here I am attaching code for these files:

  • Driver.java
  • River.java
  • CTRivers.java

Driver.java:

import java.io.*;
import java.util.*;

/**
* Class Driver invokes the methods in class CTRivers.
*/
public class Driver
{
public static void main(String [] args) throws IOException
{
River[] riverList = new River[100];
String name;
int length;
int counter = 0;
Scanner fileScan = new Scanner (new File("input.txt"));
CTRivers obj = new CTRivers();
  
while(fileScan.hasNext())
{
name = fileScan.next();
length = fileScan.nextInt();
River oneRiver = new River(name, length);
riverList[counter] = oneRiver;
counter++;
}
  
// Method 1
System.out.println("List of Rivers:");
obj.printListRec(riverList, counter);
  
// Method 2
// successful
String river = "Connecticut";
System.out.println("\nSearching for: " + river);
System.out.println(obj.linearSearch(riverList, counter, river));
  
// unsuccessful
river = "Rhode Island";
System.out.println("\nSearching for: " + river);
System.out.println(obj.linearSearch(riverList, counter, river));
  
// Method 3
int min = 20;
int max = 50;
System.out.println("\nList of rivers between " + min + " and " + max + ":");
for(River r : obj.searchRange(riverList, counter, min, max))
{
System.out.println(r);
}
  
// Method 4
System.out.println("\nRivers sorted by name:");
obj.sortByNameRec(riverList, counter);
obj.printListRec(riverList, counter);
  
// Method 5
String riverName = "Pawcatuck";
String riverNameUnsuccessful = "Rhode Island River";
int first = 0;
int last = counter;
System.out.println("\nSearch for the river named " + riverName + ":");
System.out.print(obj.binarySearchRec(riverList, first, last, riverName));
  
System.out.println("\nSearch for the river named " + riverNameUnsuccessful + ":");
System.out.print(obj.binarySearchRec(riverList, first, last, riverNameUnsuccessful));
}
}

River.java:


/**
* Class River describes river's name and length as well as provides
* getter and setter methods.
*/
public class River
{
private String name;
private int length;
  
public River(String name, int length)
{
this.name = name;
this.length = length;
}
  
public String getName()
{
return name;
}
  
public int getLength()
{
return length;
}
  
public String toString()
{
return "Name: " + name + "\tLength: " + length;
}
}

CTRivers.java:

import java.io.*;
import java.util.*;
  
/**
* Class CTRivers describes a collection of CT rivers.
*/
public class CTRivers
{
// Prints all rivers recursively in the same order as they were in the list
// List may or may not be empty
void printListRec(River[] list, int n)
{
if (n > 0)
{
printListRec(list, n - 1);
System.out.println(list[n-1]);
}
}
  
// Returns index for the river object with given name. Returns -1 for unsuccessful search. List can
// be empy or not.
int linearSearch(River[] list, int n, String name)
{
int index = 0;
boolean found = false;
  
while (!found && index < n)
{
if(list[index].getName().equals(name))
found = true;
else
index++;
}
  
if (found)
return index;
else
return -1;
}
  
// Returns ArrayList of rivers with length between min and max inclusive. Returns an empty
// Arraylist<River> if no such river was found. List can be empy or not.
ArrayList <River> searchRange(River[] list, int n, int min, int max)
{
ArrayList<River> riverList = new ArrayList<River>();
if(n > 0)
{
for(int i = 0; i < n; i++)
{
if (list[i].getLength() >= min && list[i].getLength() <= max)
riverList.add(list[i]);
}
}
return riverList;
}
  
// Sorts list of rivers by selection sort recursively (Compare river by their names.) List can be empy or not.
public void sortByNameRec(River[] list, int n)
{
if(n > 1)
{
River temp;
for (int i = 0; i < n-1; i++)
{
if (list[i].getName().compareTo(list[i+1].getName()) > 0)
{
temp = list[i];
list[i] = list[i+1];
list[i+1] = temp;
}
}
sortByNameRec(list, n-1);
}
}

// PRECONDITION: Method assumes that input list is sorted by names. First and last are indices of the first
// and last character of the current substring. Returns index of River object with given name or returns -1
// if none of the rivers has that name. List can be empy or not.
int binarySearchRec(River[] list, int first, int last, String name)
{
int mid;
if(first > last)
return -1;
else
{
mid = (first+last)/2;
if(list[mid].getName().equals(name))
return mid;
else if(list[mid].getName().compareTo(name) > 0)
return binarySearchRec(list, first, mid-1, name); // name on left. call again and search left side.
else
return binarySearchRec(list, mid+1, last, name); // name on right. call again and search right side.
}
}
}

Sample Output Screenshots:

C:\Windows\System32\cmd.exe Microsoft Windows [Version 10.0.18363.1082] (c) 2019 Microsoft Corporation. All rights reserved.

Add a comment
Know the answer?
Add Answer to:
All those points need to be in the code Project 3 Iterative Linear Search, Recursive Binary...
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
  • Class River describes river’s name and its length in miles. It provides accessor methods (getters) for...

    Class River describes river’s name and its length in miles. It provides accessor methods (getters) for both variables and toString() method that returns String representation of the river. Class CTRivers describes collection of CT rivers. It has no data, and it provides the following service methods. None of the methods prints anything, except method printListRec, which prints all rivers. // Prints all rivers recursively. Print them is same order as they were in the list . List can be empy...

  • C++ Time the sequential search and the binary search methods several times each for randomly generated...

    C++ Time the sequential search and the binary search methods several times each for randomly generated values, then record the results in a table. Do not time individual searches, but groups of them. For example, time 100 searches together or 1,000 searches together. Compare the running times of these two search methods that are obtained during the experiment. Regarding the efficiency of both search methods, what conclusion can be reached from this experiment? Both the table and your conclusions should...

  • I need to create a code for this prompt: In this project we will build a...

    I need to create a code for this prompt: In this project we will build a generic UserInput class for getting keyboard input from the user. Implementation: The class UserInput is a 'Methods only' class, and all the methods should be declared static. Look at the TestScanner.java program at the bottom of this page that inputs a string, int and double. It shows you how to use Scanner class to get input from the keyboard. Write FOUR simple methods, one...

  • Please help. I need a very simple code. For a CS 1 class. Write a program...

    Please help. I need a very simple code. For a CS 1 class. Write a program in Java and run it in BlueJ according to the following specifications: The program reads a text file with student records (first name, last name and grade). Then it prompts the user to enter a command, executes the command and loops. The commands are the following: "printall" - prints all student records (first name, last name, grade). "firstname name" - prints all students with...

  • *** FOR A BEGINNER LEVEL JAVA CLASS, PLEASE KEEP CODE SIMPLE, AND WE USE BLUE J...

    *** FOR A BEGINNER LEVEL JAVA CLASS, PLEASE KEEP CODE SIMPLE, AND WE USE BLUE J IN CLASS (IF IT DOESNT MATTER PLEASE COMMENT TELLING WHICH PROGRAM YOU USED TO WRITE THE CODE, PREFERRED IS BLUE J!!)*** ArrayList of Objects and Input File Use BlueJ to write a program that reads a sequence of data for several car objects from an input file. It stores the data in an ArrayList<Car> list . Program should work for input file containing info...

  • *Java* Hi. I need some help with creating generic methods in Java. Write a program GenMethods...

    *Java* Hi. I need some help with creating generic methods in Java. Write a program GenMethods that has the following generic methods: (1) Write the following method that returns a new ArrayList. The new list contains the nonduplicate (i.e., distinct) elements from the original list. public static ArrayList removeDuplicates(ArrayList list) (2) Write the following method that shuffles an ArrayList. It should do this specifically by swapping two indexes determined by the use of the random class (use Random rand =...

  • In Java programming language Please write code for the 6 methods below: Assume that Student class...

    In Java programming language Please write code for the 6 methods below: Assume that Student class has name, age, gpa, and major, constructor to initialize all data, method toString() to return string reresentation of student objects, getter methods to get age, major, and gpa, setter method to set age to given input, and method isHonors that returns boolean value true for honors students and false otherwise. 1) Write a method that accepts an array of student objects, and n, the...

  • what is the solution for this Java problem? Generics Suppose you need to process the following...

    what is the solution for this Java problem? Generics Suppose you need to process the following information regarding Students and Courses: A Student has a name, age, ID, and courseList. The class should have a constructor with inputs for setting name, ID and age. The class should have setters and getters for attributes name, ID and age, and a method addCourse, removeCourse, printSchedule. courseList: use the generic class ArrayList<E> as the type of this attribute addCourse: this method takes as...

  • Use BlueJ to write a program that reads a sequence of data for several car objects...

    Use BlueJ to write a program that reads a sequence of data for several car objects from an input file. It stores the data in an ArrayList<Car> list . Program should work for input file containing info for any number of cars. (You should not assume that it will always be seven lines in the input file). Use notepad to create input file "inData.txt". File should be stored in the same folder where all files from BlueJ for this program...

  • C++ (Using Binary Search Trees) other methods will result in downvote Implement the binary search tree...

    C++ (Using Binary Search Trees) other methods will result in downvote Implement the binary search tree methods (bst.cpp) for the binary search tree provided in the header file. Test your implementation with the included test. bst.h bst_test.cpp Note: Your implementation must correspond to declarations in the header file, and pass the test. Do not modify these two. I will compile your code against these. If the compilation fails, you will get down vote. bst.h #ifndef BINARY_SEARCH_TREE_H #define BINARY_SEARCH_TREE_H #include <string>...

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