Programming/JAVA

[5์ฃผ์ฐจ] ๋™์  ๋ฐฐ์—ด๊ณผ ๊ฐ์ฒด ๋ฐฐ์—ด / ์ƒ์† / Rectangle ํด๋ž˜์Šค, OddEven ํด๋ž˜์Šค

mnzy๐ŸŒฑ 2022. 4. 3. 22:35

0. ๋ณต์Šต

(1) (์ •์ )๋ฐฐ์—ด

- ๋ฐฐ์—ด์˜ ์„ ์–ธ (๋ฐฐ์—ด ๋ณ€์ˆ˜์˜ ์„ ์–ธ)  // ๋ฐฐ์—ด์„ ๊ฐ€๋ฅดํ‚ฌ ๋ณ€์ˆ˜ ์„ ์–ธ

-> int[] scores;

-> int scores[];

-> int scores[] = new int[4];    // ๋‚˜์ค‘์— ๊ฐ ์š”์†Œ์— ๊ฐ’ ๋„ฃ์–ด์ฃผ๊ธฐ

-> int scores[4]; (X)    // ๋ฐฐ์—ด์˜ ํฌ๊ธฐ๋Š” ๋ฐฐ์—ด ๊ฐ์ฒด๊ฐ€ ์ƒ์„ฑ๋  ๋•Œ ์ง€์ •ํ•  ์ˆ˜ ์žˆ๋‹ค. ์„ ์–ธํ•  ๋•Œ๋Š” X

 

 

1. ๋™์  ๋ฐฐ์—ด๊ณผ ๊ฐ์ฒด ๋ฐฐ์—ด

(1) ๋™์  ๋ฐฐ์—ด 

- ArrayList ํด๋ž˜์Šค ์ œ๊ณต

-> ArrayList  ๊ฐ์ฒด ์ƒ์„ฑ

: ArrayList<์ฐธ์กฐํƒ€์ž…> ์ฐธ์กฐ๋ณ€์ˆ˜ = new ArrayList<>();

// ๊ธฐ์ดˆํƒ€์ž…: Integer, Long, Short, Float, Double ๋“ฑ ์‚ฌ์šฉ 

// int ํƒ€์ž…์€ Integer ์‚ฌ์šฉ 

*์•ž๊ธ€์ž๊ฐ€ ๋Œ€๋ฌธ์ž์ž„! (ํด๋ž˜์Šค์ด๊ธฐ ๋•Œ๋ฌธ)

 

 

(2) ์›์†Œ ์ ‘๊ทผ //ArrayList ํด๋ž˜์Šค์— ํฌํ•จ๋˜๋Š” ๋ฉ”์„œ๋“œ

- ์ฐธ์กฐ๋ณ€์ˆ˜.add(๋ฐ์ดํ„ฐ) : ์ˆœ์„œ๋Œ€๋กœ ๋ฆฌ์ŠคํŠธ๋ฅผ ์ถ”๊ฐ€, ๋ฐฐ์—ด ์‚ฌ์ด์ฆˆ ์ดˆ๊ณผ ์‹œ ์ดˆ๊ธฐ ์„ค์ •๋œ ์‚ฌ์ด์ฆˆ๋งŒํผ ์ž๋™์œผ๋กœ ์‚ฌ์ด์ฆˆ๊ฐ€ ์ฆ๊ฐ€ํ•จ, ์ธ๋ฑ์Šค๋ฅผ ์ถ”๊ฐ€๋กœ ์ง€์ •ํ•ด์ฃผ๋ฉด ํ•ด๋‹น ์ธ๋ฑ์Šค์— ๊ฐ’์„ ์‚ฝ์ž…   //add((index), val)

- ์ฐธ์กฐ๋ณ€์ˆ˜.remove(์ธ๋ฑ์Šค ๋ฒˆํ˜ธ or val) : ํ•ด๋‹น ์ธ๋ฑ์Šค์˜ ๊ฐ’ or ํ•ด๋‹น ๊ฐ’ ์ค‘ ์ฒซ๋ฒˆ์งธ ๊ฐ’ ์‚ญ์ œ

- ์ฐธ์กฐ๋ณ€์ˆ˜.get(์ธ๋ฑ์Šค ๋ฒˆํ˜ธ) : ํ•ด๋‹น ์ธ๋ฑ์Šค์˜ ๊ฐ’ ๋ฐ˜ํ™˜

- ์ฐธ์กฐ๋ณ€์ˆ˜.size() : ์š”์†Œ ๊ฐœ์ˆ˜ ๋ฐ˜ํ™˜

import java.util.ArrayList; //๋™์  ๋ฐฐ์—ด ํด๋ž˜์Šค ArrayList importํ•ด์คŒ
import java.util.Scanner;

