After compiling this program, I get this error: Exception in thread \"main\" jav
ID: 3567636 • Letter: A
Question
After compiling this program, I get this error: Exception in thread "main" java.lang.NoClassDefFoundError. Can someone help me with this, please. Thanks
public class Student
{
String lname;
String fname;
String id;
public Student(String ln,String fn, String idnum )
{
lname=ln;
fname=fn;
id=idnum;
}
//get methods to retrieve the values of instance variables
public String getLname()
{
return lname;
}
public String getFname()
{
return fname;
}
public String getId()
{
return id;
}
}
//import the required packages
//those are, io package to deal with files
//util package to deal with Scanner class
import java.util.*;
import java.io.*;
//driver class
public class QuickSortOnStudentData
{
//main function
public static void main(String args[])throws Exception
{
//declare the variables
String fname;
String lname;
String id;
String sid;
//declare an array of size 30 of Student type
Student sArray[]=new Student[30];
//by using scanner object input read the data from
//the file
Scanner input=new Scanner(new FileReader(new File("StudentData")));
//by using scanner object in read the data
//from the console
Scanner in=new Scanner(System.in);
//declare a variable count
int count=0;
//loop to read each individual data
//from the text file
while(input.hasNext())
{
lname=input.next();
fname=input.next();
id=input.next();
//create object ot Student class
Student s=new Student(lname, fname, id);
//add the object to the sArray
sArray[count]=s;
//increment the count
count++;
}
//close the file
input.close();
//print the data present in the sArray before sorting
System.out.println("The students details before sorting are: ");
for(int i=0;i<count;i++)
System.out.println(sArray[i].getLname()+" "+sArray[i].getFname()+" "+sArray[i].getId());
System.out.println();
System.out.println("==========================================================");
//print the data present in the sArray after calling the
//quickSort so that the sArray is sort by last name
System.out.println("Sort the array by last name using Quick sort: ");
//call the quickSort
quickSort(sArray , 0, count);
for(int i=0;i<count;i++)
{
System.out.println(sArray[i].getLname()+" "+sArray[i].getFname()+" "+sArray[i].getId());
}
System.out.println();
System.out.println("==========================================================");
//print the data present in the sArray after calling the
//quickSort1 so that the sArray is sort by id
System.out.println("Sort the array by id using Quick sort: ");
quickSort1(sArray, 0, count);
for(int i=0;i<count;i++)
{
System.out.println(sArray[i].getLname()+" "+sArray[i].getFname()+" "+sArray[i].getId());
}
System.out.println();
System.out.println("==========================================================");
//prompt the user to enter an id to search and
//print the details
System.out.println(" Enter the Id of the student to search: ");
sid=in.next();
//call the seachId function and store the return index value in index variable
int index=searchId(sArray, count, sid);
//display the student details whose index is found
System.out.println("The student details at ["+(index+1)+"] are: ");
System.out.println(sArray[index].getLname()+" "+sArray[index].getFname()+" "+sArray[index].getId());
}
//partition method to get the pivot value and sort the data
public static int partition(Student[] a, int beg, int end)
{
int p=beg; ;
Student pivot=a[beg];
//for loop to retrieve the pivot element
for(int j=beg+1;j<end;j++)
{
if(pivot.getLname().compareTo(a[j].getLname())>0)
{
a[p]=a[j];
a[j]=a[p+1];
a[p+1]=pivot;
p=p+1;
}
}
return p;
}
//quickSort function
public static void quickSort(Student a[], int beg, int end)
{
if(beg<end)
{
//retrieving the pivot element
int p=partition(a,beg,end);
//calling the self quickSort function
quickSort(a,beg,p-1);
quickSort(a,p+1,end);
}
}
//quick sort function to sort by id
public static void quickSort1(Student a[], int beg, int end)
{
if(beg<end)
{
//retrieving the pivot element
int p=partition1(a,beg,end);
//calling the self quickSort function
quickSort1(a,beg,p-1);
quickSort1(a,p+1,end);
}
}
//partition to sort the array by id
public static int partition1(Student[] a, int beg, int end)
{
int p=beg; ;
Student pivot=a[beg];
//for loop to retrieve the pivot element
for(int j=beg+1;j<end;j++)
{
if(pivot.getId().compareTo(a[j].getId())<0)
{
a[p]=a[j];
a[j]=a[p+1];
a[p+1]=pivot;
p=p+1;
}
}
return p;
}
//searchid method to search the respective id and
//return its index
public static int searchId(Student a[],int size, String id)
{
int ind=0;
for(int i=0;i<size;i++)
{
if(a[i].getId().equals(id))
{
ind=i;
}
}
return ind;
}
}
Explanation / Answer
import java.util.*;
import java.io.*;
class Student
{
String lname;
String fname;
String id;
Student(String ln,String fn, String idnum )
{
lname=ln;
fname=fn;
id=idnum;
}
//get methods to retrieve the values of instance variables
public String getLname()
{
return lname;
}
public String getFname()
{
return fname;
}
public String getId()
{
return id;
}
}
//import the required packages
//those are, io package to deal with files
//util package to deal with Scanner class
//driver class
public class QuickSortOnStudentData
{
//main function
public static void main(String args[])throws Exception
{
//declare the variables
String fname;
String lname;
String id;
String sid;
//declare an array of size 30 of Student type
Student sArray[]=new Student[30];
//by using scanner object input read the data from
//the file
Scanner input=new Scanner(new FileReader(new File("StudentData")));
//by using scanner object in read the data
//from the console
Scanner in=new Scanner(System.in);
//declare a variable count
int count=0;
//loop to read each individual data
//from the text file
while(input.hasNext())
{
lname=input.next();
fname=input.next();
id=input.next();
//create object ot Student class
Student s=new Student(lname, fname, id);
//add the object to the sArray
sArray[count]=s;
//increment the count
count++;
}
//close the file
input.close();
//print the data present in the sArray before sorting
System.out.println("The students details before sorting are: ");
for(int i=0;i<count;i++)
System.out.println(sArray[i].getLname()+" "+sArray[i].getFname()+" "+sArray[i].getId());
System.out.println();
System.out.println("==========================================================");
//print the data present in the sArray after calling the
//quickSort so that the sArray is sort by last name
System.out.println("Sort the array by last name using Quick sort: ");
//call the quickSort
quickSort(sArray , 0, count);
for(int i=0;i<count;i++)
{
System.out.println(sArray[i].getLname()+" "+sArray[i].getFname()+" "+sArray[i].getId());
}
System.out.println();
System.out.println("==========================================================");
//print the data present in the sArray after calling the
//quickSort1 so that the sArray is sort by id
System.out.println("Sort the array by id using Quick sort: ");
quickSort1(sArray, 0, count);
for(int i=0;i<count;i++)
{
System.out.println(sArray[i].getLname()+" "+sArray[i].getFname()+" "+sArray[i].getId());
}
System.out.println();
System.out.println("==========================================================");
//prompt the user to enter an id to search and
//print the details
System.out.println(" Enter the Id of the student to search: ");
sid=in.next();
//call the seachId function and store the return index value in index variable
int index=searchId(sArray, count, sid);
//display the student details whose index is found
System.out.println("The student details at ["+(index+1)+"] are: ");
System.out.println(sArray[index].getLname()+" "+sArray[index].getFname()+" "+sArray[index].getId());
}
//partition method to get the pivot value and sort the data
public static int partition(Student[] a, int beg, int end)
{
int p=beg; ;
Student pivot=a[beg];
//for loop to retrieve the pivot element
for(int j=beg+1;j<end;j++)
{
if(pivot.getLname().compareTo(a[j].getLname())>0)
{
a[p]=a[j];
a[j]=a[p+1];
a[p+1]=pivot;
p=p+1;
}
}
return p;
}
//quickSort function
public static void quickSort(Student a[], int beg, int end)
{
if(beg<end)
{
//retrieving the pivot element
int p=partition(a,beg,end);
//calling the self quickSort function
quickSort(a,beg,p-1);
quickSort(a,p+1,end);
}
}
//quick sort function to sort by id
public static void quickSort1(Student a[], int beg, int end)
{
if(beg<end)
{
//retrieving the pivot element
int p=partition1(a,beg,end);
//calling the self quickSort function
quickSort1(a,beg,p-1);
quickSort1(a,p+1,end);
}
}
//partition to sort the array by id
public static int partition1(Student[] a, int beg, int end)
{
int p=beg; ;
Student pivot=a[beg];
//for loop to retrieve the pivot element
for(int j=beg+1;j<end;j++)
{
if(pivot.getId().compareTo(a[j].getId())<0)
{
a[p]=a[j];
a[j]=a[p+1];
a[p+1]=pivot;
p=p+1;
}
}
return p;
}
//searchid method to search the respective id and
//return its index
public static int searchId(Student a[],int size, String id)
{
int ind=0;
for(int i=0;i<size;i++)
{
if(a[i].getId().equals(id))
{
ind=i;
}
}
return ind;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.