0

I know it may be weird question but I'm really stuck. I have simple program with two classes. I need pass array from class A to class B. I did it but I cannot test it because I have no idea how to run program. When I click on the run then only one class started. I wanted test whole program and cannot find anything how to do it. Is there any command or something which say run class A and then class B? Without it I cannot test class B because values from Array (class A) are not loaded :/ Hope you understand what I mean.

I'm using eclipse.

Thanks!

Class MarkCalculator

import java.util.Scanner;

public class MarkCalculator {

public static int[] exam_grade = new int[6];
public static int[] coursework_grade = new int[6];
public static int[] coursework_weight = new int[2];
public static int[] module_points = new int[6];
public static String module_grade, holder;
public static int counter1 = 0, counter2 = 0;

public static void main(String[] args) {

Scanner input = new Scanner (System.in);

for (int i=0; i<3; i++){

    System.out.printf(i+1+". Modelue"+" Enter grade of exam:");

    while (!input.hasNextInt() ){

        System.out.printf("Enter only numbers! Enter grade of your exam: ");
        input.next();
    }

    exam_grade[i]=input.nextInt();

    System.out.printf(i+1+". Modelue"+" Enter grade of coursework:");

    while (!input.hasNextInt()){

        System.out.printf("Enter only numbers! Enter grade of your coursework: ");
        input.next();
    }

    coursework_grade[i]=input.nextInt(); 
}


computeMark(coursework_grade, exam_grade, module_points);


// calculate module grade
for(int i = 0 ;i < 3; i++){

    if (module_points[i] < 35){
        System.out.println(i+1+".Module: Fail");
    }

    else if (module_points[i] >= 35 && module_points[i] <= 40){
        System.out.println(i+1+".Module: Pass by compensation");
        counter1++;
    }

    else {
        System.out.println(i+1+".Module: Pass");
        counter2++;
    }


}

holder = computeResult(module_points, counter1,counter2, module_grade);
System.out.println("Your stage result is: "+ holder);


input.close();
}



public static int[] computeMark (int coursework_grade[], int exam_grade[], int module_points[]){

coursework_weight[0]= 50;
coursework_weight[1]= 50;

for(int i=0;i<3;i++)
{

    if (coursework_grade[i] < 35 || exam_grade[i] < 35){

        module_points[i]=(coursework_grade[i]*coursework_weight[0] + (exam_grade[i]*(100-coursework_weight[1])))/100;

        if (module_points[i] > 35){
            module_points[i] = 35; } 

        else {
            module_points[i] = 0;
        }

    }

    else {
        module_points[i]=((coursework_grade[i]*coursework_weight[0] + (exam_grade[i]*(100-coursework_weight[1])))/100); }


}

return module_points;       
}

public static String computeResult (int module_points[], int counter1, int                 counter2,     String module_grade ){

int sum = 0;
double average = 0;

for (int i = 0; i < 3; i++){

    sum = sum + module_points[i];
    average = sum / 3;
}

for (int i = 0; i < 3; i++){


    if (counter2 == 3){
        module_grade = "Pass";
    }

    else if (average >= 40 && counter1 <= 2) {
        module_grade = "Pass by compensation";
    }

    else {
        module_grade = "Fail";
    }



}

return module_grade;






}



}

Class StudentChart

public class StudentChart {

public static void main(String[] args) {


    for (int i = 0; i < 3; i++){
        System.out.println(MarkCalculator.coursework_weight);
    }




}

}
6
  • 5
    Please post your code... Commented Nov 13, 2013 at 18:55
  • Have you created a main method to use as a driver to test both methods? Commented Nov 13, 2013 at 18:56
  • One of your classes in the project must have a main method, and then you click the little green run button (green circle with white 'play' arrow), or in the menu select Run->Run or Run->Debug. Commented Nov 13, 2013 at 18:56
  • This looks like a complete misunderstanding of how Java classes work. Post some code and we'll be able to help. Commented Nov 13, 2013 at 18:57
  • What's the purpose of the second class? What can't you do in the first class, that you would need a second class? Commented Nov 13, 2013 at 19:08

2 Answers 2

3

You only need one main method.

class A {
    String s;

    public A(String s){
        this.s = s;
    }
}

public class B {
    public static void main(String[] args){
        A a = new A("Hello");
        System.out.println(a.s + " world!");
    }
}

class B will be the application program, the one with the main method. It will get values from class A. class A does not need to run for class B app to work, even though it uses values from class A.

Sign up to request clarification or add additional context in comments.

Comments

1

You can have a method with a different name in another class, and call that method from your main method.

Do not call it public static void main though - that should only be used for standalone programs. If the method requires some other code to be run prior to it, it should not be the main method of a Java program.

1 Comment

@user2956231 great, you can mark this answer as accepted by clicking the check mark / tick mark next to it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.