Jump to content

computing in other method, need help

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
3 replies to this topic

#1
deadaimrf

deadaimrf

    Newbie

  • Members
  • Pip
  • 2 posts
can anyone help me to compute the values in the last method in total average...
I'm solving this for 4 hrs and I still cant get it..

import javax.swing.JOptionPane;


public class finals
{


public static void main(String[] args)
{



double quiz[] = new double[2];
double cs[] = new double[1];
double ex[] = new double[1];
double ave[] = new double[4];

for(int x = 0; x < quiz.length; x++){
quiz[x=0] = Double.parseDouble(JOptionPane.showInputDialog("Enter your Quiz1"));
quiz[x=1] = Double.parseDouble(JOptionPane.showInputDialog("Enter your Quiz2"));
}
cs[0] = Double.parseDouble(JOptionPane.showInputDialog("Enter your CS"));
ex[0] = Double.parseDouble(JOptionPane.showInputDialog("Enter your Exam"));

totalq(quiz);
totalcs(cs);
totalex(ex);



System.out.println("Quiz grade is "+quiz[1]);
System.out.println("CS grade is "+cs[0]);
System.out.println("Exam grade is "+ex[0]);
totalave(ave);
System.out.println("Average grade is "+ave[3]);


}

public static void totalq(double[] q12){
q12[1] = ((q12[0] + q12[1])/2)*.4;
totalave(q12);
}

public static void totalcs(double[] cs){
cs[0] = cs[0]*.1;

}

public static void totalex(double[] ex){
ex[0] = ex[0]*.5;
totalave(ex);
}




public static void totalave(double[] a){
a[3]=a[0]+a[1]+a[2];


}








}

#2
Roman Y

Roman Y

    Programmer

  • Members
  • PipPipPipPip
  • 189 posts
could you explain what you're trying to do with the code? becuase right now it doesn't make a lot of sense... for instance your for-loop I don't know if it works because I don't know if you even can point out index with assignment operation, plus it's just one step away from entering an infinit loop. Try to explain what you are trying to do with every step. and pls put your code within the code tags.

#3
farrell2k

farrell2k

    Learning Programmer

  • Members
  • PipPipPip
  • 60 posts
I have no idea what your doing, but here goes:

totalq() method.

Assume that the score from quiz 1 is 80, and the score from quiz 2 is 100.

so right now:

q12[0] = 80;
q12[1] = 100;
the method:

public static void totalq(double[] q12){
q12[1] = ((q12[0] + q12[1])/2)*.4;

q12[1] = (80 + 100) * .4
so now we have:

q12[0] = 80
q12[1] =  72
then you pass that double array to totalave()

totalave(q12)
When this method is run, you are going to get an index out of bounds exception. The q12 double[] that you passed to this method is now known to that method as double[] a. Because q12's length is only 2, everything blows up.

If you're looking to calculate the average of the two quiz scores, why don't you just do this?

double quizAverage = (q12[0] + q12[1]) / 2;

Then you could do the same with the others. to calculate a totalAverage.

#4
mr mike

mr mike

    Learning Programmer

  • Members
  • PipPipPip
  • 96 posts
You should use better naming conventions but here is your program written in just a main method:

You should also use comments with your code
// one line comment

/*
/ say something
/ here because this is a multiple line comment
*/

import java.io.PrintStream;
import javax.swing.JOptionPane;

/**
 *
 * @author your code
 */
public class FinalGrade {

    public static void main(String[] args) {

        double [] quiz = new double[2];
        double [] cs   = new double[1];
        double [] ex   = new double[1];

        quiz[0] = Double.parseDouble(JOptionPane.showInputDialog("Enter your " +
                "first quiz score: "));
        quiz[1] = Double.parseDouble(JOptionPane.showInputDialog("Enter your " +
                "second quiz score: "));
        cs[0] = Double.parseDouble(JOptionPane.showInputDialog("Enter your CS "+
                "score: "));
        ex[0] = Double.parseDouble(JOptionPane.showInputDialog("Enter your "   +
                "Exam score: "));

        double finalCs = cs[0] * .1;
        double finalEx = ex[0] * .5;
        // get quiz
        double finalQuizTotal =quiz[0] + quiz[1];
        double finalQuizPoints = (finalQuizTotal / 2) * .4;

        double finalGrade = finalCs + finalEx + finalQuizPoints;

        // so you dont have to keep writing System.out. //
        PrintStream out = System.out;
        out.println("Your quiz grades give you this score: " + finalEx);
        out.println("Your cs grade gives you this score: " + finalCs);
        out.println("your exam grade gives you this score: " + finalQuizPoints);
        out.println("\n\n\n Your final course grade is: " + finalGrade);

    }
}
Hope you can make your methods now looking at this main that works using your style.