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 8 of 8

Thread: I need help for my code...

  1. #1
    Junior Member
    Join Date
    Mar 2013
    Posts
    16
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default I need help for my code...

    Hello fellas.. I have a homework, but I don't know how to start with it.. I have a plan how to do some methods but I can't do them without these stuff that I don't know how to make. Anyone pls help. Here is the homework document.

    Assignment08J.pdf

    I need help for the constructor and first two methods (getScores and setScores)!

    Thank you.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,197
    Thanks
    49
    Thanked 2,172 Times in 2,145 Posts

    Default Re: I need help for my code...

    Post your code and any questions about the problems you are having with it.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Mar 2013
    Posts
    16
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: I need help for my code...

    public class ExamScores
    {
        private int scores;
        private boolean isSorted;
     
     
     
        public void sort()
        {
            if(isSorted) 
            {
                return;
            }
     
            int temp = 0;
     
            for(int i = 0; i < scores.length - 1 && !isSorted; i++)
            {
                isSorted = true;
     
                for(int j = 0; j < scores.length - i - 1; j++)
                {
                    if(scores[j] > scores[j + 1])
                    {
                        temp = scores[j + 1];
                        scores[j + 1] = scores[j];
                        scores[j] = temp;
                        isSorted = false;
                    }
                }
            }
        }
     
     
    }

    public class ExamScoresClient
    {
        public static void main(String[] args)
        {
            int[] examScores = {74, 67, 55, 91, 96, 49, 84, 75, 65, 95};
            ExamScores scores = new ExamScores(examScores);
            System.out.println("The scores:\t\t\t\t" + scores);
            System.out.println("The leftmost score is:\t\t\t" + scores.getScores()[0]);
            System.out.println("The rightmost score is:\t\t\t"
            + scores.getScores()[scores.getScores().length - 1]);
            System.out.println("The average of the scores is:\t\t" , scores.average());
            System.out.printf("The average of the scores is:\t\t%.1f\n" + scores.average());
            System.out.println("The highest score is:\t\t\t" + scores.highestScore());
            System.out.println("The lowest score is:\t\t\t" + scores.lowestScore());
            System.out.println("The median score is:\t\t\t" + scores.medianScore());
            System.out.println("The scores in ascending sequence:\t" + scores);
            System.out.println("----------------------------------------------------------------------");
            int[] moreExamScores = {73, 57, 59, 93, 86, 48, 81, 65, 85, 92, 77};
            scores.setScores(moreExamScores);
            System.out.println("The scores:\t\t\t\t" + scores);
            System.out.println("The leftmost score is:\t\t\t" + scores.getScores()[0]);
            System.out.println("The rightmost score is:\t\t\t"
            + scores.getScores()[scores.getScores().length - 1]);
            System.out.println("The average of the scores is:\t\t" , scores.average());
            System.out.printf("The average of the scores is:\t\t%.1f\n" + scores.average());
            System.out.println("The highest score is:\t\t\t" + scores.highestScore());
            System.out.println("The lowest score is:\t\t\t" + scores.lowestScore());
            System.out.println("The median score is:\t\t\t" + scores.medianScore());
            System.out.println("The scores in ascending sequence:\t" + scores);
        }
    }



    So, as you can see there are two code.. The 2nd one is good code and it executes the program.. In the first code I need help to make a constructor and two methods.. Here are instructions what do I have to do:

    Constructor: There is only one constructor which takes one parameter: an array of integers. It calls the setScores method passing it the parameter away.

    Methods:

    getScore: a public accessor method that takes no parameters and returns an array of integers. It declares and instantiates a local array of integers using the length of the scores array as the size specifier. It then copies the elements from the scores array into the local array. Finally, it returns the local array.

    setScores: a public mutator method that returns void and takes one parameter, an array of integers. It instantiates the instance variable array scores using the length of the parameter array as the size specifier. It then copies the elements from the parameter array into the scores array. Finally, it assigns false to isSorted

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,197
    Thanks
    49
    Thanked 2,172 Times in 2,145 Posts

    Default Re: I need help for my code...

    In the first code I need help to make a constructor and a two methods..
    Please explain what your problems are. What have you tried?

    See the tutorial:
    Providing Constructors for Your Classes (The Java™ Tutorials > Learning the Java Language > Classes and Objects)
    Defining Methods (The Java™ Tutorials > Learning the Java Language > Classes and Objects)
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Mar 2013
    Posts
    16
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: I need help for my code...

    public class ExamScores
    {
        private int[] scores;
        private boolean isSorted;
     
     
        public int[] getScores()
        {
            int[] examScores = new int[scores.length];
     
            for(int i = 0; i < scores.length; i++)
            {
                examScores[i] = scores[i];
            }
            return examScores;
        }
     
        public void setScores(int[] examScores)
        {
            int[] scores = new int[examScores.length];
     
            for(int i = 0; i < scores.length; i++)
            {
                scores[i] = examScores[i];
            }
            isSorted = false;
        }
     
        public int sum()
        {
            int sum = 0;
     
            for (int i = 0; i < scores.length; i++)
            {
                sum += scores[i];
            }
            return sum;
        }
     
        public double average()
        {
           double average = 0.0;
     
           for (int i = 0; i < scores.length; i++)
           {
               average = sum() / scores.length;
           }
           return average;
        }
     
        public int highestScore()
        {
            int highestScore = scores[0];
     
            if(isSorted)
            {
                for(int i = 0; i < scores.length; i++)
                {
                    return scores.length - 1;
                }
            }
     
            else
            {           
                for(int i = 1; i < scores.length; i++)
                {
                    if(scores[i] > highestScore)
                    {
                        highestScore = scores[i];
                    }
                }
            }
     
            return highestScore;
        }
     
        public int lowestScore()
        {
            int lowestScore = scores[0];
     
            if(isSorted)
            {
                for(int i = 0; i < scores.length; i++)
                {
                    return scores[0];
                }
            }
     
            else
            {            
                for(int i = 1; i < scores.length; i++)
                {
                    if(scores[i] < lowestScore)
                    {
                        lowestScore = scores[i];
                    }
                }
            }
     
            return lowestScore;
        }
     
        public double medianScore()
        {        
            double medianScore = scores[0];
     
            if(scores.length%2 = 0)
            {
                medianScore = (scores(scores.lentgth/2) + scores(scores.length/2+1))/2;
            }
            else
            {
                medianScore = scores(scores.length/2);
            }
            return medianScore;
        }
     
        public void sort()
        {
            if(isSorted) 
            {
                return;
            }
     
            int temp = 0;
     
            for(int i = 0; i < scores.length - 1 && !isSorted; i++)
            {
                isSorted = true;
     
                for(int j = 0; j < scores.length - i - 1; j++)
                {
                    if(scores[j] > scores[j + 1])
                    {
                        temp = scores[j + 1];
                        scores[j + 1] = scores[j];
                        scores[j] = temp;
                        isSorted = false;
                    }
                }
            }
        }
    }

    public class ExamScoresClient
    {
        public static void main(String[] args)
        {
            int[] examScores = {74, 67, 55, 91, 96, 49, 84, 75, 65, 95};
            ExamScores scores = new ExamScores(examScores);
            System.out.println("The scores:\t\t\t\t" + scores);
            System.out.println("The leftmost score is:\t\t\t" + scores.getScores()[0]);
            System.out.println("The rightmost score is:\t\t\t"
            + scores.getScores()[scores.getScores().length - 1]);
            System.out.println("The average of the scores is:\t\t" , scores.average());
            System.out.printf("The average of the scores is:\t\t%.1f\n" + scores.average());
            System.out.println("The highest score is:\t\t\t" + scores.highestScore());
            System.out.println("The lowest score is:\t\t\t" + scores.lowestScore());
            System.out.println("The median score is:\t\t\t" + scores.medianScore());
            System.out.println("The scores in ascending sequence:\t" + scores);
            System.out.println("----------------------------------------------------------------------");
            int[] moreExamScores = {73, 57, 59, 93, 86, 48, 81, 65, 85, 92, 77};
            scores.setScores(moreExamScores);
            System.out.println("The scores:\t\t\t\t" + scores);
            System.out.println("The leftmost score is:\t\t\t" + scores.getScores()[0]);
            System.out.println("The rightmost score is:\t\t\t"
            + scores.getScores()[scores.getScores().length - 1]);
            System.out.println("The average of the scores is:\t\t" , scores.average());
            System.out.printf("The average of the scores is:\t\t%.1f\n" + scores.average());
            System.out.println("The highest score is:\t\t\t" + scores.highestScore());
            System.out.println("The lowest score is:\t\t\t" + scores.lowestScore());
            System.out.println("The median score is:\t\t\t" + scores.medianScore());
            System.out.println("The scores in ascending sequence:\t" + scores);
        }
    }


    In the first code when I go to compile there is error which says: unexpected type, required: variable, found: value and it show that the error is at the medianScore method if(scores.length%2 = 0)

    Someone help pls

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,197
    Thanks
    49
    Thanked 2,172 Times in 2,145 Posts

    Default Re: I need help for my code...

    Use the equality operator == for comparing two primitive values, not the assignment operator: =
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Mar 2013
    Posts
    16
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: I need help for my code...

    public class ExamScoresClient
    {
        public static void main(String[] args)
        {
            int[] examScores = {74, 67, 55, 91, 96, 49, 84, 75, 65, 95};
            ExamScores scores = new ExamScores(examScores);
            System.out.println("The scores:\t\t\t\t" + scores);
            System.out.println("The leftmost score is:\t\t\t" + scores.getScores()[0]);
            System.out.println("The rightmost score is:\t\t\t"
            + scores.getScores()[scores.getScores().length - 1]);
            System.out.println("The average of the scores is:\t\t" , scores.average());
            System.out.printf("The average of the scores is:\t\t%.1f\n" + scores.average());
            System.out.println("The highest score is:\t\t\t" + scores.highestScore());
            System.out.println("The lowest score is:\t\t\t" + scores.lowestScore());
            System.out.println("The median score is:\t\t\t" + scores.medianScore());
            System.out.println("The scores in ascending sequence:\t" + scores);
            System.out.println("----------------------------------------------------------------------");
            int[] moreExamScores = {73, 57, 59, 93, 86, 48, 81, 65, 85, 92, 77};
            scores.setScores(moreExamScores);
            System.out.println("The scores:\t\t\t\t" + scores);
            System.out.println("The leftmost score is:\t\t\t" + scores.getScores()[0]);
            System.out.println("The rightmost score is:\t\t\t"
            + scores.getScores()[scores.getScores().length - 1]);
            System.out.println("The average of the scores is:\t\t" , scores.average());
            System.out.printf("The average of the scores is:\t\t%.1f\n" + scores.average());
            System.out.println("The highest score is:\t\t\t" + scores.highestScore());
            System.out.println("The lowest score is:\t\t\t" + scores.lowestScore());
            System.out.println("The median score is:\t\t\t" + scores.medianScore());
            System.out.println("The scores in ascending sequence:\t" + scores);
        }
    }

    Now the first code is alright, no errors, but in the second one when I go to compile there is error about ExamScores scores = new ExamScores(examScores);... it says that constructor ExamScores in class ExamScores cannot be applied to given types; required: no arguments; found: int[]; reason: actual and formal argument lists differ in length...

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,197
    Thanks
    49
    Thanked 2,172 Times in 2,145 Posts

    Default Re: I need help for my code...

    Do you understand about arguments to methods and constructors?
    The args used when calling a method or a constructor must match those used in that method or constructor's definition. Look at the definition for the class's constructor(s) and make sure when you call it, you use what has been defined as valid args.
    required: no arguments; found: int[];
    The error message says the constructor has no args
    and the call to the constructor tried to use an int array.

    Note: The default constructor has no args.

    A option might be to add a constructor with the args you want to use.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replies: 3
    Last Post: April 27th, 2013, 07:19 AM
  2. Replies: 4
    Last Post: January 24th, 2013, 10:20 AM
  3. Replies: 7
    Last Post: January 24th, 2013, 09:41 AM
  4. Replies: 5
    Last Post: November 14th, 2012, 09:47 AM
  5. Replies: 3
    Last Post: September 3rd, 2012, 11:36 AM