简体   繁体   中英

The input from main class can't be accessed in the another class and would only display the initialized value

I need to access the inputs from the main class and let them be applied to the department class, however, it won't let me. Also, I am not allowed to use parameters. I need to follow this UML diagram. Please guide me on how can I fix this. Our topic is all about inheritance, static variables and method, method overriding and super keyword.

在此处输入图像描述

public class Department {
    private int groupScore;
    private double groupAverage;
    private static int overallScore;
    private static double overallAverage;
    
    public Department(){
        int a = 0; 
        int b = 0; 
        int c = 0; 
        int d = 0; 
        int e = 0;
        
        int gscore = groupScore;
        gscore = a + b + c + d + e;
        
        int oascore = overallScore;
        oascore = overallScore + groupScore;
        
        overallAverage = overallAverage/10.0;
        
        groupAverage = groupScore/5.0;
    }
    public double getGroupAverage() {
        return groupAverage;
    }
    public static void displayAverage() {
        double oave = overallAverage;
        System.out.println("Overall Average: " + oave);
    }
}

import java.util.Scanner;
public class Main {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        Department objdept = new Department();
        
        System.out.println("Enter 5 rates for Department A (1-5)");
        int a = sc.nextInt();
        int b = sc.nextInt();
        int c = sc.nextInt();
        int d = sc.nextInt();
        int e = sc.nextInt();
        System.out.println("Department 1 group average: " + objdept.getGroupAverage());
        objdept.getGroupAverage();
        objdept.displayAverage();
        System.out.println();
        System.out.println("Enter 5 rates for Department B (1-5)");
        int f = sc.nextInt();
        int g = sc.nextInt();
        int h = sc.nextInt();
        int i = sc.nextInt();
        int j = sc.nextInt();
        objdept.getGroupAverage();
        System.out.println("Department 2 group average: "+ objdept.getGroupAverage());
        System.out.println();
        Department.displayAverage();
    }
}

If you aren't allowed arguments in the constructor based on the diagram. You could add the scanner code into the Department constructor. Then in the main create two different department objects.

public Department(){
    Scanner sc = new Scanner(System.in);
    int a = sc.nextInt();
    int b = sc.nextInt();
    // c,d,e etc...
    groupScore = a + b + c + d + e;
    groupAverage = groupScore / 5.0;

    overallScore = overallScore + groupScore;

    if (overallAverage == 0.0) {
        overallAverage = groupAverage;
    } else {
        overallAverage = (groupAverage + overallAverage) / 2.0;
    }
}

public class Main {
    public static void main(String[] args) {
        System.out.println("Enter 5 rates for Department 1 (1-5)");
        Department department1 = new Department();
        System.out.println("Department 1 group average: " + department1.getGroupAverage());
        Department.displayAverage();
        System.out.println();

        System.out.println("Enter 5 rates for Department 2 (1-5)");
        Department department2 = new Department();
        System.out.println("Department 2 group average: " + department2.getGroupAverage());
        Department.displayAverage();
    }
}

If you are allowed arguments , You need to pass in your variables into your Department constructor while creating the object. First create a constructor that takes in 5 integer values

public Department(int a, int b, int c, int d, int e){
    groupScore = a + b + c + d + e;
    groupAverage = (a + b + c + d + e) / 5;
}

Second pass the values to the constructor in the main method. Like this

int a = sc.nextInt();
int b = sc.nextInt();
int c = sc.nextInt();
int d = sc.nextInt();
int e = sc.nextInt();
Department objdept = new Department(a,b,c,d,e);

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