Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

Java array assignment

  • 22-04-2016 06: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,919 ✭✭✭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,919 ✭✭✭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,919 ✭✭✭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