Please add comments to this code import java.io.*; import java.util.*; import ja
ID: 640521 • Letter: P
Question
Please add comments to this code
import java.io.*;
import java.util.*;
import java.lang.String;
class Student implements Serializable
{
String fname,lname,userid;
double gpa;
@Override
public String toString() {
return new StringBuffer("")
.append(this.fname)
.append(" ")
.append(this.lname)
.append(" ")
.append(this.userid)
.append(" ")
.append(this.gpa).toString();
}
}
public class studentInfo{
private Scanner x; //Creates scanner
private Student stud[];
private void openFile(String filename){ //Sets scanner to file
try{
x= new Scanner(new File(filename));
stud = new Student[20];
}catch(Exception e){
System.out.println("File not found!");
}
}
public void readFile()
{
int i = 0;
try{
FileOutputStream out = new FileOutputStream("deans.txt");
ObjectOutputStream oout = new ObjectOutputStream(out);
PrintStream fileStream = new PrintStream(new FileOutputStream("deans.txt", true));
while(x.hasNext()) //Reads file until it reaches the end
{
stud[i] = new Student();
stud[i].fname = new String(x.next());
stud[i].lname = x.next();
stud[i].userid = x.next();
stud[i].gpa = x.nextDouble();
if(stud[i].gpa > 3.5)
{
fileStream.println(stud[i].toString());
System.out.printf("%s %s %s %s ", stud[i].fname,stud[i].lname,stud[i].userid,stud[i].gpa);
}
i++;
}
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
public void closeFile()
{
x.close();
}
public static void main(String[] args)
{
studentInfo Tests = new studentInfo();
Tests.openFile(args[0]);
Tests.readFile();
Tests.closeFile();
}
}
Explanation / Answer
import java.io.*;
import java.util.*;
import java.lang.String;
class Student implements Serializable
{
String fname,lname,userid;//you are declaring variables..
double gpa;
@Override//this annotation provides override functionality...
public String toString() {//in this method you are implementing the code to append values to string buffer..
return new StringBuffer("")
.append(this.fname)
.append(" ")
.append(this.lname)
.append(" ")
.append(this.userid)
.append(" ")
.append(this.gpa).toString();
}
}
public class studentInfo{//in this we are going to recieve student information from user cconsole..
private Scanner x; //Creates scanner
private Student stud[];
private void openFile(String filename){ //Sets scanner to file
try{
x= new Scanner(new File(filename));
stud = new Student[20];
}catch(Exception e){
System.out.println("File not found!");
}
}
public void readFile() //in this method we are opening the file to read the content of the file..
{
int i = 0;
try{
FileOutputStream out = new FileOutputStream("deans.txt");
ObjectOutputStream oout = new ObjectOutputStream(out);
PrintStream fileStream = new PrintStream(new FileOutputStream("deans.txt", true));
while(x.hasNext()) //Reads file until it reaches the end
{
stud[i] = new Student();
stud[i].fname = new String(x.next());
stud[i].lname = x.next();
stud[i].userid = x.next();
stud[i].gpa = x.nextDouble();
if(stud[i].gpa > 3.5)
{
fileStream.println(stud[i].toString());
System.out.printf("%s %s %s %s ", stud[i].fname,stud[i].lname,stud[i].userid,stud[i].gpa);
}
i++;
}
}
catch(Exception ex)
{//we are catching the exception to handke for it....
ex.printStackTrace();
}
}
public void closeFile()
{
x.close();
}
public static void main(String[] args)
{
studentInfo Tests = new studentInfo();
Tests.openFile(args[0]);
Tests.readFile();
Tests.closeFile();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.