Latest Tutorials| Questions and Answers|Ask Questions?|Site Map




Have Programming Question? Ask it here!
 
 
 


dylan
arrays part 2
1 Answer(s)      3 years and 11 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.

Advertisement
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 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
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 -- 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
Logging part 2
Logging part 2 2001-01-11 The Java Specialists' Newsletter [Issue 004] - Logging part 2 Author: Dr. Heinz M. Kabutz If you are reading... up winning a free lunch :] Logging Part 2 of ... (still cannot decide
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 arrays
. Add a new student record 2. Delete a student record 3. Display a student record
interpreted as part of my document
interpreted as part of my document  How can I show HTML examples without them being interpreted as part of my document
interpreted as part of my document
interpreted as part of my document  How can I show HTML examples without them being interpreted as part of my document
which part to choose.....
which part to choose.....  whether working in database is good for career or in coding
arrays in java - Java Beginners
arrays in java  Hi All, I have two arrays. in this two array some name are same. I want to merge those arrays into single. But while merging I want to delete duplicate entries. How merge those arrays. Thanks, mln15584
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
java; arrays - Java Beginners
java arrays example  How can you create a program, by using arrays and the output would be X. by using char or string.Thank you
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
Are arrays primitive data types?
Are arrays primitive data types?   Hi, Are arrays primitive data types? thanks   Hi, In Java, Arrays are objects. Identifier are some simple variable names which is defined as the value container. The type of value
What is javascript Arrays?
What is javascript Arrays?   Hi, I am learner of JavaScript. My question is that What is JavaScript Arrays ? How to Define the JavaScript arrays in you program. Please give an example show that i will try my self. Thanks
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
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
part of AES code in JSP
part of AES code in JSP  org.apache.jasper.JasperException: Unable to compile class for JSP An error occurred at line: 19 in the jsp file: /index.jsp Generated servlet error: C:\Users\Welcome\.netbeans\5.5\apache
combine two arrays in php
combine two arrays in php  combine two arrays in php   $newArray = array_combine($diff, $pages_name['data']); foreach ($newArray as $data) { var_dump($data); exit('zzzzz'); echo $data . '<br>
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
What are Interceptors in Struts 2 and how do they work?
What are Interceptors in Struts 2 and how do they work? Understanding Struts 2 Interceptors concept is very important in learning the Struts 2. You should have good knowledge of different types of Interceptors in Struts 2
Hibernate 3 Query Example Part 2
Hibernate 3 Query Example Part 2       Hibernate 3 Query Example Part 2 This tutorial  explains  how to create  Hibernate 3 query example with Spring 3 
 

 

 

DMCA.com