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
(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);
}
}
Get Answers For Free
Most questions answered within 1 hours.