09009

[Java ์ฃผ์š” ๋ฌธ์ œ ํ’€์ด (1)] - ๋ฐฐ์—ด, ์ƒ์„ฑ์ž, ๋ฉ”์„œ๋“œ ๋ณธ๋ฌธ

Back-End/JAVA
[Java ์ฃผ์š” ๋ฌธ์ œ ํ’€์ด (1)] - ๋ฐฐ์—ด, ์ƒ์„ฑ์ž, ๋ฉ”์„œ๋“œ
09009

๐Ÿ“– ๋ฌธ์ œ

๋ฐ‘๋ณ€๊ณผ ๋†’์ด ์ •๋ณด๋ฅผ ์ €์žฅํ•  ์ˆ˜ ์žˆ๋Š” Triangle ํด๋ž˜์Šค๋ฅผ ์ •์˜ํ•˜์ž.

๊ทธ๋ฆฌ๊ณ  ์ƒ์„ฑ๊ณผ ๋™์‹œ์— ์ดˆ๊ธฐํ™”๊ฐ€ ๊ฐ€๋Šฅํ•œ ์ƒ์„ฑ์ž๋„ ์ •์˜ํ•˜์ž. ๋์œผ๋กœ ๋ฐ‘๋ณ€๊ณผ ๋†’์ด ์ •๋ณด๋ฅผ ๋ณ€๊ฒฝ์‹œํ‚ฌ ์ˆ˜ ์žˆ๋Š” ๋ฉ”์†Œ๋“œ์™€ ์‚ผ๊ฐํ˜•์˜ ๋„“์ด๋ฅผ ๊ณ„์‚ฐํ•ด์„œ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ฉ”์†Œ๋“œ๋„ ํ•จ๊ป˜ ์ •์˜ํ•˜์ž. (๋ฉ”์†Œ๋“œ์˜ ๋ฐ˜ํ™˜ํ˜•๊ณผ ๋งค๊ฐœ๋ณ€์ˆ˜์˜ ์ž๋ฃŒํ˜•์€ double๋กœ ์„ ์–ธ)

// ๋ฐ‘๋ณ€๊ณผ ๋†’์ด ์ •๋ณด๋ฅผ ์ €์žฅํ•  ์ˆ˜ ์žˆ๋Š” Triangle ํด๋ž˜์Šค๋ฅผ ์ •์˜ํ•˜์ž. ๊ทธ๋ฆฌ๊ณ  ์ƒ์„ฑ๊ณผ ๋™์‹œ์— ์ดˆ๊ธฐํ™”๊ฐ€ ๊ฐ€๋Šฅํ•œ ์ƒ์„ฑ์ž๋„ ์ •์˜ํ•˜์ž.
// ๋์œผ๋กœ ๋ฐ‘๋ณ€๊ณผ ๋†’์ด ์ •๋ณด๋ฅผ ๋ณ€๊ฒฝ์‹œํ‚ฌ ์ˆ˜ ์žˆ๋Š” ๋ฉ”์†Œ๋“œ์™€ ์‚ผ๊ฐํ˜•์˜ ๋„“์ด๋ฅผ ๊ณ„์‚ฐํ•ด์„œ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ฉ”์†Œ๋“œ๋„ ํ•จ๊ป˜ ์ •์˜ํ•˜์ž.

class Triangle {
    double bottom, height;
    // public์œผ๋กœ ์ ‘๊ทผ์ œํ•œ์ž๋ฅผ ์ง€์ •ํ•ด์ค˜์•ผ ์™ธ๋ถ€์—์„œ๋„ ์ƒ์„ฑ์ž๋ฅผ ๋ถˆ๋Ÿฌ์˜ฌ ์ˆ˜ ์žˆ๋‹ค.
    public Triangle() {}
    public Triangle(double bottom, double height) {
        this.bottom = bottom;
        this.height = height;
    }

//    public void setBottom(double bottom) {
//        this.bottom = bottom;
//    }
//
//    public void setHeight(double height) {
//        this.height = height;
//    }

