Privacy Policy Terms Of Use. Copyright © 2006-2010 Java Tutorials and Examples.
Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their respective owners.
- /*
- * Create a class called Student with number, name and marks of 3 subjects. Get number of students
- * to enter followed by details of each student and then display the student with highest percentage.
- */
- import java.io.*;
- class Student
- {
- int sno;
- String sname;
- int m[] = new int[3];
- int total;
- float percentage;
- void getdata()
- {
- DataInputStream in = new DataInputStream(System.in);
- try
- {
- System.out.print("Enter Student No. ");
- sno = Integer.parseInt(in.readLine());
- System.out.print("Enter Student Name. ");
- sname = in.readLine();
- System.out.print("Marks of Subject # 1: ");
- m[0] = Integer.parseInt(in.readLine());
- System.out.print("Marks of Subject # 2: ");
- m[1] = Integer.parseInt(in.readLine());
- System.out.print("Marks of Subject # 3: ");
- m[2] = Integer.parseInt(in.readLine());
- }
- catch(Exception e)
- {
- System.out.println("IO Exception " + e);
- System.exit(1);
- }
- }
- int getTotal()
- {
- return(m[0] + m[1] + m[2]);
- }
- float getPercentage()
- {
- return(getTotal()/3.0f);
- }
- void printStudent()
- {
- System.out.println("Student No. : "+ sno);
- System.out.println("Student Name. : "+ sname);
- System.out.println("Subject # 1 : "+ m[0]);
- System.out.println("Subject # 2 : "+ m[1]);
- System.out.println("Subject # 3 : "+ m[2]);
- }
- }
- class Students
- {
- public static void main(String args[])
- {
- int i, n = 0;
- DataInputStream in = new DataInputStream(System.in);
- try
- {
- System.out.print("How Many Students? ");
- n = Integer.parseInt(in.readLine());
- }
- catch(Exception e)
- {
- System.out.println("Exception " + e);
- System.exit(1);
- }
- Student s[] = new Student[n];
- for(i=0; i<n; i++)
- {
- s[i] = new Student();
- s[i].getdata();
- }
- float maxPercentage = s[0].getPercentage();
- int max = 0;
- for(i=1; i<n; i++)
- {
- if(maxPercentage < s[i].getPercentage())
- {
- maxPercentage = s[i].getPercentage();
- max = i;
- }
- }
- System.out.println("Student with highest Percentage");
- s[max].printStudent();
- System.out.println("Total Marks: " + s[max].getTotal());
- System.out.println("Percentage: " + s[max].getPercentage());
- }
- }

jaaavaaaaaaaaaaa
Post new comment