Write a program to convert from section to fixed format.
Input (Section object) | Output (Fixed Format String)(New lines are shown as $ for display purpose) |
---|---|
sectionName = MPC |
MPC Srinivas K 3 $Raju 1 98 076.50M$Mohini 2 88 092.50F$Anand 3 75 098.50M$ |
sectionName = BiPC |
BiPC Rama S 2 $Mohit 1 67 096.50M$Kavya 2 88 092.50F$ |
sectionName = HEC |
HEC Lavanya M 1 $Ashok 1 89 076.50M$ |
class ConvertSectionToFixedFormat
{ public static void main(String s[])
{
Section section = new Section("MPC", "Srinivas K", 3);
section.addStudent(new Student("Raju", 1, 98, 76.5, 'M'));
section.addStudent(new Student("Mohini", 2, 88, 92.5, 'F'));
section.addStudent(new Student("Anand", 3, 75, 98.5, 'M'));
System.out.println("Fixed length format is : \n" + convertToFixedFormat(section));
}
public static String convertToFixedFormat(Section section) {
String result = null;
//Write code here to convert Section object to fixed format.
return result;
}
//If required write any other methods here
}
class Section {
String sectionName;
String classTeacher;
int numberOfStudents;
ArrayList<Student> students = new ArrayList<Student>();
public Section(String sectionName, String classTeacher, int numberOfStudents) {
this.sectionName = sectionName;
this.classTeacher = classTeacher;
this.numberOfStudents = numberOfStudents;
}
public void addStudent(Student student) {
students.add(student);
}
public String getAllStudentsInFixedFormat() {
String result = "";
for (int i = 0; i < students.size(); i++) {
result += students.get(i).fixedFormat() + "\n";
}
return result;
}
}
class Student {
String name;
int roll_number;
int marks;
double percentage;
char gender;
public Student(String name, int roll_number, int marks, double percentage, char gender) {
this.name = name;
this.roll_number = roll_number;
this.marks = marks;
this.percentage = percentage;
this.gender = gender;
}
public String fixedFormat() {
String result = "";
//Write code here to get the student details in fixed format
return result;
}
}
Topic: Collection Framework In Java