public class ArrayListDemo {
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		ArrayList<Integer> scores = new ArrayList<>();
		int data;
		int sum = 0;

		while ((data = in.nextInt()) >= 0)
			scores.add(data);

		for (int i = 0; i < scores.size(); i++)
			sum += scores.get(i);

		System.out.println("ํ‰๊ท  = " + sum / scores.size());
	}
}

-> import java.util.ArrayList    // ArrayListํด๋ž˜์Šค ํฌํ•จํ•ด์คŒ

-> ArrayList<Integer> scores = new ArrayList<>();

// ํƒ€์ž…์ด intํ˜•์ธ scores๋ผ๋Š” ๋™์ ํ• ๋‹น ๋ฐฐ์—ด ์„ ์–ธ (scores๋ผ๋Š” ๋ณ€์ˆ˜๊ฐ€ ๊ฐ€๋ฅดํ‚ค๋Š” ๋ฐฐ์—ด)

-> data = in.nextInt()    // ์ •์ˆ˜(int)๋ฅผ ์ž…๋ ฅ๋ฐ›์•„ data์— ์ €์žฅ 

-> scores.add(data)   // ์ˆœ์„œ๋Œ€๋กœ data๊ฐ’์„ scores์— ์ €์žฅ ( 

-> scores.size()    // ๋ฐฐ์—ด scores์˜ ํฌ๊ธฐ ๋ฐ˜ํ™˜

-> scores.get(i)    // [i]๋ฒˆ์งธ ๊ฐ’ ๋ฐ˜ํ™˜

 

 

(3) ๊ฐ์ฒด ๋ฐฐ์—ด 

- ๊ฐ์ฒด ๋ฐฐ์—ด์€ ๊ฐ์ฒด๋ฅผ ์ฐธ์กฐํ•˜๋Š” ์ฃผ์†Œ๋ฅผ ์›์†Œ๋กœ ๊ตฌ์„ฑ 

// ์›์†Œ๊ฐ’ = ๊ฐ์ฒด์˜ ์ฃผ์†Œ 

-> Ball[] balls = new Ball[5];

// 5๊ฐœ์˜ Ball ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ 5๊ฐœ์˜ Ball ๊ฐ์ฒด๋ฅผ ์ฐธ์กฐํ•  ๋ณ€์ˆ˜๋ฅผ ์ค€๋น„ 

// ์ƒ์„ฑ์ž๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ Ball ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•ด์•ผ ํ•จ

 

class Circle {
	double radius;

	public Circle(double radius) {
		this.radius = radius;
	}

	public double getRadius() {
		return radius;
	}

	double findArea() {
		return 3.14 * radius * radius;
	}
}

public class CircleArrayDemo {
	public static void main(String[] args) {
		Circle[] circles = new Circle[5];

		for (int i = 0; i < circles.length; i++) {
			circles[i] = new Circle(i + 1.0);
			System.out.printf("์›์˜ ๋„“์ด(๋ฐ˜์ง€๋ฆ„ : %.1f) = %.2f\n", circles[i].radius, circles[i].findArea());
		}
	}
}

-> Circle[] circles = new Circle[5]

// ๋ฐฐ์—ด ๊ณต๊ฐ„ ์„ ์–ธ (ํฌ๊ธฐ = 5)

-> circles[i] = new Circle(i + 1.0); 

// ๊ฐ ์›์†Œ์— ์ฃผ์†Œ๊ฐ’ ์ €์žฅ 

// ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•ด์„œ ์›์†Œ๊ฐ’์— ์ €์žฅ(๊ฐ€๋ฅดํ‚ค๋„๋ก ํ•จ)

 

 

(4) ๊ฐ์ฒด ์ธ์ˆ˜์™€ ๊ธฐ์ดˆํƒ€์ž… ์ธ์ˆ˜   

// ๊ฐ์ฒด = ์ฐธ์กฐํƒ€์ž… 

public class ObjectArgumentDemo {
	public static void main(String[] args) {
		Circle c1 = new Circle(10.0);
		Circle c2 = new Circle(10.0);

		zero(c1);	
		System.out.println("์›(c1)์˜ ๋ฐ˜์ง€๋ฆ„ : " + c1.radius);

		zero(c2.radius);
		System.out.println("์›(c2)์˜ ๋ฐ˜์ง€๋ฆ„ : " + c2.radius);
	}

	public static void zero(Circle c) {		//(a)
		c.radius = 0.0;
	}

	public static void zero(double r) {		//(b)
		r = 0.0;
	}
}

-> zero(c1)     // ์ƒ์„ฑ์ž (a) ํ˜ธ์ถœ

