Question
java
estion7 For this question, assume all input comes from the keyboard, and al output goes to the screen. Include method prototypes and comments. The array should have room for 100 integers Write a complete Java program, including at least one comment in the main propram and one in e to do the following: Write a main program which will call the methods described below (a) First the main program will read an integer (this integer is a parameter or header Then the main values into an integer array which the main program will call x. cal k. program will call the method readdata) (see part 1 below) to read a set of data containing k value) which it wi (b) Th e main program will call the method countss() (see part 2 below), sending the x array (and k) (c) The main program will call the method sortarray0 (see part 3 below) to sort the first k elements of the x array into dessending order. The new values in the x array will be printed in the main program. Details of the methods: 1. Write a method called readdatal) which receives two parameters, an integer n and an integer array arr The method will be sent a value for n. The method will read a set of n data items into the arr array. The will print the set of data values as they are being read in method 2. Write a method called countss0 which receives two parameters, an integer n and an integer array vT method will count how many of the first n elements of the v array are greater t and how many are equal to 5. The han 5, how many are less than s, The method will print these values, with messages for each. As an example:if the array holds the values 5 66 -4 5 1-3, with n 6: there is 1 value greater than 5,there are 3 values less than 5, and there are 2 values equal to 5. 3. Write a method called sortarrayl) which receives two parameters, an integer array w and an integer the size of the array. The method will sort the first n elements of the w array into descending order. As an example, assume that method starts with these values in the w array: 5 66 4 5 1 -3, with n -6. Inside the method, these values will be sorted into descending order, the array willnow hold 6 5 5 1-3 4.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Coding :-

import java.util.Scanner;
public class p10
{
public int readdata(int n, int arr[])//method read array elements
{
int count=0;
for(int j=0;j<n;j++)
{
System.out.print(arr[j]+" ");
count=count+1;
  
}
System.out.println();
return count;
}
public void count5s(int n, int v[])//this method count array element greater than 5,less than 5 or equal to 5
{
int g5=0,l5=0,e5=0;
for(int k=0;k<n;k++)
{
if(v[k]>5)
{
g5=g5+1;
}
else if(v[k]<5)
{
l5=l5+1;
}
else if(v[k]==5)
{
e5=e5+1;
}
}
System.out.println("there is "+g5+" value greater than 5");
System.out.println("there is "+l5+" value less than 5");
System.out.println("there is "+e5+" value equalthan 5");
  
}
public void sortarray(int n, int w[])//this method sort the array elements in decending order
{
int temp=0;
for(int i=0;i<n;i++)
{
for(int j=0;j<n-i-1;j++)
{
if(w[j+1]>w[j])
{
temp=w[j];
w[j]=w[j+1];
w[j+1]=temp;
}
}
}
System.out.println("Now array elements are:");
for(int i=0;i<n;i++)
{
System.out.print(w[i]+" ");
}
}
public static void main()
{
Scanner s = new Scanner(System.in);
p10 ob = new p10();
System.out.println("Enter number of elements do you want to insert in array :");
int x=s.nextInt();//variable to hold size of array
int a[]=new int[x];//Integer type variable
for(int i=0;i<x;i++)//loop to insert array element
{
System.out.println("Enter "+(i+1)+" array element of array");
a[i]=s.nextInt();
}
System.out.println("OUTPUT 1: ");
int n=ob.readdata(x,a);
System.out.println("OUTPUT 2: ");
ob.count5s(n,a);
System.out.println("OUTPUT 3: ");
ob.sortarray(n,a);
}
}

Output:-

Enter number of elements do you want to insert in array :
6
Enter 1 array element of array
5
Enter 2 array element of array
66
Enter 3 array element of array
-4
Enter 4 array element of array
5
Enter 5 array element of array
1
Enter 6 array element of array
-3
OUTPUT 1:
5 66 -4 5 1 -3
OUTPUT 2:
there is 1 value greater than 5
there is 3 value less than 5
there is 2 value equal than 5
OUTPUT 3:
Now array elements are:
66 5 5 1 -3 -4