    public double getArea() {
        return bottom * height / 2;
    }
}

public class TriangleText {
    public static void main(String[] args) {
        Triangle tr = new Triangle(10.2, 17.3);
        System.out.println("์‚ผ๊ฐํ˜•์˜ ๋„“์ด : " + tr.getArea());
    }
}

โœ ๋‹ต์•ˆ ์ฝ”๋“œ

class Triangle {
    private double bottom;
    private double height;

    public Triangle(double bottom, double height) {
//        super();
        this.bottom = bottom;
        this.height = height;
    }

    public void setBottom(double bottom) {
        if(bottom <= 0) {
            bottom = 1;
            return;
        }
        this.bottom = bottom;
    }

    public void setHeight(double height) {
        this.height = height;
    }
    public double getArea() {
        return bottom * height / 2;
    }
}
public class TriangleTest {
    public static void main(String[] args) {
        Triangle tr = new Triangle(10.2, 17.3);
        tr.setBottom(5);
        tr.setHeight(6);
        System.out.println("์‚ผ๊ฐํ˜•์˜ ๋„“์ด : " + tr.getArea());
    }
}

๐Ÿ“– ๋ฌธ์ œ

์œ„ ํด๋ž˜์Šค๋ฅผ ๋ฐ”ํƒ•์œผ๋กœ ์›์„ ์˜๋ฏธํ•˜๋Š” Circleํด๋ž˜์Šค๋ฅผ ์ •์˜ํ•˜์—ฌ๋ผ.

์ค‘์‹ฌ์ ์˜ ์ขŒํ‘œ์™€ ๋ฐ˜์ง€๋ฆ„(int)์˜ ๊ธธ์ด ์ •๋ณด๋ฅผ ์ €์žฅํ•˜์—ฌ๋ผ.
์› ๋‘๊ฐœ๋กœ ์ด๋ฃจ์–ด์ง„ Ring ํด๋ž˜์Šค๋ฅผ ์ •์˜ํ•˜์—ฌ๋ผ.

class Point
{
	int xPos, yPos;
	
	public Point(int x, int y)
	{
		xPos=x;
		yPos=y;
	}
	public void showPointInfo() {System.out.println("["+xPos+","+yPos+"]");}
}

public class RingTest {
	public static void main(String[] args) {
		Ring ring=new Ring(1,1,4,2,2,9); //Inner Circle x์ขŒํ‘œ, y์ขŒํ‘œ, ๋ฐ˜์ง€๋ฆ„, Outter Circle x์ขŒํ‘œ, y์ขŒํ‘œ, ๋ฐ˜์ง€๋ฆ„
		ring.showRingInfo();
	}
}

 

โœ ๋‹ต์•ˆ ์ฝ”๋“œ

class Point {
    int xPos, yPos;
    public Point(int x, int y) {
        xPos=x;
        yPos=y;
    }
    public void showPointInfo() {
        System.out.println("["+xPos+","+yPos+"]");
    }
}

class Circle {
    Point p;
    int r;
    public Circle(int x, int y, int r) {
        p = new Point(x, y); this.r = r;
    }
    public void showPointInfo() {
        System.out.println("radius : " + r);
        p.showPointInfo();
    }
}

class Ring {
    Circle c1, c2;
    public Ring(int x1, int y1, int r1, int x2, int y2, int r2) {
        c1 = new Circle(x1, y1, r1);
        c2 = new Circle(x2, y2, r2);
    }
    public void showRingInfo() {
        System.out.println("Inner Circle Info...");
        c1.showPointInfo();
        System.out.println("Outer Circle Info...");
        c2.showPointInfo();
    }
}

public class RingTest {
    public static void main(String[] args) {
        Ring ring = new Ring(1, 1, 4, 2, 2, 9); //Inner Circle x์ขŒํ‘œ, y์ขŒํ‘œ, ๋ฐ˜์ง€๋ฆ„, Outter Circle x์ขŒํ‘œ, y์ขŒํ‘œ, ๋ฐ˜์ง€๋ฆ„
        ring.showRingInfo();
    }
}

 