-> c1.radius = 0.0   // 10.0 -> 0.0

// ์ฐธ์กฐํƒ€์ž…(call by reference)

-> zero (c2.radius)    // ์ƒ์„ฑ์ž (b) ํ˜ธ์ถœ

-> c2.radius     // 10.0 -> 10.0 (radius๊ฐ’์„ ๋ณต์‚ฌํ•œ r๊ฐ’ = 0.0, but ์ƒ์„ฑ์ž ๋๋‚˜๋ฉด r๋„ ํ•จ๊ป˜ ์†Œ๋ฉธ)

// ๊ฐ™์€ ์ด๋ฆ„์˜ ์ƒ์„ฑ์ž(but ๋งค๊ฐœ๋ณ€์ˆ˜ ํƒ€์ž… ๋‹ค๋ฆ„) = ๋ฉ”์„œ๋“œ ์˜ค๋ฒ„๋กœ๋”ฉ

 

 

2. ์ƒ์†

- ์ƒ์†: ์ž๋…€๊ฐ€ ๋ถ€๋ชจ ์žฌ์‚ฐ์„ ์ƒ์†๋ฐ›์•„ ์‚ฌ์šฉํ•˜๋“ฏ์ด ์ƒ์œ„ ๊ฐ์ฒด๋ฅผ ์ƒ์†๋ฐ›์€ ํ•˜์œ„ ๊ฐ์ฒด๊ฐ€ ์ƒ์œ„ ๊ฐ์ฒด์˜ ๋ฉ”์„œ๋“œ์™€ ํ•„๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ

- ๋ถ€๋ชจ ํด๋ž˜์Šค (์Šˆํผ ํด๋ž˜์Šค, ๊ธฐ๋ณธ ํด๋ž˜์Šค)

- ์ž์‹ ํด๋ž˜์Šค (์„œ๋ธŒ ํด๋ž˜์Šค, ํŒŒ์ƒ ํด๋ž˜์‹€, ํ™•์žฅ ํด๋ž˜์Šค)

- ์ž์‹ ํด๋ž˜์Šค๋Š” ๋ถ€๋ชจ ํด๋ž˜์Šค์—์„œ ๋ฌผ๋ ค๋ฐ›์€ ๋ฉค๋ฒ„๋ฅผ ๊ทธ๋Œ€๋กœ ์‚ฌ์šฉํ•˜๊ฑฐ๋‚˜ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ๊ณ , ์ƒˆ๋กœ์šด ๋ฉค๋ฒ„๋„ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ์Œ 

 

(1) ์ƒ์†์˜ ์„ ์–ธ

- extends ํ‚ค์›Œ๋“œ ์‚ฌ์šฉ 

-> ๋ถ€๋ชจ ํด๋ž˜์Šค ์„ ์–ธ: class ๋ถ€๋ชจํด๋ž˜์Šค์ด๋ฆ„ {..}    // ๊ทธ๋ƒฅ ๊ณ„์† ํ•ด์™”๋˜ ํด๋ž˜์Šค ์„ ์–ธ์ž„ 

-> ์ž์‹ ํด๋ž˜์Šค ์„ ์–ธ: class ์ž์‹ํด๋ž˜์Šค์ด๋ฆ„ extends ๋ถ€๋ชจํด๋ž˜์Šค์ด๋ฆ„ {..}

* ๋‹ค์ค‘ ์ƒ์†์€ ์•ˆ๋จ

 

(2)

public class Circle {
	private void secret() {
		System.out.println("๋น„๋ฐ€์ด๋‹ค.");
	}

	protected void findRadius() {
		System.out.println("๋ฐ˜์ง€๋ฆ„์ด 10.0์„ผํ‹ฐ์ด๋‹ค.");
	}

	public void findArea() {
		System.out.println("๋„“์ด๋Š” (π*๋ฐ˜์ง€๋ฆ„*๋ฐ˜์ง€๋ฆ„)์ด๋‹ค.");
	}
}
public class Ball extends Circle {
	private String color;

	public Ball(String color) {
		this.color = color;
	}

	public void findColor() {
		System.out.println(color + " ๊ณต์ด๋‹ค.");
	}

	public void findVolume() {
		System.out.println("๋ถ€ํ”ผ๋Š” 4/3*(π*๋ฐ˜์ง€๋ฆ„*๋ฐ˜์ง€๋ฆ„*๋ฐ˜์ง€๋ฆ„)์ด๋‹ค.");
	}
}

-> public class Circle    // ๋ถ€๋ชจ ํด๋ž˜์Šค 

// secret() : private - ํ•ด๋‹น ํด๋ž˜์Šค ๋‚ด๋ถ€๋งŒ ์ ‘๊ทผ ํ—ˆ์šฉ

