# arrays part 2

Ads

Share on Google+

dylan
arrays part 2
1 Answer(s)      7 years ago
Posted in : Java Beginners
Question 2: Useful Array Algorithms and Operations (5 marks)
Upgrade the program in Question 1 so that the program includes:
? A static method search() that:
o Has a parameter of integer array and another integer value
o Searches the integer value in the array, and
o Returns the element index if the integer appears in the array
o Returns -1 otherwise.
? A static method sum() that computes the summation of all the values in the array and returns it.
? The program will then read user inputs and computes the summation by using the above method.
? The program reads another integer from the user.
? The program searches the value in the integer array by using the above method, and prints the result.
Ads
View Answers

September 1, 2010 at 5:53 PM

Hi Friend,

Try the following code:

import java.util.*;
public class ArrayExamples{

public static int[] readIntegers(){
int arr[]=new int[5];
Scanner input=new Scanner(System.in);
System.out.println("Enter numbers: ");
for(int i=0;i<arr.length;i++){
arr[i]=input.nextInt();
}
return arr;
}
public static int largestNo(int arr[]){
int max=arr[0];
for (int i=1; i<arr.length; i++) {
if (arr[i] > max) {
max = arr[i];
}
}
return max;
}
public static int smallestNo(int arr[]){
int min=arr[0];
for (int i=1; i<arr.length; i++) {
if (arr[i]< min) {
min = arr[i];
}
}
return min;
}
public static int search(int arr[], int element){
boolean found=false;
int index=0;
for(int i=0;i<arr.length;i++){
if(arr[i]==element){
index=i;
found=true;
}
}
if(found){
return index;
}
else{
return -1;
}

}
public static int sum(int arr[]){
int summ=0;
for (int i=0; i<arr.length; i++) {
summ+=arr[i];
}
return summ;
}
public static void main(String[]args){
Scanner input=new Scanner(System.in);
int arr[]=ArrayExamples.readIntegers();
int largest=ArrayExamples.largestNo(arr);
int smallest=ArrayExamples.smallestNo(arr);
System.out.println("Largest No: "+largest);
System.out.println("Smallest No: "+smallest);
System.out.println("Enter element to search: ");
int element=input.nextInt();
int index=ArrayExamples.search(arr,element);
System.out.println("Element "+element+" is at postion "+index);
int summation=ArrayExamples.sum(arr);
System.out.println("Summation of all the elements of array: "+summation);

}
}

Thanks

Ads

Related Tutorials/Questions & Answers:
arrays part 2 - Java Beginners
arrays part 2  Question 2: Useful Array Algorithms and Operations (5 marks) Upgrade the program in Question 1 so that the program includes: ? A static method search() that: o Has a parameter of integer array and another
Printing 2 arrays
Printing 2 arrays  Hi, I have 2 arrays: String [] head = {"Name... want this 2 arrays to be printed out in the following manner: head[0] tab personal[0] head[1] tab personal[1] head[2] tab personal [2] I tried different
Advertisements
how to compare 2 arrays using java?
how to compare 2 arrays using java?  hi can anyone give me the information regarding comparision of arrays.   Java Compare Arrays
Arrays
Arrays   Hi I need help with the following exercises. Exercise 1: Write a Java application in which the user is prompted for the total number... of all the values as well. Exercise 2: Write a Java application in which you
Arrays
called Rebel.java; 2.Create two arrays. One of them must store integer numbers... the numbers in the second array by 3; 7.Print out the contents of both arrays. 8.Swap...); } numbers[0] = 6; words[0] = "1"; numbers[2] = 8; words[2] = "3
arrays
arrays  using arrays in methods   Java use arrays in methods import java.util.*; class ArrayExample{ public static int getMaxValue(int[] arr){ int maxValue = arr[0]; for(int i=1;i < arr.length;i
arrays
Store a table with students and information (name, ID, password, crypted password, etc) in a multi-dimensional array "stud"  Arrays and Strings: Store a table with students and information (name, ID, password, crypted password
Arrays
Implement Java code which takes 2 dimensional integer array as input and prints out heaviest island  Implement Java code which takes 2 dimensional integer array as input and prints out heaviest island
arrays
tempArray1And2[] = new int[(tempArray1.length+ tempArray1.length)-2
Arrays in java
Arrays in java  what is param array in java
Medicare Part A
Medicare Part A  unbiased solution for comparing and enrolling in Medicare Advantage, Medicare Supplement and Prescription Drug Plans
java arrays
java arrays  how do you write the code for multipliying 2 double arrays to be in a 3rd double array? here is my code: package employeepay; /** * * @author Owner */ public class Main { /** * @param args the command line
java arrays
java arrays  can i know how can we intilize the three dimentional arrays in java? and how can we assign thae values to that array
java arrays
java arrays  i need a java program to store student details like id,name,addr,marks,average,total..using arrays..input data using scanner class and by using class, object and constructor
Java with Arrays
Java with Arrays  I was given the assignment to create two parallel arrays; both of size 50 (declares 50 as a constant) One is an array of strings... and store it in the arrays. The input for the problem should be provided in a text
Part I
Part I. Understanding XML  A1. Understanding XML :  Learn XML...; JAXP -XSLT API     Know  all XML APIs for XSLT Part II. The Simple API for XML Parsing (SAX)   Part III. XML and the Document
create arrays in JavaScript
create arrays in JavaScript  How to create arrays in JavaScript
How to create arrays in JavaScript?
How to create arrays in JavaScript?  How to create arrays in JavaScript
Compare two Byte Arrays?
Compare two Byte Arrays?  Compare two Byte Arrays
Comparing arrays not working correctly?
Comparing arrays not working correctly?  Comparing arrays not working correctly

Ads

Advertisement null

Ads