๐Ÿ“– ๋ฌธ์ œ

SimpleNumber ํด๋ž˜์Šค๋Š” ์ƒ์„ฑ์ž๊ฐ€ private, ์ฆ‰ ์™ธ๋ถ€์—์„œ ์ƒ์„ฑํ•˜์ง€ ๋ชปํ•œ๋‹ค. ํด๋ž˜์Šค ์•ˆ์—์„œ๋Š” ๊ฐ€๋Šฅ.
์˜ˆ์ œ๋ฅผ ๋ณ€๊ฒฝํ•˜์—ฌ num1, num2๊ฐ€ ๋™์ผํ•œ ์ธ์Šคํ„ด์Šค๊ฐ€ ๋˜๊ฒŒ ๋ณ€๊ฒฝ. SimpleNumber์˜ ์ธ์Šคํ„ด์Šค๊ฐ€ ํ•˜๋‚˜๋งŒ ์ƒ์„ฑ๋˜๊ฒŒ ์ œํ•œ.

 

class SimpleNumber {
    int num = 0;
    private SimpleNumber() {}
    public void addNum(int n) { num += n;}
    public void showNum() {
        System.out.println(num);
    }
    // ํ•„๋“œ์— static์ด ๋ถ™์œผ๋ฉด 
    public static SimpleNumber getSimpleNumberInst() {
        return new SimpleNumber();
    }
}
public class OnlyOneInstance {
    public static void main(String[] args) {
        SimpleNumber num1 = SimpleNumber.getSimpleNumberInst();
        num1.addNum(20);
        SimpleNumber num2 = SimpleNumber.getSimpleNumberInst();
        num2.addNum(30);
        num1.showNum(); // 20 -> 50
        num2.showNum(); // 30 -> 50
    }
}

// SimpleNumber ํด๋ž˜์Šค๋Š” ์ƒ์„ฑ์ž๊ฐ€ private, ์ฆ‰ ์™ธ๋ถ€์—์„œ ์ƒ์„ฑ๋ชปํ•จ. ํด๋ž˜์Šค ์•ˆ์—์„œ๋Š” ๊ฐ€๋Šฅ.
//์˜ˆ์ œ๋ฅผ ๋ณ€๊ฒฝํ•˜์—ฌ num1, num2๊ฐ€ ๋™์ผํ•œ ์ธ์Šคํ„ด์Šค๊ฐ€ ๋˜๊ฒŒ ๋ณ€๊ฒฝ. SimpleNumber์˜ ์ธ์Šคํ„ด์Šค๊ฐ€ ํ•˜๋‚˜๋งŒ ์ƒ์„ฑ๋˜๊ฒŒ ์ œํ•œ.

โœ ๋‹ต์•ˆ ์ฝ”๋“œ 1)

class SimpleNumber {
    int num = 0;
    private SimpleNumber() {}
    public void addNum(int n) { num += n;}
    public void showNum() {
        System.out.println(num);
    }
    private static SimpleNumber instance = new SimpleNumber();

    public static SimpleNumber getInstance() {
        return instance;
    }
}
public class OnlyOneInstance {
    public static void main(String[] args) {
        SimpleNumber num1 = SimpleNumber.getInstance();
        num1.addNum(20);
        SimpleNumber num2 = SimpleNumber.getInstance();
        num2.addNum(30);
//        SimpleNumber num3 = SimpleNumber.getInstance();
//        num3.addNum(80);
        num1.showNum(); // 20 -> 50
        num2.showNum(); // 30 -> 50
//        num3.showNum();
    }
}

2)

class SimpleNumber
{
    int num=0;
    private SimpleNumber() {}
    public void addNum(int n) {num+=n;}
    public void showNum() {System.out.println(num);}

