+ 1
Program to add an element at the beginning/end/middle operations on an array using generic classes
5 Réponses
+ 3
package com.company;
class math{
int sum;
public void setSum(int s) {
this.sum = s;
}
public int getSum(){
return this.sum;
}
}
public class Main {
public static void main(String[] args) {
int [] number =new int[10];
number [0]=1;//1st term
number [1]=2;
number [2]=3;
number [3]=4;
number [4]=5;//middle term
number [5]=6;
number [6]=7;
number [7]=8;
number [8]=9;
number [9]=10;//last term
System.out.printf("first term : %d\n" ,number[0] );
System.out.printf("middle term : %d\n",number[(number.length/2)-1]);
System.out.printf("last term : %d\n",number.length-1);
int firstTerm =number[0];
int middleTerm =number[(number.length/2)-1];
int lastTerm =number[number.length-1];
math math = new math();
math.setSum(firstTerm+middleTerm+lastTerm);
System.out.printf("the sum of the first term , middle term and the last term is %d",math.getSum());
}
}
0
Output for this
0
16
0
Give an output
- 5
Try it .