Jump to content
  • Advertisement
Sign in to follow this  
rubsnick

How exactly is Vector Being implemented in this java code?

This topic is 2668 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm studying Java (Gots a test) and the professor gave us an example of code Essentially it reads an input.txt that has a user ID, and 4 grades per user. The ID become the index for a vector and the grades are then saved in said vector. Not a biggie it's a simply data structure. Then the program outputs it into a record.txt in a proper format. Simple enough.... but what I don't understand is why he adds <StudentRecrods> after he calls a vector when in the java api it just has vector() or vector (i,l) or stuff like that. Nothing in the api leads me to believe this Vector<StudentRecords> is doable but the code does compile so yeah...

Heres the code the implementation that has me curious is the one in line 9. I have it bolded.

import java.util.* ;
import java.io.* ;

public class Project01
{
public static void main(String[] args)
{
// 1. Read student records from input file

Vector<StudentRecord> records = new Vector<StudentRecord>() ;


try
{
File inputFile = new File("input.txt") ;
Scanner s = new Scanner(inputFile) ;

while(s.hasNext())
{
String id = s.next() ;

StudentRecord sr = new StudentRecord(id) ;

for(int i=0 ; i<4 ; i++)
{
if(s.hasNextFloat())
{
float g = s.nextFloat() ;

sr.addGrade(g) ;
}
else
{
throw new IOException("missing grade") ;
}
}

records.add(sr) ;
}

s.close() ;
}
catch(FileNotFoundException e)
{
System.out.println("The file input.txt could not be found. The application will now exit.") ;
}
catch(Exception e)
{
e.printStackTrace() ;
}

// 2. Sort student records by student ID

Collections.sort(records) ;

// 3. Display each student record including the calculated GPA.

for(StudentRecord sr : records)
{
System.out.println(sr + " " + sr.calculateGPA()) ;
}

// 4. Write a report in a output file.

try
{
File outputFile = new File("report.txt") ;
FileWriter fw = new FileWriter(outputFile) ;

String nl = System.getProperty("line.separator") ;

for(StudentRecord sr : records)
{
fw.write(sr.getStudentID() + " " + sr.calculateGPA() + nl) ;
}

fw.close() ;
}
catch(Exception e)
{
e.printStackTrace() ;
}
}
}

class StudentRecord implements Comparable
{
private String studentID ;
private Vector<Float> grades ;

public StudentRecord(String id)
{
setStudentID(id) ;
grades = new Vector<Float>(4, 1) ;
}

public void setStudentID(String id)
{
// Input validate id parameter

studentID = id ;
}

public String getStudentID()
{
return studentID ;
}

public void addGrade(float grade) throws IllegalArgumentException
{
if(!(grade >= 0.0F && grade <= 100.0F))
{
throw new IllegalArgumentException("invalid grade!") ;
}

grades.add(grade) ;
}

public float getGrade(int index)
{
return grades.get(index) ;
}

public float calculateGPA()
{
float gpa = 0.0F ;

int size = grades.size() ;

for(int i=0 ; i<size; i++)
{
gpa += grades.get(i) ;
}

return gpa/((float) size) ;
}

public String toString()
{
return getStudentID() + " " +
grades.toString() ;
}

public int compareTo(Object o)
{
return getStudentID().compareTo(((StudentRecord)o).getStudentID()) ;
}
}

Share this post


Link to post
Share on other sites
Advertisement
It's a custom data type of the StudentRecords Class.... am I correct? [/quote]
No. It may be you're confusing a collection ( Vector ) with a coordinate or direction vector. Googling for "java vector" results in ~15 million hits, many of which provide an explanation for java.Util.Vector.

Share this post


Link to post
Share on other sites

It's a custom data type of the StudentRecords Class.... am I correct?

No. It may be you're confusing a collection ( Vector ) with a coordinate or direction vector. Googling for "java vector" results in ~15 million hits, many of which provide an explanation for java.Util.Vector.
[/quote]

Eh? I did the search and found the following link
http://www.easywayserver.com/blog/java-vector-example/
This link has a similarly implemented vector at line 7

Vector<String> vc=new Vector<String>();


Now I see the one my professor has and I realized he's using the Class name (StudentRecords) where the sting is on this one. Essentially using a User Defined Data Type (Class) according to this link at least
http://msdn.microsoft.com/en-us/library/ms173104%28v=vs.80%29.aspx

Please Correct me if I"m wrong.... but I have little to no idea what the difference between a colection (vector) is with a coordinate or direction vector (Unless we are talking about graphics)

Share this post


Link to post
Share on other sites
You're not far off :)

The syntax being used is todo with Generics (Type Safety).

Vector, in this case is simply a list of objects. (Like an array, linked list, etc ...).


Since this is sort of homework, I'll leave you with this further reading.

If that doesn't make sense, try making the following changes and look at the errors/warnings generated by the compiler.



// Change this line:
Vector<StudentRecord> records = new Vector<StudentRecord>();


// To this:
Vector records = new Vector();

Share this post


Link to post
Share on other sites
Vectors are the thread safe version of ArrayList. As mentioned, the <> notation is the use of generics. Generics all you to ensure that your code is type-safe. By creating a Vector<StudentRecord> you can check at compile time that every item added to the vector is of the type StudentRecord, rather than runtime. In general, this makes your code less error prone.

Share this post


Link to post
Share on other sites

You're not far off :)

The syntax being used is todo with Generics (Type Safety).

Vector, in this case is simply a list of objects. (Like an array, linked list, etc ...).


Since this is sort of homework, I'll leave you with this further reading.

If that doesn't make sense, try making the following changes and look at the errors/warnings generated by the compiler.



// Change this line:
Vector<StudentRecord> records = new Vector<StudentRecord>();


// To this:
Vector records = new Vector();




Yeah I already changed that that's what led me to my previous conclusion. It's essentially telling me incompatible types. These Generics are essentially forcing compatibility? converting them? So I could use this to make a INT number into a string? Kind of sort of?

For example I want a function to store strings from INT's. I'd just do something like storage<string>= read<string>; even if read was int read? something like that?

Share this post


Link to post
Share on other sites

Vectors are the thread safe version of ArrayList. As mentioned, the <> notation is the use of generics. Generics all you to ensure that your code is type-safe. By creating a Vector<StudentRecord> you can check at compile time that every item added to the vector is of the type StudentRecord, rather than runtime. In general, this makes your code less error prone.


Haha alright thanks then.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!