-
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, ์์ ๊ณต๋ถํ๊ธฐ
๋ฐ์ํ'JAVA' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
JAVA ์๋ฐ ์์, ์ ๊ทผ์ง์ ์ (0) 2020.08.06 JAVA ์๋ฐ ์ค๋ฒ๋ก๋ฉ, this, final, static, varags (0) 2020.08.04 JAVA ์๋ฐ ๊ฐ์ฒด, ํด๋์ค, ๋ฉ์๋, ์์ฑ์ (0) 2020.08.02 JAVA ์๋ฐ ๋ฐฐ์ด (0) 2020.07.31 JAVA ์๋ฐ ์กฐ๊ฑด๋ฌธ๊ณผ ๋ฐ๋ณต๋ฌธ (0) 2020.07.30