Question

JAVA Write a program that has two functions in which the user chooses which one to...

JAVA

Write a program that has two functions in which the user chooses which one to perform;

1. reads in a CSV file of integers into an array and insert it into a binary search tree class

2. deserializes an object and inserts it into a binary search tree class

the tree class must be in a separate class from the CSV file read and deserialize object load

Homework Answers

Answer #1

(a)

import java.util.*;

class BinarySort{

public static void main(String args[]){

Scanner in = new Scanner(System.in);

int num[]={1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

int first= 0;

int last= num.length-1;

int mid = (first+last) / 2;

System.out.println("Enter the number to

be searched");

int n = in.nextlnt();

while(first<=last)

{

if (num [mid]> n)

{

last= mid-1;

}

else if(num[mid]== n){

System.out.println("Found at " + (mid +

1));

break;

}else {

first = mid +1;

}

mid = (first + last) / 2;

}

if(first> last}{

System.out.println("Not Found");

}

}

}

(b)

Deserialization of Object in Java

To deserialize the object, we are using ObjectInputStream class that will read the object from the specified file. See the below example.

import java.io.*;

class Studentinfo implements Serializable

{

String name;

int rid;

static String contact;

Studentinfo(String n, int r, String c)

{

this.name = n;

this.rid = r;

this.contact = c;

}

}

class Demo

{

public static void main(String[] args)

{

Studentinfo si=null ;

try

{

FileInputStream fis = new FileInputStream("/filepath/student.txt");

ObjectInputStream ois = new ObjectInputStream(fis);

si = (Studentinfo)ois.readObject();

}

catch (Exception e)

{

e.printStackTrace(); }

System.out.println(si.name);

System.out. println(si.rid);

System.out.println(si.contact);

}

}

Know the answer?
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
Similar Questions
Binary Search Tree Code in Java Write a program that prompts user to enter however many...
Binary Search Tree Code in Java Write a program that prompts user to enter however many numbers they want to add to the binary search tree. Then have the user input numbers until the tree contains that many elements. If the user enters a number that already exists in the tree, then a message should be displayed "Number is already in the tree". Once all elements are added to the tree print its contents in sorted order. Assume all user...
IN JAVA PLEASE write code to MERGESORT an array of user inputted OBJECTS you have two...
IN JAVA PLEASE write code to MERGESORT an array of user inputted OBJECTS you have two different classes in the program being made as objects in the main (Bunny class and Dog Class). ask the user "what kind of animal would you like to sort?" user chooses the type of animal (bunny or dog), then you ask the user how big would you like the array of animals to be (maximum array size = 20)? and then you ask what...
JAVA CODE Write a program which has a 3D array and prints the integers from the...
JAVA CODE Write a program which has a 3D array and prints the integers from the array. Then then convert the output into a file using filewriter
Write a Java program that prompts the user to input a word (String). The program must...
Write a Java program that prompts the user to input a word (String). The program must print the reversed word with all consecutive duplicate characters removed. The program must contain the following classes: - The StackX class (you can use the Java Stack class). - The Reverse class which must contain a private data field called “word” of type string, a constructor, and a void method called revNoDup(). The revNoDup() method must reverse the word and remove the consecutive duplicate...
A. Write a Java program that asks the user to enter “bus” or “subway” or “walk”....
A. Write a Java program that asks the user to enter “bus” or “subway” or “walk”. If the user enters “bus” display “$1.00”. If they enter “subway” display “$1.50 and if they enter “walk” display “Free”. B. Write a java program that creates the following two arrays: String[] candidates = {“S Jones”,”Justin Fairfax”,”Clark Duncan”}; int[] votes = {7345,4324,3211}; Write the code that will search the votes array for the candidate with the largest number of votes and prints the name...
Write Java program Lab51.java which takes in a string from the user, converts it to an...
Write Java program Lab51.java which takes in a string from the user, converts it to an array of characters (char[] word) and calls the method: public static int countVowels(char[]) which returns the number of vowels in word. (You have to write countVowels(char[]) ).
Lab 6    -   Program #2   -   Write one number to a text file. Use the write()...
Lab 6    -   Program #2   -   Write one number to a text file. Use the write() and read() functions with binary                                                        data, where the data is not char type.              (Typecasting is required) Fill in the blanks, then enter the code and run the program. Note:   The data is int type, so typecasting is            required in the write() and read() functions. #include <iostream> #include <fstream> using namespace std; int main() {    const int SIZE = 10;   ...
Write a program in Java where a user of this program will play a game in...
Write a program in Java where a user of this program will play a game in which he/she needs to guess a target number, which is a number that the program has randomly picked in the range that the user chooses. The program will repeatedly prompt for the guessed number and provide a clue whether the guessed number is bigger or smaller than the target number, until the guessed number equals the target number.
Write a Java program that asks the user to enter an array of integers in the...
Write a Java program that asks the user to enter an array of integers in the main method. The program should prompt the user for the number of elements in the array and then the elements of the array. The program should then call a method named isSorted that accepts an array of and returns true if the list is in sorted (increasing) order and false otherwise. For example, if arrays named arr1 and arr2 store [10, 20, 30, 41,...
IN JAVA 1. Write up a small program that accepts two integers from the user. Print...
IN JAVA 1. Write up a small program that accepts two integers from the user. Print which of the two values is bigger. If they are the same, print that they are the same. 2. Write a method that accepts three doubles. Calculate and return the average of the three passed double values. 3. Write up a method that accepts a score between 0-10. Print out a message according to the following table. You ay personally decide the boundaries. i.e.,...