Java Data Structures and Big-Oh Performance Estimation Create a class for testin
ID: 3662266 • Letter: J
Question
Java Data Structures and Big-Oh Performance Estimation
Create a class for testing data structures. This class should be able to read and write text files. It should be able to store this in index integer fields.
This class needs 5 index integer fields (first name, last name, year, gpa, major). Use String, int, and double types.
Write a method that will create an array of N random instances of this class where N is an integer parameter to this method. Use this for gpa random between 2.0 and 4.0.
Provide a toString method that will format an instance of this class nicely.
Provide appropriate constructors.
Write methods that will write and read text files of this class:
o to make this easy, assume that the first line of the file is the number of elements in the file
o make the file one line per entry
o use spaces as field delimiters in the files
o use the toString method of the class to write to the file
text files to be read can look like this:
Bobby Ong 1 3.9 ComputerScience
Martha Jones 2 3.5 English
Jane Sully 3 3.2 Nursing
Thomas Berkley 4 3.6 Business
Michael Smith 5 3.0 ComputerScience
Provide a good GUI interface to control and test this class:
o use buttons to control the functions available
o use JFileChoose for selecting input and output files
o use a JTextArea and JScrollPane to create a scrolling text area for output
o the window should resize nicely - use BorderLayout with the text area in the center
Explanation / Answer
import java.io.*;
import java.util.*;
class performance{
String first_name;
String last_name;
int year;
double gpa;
String major;
public performance(String f,String l,int y,double cg,String m){
first_name = f;
last_name = l;
year = y;
gpa = cg;
major = m;
}
public String toString(){
return "first name : "+first_name+" last name : "+last_name+" year : "+year+" gpa : "+gpa+" major : "+major+" ";
}
}
class main{
public static void random_ins(int n){
performance[] per = new performance[n];
Scanner sc = new Scanner(System.in);
for (int i = 0; i < n; i++){
System.out.print("Enter first name : ");
String first_name = sc.next();
System.out.print("Enter last name : ");
String last_name = sc.next();
System.out.print("Enter year of entrance : ");
int year = sc.nextInt();
System.out.print("Enter your CGPA : ");
double gpa = sc.nextDouble();
System.out.print("Enter your major : ");
String major = sc.next();
performance p = new performance(first_name,last_name,year,gpa,major);
per[i] = p;
}
}
public static void main(String[] args){
performance[] per = new performance[100];
int size = 0;
BufferedReader br = null;
try{
String line;
br = new BufferedReader(new FileReader("input.txt"));
while ((line = br.readLine()) != null){
String[] temp = line.split(" ");
performance p = new performance(temp[0],temp[1],2015,Double.parseDouble(temp[3]),temp[4]);
per[size] = p;
size += 1;
}
}
catch (IOException e) {
e.printStackTrace();
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.