The document contains code examples for:
1) Summing the elements of an integer array
2) Finding the smallest element of an integer array
3) Finding the largest element of an integer array
It also contains examples of:
4) Using a non-static counter variable to count object instances
5) Using a static counter variable to count object instances
The document contains code examples for:
1) Summing the elements of an integer array
2) Finding the smallest element of an integer array
3) Finding the largest element of an integer array
It also contains examples of:
4) Using a non-static counter variable to count object instances
5) Using a static counter variable to count object instances
The document contains code examples for:
1) Summing the elements of an integer array
2) Finding the smallest element of an integer array
3) Finding the largest element of an integer array
It also contains examples of:
4) Using a non-static counter variable to count object instances
5) Using a static counter variable to count object instances
public class SumArray { public static void main(String[] args) { int[] num={10,20,15,30,25}; int sum=0; for(int i=0;i<num.length;i++) { sum=sum+num[i]; } System.out.println("sum of array is"+ sum); } } OUTPUT:
B.SMALLEST ELEMENET OF AN ARRAY:
public class SArray { public static void main(String[] args) { int[] myList ={10,20,12,15,23}; for(int i =0; i < myList.length; i++) { System.out.println(myList[i]+" "); } int total =0; for(int i =0; i < myList.length; i++) { total += myList[i]; } System.out.println("Total is "+ total); double min = myList[0]; for(int i =1; i < myList.length;i++) { if(myList[i]< min) min = myList[i]; } System.out.println("largest element in array is "+ min); } } OUTPUT:
C.LARGEST ELEMENT OF AN ARRAY:
public class TestArray { public static void main(String[] args) { int[] myList ={10,20,12,15,23}; for(int i =0; i < myList.length; i++) { System.out.println(myList[i]+" "); } int total =0; for(int i =0; i < myList.length; i++) { total += myList[i]; } System.out.println("Total is "+ total); double max = myList[0]; for(int i =1; i < myList.length;i++) { if(myList[i]> max) max = myList[i]; } System.out.println("largest element in array is "+ max); } } OUTPUT: 4: A. public class Counter { int count=0; Counter() { count++; System.out.println(count); } public static void main(String args[]) { Counter c1=new Counter(); Counter c2=new Counter(); Counter c3=new Counter(); } } Output: B: public class Counte { static int count=0; Counte() { count++; System.out.println(count); } public static void main(String args[]) { Counte c1=new Counte(); Counte c2=new Counte(); Counte c3=new Counte(); } }