    public static SimpleNumber snInst = null;
    public static SimpleNumber getSimpleNumberInst()
    {
        if(snInst==null)
            snInst=new SimpleNumber();

        return snInst;
    }
}

public class OnlyOneInstance {
    public static void main(String[] args) {
        SimpleNumber num1=SimpleNumber.getSimpleNumberInst();
        num1.addNum(20);
        SimpleNumber num2=SimpleNumber.getSimpleNumberInst();
        num2.addNum(30);
        num1.showNum();
        num2.showNum();
    }
}

3)

class SimpleNumber {
    int num = 0;
    private SimpleNumber() {}
    public void addNum(int n) { num += n;}
    public void showNum() {
        System.out.println(num);
    }
    private static SimpleNumber instance = new SimpleNumber();
    // ํ•„๋“œ์— static์ด ๋ถ™์œผ๋ฉด
    public static SimpleNumber getSimpleNumberInst() {
        return instance;
    }
    
}
public class OnlyOneInstance {
    public static void main(String[] args) {
        SimpleNumber num1 = SimpleNumber.getSimpleNumberInst();
        num1.addNum(20);
        SimpleNumber num2 = SimpleNumber.getSimpleNumberInst();
        num2.addNum(30);
        num1.showNum(); // 20 -> 50
        num2.showNum(); // 30 -> 50
    }
}

๐Ÿ“– ๋ฌธ์ œ

String str="990208-1012752";

StringBuilder๋ฅผ ํ™œ์šฉํ•˜์—ฌ - ๋ฅผ ์‚ญ์ œํ•œ String ์ธ์Šคํ„ด์Šค ์ƒ์„ฑํ•˜๋ผ.

public class RemoveBar {

public static void main(String[] args) {
String str="990208-1012752";

System.out.println(str);
}
}

โœ ๋‹ต์•ˆ ์ฝ”๋“œ 1)

public class RemoveBar {
    public static void main(String[] args) {
        String str = "990208-1012752";
        StringBuilder sbuf = new StringBuilder(str);
        
        for(int i = 0; i < sbuf.length(); i++) {
            if (sbuf.charAt(i) == '-') {
                sbuf.deleteCharAt(i);
                break;
            }
        }
        str = sbuf.toString();
        System.out.println(str);

    }
}

๋ฌธ์ž์—ด์˜ ๊ธธ์ด๋ฅผ ํ™•์ธํ•ด์•ผํ•˜๋ฏ€๋กœ length๊ฐ€ ์•„๋‹ˆ๋ผ length()๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.

* deleteCharAt๋Š” StringBuilder ํด๋ž˜์Šค์— ๋‚ด์žฅ๋œ ๋ฉ”์„œ๋“œ์ด๋‹ค.

 

2)

public class RemoveBar {
    public static void main(String[] args) {
        String str = "990208-1012752";
        StringBuilder sbuf = new StringBuilder(str);

        int idx = sbuf.lastIndexOf("-");
        if (idx != -1) sbuf.deleteCharAt(idx);

        str = sbuf.toString();
        System.out.println(str);

    }
}

๐Ÿ“– ๋ฌธ์ œ

intํ˜• 1์ฐจ์› ๋ฐฐ์—ด์„ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ „๋‹ฌ๋ฐ›์•„ ์ตœ๋Œ€๊ฐ’, ์ตœ์†Ÿ๊ฐ’์„ ๊ตฌํ•˜์—ฌ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ฉ”์„œ๋“œ ๊ตฌํ˜„ํ•˜๊ธฐ

import java.util.Scanner;

public class ArrayMinMax {	
	public static int maxValue(int[] arr)
	{
		
	}
	
	public static int minValue(int[] arr)
	{
		
	}

	public static void main(String[] args) {
		Scanner keyboard=new Scanner(System.in);
		int[] intArr=new int[7];
		for(int i=0; i<intArr.length; i++)
		{
			System.out.print("์ •์ˆ˜ ์ž…๋ ฅ : ");
			intArr[i]=keyboard.nextInt();
		}
		System.out.println("์ตœ์†Œ๊ฐ’ : "+minValue(intArr));
		System.out.println("์ตœ๋Œ€๊ฐ’ : "+maxValue(intArr));


	}
}

