QuestionAsk Questions?

ALL program are written in java!!!

Q.1.write a program which have 4 integer value is int a=2;int b=4;intc=5;intd=6; and its check all four integer number that which have maximum value and which have smaller.??

Q.2.write a program in whch class have two integer value and their value is 8 and 10 which is hard coted and have 6 method . first method .done a sum of both value and return it. second method done multiplication and return it. 3rd method take modulus and also check which value is greater and which is smaller and also return it. 4th method do division and also check which value is greater and which is smaller and also return it. 5th methid do subtraction and also check which value is greater and which is smaller and also return it. 6th method is check which value is great which integer value we take it in above program which is hardcoted

View Answers

October 25, 2010 at 3:02 PM

Hi Friend,

Try the following codes:

1)

public class MaxMin { public static void main(String[] args) { int a=2; int b=4; int c=5; int d=6; int arr[]={a,b,c,d}; int max = arr[0]; int min = arr[0]; for(int i=1;i<arr.length;i++){ if (arr[i] > max) { max = arr[i]; } } System.out.println("Largest Number: "+max); for (int i=1; i<arr.length; i++) { if (arr[i]< min) { min = arr[i]; } } System.out.println("Smallest Number: "+min); } }

2)

class Calculate{ int i,j; Calculate(int i,int j){ this.i=i; this.j=j; } public int add(){ return i+j; } public int multiply(){ return i*j; } public int subtract(){ int result=0; if(i>j){ result= i-j; } else{ result= j-i; } return result; } public int divide(){ int result=0; if(i>j){ result= i/j; } else{ result= j/i; } return result; } public int modulus(){ int result=0; if(i>j){ result= i%j; } else{ result= j%i; } return result; } public int check(){ int greater=0; if(i>j){ greater=i; } else{ greater=j; } return greater; } public static void main(String[] args){ Calculate c=new Calculate(8,10); System.out.println("Addition= "+c.add()); System.out.println("Subtraction= "+c.subtract()); System.out.println("Multiplication= "+c.multiply()); System.out.println("Division= "+c.divide()); System.out.println("Modulus= "+c.modulus()); System.out.println("Greater value= "+c.check()); } }

Thanks

Related Tutorials/Questions & Answers: