Question: arrays part 2



Share on Google+Share on Google+

arrays part 2
1 Answer(s)      6 years and 8 months 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.
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.out.println("Enter numbers: ");
for(int i=0;i<arr.length;i++){
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++){
return index;
return -1;

public static int sum(int arr[]){
int summ=0;
for (int i=0; i<arr.length; i++) {
return summ;
public static void main(String[]args){
Scanner input=new Scanner(;
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();
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);




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
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   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
called; 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  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
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
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
tempArray1And2[] = new int[(tempArray1.length+ tempArray1.length)-2
Medicare Part A
Medicare Part A  unbiased solution for comparing and enrolling in Medicare Advantage, Medicare Supplement and Prescription Drug Plans
Arrays in java
Arrays in java  what is param array in java
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  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 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 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
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
How to create arrays in JavaScript?
How to create arrays in JavaScript?  How to create arrays in JavaScript
create arrays in JavaScript
create arrays in JavaScript  How to create arrays in JavaScript
Java Spring Hibernate Struts Training What is the meaning of Java Platform? Why Java is a platform independent language? What is the benefits of learning Core Java? Which technology should I learn after Java? What is array in java with example? How to Convert ArrayList to Array? How to substring in Java? How to format number in Java? What is instance variable in Java? How to download MySQL JDBC driver? What is Calendar class in Java? Which is the best Java tutorials for beginners? How to rename a file in Java? How to delete file in Java code? How to get day from date in Java using Calendar? How to get day of week in Java? How to calculate Date Difference in Java? How to compare date in Java? How to declare array in Java? How to calculate average of array in Java? What is Array in Java? write a java program to find the summation of all the integers entered on command line Sum of two numbers using command line arguments in Java How to create and use Array in Java? How to pass command line arguments in Java? How to create Applet Hello World? Appending String efficiently in Java How to append String in Java? How to list even numbers between 1 and 100? How to add BigDecimal in Java? What is Abstraction In Java? Which is best Beginners Java Tutorial? What is java.util package? Create list from array in Java Filter collection in Java 8 What is the best way to filter a Java Collection? Easy way to transform Collection to Array? How to convert Collection to Array in Java? What are Basic Java Language Elements? Advanced Java Tutorials in 2017 Java brief history Best Reasons to learn Java Java Example Codes and Tutorials in 2017 How do I read a large file quickly in Java? Is learning Java worthwhile? How to create first Java Program? Retrieve database from the table dynamically in jsp from oracle using servlet What does core Java include? java What are some way to learn Java quickly?


Advertisement null