Page 394 - computer science (868) class 11
P. 394
Program 6 Design a class Maxnum to print the largest number in an array. Some of the members of the
class are given below:
Class name : Maxnum
Data members/Instance variables
arr[ ] : To store integer elements
n : Integer to store the size of the array
max : To store the largest number
Member functions/Methods
Maxnum (int nn ) : Constructor to initialise n=nn and declare the array
void fillarray( ) : Accepts ‘n’ numbers in array arr[]
int maximum(int i ) : Using recursive technique, returns the largest number from the
array where parameter ‘i’ denotes the index position
void print() : Calls int maximum(int) and prints the array along with its
largest number.
Define the class Maxnum giving details of the constructor, void fillarray( ), int maximum(int). Also
define the main( ) function to create an object and call the functions accordingly to enable the task.
1 import java.util.*;
2 class Maxnum
3 {
4 int arr[];
5 int n, max;
6 Scanner sc=new Scanner(System.in);
7 Maxnum(int nn) // constructor
8 {
9 n=nn;
10 arr=new int[n];
11 }
12 void fillarray() // input
13 {
14 System.out.println("Enter "+n + " elements");
15 for(int i =0;i<n;i++)
16 arr[i]=sc.nextInt();
17 max=arr[0];
18 }
19 int maximum(int i)
20 {
21 if(i==n) // index=size base case
392392 Touchpad Computer Science-XI

