반응형
Notice
Recent Posts
Recent Comments
Link
«   2025/09   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30
Tags
more
Archives
Today
Total
관리 메뉴

비전공개미 개발노트

Java 4일차 문제풀이 - 배열, static, 기본생성자, 지역변수, 전역변수, 가변매개변수 본문

프로그래밍/Java

Java 4일차 문제풀이 - 배열, static, 기본생성자, 지역변수, 전역변수, 가변매개변수

비전공개미 2022. 9. 21. 17:13
반응형
SMALL
//Test179.java //179페이지
package net.bit.day21;

public class Test179 {
	public static void main(String[] args) {
		boolean[] flagA = new boolean[3];
		flagA[0] = true;
		flagA[1] = false;
		flagA[2] = true;
		
		//boolean[] flagB = false; //오류 배열값이기때문에 배열에 넣어줘야함
		boolean[] flagB = {false, true, false};
		boolean[] flagC = new boolean[]{false, true, false};
		
		//181페이지 다차원배열 [][]
		int[][] scoresA = {{3, 5, 6}, {7, 9, 8}, {2, 3, 4}, {4, 6, 5}, {1, 8, 9}};
		int[][] scoresB = new int[5][3];
	}
}

 

 

//Test185.java //185페이지
package net.bit.day21;

public class Test185 {
	public static void main(String[] args) {
		boolean[] flagA = new boolean[3];
		flagA[0] = true;
		flagA[1] = false;
		flagA[2] = true;
		
//		for(int i=0; i<flagA.length; i++) {
//			System.out.println(flagA[i]);
//		}
		for(boolean temp : flagA) { //flagA배열의 값을 그대로 temp에게 넘긴다
			System.out.println(temp);
		}
		
		System.out.println();
		String[] dong = new String[3];
		dong[0]="시청";
		dong[1]="강남";
		dong[2]="분당";
		
//		for(int i=0; i<dong.length; i++) {
//			System.out.println(dong[i]);
//		}
		for(String temp : dong) { //dong배열의 값을 그대로 temp에게 넘긴다
			System.out.println(temp);
		}
		
	}
}
[출력]
true
false
true

시청
강남
분당

 

 

 

//Gugudan.java
package net.bit.common;
import java.util.Scanner;

public class Gugudan {
	public static void main(String[] args) {
		 Gugudan gg = new Gugudan();
		 int ret = gg.input();
		 gg.output(ret);
	}//main end
	
	public int input() { 
		System.out.println("input함수");
		Scanner sc = new Scanner(System.in) ;
	  System.out.print("단 입력>>> ");
		int dan = sc.nextInt() ;
		sc.close();
		return dan;
	}//end
	
	public void output(int dan) {
		System.out.println("\noutput함수");
		for(int i=1; i<10; i++) {
			System.out.println(dan+"*"+i+"="+(dan*i));
		}
	}//end
		
	
	
	//09-21-수요일
	public int[] myinput() { //리턴값배열 int[], non-static
		int x=7, y=8, z=9, a=1, b=2, c=3;
		int[] su = {7, 8, 9, 1, 2, 3};
		//return x, y, z, a, b, c; //오류 - 리턴값은 하나만 던져줄 수 있다.
		return su;
	}
	
	public void myoutput(int[] data) { //매개인자배열, non-static
		for(int temp : data) {
			System.out.println(temp);
		}
	}
}
//First.java
package net.bit.day21;

import net.bit.common.Gugudan; //다른패키지에 있을경우 import해줘야 한다.

public class First {
	public static void main(String[] args) {
		//189페이지 그림참고
		String owner = args[0]; //우측버튼 - Run As - Run Configuration - Arguments에 입력
		String age = args[1];
		System.out.println(owner);
		System.out.println(age);
		System.out.println("11:21 11:24");
		//main의 args를 활용할일이 없다...
		
		
		//Gugudan.java의 함수를 호출한다.
		Gugudan ob = new Gugudan();
		int[] val = ob.myinput();
		ob.myoutput(val);
		System.out.println();
		
		//구구단호출
		int gg = ob.input();
		ob.output(gg);
	}
}
[출력]
제갈량
56
11:21 11:24
7
8
9
1
2
3

[입력]
input함수
단 입력>>> 5

[출력]
output함수
5*1=5
5*2=10
5*3=15
5*4=20
5*5=25
5*6=30
5*7=35
5*8=40
5*9=45

 

 

 

//Test1.java  //190페이지 가변개수매개변수
package net.bit.day21;

public class Test1 {
	public static void main(String[] args) {
		disp(7, 8);
		disp(7, 8, 9);
		disp(7, 8, 9, 5);
		disp(7, 8, 9, 1, 2);

	}
	
