Monday, September 21, 2015

(IGNOU MCA 031 SOLVED Q.2)Write a program for the arithmetic negation of an integer, addition, subtraction and multiplication of two integers using 3’s complement representation.

Suppose that instead of binary or decimal representation of integers, we have ternary, along with 3’s complement, representation of integers, i.e., integers are represented using three digits, viz., 0, 1,2. For example, the integer 47 is represented as 01202  =  (in decimal)    1. 33 + 2. 32 + 0. 31 + 2. 30, where, the leading zero indicates positive sign.  And the integer (- 47 ) in 3’s complement is represented by   11021, the leading 1 indicates negative sign. The other digits, except the right-most, in the representation of (−   47) are obtained by subtracting from 2 the corresponding digit in 47’s representation, and then adding 1 (the representation of – 47 is obtained as 11020 + 00001).

Following is the JAVA program to find the negation of an inputed number and perform addition, subtraction and multiplication operation based on the ternay base of the given numbers.

The program consists of three classes: Number, NumberOperation, and TernaryNumber.

The class Number holds and converts the base 10 number to ternary number.
The class NumberOperation performs various operations on the objects of class Number.
The TernaryNumber class holds the driver program for the whole operation.

All the three classes are defined in package named: ternaryConverter.

Following is the definition of the various classes used in the program:

TernaryNumber.java

package ternaryConverter;
import java.io.*;
/**
* @author GeniusDante
* @program To convert any inputed number to its base 3 equivalent and perform add,subtract and multiply operations on it.
* @version Rev2.3
* @category NumberSystem
* @Date 20-Sep-2015
*/
public class TernaryNumber {
public static void main(String[] args) throws IOException
{
// TODO Auto-generated method stub
NumberOperation num=new NumberOperation();
int n1,n2,ch;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Select an operation to perform");
System.out.println("1. Add");
System.out.println("2. Subtract");
System.out.println("3. Multiply");
System.out.println("4. Negation");
System.out.print("Enter your choice: ");
ch=Integer.parseInt(br.readLine());
switch(ch)
{
case 1:
System.out.print("Enter first number :");
n1=Integer.parseInt(br.readLine());
System.out.print("Enter second number :");
n2=Integer.parseInt(br.readLine());
num.input(n1, n2);
num.add();
num.showresult();
break;
case 2:
System.out.print("Enter first number :");
n1=Integer.parseInt(br.readLine());
System.out.print("Enter second number :");
n2=Integer.parseInt(br.readLine());
num.input(n1, n2);
num.sub();
num.showresult();
break;
case 3:
System.out.print("Enter first number :");
n1=Integer.parseInt(br.readLine());
System.out.print("Enter second number :");
n2=Integer.parseInt(br.readLine());
num.input(n1, n2);
num.mul();
num.showresult();
break;
case 4:
System.out.print("Enter a number to negate:");
n1=Integer.parseInt(br.readLine());
num.input(n1);
num.show();
break;
default: System.out.println("Invalid Choice");
}
}
}

NumberOperations.java

