0% found this document useful (0 votes)
10 views10 pages

OOP Assignment 2

Uploaded by

Dhanush Allenki
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views10 pages

OOP Assignment 2

Uploaded by

Dhanush Allenki
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

OOP ASSIGNMENT -2

Name: Dhanush Rollo:1602-23-737-140

//Program to creates a csv file from user input


MypackageCSV;
import
java.io.BufferedWriter;
import java.io.File;
import
java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.Scanner;

public class StudentCSV {


public static void main(String[] args)
{ Scanner sc = new
Scanner(System.in); File fl=new
File("student.csv");
if(args.length==0){
System.out.println("Provide the number of the students required in the
cmd
line");

}
else{
int n=Integer.parseInt(args[0]);
if(n<10){
System.out.println("Atleast 10 students are requried");
}
else{
try(BufferedWriter bw = new BufferedWriter(new FileWriter(fl))){ ;

bw.write("Roll
No.,Name,Subject1,Subject2,Subject3,Subject4,Subject5,Subject6,Subject7\n");

for (int i=1;i<=n;i++) {


System.out.println("Enter details for student " + i + ":");

System.out.print("Roll Number:
"); int rn=sc.nextInt();
sc.nextLine(); //"enter" gives an extra new line char so we
consume that by this statement

System.out.print("Name: ");
String name=sc.nextLine();

int[] marks=new
int[7]; int
totalMarks=0;
for (int j=0;j<7;j++) {
System.out.print("Marks for subject "+(j + 1)+":");
marks[j]=sc.nextInt();
}
sc.nextLine(); // same reason as before consume newline
bw.write(rn+","+name);
for (int x :
marks) {
bw.write("," +
x);
}
bw.newLine();
System.out.println("Student "+i+" data has been added to CSV
file.\n");
}

System.out.println("All student data has been written to "+fl);


} catch (IOException e) {
System.out.println("An error occurred while writing to the file: " +
e.getMessage());
} finally {
sc.close(
);

}
}
}

}
}

Output:reading the data for the file

The .csv file—


Creating a studentdetails class which prints the output
onto the console which reads from the .csv file
//this code uses the csv file to take input and converts it into obj of Student class
//the objof student class are stored in a list
package MyStudentDetails;
import
java.io.BufferedReader;
import
java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
//this code creates a student class with rollno and name and their avg in
7 sub package MyStudent;

class Student {
private int rn;
private String
name; private
int[] marks;

public Student(int rn, String name, int[]


marks) { this.rn=rn;
this.name=name;
this.marks=marks;
}
public void setRn(int rn)
{this.rn=rn;} public int getrn() {
return rn;
}
public void setName(String Name)
{this.name=name;} public String getName() {
return name;
}
public void setMarks(int[] marks)
{this.marks=marks;} public int[] getMarks() {
return marks;
}

public int
calAggre() { int
total=0;
for(int
mark:marks) {
total+=mark;
}
return total/marks.length;
}
public String toString(){//overridding to tostring
return "RollNumber:"+getrn()+"Name:"+getName()+"aggregate:"+calAggre()+"\
n";
}
}

public class StudentDetails {


public static void main(String[] args) throws IOException {//throws when exceptionis
encountered List<Student> stu=new ArrayList<>();
// Read the student details from student.csv
BufferedReader br=new BufferedReader(new
FileReader("student.csv") ); String line;
br.readLine();
while ((line=br.readLine()) !=
null) { String[]
data=line.split(",");
int rn=Integer.parseInt(data[0]);
String name=data[1];

int[] marks=new
int[7]; for (int i
=0;i<7;i++) {
marks[i]=Integer.parseInt(data[i + 2]);
}

stu.add(new Student(rn, name, marks));


}

// Displaying System.out.println("Roll No. |


Name | Aggregate"); for(Student student : stu)
{
System.out.println(student.toString());
}

br.close();
}
}
The reason why I chose List is because the insertion or
finding any detail , accessing the values of a list
collection is earlier and simple than linkedlist and
maps is mostly used for random access of details

Output of the given code is

You might also like