	//함수이름동일 = 오버로딩 = OverLoading //갯수나 타입이 달라야한다
	public static void disp(int...value) {  } //가변매개변수인자
	//public static void disp(int a, int b) {System.out.println(a + " " + b); }
	//public static void disp(int a, int b, int c) {System.out.println(a+" "+b+" "+c);}
	//public static void disp(int a, int b, int c, int d) {	System.out.println(d); }
	//public static void disp(int a, int b, int c, int d, int e) { }
	
}

 

 

//Game.java
package net.bit.day21;

public class Game {
	private int count; //필드 = 전역변수
	private String kind; //필드 = 전역변수
	
	public Game() { 
		//146페이지 생성자설명 참고
		//기본생성자는 생략가능, 사용 => new키워드 다음에 기술
		//생성자는 클래스이름 동일, 생성자는 리턴값이 없다. void기술안함
		//생성자는 호출 객체화 할때 new키워드 다음에 기술
		//constructor = 생성자 중복가능
//		count = 5;
//		kind = "얍카";
		System.out.println(count); //직접 초기화 하지않아도 자동으로 초기화됨.
		System.out.println(kind);
		System.out.println();
	}
	
	public Game(String company) {
		System.out.println(company);
	}
	
	public Game(int year, String company) {
		System.out.println(year + "년 출시, 제작사 : " + company);
	}
	
	//호출에서만 함수를 사용한다면 main함수도 필요없음 / main단독실행///
	public static void main(String[] args) {
		Game.disp(); //static은 바로접근가능
		Game gf = new Game(); //non-static은 불러줘야 접근가능
		gf.golf();
	}
	////////////////////////////////////////////////////////
	
	public void golf() { //리턴값x, 매개인자x, non-static=일반함수
		//static키워드가 없으면 외부에서 클래스객체 = new 클래스(); 객체.golf();
		System.out.println("golf메소드");
	}
	
	public static void disp() { //리턴값x, 매개인자x, static=정적함수=클래스합수
		//static키워드가 있으면 외부에서 접근시 클래스이름. 메소드이름으로 접근가능 ex)Game.disp()
		String title = "수요일";
		
	}
	
}
[출력]
0
null

golf메소드

 

 

//GameTest.java
package net.bit.day21;

public class GameTest {
	public static void main(String[] args) {
//		Game gf = new Game();
//		Game gf = new Game("비트컴제작"); //non-static은 불러줘야 접근가능
		Game gf = new Game(2021, "비트컴제작");
		//gf=인스턴스=instance
		gf.golf();
	}
}
[출력]
2021년 출시, 제작사 : 비트컴제작
golf메소드

 

 

//AWS.java
package net.bit.day21;

public class AWS {
	String pwd = "7890"; //전역변수
	String order = "쇼핑몰";
	
	public AWS() { System.out.println("AWS의 기본생성자"); } //기본생성자기술
	
	//기본생성자 본인을 불러온다
	public AWS(String ama) { this(); } //매개인자가 있는 생성자
	
	public static void main(String[] args) {
		AWS aws = new AWS();
		aws.note();
		
	}
	
	public void note() { //리턴값x, 매개인자x, non-static=일반함수
		String pwd = "1234"; //지역변수
		System.out.println("지역변수 pwd = " + pwd);
		System.out.println("전역변수 pwd = " + this.pwd);
		//this키워드는 static키워드에서 사용하면 오류발생
	}
}


//문제1 기본생성자기술
//문제2 main함수에서 note함수를 호출
[출력]
AWS의 기본생성자
지역변수 pwd = 1234
전역변수 pwd = 7890

 

 

//Board.java
package net.bit.day21;

public class Board {
	String empNo;
	String eName;
	int sal;
	double comm;
	int deptNo;
	java.util.Date hiredate;
	//getter값가져오기 / setter변경 = Data Transfer Object = Value Object
	//우측클릭 - source - Generate Getters and Setters
	public String getEmpNo() {
		return empNo;
	}
	public void setEmpNo(String empNo) {
		this.empNo = empNo;
	}
	public String geteName() {
		return eName;
	}
	public void seteName(String eName) {
		this.eName = eName;
	}
	public int getSal() {
		return sal;
	}
	public void setSal(int sal) {
		this.sal = sal;
	}
	public double getComm() {
		return comm;
	}
	public void setComm(double comm) {
		this.comm = comm;
	}
	public int getDeptNo() {
		return deptNo;
	}
	public void setDeptNo(int deptNo) {
		this.deptNo = deptNo;
	}
	public java.util.Date getHiredate() {
		return hiredate;
	}
	public void setHiredate(java.util.Date hiredate) {
		this.hiredate = hiredate;
	}
	
}

 

 

반응형
LIST
Comments