package ternaryConverter;
public class NumberOperation
{
Number n1,n2,n3;
String num1,num2,num3;
int cnum1,cnum2,cnum3;
public NumberOperation()
{
n1=new Number();
n2=new Number();
n3=new Number();
cnum1=cnum2=cnum3=0;
}
public void input(int a)
{
n1.setnum(a);
n1.convert2base3();
num1=new String(n1.getConvertedNumber());
cnum1=convert2base10(num1);
n2.setnum(a*-1);
n2.convert2base3();
num2=new String(n2.getConvertedNumber());
cnum2=convert2base10(num2);
}
public void input(int a,int b)
{
n1.setnum(a);
n1.convert2base3();
num1=new String(n1.getConvertedNumber());
n2.setnum(b);
n2.convert2base3();
num2=new String(n2.getConvertedNumber());
cnum1=convert2base10(num1);
cnum2=convert2base10(num2);
}
public int convert2base10(String s)
{
int l1;
l1=s.length();
int sign,dif,i;
char ch[]=new char[l1];
for(i=0;i<l1;i++)
{
ch[i]=s.charAt(i);
}
int cnum=0;
sign=Integer.parseInt(String.valueOf(ch[0]));
dif=sign*((int)Math.pow(3, l1-1));
i=l1-1;
int l2=1,d,n;
while(l2<=i)
{
n=i-l2;
d=Integer.parseInt(String.valueOf(ch[l2]));
cnum=cnum+(d*((int)Math.pow(3, n)));
l2++;
}
cnum=dif-cnum;
cnum=cnum*-1;
return cnum;
}
public void add()
{
cnum3=cnum1+cnum2;
n3.setnum(cnum3);
n3.convert2base3();
num3=new String(n3.getConvertedNumber());
}
public void sub()
{
cnum3=cnum1-cnum2;
n3.setnum(cnum3);
n3.convert2base3();
num3=new String(n3.getConvertedNumber());
}
public void mul()
{
cnum3=cnum1*cnum2;
n3.setnum(cnum3);
n3.convert2base3();
num3=new String(n3.getConvertedNumber());
}
public void showresult()
{
System.out.println("First number is: "+cnum1);
System.out.println("First number in base 3 is: "+num1);
System.out.println("Second number is: "+cnum2);
System.out.println("Second number in base 3 is: "+num2);
System.out.println("After the specified operation the number in base 3 is: "+num3);
System.out.println("The result in base 10 is: "+cnum3);
}
public void show()
{
System.out.println("The inputed number is: "+cnum1);
System.out.println("The number in base 3 is: "+num1);
System.out.println("After the negation the number in base 3 is: "+num2);
System.out.println("The negative number in base 10 is: "+cnum2);
}
}


Number.java


package ternaryConverter;


public class Number {
int num;
String s;
int length;
int cn,sign;
public void setnum(int n)
{
num=n;
}
public void convert2base3()
{
long cnum=0;
int count=0;
cn=num;
if(cn<0 font="">
{
cn=cn*-1;
sign=1;
}
else
sign=0;
while(cn!=0)
{
cnum=(cnum*10)+(cn%3);
cn=cn/3;
count++;
}
reverse(cnum,count);
setsignbit();
if(sign!=0)
{
complement();
add1();
}
}
public void reverse(long r,int c)
{
long rnum=0;
while(r!=0)
{
rnum=(rnum*10)+r%10;
r=r/10;
c--;
}
while(c!=0)
{
rnum=rnum*10;
c--;
}
s=new String(String.valueOf(rnum));
}
public void setsignbit()
{
length=s.length();
char ch[]=new char[length+1];
for(int i=0;i<length;i++)
ch[i]=s.charAt(i);
for(int i=length;i>0;i--)
{
ch[i]=ch[i-1];
}
if(sign!=0)
ch[0]='1';
else
ch[0]='0';
s=new String(String.valueOf(ch));
length=s.length();
}
public void complement()
{
int n,i=s.length();
char ch[]=new char[i];
for(i=0;i<length;i++)
ch[i]=s.charAt(i);
i--;
while(i>0)
{
n=Integer.parseInt(String.valueOf(ch[i]));
n=2-n;
ch[i]=(char)('0'+n);
i--;
}
s=new String(String.valueOf(ch));
}
public void add1()
{
int i=s.length()-1;
char ch[]=new char[length];
for(i=0;i<length;i++)
ch[i]=s.charAt(i);
int c=0,n;
i--;
n=Integer.parseInt(String.valueOf(ch[i]));
n=n+1;
if(n>=3)
{
c=1;
n=n-3;
ch[i]=((char)('0'+n));
}
else
{
ch[i]=((char)('0'+n));
}
i--;
while(i>0 && c==1)
{
n=Integer.parseInt(String.valueOf(ch[i]));
n=n+c;
if(n>=3)
{
c=1;
n=n-3;
ch[i]=((char)('0'+n));
}
else
{
ch[i]=((char)('0'+n));
if(c==1)
c=0;
}
i--;
}
s=new String(String.valueOf(ch));
}
public String getConvertedNumber()
{
return s;
}
}
Here is the sample Input and output of the program:

Select an operation to perform

1. Add

2. Subtract

3. Multiply

4. Negation

Enter your choice: 4

Enter a number to negate:18

The inputed number is: 18

The number in base 3 is: 0200

After the negation the number in base 3 is: 1100

The negative number in base 10 is: -18

Please feel free to comment. I am willing to hear from you all soon.

1 comment: