Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: HELP. Passing Array to class.

  1. #1
    Junior Member
    Join Date
    Dec 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default HELP. Passing Array to class.

    hi. i need help for a problem.

    i have an array of double values in the main method. i am trying to create an object and pass the array to a different class using an overloaded constructor.

    i am having trouble with the syntax. please help, i need to use try/catch here to test values and then get average.

    so far in main i have:

    public class TestScores
    {
     
    public static void main(String[] args)
    {
     
    double[] scores1 = {55.5,60.5,70.5,130.0,80.0}; // THIS SHOULD CAUSE EXCEPTION IN Scores class
    double[] scores2 = {55.5,60.5,70.5,100.0,80.0};
     
    Scores bs = new Scores(double[] badScores); // PROBLEM HERE.HOW DO I PASS ARRAY??
     
    Scores gs = new Scores(double[] goodScores); // HERE ALSO.
     
    System.out.println("Average: " + bs.getAverage());
     
    }
    }
    and in the class Scores, this is what have so far, still not sure how to access the array elements from main:
    public class Scores
    {
    private double[] badScores;
    private double[] goodScores;
    public double average;
    public String str;
    public int LCV;
     
     
    public void Scores(double[] bad, double[] good) // HOW TO I ACCESS ARRAY FROM MAIN???
    {
    badScores = bad;
    goodScores = good;
     
    }
     
    public void TestInvalid()
    {
    try
    {
     
    if (badScores[0] > 0 || badScores[0] < 100)
    {
    average = goodScores[0]+goodScores[1]+goodScores[2]+goodScores[3]+goodScores[4]/5;
    }
    else if (goodScores[0] > 0 || goodScores[0] < 100)
    {
    average = goodScores[0]+goodScores[1]+goodScores[2]+goodScores[3]+goodScores[4]/5;
    }
     
    }
     
     
    catch (IllegalArgumentException ex)
    {
    str = "Invalid score found";
     
    }
    }
     
    public double getAverage()
    {
    return average;
    }
     
    public String getInvalid()
    {
    return str;
    }
    }
    Last edited by helloworld922; December 12th, 2010 at 11:07 PM.


  2. #2
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: HELP. Passing Array to class.

    public void Scores(double[] bad, double[] good) // HOW TO I ACCESS ARRAY FROM MAIN???
    {
    badScores = bad;
    goodScores = good;
     
    }
    get rid of the void and you now have a constructor of class Scores that takes arrays as parameters.
    Last edited by helloworld922; December 12th, 2010 at 11:07 PM.

  3. #3
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: HELP. Passing Array to class.

    Scores bs = new Scores(double[] badScores); // PROBLEM HERE.HOW DO I PASS ARRAY??

    Scores gs = new Scores(double[] goodScores); // HERE ALSO.

    first of all, you need both arrays since you passed them both in constructor.

    Second of all, you'd pass it either

    Scores gs = new Scores(scores1, scores2);

    or
    Scores gs = new Scores(scores2, scores1);

    If you want to pass an individual array,

    you're going to have to make two more constructor

    public Scores (double[] goodScores)
    {

    }

    public Scores( double[] badScores)
    {

    }

  4. #4
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: HELP. Passing Array to class.

    And you pass them the arrays you made in your main method, not the variable names from your other class.

    Also, to pass an array, all you need to call that method is the array variable name you're passing it.

    gs = new Scores(scores1, scores2);

    public class Scores
    {
    private double[] badScores;
    private double[] goodScores;
    public double average;
    public String str;
    public int LCV;
     
     
    public  Scores(double[] bad, double[] good) // HOW TO I ACCESS ARRAY FROM MAIN???
    {
    badScores = bad;
    goodScores = good;
     
    }
     
    public void TestInvalid()
    {
    try
    {
     
    if (badScores[0] > 0 || badScores[0] < 100)
    {
    average = goodScores[0]+goodScores[1]+goodScores[2]+goodScores[3]+goodScores[4]/5;
    }
    else if (goodScores[0] > 0 || goodScores[0] < 100)
    {
    average = goodScores[0]+goodScores[1]+goodScores[2]+goodScores[3]+goodScores[4]/5;
    }
     
    }
     
     
    catch (IllegalArgumentException ex)
    {
    str = "Invalid score found";
     
    }
    }
     
    public double getAverage()
    {
    return average;
    }
     
    public String getInvalid()
    {
    return str;
    }
     
    public double[] getBadScores()
    {
    return badScores;
    }
     
    public double[] getGoodScores()
    {
    return goodScores;
    }
    }

    public class TestScores
    {
     
    public static void main(String[] args)
    {
     
    double[] scores1 = {55.5,60.5,70.5,130.0,80.0}; // THIS SHOULD CAUSE EXCEPTION IN Scores class
    double[] scores2 = {55.5,60.5,70.5,100.0,80.0};
     
    Scores scores = new Scores(scores1, scores2); // or (scores2, scores1) not sure which
    // Scores bs = new Scores(double[] badScores); // PROBLEM HERE.HOW DO I PASS ARRAY??
     
    // Scores gs = new Scores(double[] goodScores); // HERE ALSO.
     
    System.out.println("Average: " + bs.getAverage());
     
    }
    }
    Last edited by javapenguin; December 12th, 2010 at 04:02 PM.

  5. #5
    Junior Member
    Join Date
    Dec 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: HELP. Passing Array to class.

    thank you. i removed void and passed both arrays through as one object. it works now. thanks.

  6. #6
    Junior Member
    Join Date
    Nov 2010
    Location
    Cracow
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: HELP. Passing Array to class.

    if (badScores[0] > 0 || badScores[0] < 100)
    {
    average = goodScores[0]+goodScores[1]+goodScores[2]+goodScores[3]+goodScores[4]/5;
    }
    else if (goodScores[0] > 0 || goodScores[0] < 100)
    {
    average = goodScores[0]+goodScores[1]+goodScores[2]+goodScores[3]+goodScores[4]/5;
    }
    is the code really calculates average ?
    i thought this should be rather:

    average = (goodScores[0]+goodScores[1]+goodScores[2]+goodScores[3]+goodScores[4])/5;

Similar Threads

  1. Passing a value to another class
    By Semple in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 4th, 2010, 05:49 PM
  2. [SOLVED] Class not passing information
    By DiPep in forum What's Wrong With My Code?
    Replies: 6
    Last Post: July 26th, 2010, 10:04 AM
  3. Replies: 0
    Last Post: April 11th, 2010, 08:56 AM
  4. [SOLVED] Passing arrayList inside class
    By KrisTheSavage in forum Collections and Generics
    Replies: 1
    Last Post: March 27th, 2010, 12:45 PM
  5. [SOLVED] Using class implicit toString() for array index
    By Quetzalma in forum Java Theory & Questions
    Replies: 2
    Last Post: February 3rd, 2010, 05:04 PM