473,406 Members | 2,956 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

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
Aug 10 '15 #1
5 1512
chaarmann
785 Expert 512MB
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?
Aug 10 '15 #2
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.........
Aug 10 '15 #3
chaarmann
785 Expert 512MB
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.
Aug 11 '15 #4
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..



Expand|Select|Wrap|Line Numbers
  1. import java.util.Scanner;
  2.  
  3. public class Main {
  4.  
  5.     public static void main(String[] args) {
  6.         Scanner in=new Scanner (System.in);
  7.         int num[]=new int[5];
  8.         int i=0;
  9.         int sum=0;
  10.  
  11.         for (i=0;i<num.length;i++) {
  12.             System.out.println("enter a number");
  13.             num[i]=in.nextInt();
  14.             sum=sum+num[i];
  15.         }
  16.         System.out.println("Total="+sum);
  17.     }
  18. }
Aug 13 '15 #5
chaarmann
785 Expert 512MB
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
Aug 14 '15 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

16
by: Ian Davies | last post by:
Hello Needing help with a suitable solution. I have extracted records into a table under three columns 'category', 'comment' and share (the category column also holds the index no of the record...
0
by: antique | last post by:
iam supposed to CREATE A FUNCTION INT ADJUSTEDSUM (INT,INT) 1. find the sum of the array of size 100 2. find the number of occurrence of a MIN and MAX number if its twice or more only use 1...
1
by: sachin10 | last post by:
hi i m sachin, i m new to VB programming.i face some problem regarding the sorting of text file contents using arrays.text file have data in the format as below seq...
7
sammyboy78
by: sammyboy78 | last post by:
I was wondering about the format of calling methods using arrays. I know that to call a method when not using arrays it would be formatted in the first class like: public void methodName(...
3
Blackout
by: Blackout | last post by:
Hi, I'm new to C and need to make a program that displays the output of the first six powers of 2 like this: 1 2 4 8 16 32 (2 to the power of 0, 2 to the power of 1, 2 to...
16
by: mike3 | last post by:
(I'm xposting this to both comp.lang.c++ and comp.os.ms- windows.programmer.win32 since there's Windows material in here as well as questions related to standard C++. Not sure how that'd go over...
2
by: theblissfulwizard | last post by:
FYI - New to ASP I'm trying to get multiple values from checkboxes inserted into a table using arrays. I'm using VBscript - ASP 1.1 (not 2.0) and here is what I want to do. I have a page that...
1
by: James Brown | last post by:
Hi, I'm fairly new to VBA and was looking to start using arrays to calculate medians of monte carlo simulations. I know how to set up an array as I have been reading through such websites as: ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.