Please use a relatively basic java coding practice, avoiding use of higher level
ID: 3803072 • Letter: P
Question
Please use a relatively basic java coding practice, avoiding use of higher level functions like private and other ones. Please comment code with explanations as well.
General Info Your project is to write a Java class which implements methods to perform symbolic computations on fractions. The input to your program will be 2 arrays which you will transform into fractions. Example A 2D array named Fractions of integers with n columns and 2 rows 3 1 4 -5 Fractions 2 -2 3 6 A 1D array of chars named Operators of length n-1 Operators The output would be 13/6 that is the result of the symbolic computation 2 4 5 5 13 2 3Explanation / Answer
import java.util.Scanner;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author Surya
*/
public class fraction {
static int GCD(int x, int y)
{
int r=0, a, b;
a = (x > y) ? x : y; // a is greater number
b = (x < y) ? x : y; // b is smaller number
r = b;
while(a % b != 0)
{
r = a % b;
a = b;
b = r;
}
return r;
}
static int LCM(int x, int y)
{
int a;
a = (x > y) ? x : y; // a is greater number
while(true)
{
if(a % x == 0 && a % y == 0)
return a;
++a;
}
}
static void ADD(int r[][],int a[][],int acol)
{
int aa=a[1][acol],bb=r[1][0];//
if(aa<0)aa=aa*-1;
if(bb<0)bb=bb*-1;
//lcm should be positive
int lcm =LCM(aa,bb);//finding lcm storing in lcm variable
//calculating
//adding fractions
//nominator calculations
r[0][0]=(r[0][0]*(lcm/r[1][0]))+((lcm/a[1][acol])*a[0][acol]);
//denominator calculations
r[1][0]=lcm;
//now fractioning using gcd
aa=r[0][0];
bb=r[1][0];
if(aa<0)aa=aa*-1;
if(bb<0)bb=bb*-1;
//gcd should not be zero..
int gcd=GCD(aa,bb);//finding gcd storing in gcd variable
r[0][0]=r[0][0]/gcd;//simplying by gcd
r[1][0]=r[1][0]/gcd;
//addition performed......
}
static void SUB(int r[][],int a[][],int acol)
{
int aa=a[1][acol],bb=r[1][0];//
if(aa<0)aa=aa*-1;
if(bb<0)bb=bb*-1;
//lcm should be positive
int lcm =LCM(aa,bb);//finding lcm storing in lcm variable
//calculating
//subtraction fractions
//nominator calculations
//fraction subtracion
r[0][0]=(r[0][0]*(lcm/r[1][0]))-((lcm/a[1][acol])*a[0][acol]);
//denominator calculations
r[1][0]=lcm;
//now fractioning using gcd
aa=r[0][0];
bb=r[1][0];
if(aa<0)aa=aa*-1;
if(bb<0)bb=bb*-1;
//gcd should not be zero..
int gcd=GCD(aa,bb);//finding gcd storing in gcd variable
r[0][0]=r[0][0]/gcd;//simplying by gcd
r[1][0]=r[1][0]/gcd;
//subtraction performed...
}
static void MUL(int r[][],int a[][],int acol)//method for multipying fractions
{
int aa,bb;//
//calculating
//multplications fractions
//nominator calculations
r[0][0]=r[0][0]*a[0][acol];
//denominator calculations
r[1][0]=r[1][0]*a[1][acol];
//now fractioning using gcd
aa=r[0][0];
bb=r[1][0];
if(aa<0)aa=aa*-1;
if(bb<0)bb=bb*-1;
//gcd should not be zero..
int gcd=GCD(aa,bb);//finding gcd storing in gcd variable
r[0][0]=r[0][0]/gcd;//simplying by gcd
r[1][0]=r[1][0]/gcd;
}
static void DIV(int r[][],int a[][],int acol)
{
//reciprocating
int k;
k=a[0][acol];
a[0][acol]=a[1][acol];
a[1][acol]=k;
MUL(r,a,acol);//calling multiplication function
}
public static void main(String argv[])
{
int n;//variable to store column number
Scanner sc =new Scanner(System.in);
System.out.print("Enter number of columns:");
n=sc.nextInt();//reading number
//declaring variable to store fractions
int f[][]=new int[2][n];
char operator[]=new char[n-1];//declaring operator array with length n-1
int i,j,r;
System.out.println("Enter "+n+" Fractions:");
//reading fraction array;
for(i=0;i<n;i++)//reading into 2d array using for loops
{
for(j=0;j<2;j++)
{
if(j==0)
{
System.out.print("Enter numerator :");//asking for numerator
r=sc.nextInt();//reading number
f[j][i]=r;//storing in 2d array
}
else
{
System.out.print("Enter denominator :");//aking denominator
r=sc.nextInt();//reading number
if(r==0)//if entered number is zero
{
//then priting error and asking again
System.out.print("Denominator must not be zero:::enter again");
j--;
}
else
{
f[j][i]=r;//storing in array
}
}
}
}
String s;
//reading operators..
while(true)
{
System.out.print("Enter operators (number of operators must be"+(n-1)+" ):");//prompting input
s =sc.next();//operators readed..
int len=s.length();
if(len==n-1)//checking operator length
{//if len==n-1 then
for(i=0;i<len;i++)
{
//checking whether the entered operators where +,-,*,/
if(s.charAt(i)=='+' || s.charAt(i)=='-' || s.charAt(i)=='/' || s.charAt(i)=='*')
{
operator[i]=s.charAt(i);//adding to operator variable
}
else
break;//if not one of those operators then breaking loop
}
if(i==len)
break;//if correctly entered then breaking loop
}
else
{
//raising error...asking him to try again
System.out.println("Error:Length not matched try again...");
}
}
//performing calculations..
int result[][]=new int[2][1];//varaible to store result
i=0;
result[0][0]=f[0][0];//storing first fraction value to result
result[1][0]=f[1][0];
while(i<n-1)
{
if(operator[i]=='+')//then performing addition between fractions
{
ADD(result,f,i+1);//calling addition function
}
else if(operator[i]=='-')//then performing subtraction between fractions
{
SUB(result,f,i+1);//calling subtraction function
}
else if(operator[i]=='*')//then performing multiplication between fractions
{
MUL(result,f,i+1);//calling multiplication function
}
else if(operator[i]=='/')//then performing division between fractions
{
DIV(result,f,i+1);//calling division function
}
i++;
}
//printing result...
System.out.println(" The Result is :"+result[0][0]+"/"+result[1][0]);
}
}
output:-
run:
Enter number of columns:4
Enter 4 Fractions:
Enter numerator :3
Enter denominator :2
Enter numerator :1
Enter denominator :-2
Enter numerator :4
Enter denominator :3
Enter numerator :-5
Enter denominator :6
Enter operators (number of operators must be3 ):+*-
The Result is :13/6
BUILD SUCCESSFUL (total time: 55 seconds)