국비/Java

[09-2] 실습문제 (클래스다이어그램, 배열)

박머루 2022. 4. 19. 20:31
문제1) 클래스다이어그램 보고 클래스 만들기
public class Student {

	private static int grade; // static 빼먹음!!!!
	private int classroom;
	private String name;
	private double height;
	private char gender;
	public String information;
	
	{ // this. 넣어주기
		this.grade = 1;
		this.classroom = 2;
		this.name = "박머루";
		this.height = 157.5;		
		this.gender = 'F';
	}
	
	
	public Student (){}	
	
	
	public void setGrade(int grade) {
		this.grade = grade;
	}
	public void setClassroom(int classroom) {
		this.classroom = classroom;
	}
	public void setName(String name) {
		this.name = name;
	}
	public void setHeight(double height) {
		this.height = height;
	}
	public void setGender(char gender) {
		this.gender = gender;
	}

	
	public int getGrade() {
		return this.grade;
	}
	public int getClassroom() {
		return this.classroom;
	}
	public String getName() {
		return this.name;
	}
	public double getHeight() {
		return this.height;
	}
	public char getGender() {
		return this.gender;
	}
	
	
	public void information() {
		System.out.println("학년 : " + this.grade + "\n반 : " + this.classroom + "\n이름 : " + this.name
				+ "\n키 : " + this.height + "\n성별 : " + this.gender);
	}
}

--------------------------------------------------------------------------------------------
public class Run {
	public static void main(String[] args) {
	
		Student s = new Student();
		
		s.information();

문제2)
public class Book {
	
	private String title;
	private String publisher;
	private String author;
	private int price;
	private double discountRate;
	
	public Book() {}
	
	public Book(String title, String publisher, String author) {
		this.title = title;
		this.publisher = publisher;
		this.author = author;
	}
	
	public Book(String title, String publisher, String author, int price, double discountRate) {
		this.title = title;
		this.publisher = publisher;
		this.author = author;
		this.price = price;
		this.discountRate = discountRate;
	}
	
	// 게터세터 없어도 됨...ㅠㅠ
/*	public void setTitle(String title) {
		this.title = title;
	}
	public void setPublisher(String publisher) {
		this.publisher = publisher;
	}
	public void setAuthor(String author) {
		this.author = author;
	}
	public void setPrice(int price) {
		this.price = price;
	}
	public void setDiscountRate(double discountRate) {
		this.discountRate = discountRate;
	}
	
	
	public String getTitle() {
		return this.title;
	}
	public String getPublisher() {
		return this.publisher;
	}
	public String getAuthor() {
		return this.author;
	}
	public int getPrice() {
		return this.price;
	}
	public double getDiscountRate() {
		return this.discountRate;
	}
	
	*/
	public void Information() {
		if(price != 0) {
		System.out.println(title + "\n" + publisher + "\n" + author + "\n" + price + "\n" + discountRate);
		}else if(title != null) {
			System.out.println(title + "\n" + publisher + "\n" + author + "\n");
		}else {
			System.out.println("정보가 없는 도서입니다\n");
		}
	}
}
--------------------------------------------------------------------------------------------
public class Run {
	public static void main(String[] args) {
		
		Book book = new Book();
		Book book2 = new Book("체공녀 강주룡", "한겨레출판사", "박서련");
		Book book3 = new Book("밝은 밤", "문학동네", "최은영", 15000, 0.2);
		
	/*	book.setTitle("불안의 서");
		book.setPublisher("봄날의 책");
		book.setAuthor("페르난두 페소아");
		book.setPrice(29000);
		book.setDiscountRate(0.2);*/
		
		book.Information();
		book2.Information();
		book3.Information();
	}

문제3)
public class Employee {

	private int empNo;
	private String empName;
	private String dept;
	private String job;
	private int age;
	private char gender;
	private int salary;
	private double bonusPoint;
	private String phone;
	private String address;
	
	public Employee() {}
	
