Menu
Question Index
...


Write a program to convert from fixed format to section object.
Assume that first 15 characters is section, next 15 characters is Class teacher and the last 2 characters represent number of students. So the Section record is of length 15 + 15 + 2 = 32 characters and this will be the first line. The remaining lines/records are for students and in the student record, name is in first 15 characters, Roll_number in next 2 characters, Marks in next 3 characters, Percentage in next 6 characters and Gender is in the last character. The student record length will be 15 + 2 + 3 + 6 + 1 = 27. Spaces are used as fillers. So trim before using the data.





Input(String) (New lines are shown as $ for display purpose) Output(Section Object)
MPC            Srinivas K     3 $Raju           1 98 076.50M$Mohini         2 88 092.50F$Anand          3 75 098.50M Section name : MPC
Class teacher : Srinivas K
Number of students : 3
1st Student : Raju : 1 : 98 : 76.5 : M
2nd Student : Mohini : 2 : 88 : 92.5 : F
3rd Student : Anand : 3 : 75 : 98.5 : M
BiPC           Rama S         2 $Mohit          1 67 096.50M$Kavya          2 88 092.50F Section name : BiPC
Class teacher : 'Rama S'
Number of students : 2
1st Student : Mohit : 1 : 67 : 96.5 : M
2nd Student : Kavya : 2 : 88 : 92.5 : F
HEC            Lavanya M      1 $Ashok          1 89 076.50MSection name : HEC
Class teacher : 'Lavanya M'
Number of students : 1
1st Student : Ashok : 1 : 89 : 76.5 : M

class ConvertFixedFormatToSection

{    public static void main(String s[])
    {
        String input = "MPC            Srinivas K     3 \nRaju           1 98 076.50M\nMohini         2 88 092.50F\nAnand          3 75 098.50M";
        Section section = convertToSectionObject(input);
        System.out.println("Section name : " + section.sectionName);
        System.out.println("Class teacher : " + section.classTeacher);
        System.out.println("Number of students : " + section.numberOfStudents);
        System.out.println(section.getStudents());

    }


public static Section convertToSectionObject(String str) {
Section result = new Section();
//Write code here to convert fixed format to section object.
return result;
}
}
class Section {

String sectionName;
String classTeacher;
int numberOfStudents;
ArrayList<Student> students = new ArrayList<Student>();

public void addStudent(Student student) {
students.add(student);
}

public String getStudents() {
String result = "";
for (int i = 0; i < students.size(); i++) {
result += students.get(i) + "\n";
}
return result;
}
}

class Student {

String name;
int roll_number;
int marks;
double percentage;
char gender;

public String toString() {
return "Name : " + name + " Roll_number : " + roll_number + " Marks : " + marks + " Percentage : " + percentage + " Gender : " + gender;
}
}

Doubts

Problems

Topic: Collection Framework In Java

Read this topic
Take test on this topic

0
Wrong
Score more than 2 points

© meritcampus 2019

All Rights Reserved.

Open In App