본문 바로가기
IT/프로그래밍

JAVA 프로그래밍 기초부터 활용까지 2-2장 소스코드

by nutrient 2021. 5. 22.
728x90
728x170

 

 

//예제02-09 InitialTestMember.java
class  InitialTestMember {
	static boolean y;
	public static void main(String args[]) {
		boolean x=true;

		System.out.println(x);
		System.out.println(y);
	}
}

 

//예제02-10  ArguProduct.java

public class ArguProduct {
   public static void main( String args[] )
   {
      int x;
      int y;
      int sum;
      
	  if(args.length==2){
         x = Integer.parseInt( args[0] );
         y = Integer.parseInt( args[1] );
	  }else{ 
		 x=1;
		 y=1;
	  }

      sum = x + y ;
      System.out.println("합 = "+sum);
   }
}

 

// 예제02-11 Product.java
import javax.swing.JOptionPane;

public class Product {
   public static void main( String args[] )
   {
      int x;
      int y;
      int sum;
      String xVal;
      String yVal;

      xVal = JOptionPane.showInputDialog("첫번째 값 입력");
      yVal = JOptionPane.showInputDialog("두번째 값 입력");

      x = Integer.parseInt( xVal );
      y = Integer.parseInt( yVal );

      sum = x + y ;
      JOptionPane.showMessageDialog( null,
         "합 = " + sum );

      System.exit( 0 );
   }
}

 

//예제02-12 Increment.java
public class Increment {

	public static void main(String args[]) {
		int  a =5;
		
		System.out.println("a = "+a+"\t++a = "+ ++a);
		System.out.println("a = "+a+"\ta++ = "+ a++);
		
		a =5;
		
		System.out.println("a = "+a+"\t--a = "+ --a);
		System.out.println("a = "+a+"\ta-- = "+ a--);
		
	}
}

 

//예제02-13 LogicBitOperator.java
class LogicBitOperator {

	public static void main(String ard[]) {
		int x=0;
		int y=0;
        
		if((5<1)&& (x++>y))
		  y++;
		System.out.println("x = "+x+",y = "+y);

		x=0;
		y=0;
		
		if((5<1)& (x++>y))
		  y++;
		System.out.println("x = "+x+",y = "+y);
	}
}

 

//예제02-14 EqualsTest.java
public class EqualsTest {
	public static void main(String args[]) {
		String str1,str2,str3,str4;

		str1 = new String("Hello");
		str2 = "Hello";
		str3 = str1;
		str4 = str2;
        
		if(str1 == str2) {
			System.out.println("str1 == str2");
		}
		if(str1 == str3) {
			System.out.println("str1 == str3");
		}
		if(str1.equals(str4)) {
			System.out.println("str1.equals(str4)");
		}
	}
}

 

//예제02-15 InstanceOf.java
class A  {
	void abc(){
		System.out.println("Hello");
	}
}

class B extends A {
}

public class InstanceOf {
	public static void main(String args[]) {
		B  b = new B();
	
		if(b instanceof B) {
			System.out.println("b is instance of B.");
		}
		if(b instanceof A) {
			System.out.println("b is instance of A.");
		}
		if(b instanceof Object) {
			System.out.println("b is instance of Object.");
		}
	}
}
728x90
그리드형

댓글