โœ ๋‹ต์•ˆ ์ฝ”๋“œ 

// intํ˜• 1์ฐจ์› ๋ฐฐ์—ด์„ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ „๋‹ฌ๋ฐ›์•„ ์ตœ๋Œ€๊ฐ’, ์ตœ์†Ÿ๊ฐ’์„ ๊ตฌํ•˜์—ฌ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ฉ”์„œ๋“œ ๊ตฌํ˜„ํ•˜๊ธฐ
import java.util.Scanner;

public class ArrayMinMax {
    public static int maxValue(int[] arr) {
       int max = arr[0];
       for(int i = 1; i < arr.length; i++) {
           if (arr[i] > max) {
               max = arr[i];
           }
       }
       return max;
    }

    public static int minValue(int[] arr) {
        int min = arr[0];
        for(int i = 1; i < arr.length; i++) {
            if (arr[i] < min) {
                min = arr[i];
            }
        }
        return min;
    }

    public static void main(String[] args) {
        Scanner keyboard = new Scanner(System.in);
        int[] intArr = new int[7];
        for(int i=0; i<intArr.length; i++)
        {
            System.out.print("์ •์ˆ˜ ์ž…๋ ฅ : ");
            intArr[i]=keyboard.nextInt();
        }
        System.out.println("์ตœ์†Œ๊ฐ’ : "+ minValue(intArr));
        System.out.println("์ตœ๋Œ€๊ฐ’ : "+ maxValue(intArr));
    }
}

๐Ÿ“– ๋ฌธ์ œ

addOneDArr ๋ฉ”์†Œ๋“œ๋ฅผ ์ด์šฉํ•˜์—ฌ 2์ฐจ์› ๋ฐฐ์—ด์˜ ๊ฐ ์š”์†Œ๋ฅผ ์ž…๋ ฅ๊ฐ’๋งŒํผ ์ฆ๊ฐ€์‹œํ‚ค๋Š” addTwoDArrํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•˜๋ผ. 

public class TwoDimensionalArrayAdder {
	public static void addOneDArr(int[] arr, int add)
	{
		for(int i=0; i<arr.length; i++)
			arr[i]+=add;
	}
	
	public static void addTwoDArr(int[][] arr, int add)
	{

	}
	
	public static void main(String[] args) {
		int[][] arr= {
				{1,2,3,4},
				{5,6,7,8},
				{9,10,11,12}
		};
		addTwoDArr(arr, 2);
		for(int i=0; i<arr.length; i++)
		{
			for(int j=0; j<arr[i].length; j++)
				System.out.print(arr[i][j]+" ");
			System.out.println("");
		}
	}
}

โœ ๋‹ต์•ˆ ์ฝ”๋“œ 

 

public class TwoDimensionalArrayAdder {
    public static void addOneDArr(int[] arr, int add) {
        for(int i=0; i<arr.length; i++)
            arr[i]+=add;
    }

    public static void addTwoDArr(int[][] arr, int add) {
//        for(int i = 0 ; i < arr.length; i++) {
//            for(int j = 0; j < arr[i].length; j++){
//                arr[i][j] += add;
//            }
//        }
        for(int i = 0; i < arr.length; i++) addOneDArr(arr[i],2);
    }

    public static void main(String[] args) {
        int[][] arr= {
                {1,2,3,4},
                {5,6,7,8},
                {9,10,11,12}
        };
        addTwoDArr(arr, 2);
        for(int i=0; i<arr.length; i++)
        {
            for(int j=0; j<arr[i].length; j++)
                System.out.print(arr[i][j]+" ");
            System.out.println("");
        }
    }
}

๐Ÿ“– ๋ฌธ์ œ

1 2 3  
4 5 6
7 8 9

 

