Page 222 - computer science (868) class 11
P. 222

Program 4      Define a class student_marks as described below:
                               Data Members/Instance variables:
                               name, age, marks1, marks2 (marks in 2 subjects), max_marks, ave_marks

                               Member Methods
                               i.   To accept the details of a student
                               ii.  To compute the average and the maximum out of two marks
                               iii. To display the name, age and marks in two subjects, maximum and average marks
                               Write a main method to create an object of a class and call the above member methods.


                1       import java.util.*;
                2       class student_marks
                3       {

                4           String name;

                5           int age, marks1, marks2, max_marks;
                6           double avg_marks;
                7           void accept()

                8           {
                9               Scanner sc=new Scanner(System.in);

                10              System.out.println("Enter your name:");
                11              name=sc.nextLine();

                12              System.out.println("Enter marks in 2 subjects:");
                13              marks1=sc.nextInt();

                14              marks2=sc.nextInt();
                15              System.out.println("Enter your age:");

                16              age=sc.nextInt();
                17          }

                18          void compute()
                19          {

                20              max_marks=(marks1>marks2)?marks1:marks2;
                21              avg_marks=(marks1+marks2)/2.0;

                22          }
                23          void display()

                24          {
                25              System.out.println("Name of the student : "+name + " Age : "+age);

                26              System.out.println("Marks:"+marks1+","+marks2);
                27              System.out.println("Maximum Marks:"+max_marks);





                220220  Touchpad Computer Science-XI
   217   218   219   220   221   222   223   224   225   226   227