Showing posts with label Java Program. Show all posts
Showing posts with label Java Program. Show all posts

Sunday, 13 September 2020

Java program to find the average of n numbers given as input in the command line argument.

 In Java programs, the simplest way to give input is to pass in the values along the command line while executing the program. This program finds the average of n numbers. The input given in the command line is assigned to the arg[ ] array as strings. To know the number of inputs, arg.length is used. Each element in the arg[ ] array is converted into an integer using the Integer.parseInt( ) function where Integer is a predefined wrapper class. If the input is a floating-point value then use Double.parseDouble() function to convert the command line string values to the corresponding floating-point values.

Note: The program uses the DecimalFormat class function format( ) to format the decimal output to two decimal places. This DecimalFormat class is defined in the java.text package. Hence the first line of the program imports this package.

Program

import java.text.*;
class Average
{
    public static void main(String arg[])
    {
        double sum=0,avg;
        int n=arg.length;
        DecimalFormat df = new DecimalFormat("#.##");
        for(int i=0;i<n;i++)
            sum=sum+Integer.parseInt(arg[i]);
        avg=sum/n;
        System.out.print("The average of the "+n+" numbers = ");
        System.out.println(df.format(avg));
    }
}


OUTPUT

>java Average 3 5 8 4 6 6 7 8

The average of the 8 numbers = 5.88        

Wednesday, 26 August 2020

Java program to process result

PROGRAM

import java.io.*;

class StudentExam
{
    String name;
    String regno;
    double CIA1, CIA2,assgn,quiz,ext_mark,int_mark;
    String result;

  StudentExam()
  {
       name="\0";
       regno="\0";
       CIA1=CIA2=assgn=quiz=ext_mark=int_mark=0;
       result="\0";
  }

  StudentExam(String n,String r,double c1,double c2,double a,double qz,double e)
  {
       name=n;
       regno=r;
       CIA1=c1;
       CIA2=c2;
       assgn=a;
       quiz=qz;
       ext_mark=e;
  }
   
  void getData() throws IOException
  {
      BufferedReader x=new BufferedReader(new InputStreamReader(System.in));
      System.out.println("Enter name, regno");
      name=x.readLine();
      regno=x.readLine();
      System.out.println("Enter CIA1(50), CIA2(50), Assig(15), quiz(15)");
      CIA1=Double.parseDouble(x.readLine());
      CIA2=Double.parseDouble(x.readLine());
      assgn=Double.parseDouble(x.readLine());
      quiz=Double.parseDouble(x.readLine());
      System.out.println("Enter Sem_mark(50)");
      ext_mark=Double.parseDouble(x.readLine());
  }

  void getResult()
  {
       double cia = (CIA1+CIA2) * 70 /100;
       int_mark = (cia + assgn+quiz)/2;
       if  (ext_mark >= 20)
       {
           if ((int_mark + ext_mark) >=40)
              result="PASS";
           else
              result = "FAIL";
       }
       else
           result="FAIL";
  } 
  
  void display()
  {
       System.out.println("Student details");
       System.out.println("--------------------");
       System.out.println("Name = "+name);
       System.out.println("RegNo = "+regno);
       System.out.println("CIA1 = "+CIA1 +", CIA2 = "+CIA2);
       System.out.println("Assignment = "+assgn+", Quiz = "+quiz);
       System.out.println("Internal Mark = "+int_mark);
       System.out.println("External Mark = "+ext_mark);
       System.out.println("Result = "+result);
  }
}

class StudentResult
{
   public static void main(String args[]) throws IOException
   {
       StudentExam s1=new StudentExam();
       s1.getData();
       s1.getResult();
       s1.display();
    }
}

OUTPUT
Enter name, regno
Safi
33
Enter CIA1(50), CIA2(50), Assig(15), quiz(15)
20
20
15
10
Enter Sem_mark(50)
25

Student details
------------------
Name = Safi
RegNo = 33
CIA1 = 20.0, CIA2 = 20.0
Assignment = 15.0, Quiz = 10.0
Internal Mark = 26.5
External Mark = 25.0
Result = PASS