7 8 9
1 2 3
4 5 6

public class ShiftArray {
	
	public static void shiftTwoDArr(int[][] arr)
	{
		
	}
	
	public static void showTwoDArr(int[][] arr)
	{
		
	}

	public static void main(String[] args) {
		int[][] arr= {
				{1,2,3},
				{4,5,6},
				{7,8,9}
		};
		System.out.println("1์ฐจ shift...");
		shiftTwoDArr(arr);
		showTwoDArr(arr);
		System.out.println("2์ฐจ shift...");
		shiftTwoDArr(arr);
		showTwoDArr(arr);
		System.out.println("3์ฐจ shift...");
		shiftTwoDArr(arr);
		showTwoDArr(arr);
	}
}

โœ ๋‹ต์•ˆ ์ฝ”๋“œ 

 

public class ShiftArray {
    public static void shiftTwoDArr(int[][] arr) {
        int[] temp = arr[arr.length-1]; // ๋งˆ์ง€๋ง‰ ๋ฐฐ์—ด์— ๋Œ€ํ•œ ์ฃผ์†Œ ์ •๋ณด ๋‹ด๊ธฐ
        for(int i = arr.length-1; i > 0; i--) {
            arr[i] = arr[i-1];
        }
        arr[0] = temp;
    }

    public static void showTwoDArr(int[][] arr) {
        for(int i = 0 ; i < arr.length; i++) {
            for(int j = 0; j < arr[i].length; j++) {
                System.out.print(arr[i][j] + " ");
            }
            System.out.println();
        }
    }

    public static void main(String[] args) {
        int[][] arr= {
                {1,2,3},
                {4,5,6},
                {7,8,9}
        };
        System.out.println("1์ฐจ shift...");
        shiftTwoDArr(arr);
        showTwoDArr(arr);
        System.out.println("2์ฐจ shift...");
        shiftTwoDArr(arr);
        showTwoDArr(arr);
        System.out.println("3์ฐจ shift...");
        shiftTwoDArr(arr);
        showTwoDArr(arr);
    }
}

๐Ÿ“– ๋ฌธ์ œ

 

๋‹คํ˜•์„ฑ์œผ๋กœ ๊ฐ™์€ ๊ฒฐ๊ณผ๊ฐ€ ๋‚˜์˜ฌ ์ˆ˜ ์žˆ๊ฒŒ ๊ตฌํ˜„ํ•˜์‹œ์˜ค.

 

์ž…๋ ฅ

class Box
{
	public void simpleWrap(){System.out.println("simple wrap");}
}

class PaperBox extends Box
{
	public void paperWrap() {System.out.println("paper wrap");}
}

class GoldPaperBox extends PaperBox
{
	public void goldWrap() {System.out.println("gold wrap");}
}

class InstanceOf
{
	public static void wrapBox(Box box)
	{
		if(box instanceof GoldPaperBox)
			((GoldPaperBox)box).goldWrap();
		else if(box instanceof PaperBox)
			((PaperBox)box).paperWrap();
		else
			box.simpleWrap();
	}
	
	public static void main(String[] args)
	{
		Box box1=new Box();
		PaperBox box2=new PaperBox();
		GoldPaperBox box3=new GoldPaperBox();
		
		wrapBox(box1);
		wrapBox(box2);
		wrapBox(box3);
	}
}

์ถœ๋ ฅ

simple wrap
paper wrap
gold wrap

โœ ๋‹ต์•ˆ ์ฝ”๋“œ 

class Box {
    public void Wrap() {
        System.out.println("simple wrap");
    }
}

class PaperBox extends Box {
    public void Wrap() {
        System.out.println("paper wrap");
    }
}

class GoldPaperBox extends PaperBox {
    public void Wrap() {
        System.out.println("gold wrap");
    }
}

class InstanceOf {
    public static void wrapBox(Box box) {
      box.Wrap();
    }

