ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • JAVA ์ž๋ฐ” ํด๋ž˜์Šค ๋ชจ๋“ˆํ™” ์—ฐ์Šตํ•˜๊ธฐ
    JAVA 2020. 8. 3. 23:41

    ๐ŸŽฏ ์ž๋ฐ” ๋ฐฐ์—ด์„ ํ™œ์šฉํ•ด ํด๋ž˜์Šค ์—ฐ์Šต๋ฌธ์ œ๋ฅผ ํ’€์–ด๋ณธ๋‹ค

     

     

     

    ์ง€์˜ฅ์˜ ํด๋ž˜์Šค ์ชผ๊ฐœ๊ธฐ ์—ฐ์Šต

     

    1. ๋‹ค์Œ ๋ฐฐ์—ด์˜ ํ•ฉ๊ณผ ํ‰๊ท ์„ ๊ตฌํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑ ํ•˜์‹œ์˜ค.

    int[] arr = {10, 20, 30, 40, 50};

    ์ถœ๋ ฅ๊ฒฐ๊ณผ : sum=150 avg=30

    public class ArrayTest01 {
    
    	public static void main(String[] args) {
    		
    		int[] arr = {10, 20, 30, 40, 50};
    		
    		int sum = 0;
    		for (int i = 0; i<arr.length; i++) {
    			sum += arr[i];
    		}
    		System.out.println(sum/arr.length);
    	}
    
    }

     

     

    1-1. ๋ฐ˜๋ณต๋ฌธ์„ ํ•˜๋‚˜์˜ ํด๋ž˜์Šค๋กœ ๋ชจ๋“ˆํ™”ํ•˜๊ธฐ

    public class printArr {
    	
    	public int print(int[] arr) {
    		int sum = 0;
    		for (int i = 0; i<arr.length; i++) {
    			sum += arr[i]; //์ดํ•ฉ์ฒ˜๋ฆฌ
    		}
    		return sum;
    	}
    
    }
    public class Arr_Test {
    
    	public static void main(String[] args) {
    		int[] arr = {10, 20, 30, 40, 50};
    		int sum = 0;
    		
    		printArr test = new printArr();
    		sum = test.print(arr); // ํ•จ์ˆ˜ํ˜ธ์ถœ, ํ•ฉ ๊ณ„์‚ฐ ๋ฐ˜ํ™˜
    		System.out.println("sum = "+ sum);
    		System.out.println("avg = "+sum/arr.length);
    
    	}
    
    }

     

     

    1-2. printArr ํด๋ž˜์Šค์— ํ•ฉ๊ณ„์™€ ํ‰๊ท ๊ตฌํ•˜๊ธฐ ๋‘˜๋‹ค ์ ์–ด๋ณด๊ธฐ

    public class printArr {
    
    	private double avg;
    	int sum;
    
    	public int print(int[] arr) {
    		for (int i = 0; i < arr.length; i++) {
    			sum += arr[i]; // ์ดํ•ฉ์ฒ˜๋ฆฌ
    		}
    		avg = sum / arr.length;
    		return sum;
    	}
    
    	public double getAvg() {
    		return avg;
    	}
    
    }
    public class Arr_Test {
    
    	public static void main(String[] args) {
    		int[] arr = {10, 20, 30, 40, 50};
    		int sum = 0;
    		printArr test = new printArr();
    		sum = test.print(arr); // ํ•จ์ˆ˜ํ˜ธ์ถœ, ํ•ฉ ๊ณ„์‚ฐ ๋ฐ˜ํ™˜
    		System.out.println("sum = "+ sum);
    		System.out.println("avg = "+test.getAvg());
    	}
    
    }

     

     

    1-3. avgํด๋ž˜์Šค๋ฅผ ์ƒˆ๋กœ ๋งŒ๋“ค์–ด์„œ 3๊ฐœ์˜ ํด๋ž˜์Šค๋ฅผ ๋ชจ๋“ˆํ™”ํ•˜๊ธฐ

    // sum
    
    public class printArr {
    
    	int sum;
    	
    	public int print(int arr[]) {
    		int sum = 0;
    		for (int i = 0; i < arr.length; i++) {
    			sum += arr[i];
    		}
    		return sum;
    	}
    	
    }
    // avg
    
    public class Arr_Avg {
    
    	public double calc_Avg(int sum, int length) {
    		return sum/length;
    	}
    	
    }
    // main
    
    public class Arr_Test {
    
    	public static void main(String[] args) {
    	
    		int[] arr = {10, 20, 30, 40, 50};
    		int sum = 0;
    		printArr test = new printArr();
    		sum = test.print(arr);
    		Arr_Avg avgTest.calc_Avg(sum, arr.length);
    		System.out.println("์ดํ•ฉ์€="+sum);
    		System.out.println("ํ‰๊ท ์€="+avg);
    	}
    
    }

     

     

    1-4. ๋ฐฐ์—ด์„ main๋ฉ”์†Œ๋“œ ๋ฐ–์œผ๋กœ ์˜ฌ๋ ค์„œ ์ธ์Šคํ„ด์Šค ๋ณ€์ˆ˜๋ฅผ ๋งŒ๋“ค์–ด๋ณด์ž.

    public class Arr_Test {
    
    	int[] arr = {10, 20, 30, 40, 50}; //๋ฉค๋ฒ„๋ณ€์ˆ˜
        
    	public static void main(String[] args) {
    		Arr_Test arrTest = new Arr_Test();//๋ฉค๋ฒ„์‚ฌ์šฉ์„ ์œ„ํ•ด ๊ฐ์ฒด์ƒ์„ฑ
    		int[] arr = arrTest.arr; //๊ฐ์ฒด์˜ ๋ฐฐ์—ด์„ ์ €์žฅ
    
    		int sum = 0;
    		printArr test = new printArr();
    		sum = test.print(arr);
    		Arr_Avg avgTest = new Arr_Avg();
    		double avg = avgTest.calc_Avg(sum, arr.length);
    		System.out.println("์ดํ•ฉ์€="+sum);
    		System.out.println("ํ‰๊ท ์€="+avg);
    	}
    
    }

    ๋ฐฐ์—ด์ด ์ธ์Šคํ„ด์Šค๋ณ€์ˆ˜๊ฐ€ ๋˜๋‹ˆ๊นŒ ๋ฉ”์ธํ•จ์ˆ˜ ์•ˆ์— ๊ฐ์ฒด์ƒ์„ฑ์„ ํ•ด์•ผํ•œ๋‹ค. ๋ฐฐ์—ด ์ƒ์„ฑ ๋ฐ ์ดˆ๊ธฐํ™”๋กœ ์ € ๊ฐ์ฒด์˜ arr๋ณ€์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๊ฒ ๋‹ค๊ณ  ์„ ์–ธํ•œ๋‹ค. ์ด ๋‘์ค„๋งŒ ์ถ”๊ฐ€ํ•˜๋ฉด ๋ฉค๋ฒ„๋ณ€์ˆ˜ ๋ฐฐ์—ด์„ ๋ฉ”์ธ์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

     

     

    1-5. ๋ฐฐ์—ด ํด๋ž˜์Šค ์ƒˆ๋กœ ๋งŒ๋“ค๊ธฐ

    public class CreateArr {
    	
    	public int[] makearr(){
    	int[] arr = {10, 20, 30, 40, 50}; //๋ฉค๋ฒ„๋ณ€์ˆ˜
    	return arr;
    	}
    	
    }
    public class Arr_Test {
    		
    	public static void main(String[] args) {
    		
    		int [] arr = new CreateArr().makearr();
    		int sum = 0;
    		
    		sum = new printArr().print(arr);
    		Arr_Avg avgTest = new Arr_Avg();
    		double avg = avgTest.calc_Avg(sum, arr.length);
    		System.out.println("์ดํ•ฉ์€="+sum);
    		System.out.println("ํ‰๊ท ์€="+avg);
    	}
    
    }
    //๋ฆฌํŒฉํ† ๋ง
    
    public class Arr_Test {
    		
    	public static void main(String[] args) {
    		//๋ฉค๋ฒ„์‚ฌ์šฉ์„ ์œ„ํ•ด ๊ฐ์ฒด์ƒ์„ฑ
    		int [] arr = new CreateArr().makearr();
    		int sum = 0;
    	
    		sum = new printArr().print(arr);
    		
    		double avg = new Arr_Avg().calc_Avg(sum, arr.length);
    		System.out.println("์ดํ•ฉ์€="+sum);
    		System.out.println("ํ‰๊ท ์€="+avg);
    	}
    
    }

     

     

     

     

    ๐Ÿ˜œ NEXT : ์ž๋ฐ” ์ƒ์„ฑ์ž ์˜ค๋ฒ„๋กœ๋”ฉ, static, ์ƒ์† ๊ณต๋ถ€ํ•˜๊ธฐ

    ๋ฐ˜์‘ํ˜•

    ๋Œ“๊ธ€

๊ฐœ๋ฐœ๊ณต๋ถ€