Results 1 to 9 of 9

Thread: What's up guys

  1. #1
    zuriick is offline Member
    Join Date
    Feb 2009
    Posts
    3
    Rep Power
    0

    Default What's up guys

    Hi,

    I am totally new to programming and my local university has us starting with java right off the bat, for my degree. I have heard a lot of programmers say that this isn't the greatest language to be learning in. Luckily, I found these forums and I see that you help each other.

    I am caught with this. I have to take 5 test scores and average them. Now, I have to be able to take an infinite amount of scores and average them.

    Do I need to start off by reassigning my variables? right now they're; double test1, test2, test3, test4, test5
    double average

    and then from that put it through a counter control loop?
    Last edited by zuriick; 02-05-2009 at 10:21 PM.

  2. #2
    y0y
    y0y is offline Member
    Join Date
    Feb 2009
    Location
    Raleigh, NC
    Posts
    5
    Rep Power
    0

    Default

    This type of problem is language agnostic. The solution you are looking for involves taking a set of data with an unknown upper bound.

    So, when you started with a known number of tests you took the path of least resistance and what seemed most obvious: you created a variable for each test score. However, if you don't know how many tests you are going to have (or even if you do, but you suspect that number might change in the future), it is best to use an array.

    If you were writing this in function form, you could use:

    Java Code:
        public double averageTestScore(double[] testScores) {
             /** Add all the test scores */
             double sumOfScores = 0;
             for (int x = testScores.length; x < testScores.length; x++) {
                 sumOfScores += testScores[x];
             }
                
             /** Compute the average */
             double averageScore = sumOfScores / testScores.length;
             return averageScore;
        }
    Then, in order to use that function, you might do something like:

    Java Code:
    double[] testScores = new double[5];
    testScores[0] = 95.4;
    testScores[1] = 86.2;
    testScores[2] = 99.4;
    testScores[3] = 100;
    testScores[4] = 75.5;
    
    double averageScore = averageTestScore(testScores);
    The concept your teacher is trying to convey is that you normally are not going to know how many items you will be working with, so it is best to create functions that can take any number of elements and perform its operation on them. That's why the averaging function takes an array of unknown length in this example.

    Like I said, this is language agnostic. It's a way of thinking that translates to any programming language.

    (Note: I am not new to programming, but I am new to Java. So, there could potentially be some syntactical errors with the code posted above.)

  3. #3
    MK12's Avatar
    MK12 is offline Senior Member
    Join Date
    Jan 2009
    Posts
    185
    Rep Power
    6

    Default Example

    Do you mean you can add the numbers to an array in the program and it will still calculate the average? or are the numbers the command line arguments? more details pls.
    if test# variables are the numbers, you should keep all the numbers in an array (int[]), not in seperate variables.
    To get the average of the array, this would work:
    (you can replace the int's with doubles if you want decimal #'s)
    Java Code:
    public class AverageCalculator {
         private static int[] nums = { 1, 5, 44, 34, 45, 6, 11 }; // you can change this
         public static void getAverage() {
              int sum, average;
              sum = 0;
              String numbers = "";
              for(int i = 0; i < nums.length; i++) { // This loop gets the sum of nums
                   sum += nums[i];          // and also puts nums in a string
                   String += Integer.toString(nums[i]) + ", ";
              }
              average = sum / nums.length; // calculate average
              System.out.println("The Average of:\n" + numbers); //display results
              System.out.println("is " + average + "!");
         }
         public static void main(String[] args) {
              getAverage();
         }
    }
    Tell me if you want a cool Java logo avatar like mine and I'll make you one.

  4. #4
    y0y
    y0y is offline Member
    Join Date
    Feb 2009
    Location
    Raleigh, NC
    Posts
    5
    Rep Power
    0

    Default

    What I was presenting was a basic answer to the problem without full implementation details. One of the major tenants of programming is to separate logic out and make it implementation agnostic if at all possible. That's the idea behind the average function. In reality, the function should simply be named "average" and it could be overloaded to support many data types (int, floats, etc.). However, the original poster required doubles.

    You could implement this as a command line script in several ways. One simple way might be to first ask the user how many tests they would like to average. Then, once you have that number you know how big to make your array. After creating your array, you can then create a loop which asks the user for each score one at a time, filling the array as you go. Once the array is filled, you can pass it to the average function and display the returned result.

    That's code you'll have to figure out on your own, however. That's half the fun of learning. :)

  5. #5
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Quote Originally Posted by zuriick View Post
    What's up
    Two letters put together to form a preposition indicating movement in a vertical direction away from the ground.

  6. #6
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default

    I have heard a lot of programmers say that this isn't the greatest language to be learning in
    Interesting statement... what would be the greatest language to be learning? I find Java rather fun and fresh to program, but unfortunately it wasn't my first programming language that I learned. It probably doesn't make any difference what your first programming language is, so long as you learn very well (the why's).... all prog languages are bascially the same in structure.
    Nough said on that...
    I have to take 5 test scores and average them. Now, I have to be able to take an infinite amount of scores and average them.
    I'm not sure if you have do the 5 or the infinite amount of scores. I'll assume that you have already done the 5 and now have to do the infinite amount. My first question would be:
    • How are you planning on getting the inifinite amount of scores? User input or manually (hardcoded in the program like y0y and MK12 show)?

    If this manually, than MK12 example is OK (using an array).
    If it's through user input, then you would probably would want to use an arraylist instead of an array.

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  7. #7
    zuriick is offline Member
    Join Date
    Feb 2009
    Posts
    3
    Rep Power
    0

    Default

    ^^ I have already done the 5 and now i have to find the average of any given number of inputs. i am not hard coding them into the the program it has to be entered in two different ways: one is through inFile.nextDouble();, and the other is straight from system.

  8. #8
    zuriick is offline Member
    Join Date
    Feb 2009
    Posts
    3
    Rep Power
    0

    Default

    here is what i have so far

    import java.io.*;
    import java.util.*;

    public class StudentGrade
    {
    public static void main(String[] args) throws IOException,
    FileNotFoundException
    {

    //declare and initialize the variables
    double test1, test2, test3, test4, test5;
    double average;
    String firstName;
    String lastName;

    Scanner inFile =
    new Scanner(new FileReader("test.txt"));

    PrintWriter outFile = new
    PrintWriter("testavg.out");

    firstName = inFile.next();
    lastName = inFile.next();

    outFile.println("Student Name: "
    + firstName + " " + lastName);

    //Step 6 - retrieve the five test scores
    test1 = inFile.nextDouble();
    test2 = inFile.nextDouble();
    test3 = inFile.nextDouble();
    test4 = inFile.nextDouble();
    test5 = inFile.nextDouble();


    outFile.printf("Test scores: %5.2f %5.2f %5.2f "
    + "%5.2f %5.2f %n", test1, test2,
    test3, test4, test5);

    average = (test1 + test2 + test3 + test4
    + test5) / 5.0;
    outFile.printf("Average test score: %5.2f %n",
    average);

    if (average >=90) outFile.printf("A");
    if (average >=80 && average <90) outFile.printf("B");
    if (average >=70 && average <80) outFile.printf("C");
    if (average >=60 && average <70) outFile.printf("D");
    if (average <60) outFile.printf("F");

    inFile.close(); //Step 10
    outFile.close(); //Step 10
    }
    }
    Last edited by zuriick; 02-06-2009 at 04:35 AM.

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

Similar Threads

  1. Hello Guys!
    By jeraldjamescapao in forum New To Java
    Replies: 3
    Last Post: 11-23-2008, 07:39 AM
  2. Hello Guys!
    By jeraldjamescapao in forum Advanced Java
    Replies: 1
    Last Post: 11-22-2008, 07:42 PM
  3. hi guys!!
    By durga in forum Advanced Java
    Replies: 5
    Last Post: 11-15-2008, 12:29 AM
  4. Hi Guys!
    By AnGuRuSO in forum Introductions
    Replies: 2
    Last Post: 10-29-2008, 09:30 AM
  5. Help Me Out Guys
    By prince24 in forum New To Java
    Replies: 10
    Last Post: 07-13-2007, 04:52 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •