Code:
6)
Output:
7)
Output:
Raw Code:
6)
public class Binary{
public static int binarySearch(char target,char[]
theValues,int firstIndex,int lastIndex){
if
(lastIndex>=firstIndex){
int mid =
firstIndex+(lastIndex-firstIndex)/2; // Finding mid value.
if
(theValues[mid] == target){ // Comparing array[mid] value and
target
return mid;
}
else
if(theValues[mid] > target){
lastIndex = mid -1; // Updating lastIndex
return
(binarySearch(target,theValues,firstIndex,lastIndex)); // Recursive
call.
}
else if
(theValues[mid] < target){
firstIndex = mid + 1; // Updating
firstIndex
return
(binarySearch(target,theValues,firstIndex,lastIndex)); // Recursive
call.
}
}
return -1;
}
public static void main(String[] args) {
// Declaring variables.
char theValues []=
{'a','b','c','d','e','f','g','h','i'};
int firstIndex = 0;
int lastIndex =
theValues.length;
char target = 'g'; // target
character.
int result =
binarySearch(target,theValues,firstIndex,lastIndex); // Calling
function.
if (result<0){
System.out.println("The target is not present in the values
array."); // If the target is not present in array.
}
else{
System.out.println("The target is present at index: " + result );
// If the target is present in array.
}
}
}
7)
// Importing required packages
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
public class TemparatureConverter extends JFrame implements
ActionListener{
JLabel label;
JButton button1,button2;
JTextField t1;
TemparatureConverter(){
this.setTitle("Temparature Converter"); // titile for frame
JPanel panel = new JPanel(); // Creating JPanel
panel.setLayout(null);
label = new JLabel(""); // Initially the JLable value in
empty.
label.setBounds(70,20,280,30);
t1 = new JTextField(); // Creating TextField
t1.setBounds(10,80,280,35); // Setting position and heigth and
width of a TextField.
button1 = new JButton("F to C"); // Setting button with Text "F to
C"
button1.setBounds(50,130,90,32);
button1.setBackground(Color.white); // Setting background color to
button1
button1.addActionListener(this); // Adding ActionListener to
button1
button2 = new JButton("C to F"); // Setting button with Text "C to
F"
button2.setBounds(170,130,90,32);
button2.setBackground(Color.white); // Setting background color to
button1
button2.addActionListener(this); // Adding ActionListener to
button1
// Adding all components to panel
panel.add(label);
panel.add(t1);
panel.add(button1);
panel.add(button2);
// Adding panel to frame.
add(panel);
setSize(300, 200); // Setting size of the frame.
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public double getDoubleFromTextField(JTextField tf) throws
Exception{
double value;
try{
value = Double.parseDouble(tf.getText()); // Converting String to
Double
}
catch (Exception e){
value = 0.0; // If an exception is raised.
}
return value; // Returning value.
}
public void actionPerformed(ActionEvent e){
try{
double value = getDoubleFromTextField(t1); // Calling
function.
if(e.getSource()==button1){
double c = (5f/9) * (value - 32); // converting f to c
label.setText(String.format("%.1f F = %.2f C",value,c)); // Setting
label text with the given format.
}
else if(e.getSource()==button2){
double f = value *(9f/5) + 32 ; // Converting c to f
label.setText(String.format("%.1f C = %.2f F",value,f)); // Setting
label text with the given format.
}
}
catch (Exception e1){
}
}
public static void main(String[] args) {
new TemparatureConverter();
}
}
if you have any doubts ask in comment section.
6 (10 points Remember the recursive Searching algorithm Binary Search. Write a recursive method to search...
5. (40 Points) Write a complete Java class named ArrayMethods that implements the methods listed below. All the methods accept the following parameters: Parameters: intar An array of int values. int firstIndex The index of the first element to include in the sum. int lastIndex The index of the last element to include in the sum. Please note that all methods must verify the validity of first Index and lastIndex. public static int sum(int[] arr, int first Index, int lastIndex)...
Write a Java application that implements the recursive Binary Search alogrithm below: Write a Java application that implements the recursive Binary Search alogrithm below:/** * Performs the standard binary search using two comparisons * per level. This is a driver that calls the recursive method * @return index where item is found or NOT FOUND if not found */public static <AnyType extends Comparable<? super AnyType>> int binarySearch(AnyType [] a, AnyType x) {return binarySearch(a, x, 0, a.length -1);}/** * Hidden recursive...
**JAVA** Write a binary search method for a String array that might contain nulls. Your method should not crash or throw a runtime exception. See the driver program for examples. The method header is: public static int binarySearchWithNulls(String[] words, String target) must be recursive.
The binary search algorithm from Chapter 9 is a very efficient algorithm for searching an ordered list. The algorithm (in pseudocode) is as follows: highIndex - the maximum index of the part of the list being searched lowIndex - the minimum index of the part of the list being searched target -- the item being searched for //look in the middle middleIndex = (highIndex + lowIndex) / 2 if the list element at the middleIndex is the target return the...
All those points need to be in the code Project 3 Iterative Linear Search, Recursive Binary Search, and Recursive Selection Sort Class River describes river's name and its length in miles. It provides accessor methods (getters) for both variables, toString() method that returns String representation of the river, and method isLong() that returns true if river is above 30 miles long and returns false otherwise. Class CTRivers describes collection of CT rivers. It has no data, and it provides the...
Write pseudocode for a recursive algorithm for inserting a key into a binary search tree. The following is the definition assumed. right? Write a) pto) You hadt written codes for for a algonithm for inserting a key into a binary seareh tree. The public class BST E extends comparable V prot fected bstNodeE protected class bstNode E V E key V value; ,V> right); bstNode E vright bstNode (E key ngvalue , bstNode«E·V> left . bstNode«E publle void insert (E...
Just Q3 and Q4 Q1] Write a C function to implement the binary search algorithm over an array of integer numbers and size n. The function should return the index of the search key if the search key exists and return - 1 if the search key doesn't exist. [10 Points] Q2] Write a C function to implement the selection sort algorithm, to sort an array of float values and size n. The function should sort the array in ascending...
in java Part III: Permutations (10 points) Write a recursive method public static ArrayList<int[] > permuteArray (int[] array) that returns an ArrayList of all permutations of the the parameter array. The ArrayList may contain the ar- rays in any order. Example: Suppose array = {4, 7, 1, 2}. Then the ArrayList would contain the following arrays, but in any order: 4 7 12 7 4 1 2 2 1 4 7 1 24 7 4 7 2 1 7 4...
Hi there, I am working on a binary search tree code in c++. The program must store and update students' academic records, each node includes the student name, credits attempted, credits earned and GPA. I have made some progress with the code and written most of the functions in the .cpp file (already did the .h file) but i am struggling with what to put in the main and how to put an update part in the insert function. I...
Preliminaries Download the template class and the driver file. Objective Learn how to traverse a binary search tree in order. Description For the template class BinarySearchTree, fill in the following methods: insert - Inserts values greater than the parent to the right, and values lesser than the parent to the left.parameters elem - The new element to be inserted into the tree. printInOrder - Prints the values stored in the tree in ascending order. Hint: Use a recursive method to...