    public static void main(String[] args) {
        Box box1=new Box();
        PaperBox box2=new PaperBox();
        GoldPaperBox box3=new GoldPaperBox();

        wrapBox(box1);
        wrapBox(box2);
        wrapBox(box3);
    }
}

๐Ÿ“– ๋ฌธ์ œ

ManageStudent ํด๋ž˜์Šค ์ƒ์„ฑ mainํ•จ์ˆ˜ ํฌํ•จ

๋”ฐ๋กœ Student ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“ค๊ณ  name, address, phone, email ํ•„๋“œ๋ฅผ ์ถ”๊ฐ€.
๋„ค ํ•„๋“œ๋ฅผ ์ž…๋ ฅ๊ฐ’์œผ๋กœ ์ƒ์„ฑ์ž ์ถ”๊ฐ€
๋„ค ํ•„๋“œ์˜ ๊ฐ’์ด ๊ฐ™์œผ๋ฉด ๊ฐ™์€ ๊ฐ์ฒด๋กœ ์ธ์‹๋˜๋„๋ก equals ๋ฉ”์†Œ๋“œ ์ถ”๊ฐ€.

ManageStudent ํด๋ž˜์Šค์˜ main์—์„œ 
Student a=new tudent("Min","Seoul","010XXXXXXXXX","ask@godofjava.com");
Student b=new tudent("Min","Seoul","010XXXXXXXXX","ask@godofjava.com");

๊ฐ์ฒด ์ƒ์„ฑํ›„ ์ž…๋ ฅํ•˜์—ฌ Equal ๋‹ค๋ฅด๋ฉด Not Equal ์„ ์ถœ๋ ฅํ•˜๋„๋ก ํ…Œ์ŠคํŠธ.

 

โœ ์ •๋‹ต ์ฝ”๋“œ 

package exercises;

class Student {
    String name, address, phone, email;
    public Student(String name,String address,String phone,String email) {
        this.name = name; this.address = address; this.phone = phone; this.email = email;
    }

    @Override
    public boolean equals(Object obj) {
        if (name == (((Student)obj).name) && address == (((Student)obj).address)
            && phone == (((Student)obj).phone) && email == (((Student)obj).email)) {
            return true;
        } else return false;
    }
}

public class ManageStudent {
    public static void main(String[] args) {
        Student a = new Student("Min", "Seoul", "010XXXXXXXXX", "ask@godofjava.com");
        Student b = new Student("Min", "Seoul", "010XXXXXXXXX", "ask@godofjava.com");

        if (a.equals(b)) System.out.println("equal");
        else System.out.println("not equal");
    }
}

๐Ÿ’ป ์ถœ๋ ฅ

equal

๐Ÿ“– ๋ฌธ์ œ

์‚ฌ์šฉ์ž๋กœ ๋ถ€ํ„ฐ ๋‘๊ฐœ์˜ ์ •์ˆ˜๋ฅผ ์ž…๋ ฅ๋ฐ›์•„ ๋‘ ์ˆ˜๋ฅผ ํฌํ•จํ•˜์—ฌ ๊ทธ ์‚ฌ์ด์˜ ์ •์ˆ˜ 100๊ฐœ๋ฅผ ์ž„์˜๋กœ ์ถœ๋ ฅํ•˜๋Š” 
๋ฉ”์†Œ๋“œ๋ฅผ ๋งŒ๋“ค๊ณ  ํ…Œ์ŠคํŠธํ•˜๊ธฐ.

import java.util.Random;
import java.util.Scanner;

public class Problem11 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("๋‘ ๊ฐœ์˜ ์ •์ˆ˜๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”");
        int num1 = sc.nextInt();
        int num2 = sc.nextInt();

        if (num1 < num2) {
            int temp = num1;
            num1 = num2;
            num2 = temp;
        }

        int length = num1 - num2 + 1;
        Random rand = new Random();
        for (int i = 0; i < 100; i++) {
            System.out.print("์ถœ๋ ฅ" + i + ":");
            System.out.println(rand.nextInt(length) + num2);
        }
    }
}