arrays part 2 1 Answer(s) 2 years and 9 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.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
Related Pages:
arrays part 2 - Java Beginners arrayspart2 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
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
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
Printing 2 arrays
Printing 2 arrays Hi,
I have 2arrays:
String [] head = {"Name... want this 2arrays 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
Arrays -- Intermediate
Java NotesArrays -- Intermediate
Anonymous arrays
Java 2 added anonymous arrays, which allow you to
create a new array of values anywhere... arrays in a loop or as
local variables because each use of new will create
Arrays arrays
Java 2 added anonymous arrays, which allow you to
create a new array...
Java NotesArrays
Java arrays are similar to ideas in mathematics
An array...++, which had a good reason for
using zero (pointer arithmetic on arrays
how to compare 2 arrays using java?
how to compare 2arrays using java? hi can anyone give me the information regarding comparision of arrays.
Java Compare Arrays
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
. Add a new student record
2. Delete a student record
3. Display a student record
Logging Tutorial - Part 1
Logging Tutorial - Part 1
2000-12-14 The Java Specialists' Newsletter [Issue 003] - Logging part 1
Author:
Dr. Heinz M. Kabutz
If you are reading... battle 2 years ago when most of the team,
on a project I was on, stopped using
Logging part 2
Logging part2
2001-01-11 The Java Specialists' Newsletter [Issue 004] - Logging part2
Author:
Dr. Heinz M. Kabutz
If you are reading... up winning a free lunch :]
Logging Part2 of ... (still cannot decide
Variable length arrays?
Variable length arrays? Hi:
I enjoy reading from your site...", "jasmine", "lily"};
itemList[1] = {"lotus"};
itemList[2] = {"apple", "orange... of arrays or if there is a smart way to handle this.
Thanks for your help.
Varma
Arrays - Java Interview Questions
Arrays 1)write a program that take integer array as input and find the second largest number in array?(Array values taken from keyboard)?
2)Given... a program that take arrays A & B as input and find missing element in B array
Introduction to java arrays
Introduction to java arrays
 ...
of Arrays in Java Programming language. You will learn how the Array class... arrays. To meet this feature java
has provided an Array class which abstracts
java 2d arrays
java 2d arrays Create a program that will:
a) Create the array and populate it with the given values.
9, 8
2, 17
49, 4
13, 119
2, 19
11, 47.... and if i do it manualy it is wrong.
public class Arrays {
public static void main
Arrays -- Examples
Java NotesArrays -- Examples
This applet shows a number of methods that use arrays. The source code for
the methods is also given below.
This applet... = m.length/2; // subscript of middle element
if (m.length%2 == 1
arrays - Java Interview Questions
arrays 1)write a program that take integer array as input and find the second largest number in array?(Array values taken from keyboard)?
2)Given... a program that take arrays A & B as input and find missing element in B array
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 - Java Beginners
an application that generates a number square like the one below.
1 2 3 4
1 1 2 3 4
22 4 6 8
3 3 6 9 12
4 4 8 12 16
Your number square should have the numbers 1...}};
System.out.println("Your Original Matrix: ");
for(int i = 0; i < 2; i
arrays - Java Interview Questions
arrays will un store objects in arrays Hi friend,
Yes u will store Objects in array.
Integer[] IntegerArray = {
new Integer(1),
new Integer(2),
new Integer(3
arrays - Java Beginners
and Alist, respectively.
another problem.,,
2.)Suppose list is an array of five... executes?
for (i = 0; i < 5; i++)
{
list[i] = 2 * i + 5;
if (i % 2 == 0)
list[i] = list[i] - 3;
}
THANK YOU GUYS FOR SHARING YOUR
arrays - Java Beginners
arrays 1. Create a new class (in the employees package) called Dependent, with String properties for first and last names, and property called dependentOf, whose type is Employee
2. It should have a constructor
Converting java arrays into list Arrays can be converted by the asList() method of the Arrays class...);
System.out.println(list.size());
System.out.println(list.get(2...("------------------\n"+list1.size());
System.out.println(list1.get(2
Arrays -- Multi-dimensional
Java NotesArrays -- Multi-dimensional
All arrays in Java are really linear, one-dimensional arrays.
However, you can easily build multi-dimensional arrays... this.
These examples all use two-dimensional arrays,
but the same syntax and coding can
Introduction to Collection Algorithms
;
Algorithms:
The Collections and Arrays classes,
available as a part of the Collections Framework, support various algorithms... this program:
Searching Algorithm :
Besides sorting, the Collections and Arrays
Two-dimensional arrays
Two-Dimensional Arrays
Two-dimensional arrays are defined as "an array of
arrays"... of arrays of ints". Such an array is said to be a two-dimensional array. 
Introduction to Java Arrays
Introduction to Java Arrays
In this section you will be introduced to the concept
of Arrays... in the contiguous memory
allocations we use the data structures like arrays. To meet
Introduction to java arrays
Introduction to java arrays
In this section you will be introduced to the concept
of Arrays... in the contiguous memory
allocations we use the data structures like arrays. To meet
Mutiple part is ot working - JSP-Servlet
Mutiple part is ot working Hi,
I have 2 systems and i m using IE7... in the document stream. Part boundaries should not occur in any of the data; how... types, each part has an optional "Content-Type" header that defaults to "text
Java Arrays Tutorial
Java Arrays Tutorial
Learn Java Arrays from scratch with ease...
download.
Introduction
to Java Arrays
In this section you
JavaScript array of arrays
JavaScript array of arrays
 ... in
understanding JavaScript array of arrays. For this we are using JavaScript...;array[2] = "+array[2]+"<br>")
document.write("
Hibernate 3 Query Example Part 2
Hibernate 3 Query Example Part2
Hibernate 3 Query Example Part2
This tutorial explains how to create Hibernate 3 query
example with Spring 3 
Structure of Java Arrays
Structure of Java Arrays
Now lets study the structure of Arrays in java. Array
is the most... type. Moreover, arrays are always of fixed length i.e. the length of an array
Passing Arrays In Jsp Methods
Passing Arrays In Jsp Methods
 ... data type. It
is one of the simplest data structures. Arrays holds equally... starts from
0. Some arrays can be multidimensional. One and two- dimensional