Programming in Java Week 9 Answers

                     

Q1. Problem statement:

Complete the code to develop a BASIC CALCULATOR that can perform operations like AdditionSubtractionMultiplication and Division.

Code:-

        int i=0;
		int j=0;
		double ans=0;
		
		char pattern[] = input.toCharArray();
		
		for(int a=0; a<pattern.length; a++)
        {
			if(pattern[a]=='+')
            {
				i= Integer.parseInt(input.substring(0,a));
				j= Integer.parseInt(input.substring(a+1,pattern.length));
				ans = (double)i+j;
			}else if(pattern[a]=='-')
            {
				i= Integer.parseInt(input.substring(0,a));
				j= Integer.parseInt(input.substring(a+1,pattern.length));
				ans = (double)i-j;
			}else if(pattern[a]=='/')
            {
				i= Integer.parseInt(input.substring(0,a));
				j= Integer.parseInt(input.substring(a+1,pattern.length));
				ans = (double)i/j;
			}else if(pattern[a]=='*')
            {
				i= Integer.parseInt(input.substring(0,a));
				j= Integer.parseInt(input.substring(a+1,pattern.length));
				ans = (double)i*j;
			}
		}
		
		System.out.print(input+" = " + Math.round(ans));

	

Q2. Problem statement:

Complete the code to develop an ADVANCED CALCULATOR that emulates all the functions of the GUI Calculator as shown in the image.

Code:-

	char pattern [] = input.toCharArray();
		int f=0;
		
		
		for(int i=0; i<pattern .length; i++){
			pattern [i]=gui_map(pattern [i]);
		}
		
		
		double operand1=0.0;
		String o1="";
		double operand2=0.0;
		String o2="";
		double ans=0.0;
		
		
		outerloop:
		for(int i=0; i<pattern .length; i++){
			int r=0;
			if(pattern [i]=='+'||pattern [i]=='-'||pattern [i]=='/'||pattern [i]=='X'||pattern [i]=='='){
				for(int j=0; j<i; j++){
					o1+=Character.toString(pattern [j]);
				}
				operand1=Double.parseDouble(o1);
				for(int k=i+1; k<pattern .length; k++){
					if(pattern [k]=='='){
						f=1;
						operand2=Double.parseDouble(o2);
						if(pattern [i]=='+'){
							ans=operand1+operand2;
						}else if(pattern [i]=='-'){
							ans=operand1-operand2;
						}else if(pattern [i]=='/'){
							ans=operand1/operand2;
						}else if(pattern [i]=='X'){
							ans=operand1*operand2;
						}
						break outerloop;
					}else{
						o2+=Character.toString(pattern [k]);
					}
				}
			}
		}
		
		
		if(f==1)
			System.out.print(ans);

Q3. problem statement

Complete the code to perform a 45 degree anti clock wise rotation with respect to the center of a 5 × 5 2D Array as shown below:

Code:-

            char mat[][]= new char[5][5];
			
			for(int L=0;L<5; L++)
            {
				String input = sc.nextLine();
				char seq[] = input.toCharArray();
				if(seq.length==5){
					for(int i=0;i<5;i++)
                    {
						mat[L][i]=seq[i];
					}
				}else
                {
					System.out.print("Wrong Input!");
					System.exit(0);
				}
			}
			
			char tran[][] = new char[5][5];
			String outer[]={"00","10","20","30",
							"40","41","42","43",
							"44","34","24","14",
							"04","03","02","01"};
							
			String inner[]={"11","21","31","32",
							"33","23","13","12"};
			
			
			for(int i=0;i<5;i++)
            {
				for(int j=0;j<5;j++)
                {
					
					for(int k=0; k<outer.length; k++)
                    {
						char indices[]=outer[k].toCharArray();
						int a = Integer.parseInt(String.valueOf(indices[0]));
						int b = Integer.parseInt(String.valueOf(indices[1]));
						if(a==i && b==j)
                        {
							if(k==15){k=1;}
							else if(k==14){k=0;}
							else {k+=2;}
							indices=outer[k].toCharArray();
							a = Integer.parseInt(String.valueOf(indices[0]));
							b = Integer.parseInt(String.valueOf(indices[1]));
							tran[a][b] = mat[i][j];
							break;
						}
					}
					
					for(int k=0; k<inner.length; k++)
                    {
						char indices[]=inner[k].toCharArray();
						int a = Integer.parseInt(String.valueOf(indices[0]));
						int b = Integer.parseInt(String.valueOf(indices[1]));
						if(a==i && b==j){
							if(k==7){k=0;}
							else {k+=1;}
							indices=inner[k].toCharArray();
							a = Integer.parseInt(String.valueOf(indices[0]));
							b = Integer.parseInt(String.valueOf(indices[1]));
							tran[a][b] = mat[i][j];
							break;
						}
					}
					
					tran[2][2] = mat[2][2];
				}
			}
			
			for(int i=0;i<5;i++){
				for(int j=0;j<5;j++){
					System.out.print(tran[i][j]);
				}
				System.out.println();
			} 
   


Q4. A program needs to be developed which can mirror reflect any 5 × 5 2D character array into its side-by-side reflection. Write suitable code to achieve this transformation as shown below:

Code:-

       char act[][]= new char[5][5];
		
		
		char ref[][]= new char[5][5];
		
		
		for(int L=0;L<5; L++)
        {
			String input = sc.nextLine();
			char seq[] = input.toCharArray();
			if(seq.length==5)
            {
				for(int i=0;i<5;i++)
                {
					act[L][i]=seq[i];
				}
			}
		}
		
		
		for(int i=0; i<5;i++)
        {
			for(int j=0; j<5;j++)
            {		
				ref[i][j]=act[i][4-j];
			}
		}
		
		
		for(int i=0; i<5;i++)
        {
			for(int j=0; j<5;j++)
            {		
				System.out.print(ref[i][j]);
			}
			System.out.println();
		}

Q5. Write suitable code to develop a 2D Flip-Flop Array with dimension 5 × 5, which replaces all input elements with values 0 by 1 and 1 by 0. An example is shown below:

Code:-

char act[][]= new char[5][5];
		
		
		for(int L=0;L<5; L++)
        {
			String input = sc.nextLine();
			char pat[] = input.toCharArray();
			if(pat.length==5){
				for(int i=0;i<5;i++)
                {
					if(pat[i]=='0' || pat[i]=='1')
                    {
						act[L][i]=pat[i];
						if(L==4 && i==4) 
							flipflop(act);
					}
					else
                    {
						System.out.print("Only 0 and 1 supported.");
						break;
					}
				}
			}else
            {
				System.out.print("Invalid length");
				break;
			}

		}
	}
	static void flipflop(char[][] flip)
    {
		
		for(int i=0; i<5;i++)
        {
			for(int j=0; j<5;j++)
            {		
				if(flip[i][j]=='1')
					flip[i][j]='0';
				else
					flip[i][j]='1';
			}
		}
	
		
		for(int i=0; i<5;i++)
        {
			for(int j=0; j<5;j++)
            {		
				System.out.print(flip[i][j]);
			}
			System.out.println();
		}