Tuesday, June 13, 2017

Java program to show the use of methods in order to implement modularity

This program is implemented and  by Manoj Gupta from NIT Uttrakhand

import java.util.Scanner;

public class Student {

        // lets have some non static data members
 int roll;
 String name;
 String email;
 int hindi;
 int english;
 int physics;
 int chemistry;
 int maths;

 // a non static method to take input for data members
 void input_details()
 {
  //scanner object is created
  Scanner sc=new Scanner(System.in);
  //data entry is being done
  System.out.println("\n Enter your name ");
  name=sc.next();
  System.out.println("\n Enter your roll number ");
  roll=sc.nextInt();
  System.out.println("\n Enter your email address ");
  email=sc.next();
  System.out.println("\n Enter your marks in hindi ");
  hindi=sc.nextInt();
  System.out.println("\n Enter your marks in english ");
  english=sc.nextInt();
  System.out.println("\n Enter your marks in physics ");
  physics=sc.nextInt();
  System.out.println("\n Enter your marks in chemistry ");
  chemistry=sc.nextInt();
  System.out.println("\n Enter your marks in maths ");
  maths=sc.nextInt();
  System.out.println("\nThank you for your details");
 }

 // a non static method to show the values of data members
void show_report_card() { //display of report card System.out.println("############################REPORT CARD#####################################"); System.out.println("Name :"+name); System.out.println("Roll :"+roll); System.out.println("Email :"+email); System.out.println("Marks in Hindi :"+hindi); System.out.println("Marks in English :"+english); System.out.println("Marks in Physics :"+physics); System.out.println("Marks in Chemistry :"+chemistry); System.out.println("Marks in Maths :"+maths); int avg; avg=(hindi+english+maths+chemistry+physics)/5; int total; total=hindi+english+maths+chemistry+physics; System.out.println("Total :"+total); System.out.println("Average :"+avg); if(avg<33) { System.out.println("Result :FAIL"); System.out.println("Remarks :Better luck next time"); } if(avg>=33&&avg<=48) { System.out.println("Result :PASS by III DIVISION"); System.out.println("Remarks :Can be improved "); } if(avg>48&&avg<=59) { System.out.println("Result :PASS by II DIVISION"); System.out.println("Remarks :Satisfactory"); } if(avg>60&&avg<=75) { System.out.println("Result :PASS by I DIVISION"); System.out.println("Remarks :VERY GOOD"); } if(avg>75) { System.out.println("Result :PASS by DISTINCTION"); System.out.println("Remarks :EXCELLENT"); } } public static void main(String[] args) {
  // object of class student is being created
                // name of object is 's'
  Student s=new Student();
                
                // invoke the methods
  s.input_details();
  s.show_report_card();

 }
 
} 

No comments:

Post a Comment