Home > Enterprise >  create a data type in java
create a data type in java

Time:09-27

I am working on a java code that calculates the average of cgpa of N of students in an array and it is working fine but I only need to create a data type for it called studentsAverage for example.

import java.util.*;
public class Test4{
   public static void main(String [] args){
      Scanner adnan = new Scanner(System.in);
      System.out.println("Enter number of students : ");
      int length = adnan.nextInt();
      double [] input = new double[length];
      System.out.println("Enter Cgpa of students : ");
      for( int i = 0; i < length; i  ){
         input[i] = adnan.nextDouble();
      }
      double averageCgpa = averageCgpa(input);
      System.out.println("Average of students Cgpa : "   averageCgpa);
      adnan.close();
   }
   public static double averageCgpa(double [] input){
      double sum = 0f;
      for ( double number : input){
         sum = sum   number;
      }
      return sum / input.length;
   }
}

Any help would be really appreciated.

CodePudding user response:

in java you must use classes to define your own data type; note that in java we have eight primitive data type : boolean int short long float double char byte. for each of them have a corresponding class.

CodePudding user response:

If you want to get the average of the numbers, you can write:

double average = Arrays.stream(input).average().getAsDouble();

But if you want to write such a class, you can write:

public static class StudentsAverage {

        public static double average(double ...numbers)
        {
            double sum = 0;
            for (double number : numbers)
            {
                sum  = number;
            }

            return sum / numbers.length;
        }

    }

two tips:

1- When we use the static keyword for a method, we can use that method by writing ClassName.MethodName and we no longer need to create an object.

2- When we use ... in our method parameters, we say that the client of this method can pass any size of arguments. Even the client of method can pass an array as an argument. We can use this structure up to once in each method, and this parameter must be the last parameter.

CodePudding user response:

i prepared for you what you want and work for you but you must deep into oop for learning java and before everything you must deal with it.

package com.test;



import java.util.Scanner;



public class Main {



public static void main(String[] args) {

    System.out.println("Enetr number of students : ");

    Scanner adnan = new Scanner(System.in);

    int length = adnan.nextInt();

    Student student = new Student(length, adnan);



    double averageCgpa = Student.averageCgpa(student.getInput());

    System.out.println("Average of students cgpa :  "   averageCgpa);

    adnan.close();

}

}

class Student {
int numberOfStudents;

double[] input = new double[numberOfStudents];



public Student(int numberOfStudents, Scanner scanner) {

    this.numberOfStudents = numberOfStudents;

    enterStudent(scanner);



}



private void enterStudent(Scanner scanner) {

    System.out.println("Enter cgpa of students : ");

    for (int i = 0; i < numberOfStudents; i  ) {

        input[i] = scanner.nextDouble();

    }

}



public int getNumberOfStudents() {

    return numberOfStudents;

}



public void setNumberOfStudents(int numberOfStudents) {

    this.numberOfStudents = numberOfStudents;

}



public static double averageCgpa(double[] input) {

    double sum = 0f;

    for (double number : input) {

        sum = sum   number;

    }

    return sum / input.length;

}



public double[] getInput() {

    return input;

}



public void setInput(double[] input) {

    this.input = input;

}

}
  • Related