Back-End/JAVA

[Java μ£Όμš” 문제 풀이 (1)] - λ°°μ—΄, μƒμ„±μž, λ©”μ„œλ“œ

09009 2023. 4. 6. 16:33

πŸ“– λ¬Έμ œ

λ°‘λ³€κ³Ό 높이 정보λ₯Ό μ €μž₯ν•  수 μžˆλŠ” 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);
        }
    }
}