Add a comment
Know the answer?
Add Answer to:
java estion7 For this question, assume all input comes from the keyboard, and al output goes...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • the code needs to be modifed. require a output for the code Java Program to Implement...

    the code needs to be modifed. require a output for the code Java Program to Implement Insertion Sort import java.util.Scanner; /Class InsertionSort * public class Insertion Sort { /Insertion Sort function */ public static void sort( int arr) int N- arr.length; int i, j, temp; for (i-1; i< N; i++) j-i temp arrli; while (j> 0 && temp < arrli-1) arrli]-arrli-1]; j-j-1; } arrlj] temp; /Main method * public static void main(String [] args) { Scanner scan new Scanner( System.in...

  • write a complete Java program with comments in main and in each method. Data: The input data for this program is given as two columns of numbers. All data will be entered from a fle named input.t...

    write a complete Java program with comments in main and in each method. Data: The input data for this program is given as two columns of numbers. All data will be entered from a fle named input.txt and all output will go to the screen Assume there will not be more than 100 7 23.56 16 88.12 10 75.1 Design a Java class with a main method that does the following 1) Reads the data into two arrays of doubles,...

  • The following code is a Java code for insertion sort. I would like this code to...

    The following code is a Java code for insertion sort. I would like this code to be modified by not allowing more than 10 numbers of integer elements (if the user enters 11 or a higher number, an error should appear) and also finding the range of the elements after sorting. /* * Java Program to Implement Insertion Sort */ import java.util.Scanner; /* Class InsertionSort */ public class InsertionSortTwo { /* Insertion Sort function */ public static void sort( int...

  • Use c++ as programming language. The file needs to be created ourselves (ARRAYS) Write a program...

    Use c++ as programming language. The file needs to be created ourselves (ARRAYS) Write a program that contains the following functions: 1. A function to read integer values into a one-dimensional array of size N. 2. A function to sort a one-dimensional array of size N of integers in descending order. 3. A function to find and output the average of the values in a one dimensional array of size N of integers. 4. A function to output a one-dimensional...

  • by netBeans C++ java by netBeans C++ java by netBeans C++ java 1. Answer the following...

    by netBeans C++ java by netBeans C++ java by netBeans C++ java 1. Answer the following question a) (4 marks) Write a Java method, which takes an integer array and two integers x and y as input parameters. The method should exchange the value in position x with the value in the position y in the array. Example: If x = 1, y=2 Input Array Output Array b) (4 marks) Write the main method to do the following: 1. Define...

  • Using C programming

    Using C, create a data file with the first number being an integer. The value of that integer will be the number of further integers which follow it in the file. Write the code to read the first number into the integer variable how_many.Please help me with the file :((This comes from this question:Write the code to dynamically allocate ONE integer variable using calloc (contiguous allocation) or malloc (memory allocation) and have it pointed to by a pointer (of type int...

  • The name of the C++ file must be search.cpp Write a program that will read data...

    The name of the C++ file must be search.cpp Write a program that will read data from a file. The program will allow the user to specify the filename. Use a loop that will check if the file is opened correctly, otherwise display an error message and allow the user to re-enter a filename until successful. Read the values from the file and store into an integer array. The program should then prompt the user for an integer which will...

  • I need java code for the following problem. Lab 7: Methods 1. Write a Java program called Numbers that calls the following methods and displays the returned value: Write a method called cubelt that a...

    I need java code for the following problem. Lab 7: Methods 1. Write a Java program called Numbers that calls the following methods and displays the returned value: Write a method called cubelt that accepts one integer parameter and returns the value raised to the third power as an integer. o Write a method called randominRange that accepts two integer parameters representing a range. The method returns a random integer in the specified range inclusive. 2. o Write a method...

  • Write the code to dynamically allocate ONE integer variable using calloc (contiguous allocation) or malloc (memory...

    Write the code to dynamically allocate ONE integer variable using calloc (contiguous allocation) or malloc (memory allocation) and have it pointed to by a pointer (of type int * ) named ptr_1. Use ptr_1 to assign the number 7 to that dynamically allocated integer, and in another line use printf to output the contents of that dynamically allocated integer variable. Write the code to dynamically allocate an integer array of length 5 using calloc or malloc and have it pointed...

  • write in java 1. Assume the availability of a method  named  makeLine that can be passed a non-negative...

    write in java 1. Assume the availability of a method  named  makeLine that can be passed a non-negative integer  n and a character  c and return a String consisting of n identical characters that are all equal to c. Write a method  named  printTriangle that receives two integer  parameters  n and k. If n is negative the method does nothing. If n happens to be an even number, itsvalue is raised to the next odd number (e.g. 4-->5). Then, when k has the value zero, the method prints...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT