본문 바로가기
카테고리 없음

java 코드 정리(package 단위) - abc, thread

by greentworkshop 2025. 6. 12.

abc

package abc;

class Circle{
	double r;
	public Circle(double a) {
		r=a;
	}

	public double getArea() {
		return r * r * 3.14;
	}


	public double getRadius() {
		return r;
	}
}
	
public class aa {
	public static void main(String[] args) {
		Circle c = new Circle(5);
		System.out.println(c.r);
		System.out.println(c.getRadius());
		System.out.println(c.getArea());
	}
}
package abc;
import java.util.*;

public class bb {
	
	int width;
	int height;
	public int getArea() {
		return width * height;
	}
	public static void main(String[] args) {
		bb rect = new bb();
		Scanner ad = new Scanner(System.in);
		System.out.print(">>");
		rect.width= ad.nextInt();
		rect.height= ad.nextInt();
		System.out.println("사각형의 면적은 "+ rect.getArea());
	}
}
package abc;

public class cc {
	int radius;
	String name;
	public double getArea() {
		return 3.14 * radius * radius;
	}
	
	public static void main(String[] args) {
		cc pizza;
		pizza = new cc();
		pizza.radius = 10;
		pizza.name = "피자";
		double area = pizza.getArea();
		System.out.println(pizza.name + "의 면적은 " + area);
		
		cc dount = new cc();
		dount.radius = 2;
		dount.name = "도넛";
		area = dount.getArea();
		System.out.println(dount.name + "의 면적은 " + area);
		
	}
}
package abc;

public class dd {
	int radius;
	void set(int r) {
		radius = r;
	}
	double getArea() {
		return 3.14 * radius * radius;
	}
	public dd(int r) {
		radius = r;
	}
	
	public static void main(String[] args) {
		dd pizza = new dd(10);
		System.out.println(pizza.getArea());
		dd dount = new dd(2);
		System.out.println(dount.getArea());
	}

}
package abc;

public class ee {
	int radius;
	String name;
	
	public ee() {
		radius = 1; name = "";
	}
	
	public ee(int r, String n) {
		radius = r; name = n;
	}

	public double getArea() {
		return 3.14 * radius * radius;
	}

	public static void main(String[] args) {
		ee pizza = new ee(10, "피자");
		double area = pizza.getArea();
		System.out.println(pizza.name + "의 면적은 " + area);
		
		ee dount = new ee();
		dount.name = "도넛";
		area = dount.getArea();
		System.out.println(dount.name + "의 면적은 " + area);

	}

}
package abc;

class ff {
	String title;
	String author;
	
	public ff(String t){
		title = t;
		author = "작자미상";
	}
	
	public ff(String t, String a) {
		title = t;
		author = a;
	}

	public static void main(String[] args) {
		ff littlePrince = new ff("어린 왕자", "생텍쥐베리");
		ff loveStory = new ff("춘향전");
		System.out.println(littlePrince.title + " " + littlePrince.author);
		System.out.println(loveStory.title + " " + loveStory.author);
	}
}

thread

package Thread;

class MyThread extends Thread{
	@Override
 public void run() {
		for(int i = 0;i<10;i++)
			System.out.println(getName());
	}
 }
 public class Main10 {
	 public static void main(String[] args) {
		 	//MyThread my_thread= new MyThread();
		 	//my_thread.start();
		 	Thread t1=new MyThread(); t1.start();
		 	Thread t2=new MyThread(); t2.start();
			System.out.println("main");


	 }
 }
package Thread;
class MyThread00 implements Runnable{
	@Override
	public void run() {
		System.out.println("Thread"); }
 }
 	public class Main11_01 {
 public static void main(String[] args) {
 Thread my_thread=new Thread(new MyThread00());
 my_thread.start();
 }
 }
package Thread;

class MyThread01 implements Runnable {
	public void run() {
		for(int i=0;i<10;i++) 
			System.out.println(Thread.currentThread().getName());
	}
}
 public class Main11 {
 public static void main(String[] args) {
	 Thread t1=new Thread(new MyThread01(),"thd0"); t1.start();
	 Thread t2=new Thread(new MyThread01(),"thd1"); t2.start();
	 System.out.println("main");
 	}
 }
package Thread;
 	class MyThread09 implements Runnable {
 			public void run() {
 				for (int i = 0; i < 100; i++) {
 					String thd_name=Thread.currentThread().getName();
 					System.out.print(thd_name + " ");
 					if (thd_name.equals("thd1")) Thread.yield();
 				}
 			}
 }
 public class Main12 {
	 public static void main(String[] args) {
		 Thread my_thread1 = new Thread(new MyThread09(), "thd1");
		 Thread my_thread2 = new Thread(new MyThread09(), "thd2");
		 my_thread1.start();
		 my_thread2.start();
	 }
 }
package Thread;

class MyThread4 extends Thread{

	public MyThread4(String szName) {
		super(szName);
	}	
	@Override
	public void run() {
		for(int i=0;i<100;i++) {
			String num=String.valueOf(i);
			if (i<10) num="0" + String.valueOf(i);
				System.out.print(getName()+"("+num+")");
		}
	}
}
	

public class Main13 {
	public static void main(String[] args) {
		MyThread4 my_thread1=new MyThread4("thd1");
		MyThread4 my_thread2=new MyThread4("thd2");
		MyThread4 my_thread3=new MyThread4("thd3");

		my_thread1.setPriority(Thread.MIN_PRIORITY);
		my_thread2.setPriority(Thread.NORM_PRIORITY);
		my_thread3.setPriority(Thread.MAX_PRIORITY);

		my_thread1.start();
		my_thread2.start();
		my_thread3.start();

	}

}
package Thread;
class MyThread05 implements Runnable{
	public void run() {
		for(int i=0;i<100;i++) {
			System.out.print((Thread.currentThread()).getName()+" ");
			Thread.yield(); }
		}
 	}
 public class Main20 {
 public static void main(String[] args) throws InterruptedException
 {
	 Thread my_thread1=new Thread(new MyThread05(),"thd1");
	 Thread my_thread2=new Thread(new MyThread05(),"thd2");
 	my_thread1.start();
 	my_thread2.start();
 	my_thread1.join();
 	my_thread2.join();
 		System.out.println("main thread"); 
 	}
}
package Thread;
class Counter{
	private int c=0;
	public synchronized void increment() { c++; }
	public synchronized void decrement() { c--; }
	public int value() { return  c; }   
}
/*
  class Counter{
 private int c=0;
 public void increment() {synchronized (this) { c++;} }
 public void decrement() {synchronized (this) {c--;} }
 public int value() { return  c; }  
 }
 */
class MyThread06 implements Runnable{
	Counter c;
	public  MyThread06(Counter c) { this.c=c;  }
	public void run() {
		for (int i=0;i<100000;i++)
			c.increment();    }    }
 	class MyThread07 implements Runnable{
 		Counter c;
 		public  MyThread07 (Counter c) { this.c=c;
 		}
 		public void run() {
 			for (int i=0;i<100000;i++) 
 				c.decrement(); 
}      }
 	
 public class Main21 {
	 public static void main(String[] args) throws InterruptedException{
		 Counter c=new Counter();
		 Thread t1=new Thread(new MyThread06(c));
		 Thread t2=new Thread(new MyThread07(c));
		 t1.start();t2.start();
		 t1.join();t2.join();
		 System.out.println(c.value());
	 }
 }