In: Computer Science
Write a java application that will read a set of values from the
user and stores them in array a[]. The application should ask the
user for the number of values he will enter. You need to do the
following methods: 1. Read: This method will read n values from the
user and store them in an array a 2. Print: this method will print
the array as given in the sample output 3. maxEven: This method
will find the maximum value of the even numbers 4. DrawSquarec:
this method will draw a square using the elements of the array (see
sample) Sample Output How many elements you have:5 Enter the
elements: 10 1 4 5 6 The array is: 10 1 4 5 6 The maximum even
number is 10 10 10 10 10 10
11111
44444
55555
66666
CODE -
package abc;
import java.util.*;
public class student
{
int[] arr=new int[1000];
void read(int n) //function for reading n values of array's
element
{
Scanner sc1=new Scanner(System.in);
System.out.println("Enter the elements-");
int x;
for(int i=0;i<n;i++)
{
x=sc1.nextInt();
arr[i]=x;
}
}
void print(int n) //function for printing array
{
System.out.println("The array is-");
for(int i=0;i<n;i++)
{
System.out.print(arr[i]+" ");
}
System.out.println();
}
void maxeven(int n) //function for finding maximum even
number
{
int num=0;
for(int i=0;i<n;i++)
{
if(arr[i]%2==0)
{
if(arr[i]>num)
num=arr[i];
}
}
System.out.println("The Maximum even number is:
"+num);
}
void square(int n) //function for drawing square
{
System.out.println("Required square is-");
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
System.out.print(arr[i]);
}
System.out.println();
}
}
public static void main(String args[])
{
student ob=new student();
Scanner sc=new Scanner(System.in);
System.out.println("How many elements you have: ");
int n=sc.nextInt(); //reading value of no of elements
ob.read(n); //calling read function
ob.print(n); //calling print function
ob.maxeven(n); //calling maxeven function
ob.square(n); //calling function for drawing square
}
}
OUTPUT -