简体   繁体   中英

How can I use the class I created in my main in java?

I'm working on Java code that takes an array of type Student and the number of students and it should return the average of students CGPA and I should use a class called Student which I have already done but I want to include it in my main.

Main code that calculates average of CGPA:

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;
   }
}

My Student class:

public class Student {

    private double grade;

    public Student() {}

    public Student(double grade) {
        this.grade = grade;
    }

    public double getGrade() {
        return grade;
    }

    public void setGrade(double grade) {
        this.grade = grade;
    }

    public String toString() {
        return "Student{" +
                "grade=" + grade +
                '}';
    }
}

I need to implement the class to work with the main but I need to keep them each in a different file.

Make sure they are in the same package, and then you can use the Student class directly in the Main class.

Instantiate from the Student class to use it in the Main class

package ThePackageYourClassesAreIn;
public class Test4{
   public static void main(String [] args){
      double grade = 100.0; // some grade
      Student student = new Student(grade);  // instantiate from Student 
      // Do stuff with student object
   }
   public static double averageCgpa(double [] input){
     // Do stuff
   }
}

If the Class files were from different packages, mention that package, too.

import OtherPackage.Student;

Here is how you can incorporate the Student class into your code. Just use a List of Student . Add a Student each time you input a grade and when you want to calculate the average just iterate the list of Students and call getGrade instead of using your double primitive types.

Test4.java

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Test4 {
   public static void main(String[] args) {
      try(Scanner adnan = new Scanner(System.in)) {

         System.out.print("Enter number of students: ");
         int length = adnan.nextInt();
         List<Student> input = new ArrayList<>(length);
         System.out.println("Enter Cgpa of students: ");

         for (int i = 0; i < length; i++) {
            System.out.print("Student " + (i + 1) + ": ");
            input.add(new Student(adnan.nextDouble()));
         }

         double averageCgpa = averageCgpa(input);
         System.out.println("Average of students Cgpa: " + averageCgpa);
      }
   }

   public static double averageCgpa(List<Student> input) {
      double total = 0f;
      for (Student s: input) {
         total += s.getGrade();
      }
      return total / input.size();
   }
}

Student.java

public class Student {
    private final double grade;
    public Student(double grade) {
       this.grade = grade;
    }
    public double getGrade() {
       return grade;
    }
    public String toString() {
       return "Student{" + "grade=" + grade + '}';
    }
}

This has no package statements as it is for demo purposes only. As long are both classes are in the same package (in this case the default package) then you won't need an import.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM