using arrays

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Onalenna
    New Member
    • Aug 2015
    • 3

    using arrays

    create a class called Findsum
    using arrays and the scanner.
    store numbers into the array and use a method called find_sum(int [] arrayNumbers), that takes the array as an argument and return the total/sum of array elements
  • chaarmann
    Recognized Expert Contributor
    • Nov 2007
    • 785

    #2
    OOOps ... You just copied your assignment.
    So where is your problem?
    Can you tell us what you have done so far (list code here) and where you are stuck?

    Comment

    • Onalenna
      New Member
      • Aug 2015
      • 3

      #3
      I copied as it is because it was one the question i was asked by my lecturer
      am just new in the course and am bit struggling..... ....

      Comment

      • chaarmann
        Recognized Expert Contributor
        • Nov 2007
        • 785

        #4
        No problem Onalenna, everyone was a beginner once.
        I hope you read the forum guidelines and knew from it that we do not do your homework for you, else you do not learn. And learning you want, else you would not do the course. And you can only learn by trying yourself and discuss your upcoming problems with others. So tell us what specific is it that you are struggling with. Show us what you have done so far.

        Comment

        • Onalenna
          New Member
          • Aug 2015
          • 3

          #5
          word taken and got into consideration and thank you very much too much appreciating it so here i tried the code but am not sure that is okay according to the question above..



          Code:
          import java.util.Scanner;
          
          public class Main {
          
              public static void main(String[] args) {
                  Scanner in=new Scanner (System.in);
                  int num[]=new int[5];
                  int i=0;
                  int sum=0;
          
                  for (i=0;i<num.length;i++) {
                      System.out.println("enter a number");
                      num[i]=in.nextInt();
                      sum=sum+num[i];
                  }
                  System.out.println("Total="+sum);
              }
          }
          Last edited by Rabbit; Aug 13 '15, 03:41 PM. Reason: Please use [code] and [/code] tags when posting code or formatted data.

          Comment

          • chaarmann
            Recognized Expert Contributor
            • Nov 2007
            • 785

            #6
            Hi Onalenna,
            the program looks promising. But you did 2 task in your for-loop in each step. First task: you scan in the integer (line 13) and second task: you build the sum (line 14).

            So you should separate the tasks and use 2 for-loops. One for the first task, and one for the second task. You can do that by copying the whole loop (line 11 to 15) and place the copy right below the end of the loop. Then delete the second task (sum) from the first for-loop. Then delete the first task from the second copied loop.

            So, now that you have the 2 tasks separated, you can just put the second loop into its own method easily, as described in your assignment. If you are using Eclipse, it will do it for you automatically by marking the second loop as selected, then right-click --> refactor --> extract method.

            And if you want to become professional, then you can clean-up a bit and replace the second loop with a newer for-each loop

            Comment

            Working...