Click here to Skip to main content
15,920,111 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
import java.util.Scanner;

public class Parent {

	public static void main(String[] args) 
	{
		Scanner scan=new Scanner(System.in);
		
		System.out.println("enter the 1st no");
		int p=scan.nextInt();
		
		System.out.println("enter the 2nd no");
		int q=scan.nextInt();
		
		System.out.println("enter the 3rd no");
		int r=scan.nextInt();
		
		System.out.println("enter the 4th no");
		int s=scan.nextInt();
		
		Sub obj=new Sub();
		Sub obj1=new Sub(p,q);
		Sub obj2=new Sub(p,q,r);
		Sub obj3=new Sub(p,q,r,s);
		
		obj.get();
		obj1.get();
		obj2.get();		
		obj3.get();
		
		
		
	 }

}




<pre>import java.util.Scanner;

public class Sub 
{
	int w=1,x=1,y=1,z=1;
	public Sub()
	{
		x=5;
		y=4;
	}
	public Sub(int a,int b)
	{
		x=a;
		y=b;
	}
	public Sub(int a,int b,int c)
	{
		x=a;
		y=b;
		w=c;
	}
	public Sub(int a,int b,int c,int d)
	{
		x=a;
		y=b;
		w=c;
		z=d;
		
	}
	public void get()
	{
		Scanner scan=new Scanner(System.in);
		System.out.println("choose the no. of operation you want to do");
		System.out.println("  1 --> +  \n  2--> - \n  3--> * \n  4--> /");
		int k=scan.nextInt();
		
		switch(k)
		
		{
		case 1: 
			
			double c22 = w+x+y+z;
			
			int i=(w==1&&x==5&&y==4&&z==1)?System.out.println("result for 1st"):
         //ERROR : a,b,c are not defined.
			(w==1&&x==a&&y==b&&z==1)?System.out.println("result for 2nd"):
			(w==c&&x==a&&y==b&&z==1)?System.out.println("result for 3nd"):
					System.out.println("result for 4th");	
			
			 	System.out.println(i);
			
			System.out.println("addition of two no. is :  " +c22);
			break;
		
		case 2: 
			
			double c1 = w-x-y-z;

			int j=(w==1&&x==5&&y==4&&z==1)?System.out.println("result for 1st"):
	//ERROR : a,b,c are not defined.
			(w==1&&x==a&&y==b&&z==1)?System.out.println("result for 2nd"):
				(w==c&&x==a&&y==b&&z==1)?System.out.println("result for 3nd"):
					System.out.println("result for 4th");	
				
				System.out.println(j);
				
			System.out.println("subtration of two no. is :  " +c1);
			break;
	
		case 3: 
			
			double c11 = w*x*y*z;
			
			int h=(w==1&&x==5&&y==4&&z==1)?System.out.println("result for 1st"):
	//ERROR : a,b,c are not defined.
		(w==1&&x==a&&y==b&&z==1)?System.out.println("result for 2nd"):
				(w==c&&x==a&&y==b&&z==1)?System.out.println("result for 3nd"):
					System.out.println("result for 4th");	

				System.out.println(h);
				
			System.out.println("multiplication of two no. is :  " +c11);
			break;
		
		case 4: 
			
			double f = w/x/y/z;
			
			int g=(w==1&&x==5&&y==4&&z==1)?System.out.println("result for 1st"):
           //ERROR : a,b,c are not defined.
				(w==1&&x==a&&y==b&&z==1)?System.out.println("result for 2nd"):
				(w==c&&x==a&&y==b&&z==1)?System.out.println("result for 3nd"):
					System.out.println("result for 4th");	
				
				System.out.println(g);
				
			System.out.println("division of two no. is :  " +f);
			break;
			
        default :
        	System.out.println("you choose wrong option.\n     "
        			+ "     please i request you to choose one of the option listed above  !!");
	//IMPORTANT	  
        	get();
        	break;
		}
	}
}



What I have tried:

in the above code i want to check whether the input are matched with which constructor for which i have use switch method and ternary condition but in ternary condition when i check whether "w==a" etc it does not check it..


how can i check such condition?
Posted
Updated 22-Jun-17 23:41pm
Comments
wseng 22-Jun-17 23:50pm    
what should i,j,h,g print ?
Member 13274362 23-Jun-17 3:24am    
The print statement i.e., result for 1st || result for 2nd || result for 3rd || result for 4th !
wseng 23-Jun-17 3:41am    
Datatype with int can only print number.
Member 13274362 23-Jun-17 4:00am    
I've change it to String but that's not the problem..
Error is when i check (w,x,y,z) contains (c,a,b,d) it does not check it which i have mention in comment.

Query is how can i resolve this error ?
wseng 23-Jun-17 5:47am    
But why you assigned 1 to w,x,y,z?

1 solution

1. ERROR : a,b,c are not defined

You should set int a,b,c in global

2.Assign value of the parameter a,b,c,d to variable a,b,c,d which you have set in the global
public Sub(int a, int b, int c, int d) {
      this.a=a;
      this.b=b;
      this.c=c;
      this.d=d;
   }

3. Modify your ocde as below. int cannot print String result

switch (k)
       {
           case 1:
               double c22 = w + x + y + z;
               if (w == 1 && x == 5 && y == 4 && z == 1) {
                   System.out.println("result for 1st");
               }
               else if(w == 1 && x == a && y == b && z == 1)
              {
               System.out.println("result for 2nd");
               }
               else if(w == c && x == a && y == b && z == 1)
               {
               System.out.println("result for 3nd");
               System.out.println("result for 4th");
               }
               else
               {
               System.out.println("no matched");
               }
           System.out.println("addition of two no. is :  " + c22);
           break;
           .....
        }
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900