// findRadius() : protected - ๋ถ€๋ชจ&์ž์‹ ํด๋ž˜์Šค๋งŒ ์ ‘๊ทผ ํ—ˆ์šฉ

// findArea() : public - ์–ด๋””์—์„œ๋‚˜ ์ ‘๊ทผ ํ—ˆ์šฉ

-> public class Ball extends Circle    // ์ž์‹ ํด๋ž˜์Šค

// ๋ถ€๋ชจ ํด๋ž˜์Šค์˜ ๋ฉ”์„œ๋“œ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์Œ

// Ball(String color) : ์ƒ์„ฑ์ž

// findColor() : ์ž์‹ ํด๋ž˜์Šค์—์„œ ์ถ”๊ฐ€ํ•œ ๋ฉ”์†Œ๋“œ

// findVolume() : ์ž์‹ ํด๋ž˜์Šค์—์„œ ์ถ”๊ฐ€ํ•œ ๋ฉ”์†Œ๋“œ

public class InheritanceDemo {
	public static void main(String[] args) {
		Circle c1 = new Circle();
		Ball c2 = new Ball("๋นจ๊ฐ„์ƒ‰");

		System.out.println("์› :");
		c1.findRadius();
		c1.findArea();

		System.out.println("\n๊ณต :");
		c2.findRadius();
		c2.findColor();
		c2.findArea();
		c2.findVolume();
	}
}

-> Circle c1 = new Circle();

// ๋ถ€๋ชจ ํด๋ž˜์Šค Circle์˜ ๊ฐ์ฒด c1 ์ƒ์„ฑ

-> c1.findRadius() = "๋ฐ˜์ง€๋ฆ„์ด 10.0์„ผํ‹ฐ์ด๋‹ค."

-> c1.findArea() = "๋„“์ด๋Š” (π*๋ฐ˜์ง€๋ฆ„*๋ฐ˜์ง€๋ฆ„)์ด๋‹ค."

 

-> Ball c2 = new Ball("๋นจ๊ฐ„์ƒ‰")

// Circle์˜ ์ž์‹ ํด๋ž˜์Šค Ball์˜ ๊ฐ์ฒด c2 ์ƒ์„ฑ

-> c2.findRadius()     // ๋ถ€๋ชจ ํด๋ž˜์Šค์˜ ๋ฉ”์†Œ๋“œ

= "๋ฐ˜์ง€๋ฆ„์ด 10.0์„ผํ‹ฐ์ด๋‹ค."

-> c2.findColor()      // ์ž์‹ ํด๋ž˜์Šค์—์„œ ์ถ”๊ฐ€ํ•œ ๋ฉ”์†Œ๋“œ

= "๋นจ๊ฐ„์ƒ‰ ๊ณต์ด๋‹ค"

-> c2.findArea()       // ๋ถ€๋ชจ ํด๋ž˜์Šค์˜ ๋ฉ”์†Œ๋“œ

= "๋„“์ด๋Š” (π*๋ฐ˜์ง€๋ฆ„*๋ฐ˜์ง€๋ฆ„)์ด๋‹ค."

-> c2.findVolume()     // ์ž์‹ ํด๋ž˜์Šค์—์„œ ์ถ”๊ฐ€ํ•œ ๋ฉ”์†Œ๋“œ

= "๋ถ€ํ”ผ๋Š” 4/3*(π*๋ฐ˜์ง€๋ฆ„*๋ฐ˜์ง€๋ฆ„*๋ฐ˜์ง€๋ฆ„)์ด๋‹ค."

 

 

3. Rectangle ํด๋ž˜์Šค, OddEven ํด๋ž˜์Šค

import java.util.Scanner;
public class Rectangle {
	public static void main(String[] args) {
		System.out.print("์ง์‚ฌํ˜•์˜ ๊ฐ€๋กœ ๊ธธ์ด๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š” : ");
		Scanner in = new Scanner(System.in);
		int width = in.nextInt();
        
		System.out.print("์ง์‚ฌํ˜•์˜ ์„ธ๋กœ ๊ธธ์ด๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š” : ");
        
		int height = in.nextInt();
		
        	System.out.print("์ง์‚ฌ๊ฐํ˜•์˜ ๋„“์ด๋Š” " + (double)width*height +"์ž…๋‹ˆ๋‹ค.");
		
       	 	in.close();
	}
}
import java.util.Scanner;
public class OddEven {
	public static void main(String[] args) {
		System.out.print("์ •์ˆ˜๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š” : ");
		Scanner in = new Scanner(System.in);
            	int num = in.nextInt();
		System.out.println( (num%2 == 0? "์ง์ˆ˜" : "ํ™€์ˆ˜") );
	}
}