Name : Shreyal Mandot
Title : Assignment 3 setA 2. Define an interface “IntOperations†with methods to check whether an integer is positive, negative, even, odd, prime and operations like factorial and sum of digits. Define a class MyNumber having one private int data member. Write a default constructor to initialize it to 0 and another constructor to initialize it to a value (Use this). Implement the above interface. Create an object in main. Use command line arguments to pass a value to the object and perform the above operations using a menu.
import java.io.* ;
interface IntOperations
{
public void isPositiveNegative() ;
public void isEvenOdd() ;
public void isPrime() ;
public void factorial() ;
public void sumOfDigits() ;
}
class ass3a2 implements IntOperations
{
private int number ;
ass3a2()
{
this.number=0 ;
}
ass3a2(int num)
{
this.number=num ;
}
public void isPositiveNegative()
{
if(this.number<0)
System.out.println("\nNumber Is Negetive...") ;
else
System.out.println("\nNumber Is Positive...") ;
}
public void isEvenOdd()
{
if(this.number%2==0)
System.out.println("\nNumber Is Even...") ;
else
System.out.println("\nNumber Is Odd...") ;
}
public void isPrime()
{
int i = 2 ;
while(i!=this.number)
{
if(this.number%i==0)
{
System.out.println("\nNumber Is Not Prime...") ;
break ;
}
i++ ;
}
if(this.number==i)
System.out.println("\nNumber Is Prime...") ;
}
public void factorial()
{
int prod=1 , i=1 ;
while(i<=this.number)
prod*=i++ ;
System.out.println("\nFactorial Of Number Is : "+prod) ;
}
public void sumOfDigits()
{
int sum=0 ;
int i=this.number ;
while(i!=0)
{
sum+=(i%10) ;
i=i/10 ;
}
System.out.println("\nSum Of The Digits Is : "+sum) ;
}
public static void main(String[] arg) throws Exception
{
int num = Integer.parseInt(arg[0]) , choice ;
ass3a2 o = new ass3a2(num) ;
BufferedReader b = new BufferedReader(new InputStreamReader(System.in)) ;
while(true)
{
System.out.println("\n==========Menu========") ;
System.out.println("\n1.Positive or Negative") ;
System.out.println("\n2.Even or Odd") ;
System.out.println("\n3.Prime") ;
System.out.println("\n4.Factorial") ;
System.out.println("\n5.Sum of digits") ;
System.out.println("\n6.Exit") ;
System.out.println("\n======================") ;
System.out.println("\nEnter your choice here : ") ;
choice = Integer.parseInt(b.readLine()) ;
switch(choice)
{
case 1:
o.isPositiveNegative() ;
break ;
case 2:
o.isEvenOdd() ;
break ;
case 3:
o.isPrime() ;
break ;
case 4:
o.factorial() ;
break ;
case 5:
o.sumOfDigits() ;
break ;
case 6:
System.exit(0) ;
break ;
default:
System.out.println("Invalid choice.\nEnter again :") ;
break ;
}
}
}
}
/*
Output-
[prady@localhost setA]$ javac ass3a2.java
[prady@localhost setA]$ java ass3a2 15
==========Menu========
1.Positive or Negative
2.Even or Odd
3.Prime
4.Factorial
5.Sum of digits
6.Exit
======================
Enter your choice here :
1
Number Is Positive...
==========Menu========
1.Positive or Negative
2.Even or Odd
3.Prime
4.Factorial
5.Sum of digits
6.Exit
======================
Enter your choice here :
2
Number Is Odd...
==========Menu========
1.Positive or Negative
2.Even or Odd
3.Prime
4.Factorial
5.Sum of digits
6.Exit
======================
Enter your choice here :
3
Number Is Not Prime...
==========Menu========
1.Positive or Negative
2.Even or Odd
3.Prime
4.Factorial
5.Sum of digits
6.Exit
======================
Enter your choice here :
4
Factorial Of Number Is : 2004310016
==========Menu========
1.Positive or Negative
2.Even or Odd
3.Prime
4.Factorial
5.Sum of digits
6.Exit
======================
Enter your choice here :
5
Sum Of The Digits Is : 6
==========Menu========
1.Positive or Negative
2.Even or Odd
3.Prime
4.Factorial
5.Sum of digits
6.Exit
======================
Enter your choice here :
6
[prady@localhost setA]$
*/
No comments:
Post a Comment