	public Employee(int empNo, String empName) {
		this.empNo = empNo;
		this.empName = empName;
	}
	public Employee(int empNo, String empName, String dept, String job, int age, char gender, int salary) {
		
		this(empNo, empName); // 16행 생성자를 호출한다
	/*	this.empNo = empNo;
		this.empName = empName; */ //19, 20행과 동일한 코드
		this.dept = dept;
		this.job = job;
		this.age = age;
		this.gender = gender;
		this.salary = salary;
	}
	
	public void setEmpNo(int empNo) {
		this.empNo = empNo;
	}
	public void setEmpName(String empName) {
		this.empName = empName;
	}
	public void setDept(String dept) {
		this.dept = dept;
	}
	public void setJob(String job) {
		this.job = job;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public void setGender(char gender) {
		this.gender = gender;
	}
	public void setSalary(int salary) {
		this.salary = salary;
	}
	public void setBonusPoint(double bonusPoint) {
		this.bonusPoint = bonusPoint;
	}
	public void setPhone(String phone) {
		this.phone = phone;
	}
	public void setAddress(String address) {
		this.address = address;
	}
	
	
	public int getEmpNo(){
		return this.empNo;
	}
	public String getEmpName() {
		return this.empName;
	}
	public String getDept() {
		return this.dept;
	}
	public String getJob() {
		return this.job;
	}
	public char getGender() {
		return this.gender;
	}
	public int getSalary() {
		return this.salary;
	}
	public double getBonusPoint() {
		return this.bonusPoint;
	}
	public String getPjone() {
		return this.phone;
	}
	public String getAddress() {
		return this.address;
	}
    
	public void Information() {
		System.out.println(empNo + empName + dept + job + gender + salary + bonusPoint + phone + address);
	}
}

------------------------------------------------------------------------------------------------
public class Run {

	public static void main(String[] args) {
		
		Employee employee = new Employee();
		
		employee.setEmpNo(100);
		employee.setEmpName("홍길동");
		employee.setDept("영업부");
		employee.setJob("과장");
		employee.setAge(25);
		employee.setGender('M');
		employee.setSalary(2500000);
		employee.setBonusPoint(0.05);
		employee.setPhone("010-1234-5678");
		employee.setAddress("서울시 강남구");

		employee.Information();
	}
}

 

문제4) 사용자가 배열의 길이를 직접 입력하여 그 값만큼 정수형 배열을 할당하고, 배열의 크기만큼 사용자가 값을 직접 입력하여 인덱스에 값을 초기화하기, 그리고 배열 전체 값을 나열하고 각 인덱스에 저장된 값의 합 출력
	public void SumArray(){
		
		Scanner sc = new Scanner(System.in);
		System.out.println("배열의 길이를 입력하세요");
		
		int num = sc.nextInt();
		
		int[]arr = new int[num];
		int sum = 0;
		
		for(int i = 0; i < num; i++) {
			
			System.out.println(i + "번째 인덱스에 넣을 값을 입력하세요");
			
			arr[i] = sc.nextInt();
			
			sum += arr[i];	
		}
		
		for(int i = 0; i < arr.length; i++) {
			System.out.print(arr[i] + " ");
		}
		
	//	System.out.println(Arrays.toString(arr));
		System.out.println("\n총 합 : " + sum);
		
	}

문제5) 사용자에게 문자열을 입력받아 문자 하나하나를 배열에 넣고, 검색할 문자가 문자열에 몇 개 들어가 있는지 개수와 몇 번째 인덱스에 위치하는지 인덱스 출력하기
public void CharIndex() {
		
		Scanner sc = new Scanner(System.in);
		
		System.out.println("문자열을 입력하세요");
		String str = sc.next();
		
		System.out.println("검색할 문자를 입력하세요");
		char ch = sc.next().charAt(0);
		
		char[] arr = new char [str.length()];
		int count = 0;
		
		System.out.print(str + "에서 " + ch + "(이)가 존재하는 위치 : ");
		
		for(int i = 0; i < str.length(); i++) {
			arr[i] = str.charAt(i);
			
			if(arr[i] == ch) {
				
				System.out.print(i + " ");
				count ++;
			}
		}
		System.out.println("\n" + ch + " 의 개수 : " + count);
		
	}