Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Java array assignment

  • 22-04-2016 5:03pm
    #1
    Closed Accounts Posts: 191 ✭✭


    Hi for my assignment I need to make a 10 value array to take input as marks. I must then call a method to calculate average and then call another method to give a point. The points work like this... If the average is between 90-100 you get 4 point if average is between 80-89 you get 3 pints etc... I'll post my code I think I almost have it complete can someone explain wjere I'm going wrong thanks


Comments

  • Closed Accounts Posts: 191 ✭✭chocolate boy123


    here is my code...

    import javax.swing.JOptionPane;

    public class practical19 {


    public static void main(String[] args) {
    // TODO Auto-generated method stub

    double array1;

    double array2;

    double resultsArray [] = new double [10];

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

    resultsArray = Double.parseDouble(JOptionPane.showInputDialog("Enter each grade"));
    }

    double method1 = averageGrade(array1);

    double method2 = qualityPoints (array2);

    String output = "Student score " + average;

    output += "Point is " + answer;

    JOptionPane.showMessageDialog(null, output, "Results",
    JOptionPane.INFORMATION_MESSAGE);


    }
    public static double averageGrade (double array1 []){
    double sum =0;

    double average = 0;

    for (int j=0; j <array1.length; j++){

    sum = sum + array1 [j];

    average = sum / array1.length;

    }
    return average;
    }


    public static double qualityPoints (double array2 []){

    double average;

    for (int k= 0; k < array2.length; k++){

    if (average <=100 && average > 90){

    double answer = 4;
    }
    else if (average <=89 && average >=80){

    double answer = 3;
    }
    else if (average <=79 && average >=70){

    double answer = 2;
    }
    else if (average <=69 && average >=60){

    double answer = 1;
    }
    else {
    double answer = 0;
    }
    }
    return answer;
    }
    }


  • Registered Users, Registered Users 2 Posts: 2,824 ✭✭✭mightyreds


    I won't be able to test the code till later what seems to be the problem with it


  • Registered Users, Registered Users 2 Posts: 2,824 ✭✭✭mightyreds


    well for starters you never initialize your arrays properly
    double[] array1 = new double[10];
    
    double[] array2 = new double[10]
    

    then I presume you left method1 and method2 in by mistake here
    double average = averageGrade(array1);
    
    double answer = qualityPoints (array2);
    
    String output = "Student score " + average;
    
    output += "Point is " + answer;
    

    you may look up scope for this method
    public static double qualityPoints (double array2 []){
    
    		double average = 0;
    		double answer = 0;
    
    		for (int k= 0; k < array2.length; k++){
    
    			if (average <=100 && average > 90){
    
    				answer = 4;
    			}
    			else if (average <=89 && average >=80){
    
    				answer = 3;
    			}
    			else if (average <=79 && average >=70){
    
    				answer = 2;
    			}
    			else if (average <=69 && average >=60){
    
    				answer = 1;
    			}
    			else { 
    				answer = 0;
    			}
    		}
    		return answer;
    	}
    }
    


  • Registered Users, Registered Users 2 Posts: 2,824 ✭✭✭mightyreds


    its still not working right but your most of the way there as far as i can see, I would look at your arrays, you create 2 and do nothing with them then call methods on them


  • Closed Accounts Posts: 191 ✭✭chocolate boy123


    Thank you for your input !


  • Advertisement
Advertisement