👨‍💻Programming/JAVA & C언어 예제

JAVA (자바) - 클래스 연습 문제 모음 - (1)

코너(Corner) 2020. 11. 19.
반응형

JAVA (자바) - 클래스 연습 문제 모음 - (1)

 

 

문제 1 - 다음을 만족하는 Student 클래스를 작성하시오.

· String형의 학과와 정수형의 학번을 필드로 선언

· Student 클래스의 main() 메소드에서 Student 객체를 생성하여 학과와 학번 필드에 적당한 값을 입력 후 출력


문제 - 2. 위에서 구현한 Student 클래스를 다음을 만족하도록 기능을 추가하여 작성하시오.

· 필드를 모두 private로 하고, getter와 setter를 구현하고

· Student 클래스의 main() 메소드에서 Student 객체를 생성하여 setter를 사용하여 학과와 학번 필드에 적당한 값을 입력 후 출력


문제 - 3. 다음에 구현된 Circle 클래스를 참고로 다음을 만족하는 Cylinder 클래스를 작성하시오.

· 원통을 나타내는 Cylinder 클래스는 Circle형의 원과 실수형의 높이를 필드로 선언

· 메소드 getVolume()은 원통의 부피를 반환

· Cylinder 클래스의 main() 메소드에서 반지름이 2.8, 높이가 5.6의 원통의 부피를 출력

· 다음은 원을 나타내는 클래스 Circle

public class Circle {

public double radius;

public static double PI = 3.141592;

 

//생성자 구현

public Circle(double radius) {

this.radius = radius;

}

//현재 반지름을 사용하여 원의 면적을 구하는 메소드

public double getArea() {

return radius * radius * PI;

}

}

 


문제 4 -  위에서 구현한 Cylinder를 다음 조건에 맞도록 기능을 추가하여 작성하시오.

· 다음과 같은 객체 생성이 가능하도록 생성자를 구현

Cylinder cd = new Cylinder(new Circle(2.8), 5.6);


문제 5 -  다음을 만족하는 클래스 SalaryMan을 작성하시오.

· 필드 salary는 월 급여액를 저장하며, int형으로 초기 값으로 1000000 저장

· 메소드 getAnnualGross()는 연봉을 반환하는 메소드로 월급에 보너스 500%로 계산

· 기본 생성자에서 필드 salary의 초기 값을 사용하며, 정수형 인자인 생성자에서 인자가 월 급여액으로 지정

· 다음과 같이 객체를 생성하여 메소드 getAnnualGrass()를 호출하여 출력

System.out.println(new SalaryMan().getAnnualGross());

System.out.println(new SalaryMan(2_000_000).getAnnualGross());


문제 6 -   다음을 만족하는 클래스 Account를 작성하시오.

· 다음의 2 개의 필드를 선언

private String owner;

private long balance;

· 위 모든 필드에 대한 getter와 setter의 구현

· 위 모든 필드를 사용하는 가능한 모든 생성자의 구현


문제 7 - 위에서 구현된 클래스 Account에서 다음 기능을 추가하여 작성하시오.

· 메소드 deposit()의 헤드는 다음과 같으며 인자인 금액을 저축하는 메소드

public long deposit(long amount)

· 메소드 withdraw()의 헤드는 다음과 같으며 인자인 금액을 인출하는 메소드

public long withdraw(long amount)

· Account 클래스의 main() 메소드에서 Account 객체를 생성하여 적당한 저축과 인출을 수행한 후 잔금을 출력


문제 8 - 위에서 구현된 메소드 withdraw()를 다음 조건에 맞게 다시 작성하시오.

· 인출 상한 금액은 잔액까지로 하며, 이 경우 이러한 상황을 출력

· 클래스 AccountTest의 main() 메소드에서 인출 상한 이상의 금액을 인출하려는 메소드를 호출하여 출력

 


문제 - 9. 다음을 만족하는 클래스 Rectangle을 작성하시오.

· 사각형의 가로와 세로로 객체를 생성하는 생성자

· 면적을 반환하는 메소드 getArea(), 둘레를 반환하는 메소드 getCircumference(),

· 다음과 같이 클래스 Rectangle 이용

Rectangle rc = new Rectangle(3.82, 8.65);

System.out.println("면적: " + rc.getArea());

System.out.println("둘레: " + rc.getCircumference());


문제 - 10. 다음을 만족하는 클래스 Computer를 작성하시오.

· 다음을 상수 필드로 선언

public … String[] osType = {"원도7", "애플 OS X", "안드로이드"};

· 다음과 같은 클래스 Computer의 객체의 사용 결과에 적합하도록 생성자와 메소드 구현

Computer pc = new Computer(0, 16);

Computer apple = new Computer(1, 32);

Computer galaxy = new Computer(2, 16);

pc.print();

apple.print();

galaxy.print();

운영체제: 원도7, 메인메모리: 16

운영체제: 애플 OS X, 메인메모리: 32

운영체제: 안드로이드, 메인메모리: 16

 

public cla

 

 


 


소스 코드

문제 1 + 2 

package ex1;

import java.util.Scanner;

class StudentEx2{
	private String name;
	private int id;

	void setName(String name) { // setter 네임
		this.name = name;
	}
	String getName() { // getter id
		return this.name+" 학과";
	}
	void setId(int id) {
		this.id = id;
	}
	int getId() {
		return this.id;
	}
}

public class StudentEx1 {

	public static void main(String[] args) {
		/*
		 * 다음을 만족하는 Student 클래스를 작성하시오
		 * String 형의 학과와 정수형의 학번을 필드로 선언 
		 * Student 클래스의 main()메소드에서 Student 객체를 생성하여 학과와 학번 필드에 
		 * 적당한 값을 입력한 후 출력
		 * 2. 위에서 구현한 Student 클래스를 다음을 만족하도록 기능을 추가하여 작성하시오.
          필드를 모두 private로 하고, getter와 setter를 구현하고 
          Student 클래스의 main() 메소드에서 Student 객체를 생성하여 setter를 사용하여 
			학과와 학번 필드에 적당한 값을 입력 후 출력
		 */
		Scanner sc = new Scanner(System.in);
		StudentEx2 student1 = new StudentEx2();
		
		System.out.println("---회원가입---");
		System.out.print("학과 입력 :");
		String name = sc.next();
		student1.setName(name);
		
		System.out.print("학번 입력 :");
		int id = sc.nextInt();
		student1.setId(id);
	      
		System.out.println("학생1의 학과 :"+student1.getName()+", 학번 :"+student1.getId());
		
		
		
	}

}

문제 3+4

package ex1;

public class Circle {
	public double radius; // 필드(==인스턴스)변수 선언
	public static double PI = 3.141592; // 원주율
	// 생성자 구현
	public Circle(double radius) {
		this.radius = radius;
	}

	// 기능 구현
	public double getArea() {
		return radius * radius * PI;
		// 원의 면적을 구하는 공식 반지름*반지름*파이
	}
}
package ex1;
public class Cylinder {

	Circle cir;
	double height;
	public Cylinder(Circle cir,double height) {
		// TODO Auto-generated constructor stub
		this.cir=cir;
		this.height=height;
	} // 생성자 구현
	
	public double Volum() {
		return cir.getArea()*height; // 반지름과 높이를 곱함..
	}
	
	public static void main(String[] args) {
		// 반지름이 2.8 , 높이가 5.6의 원통의 부피를 출력
		Circle cp = new Circle(2.8); // 반지름 2.8
		 Cylinder cd = new Cylinder(new Circle(2.8), 5.6);
		 // Circle 생성자의 2.8, Cylinder 5.6 
		double height = 5.6; // radius 2.8 height 5.6
		
		// 부피 구하는 법 : 원의 면적 * 높이 . >> 면적은 반지름+반지름+파이 파이먹고싶다
		System.out.println("부피 : "+(cp.getArea()*height));
		System.out.println(cd.Volum()); // 동일한 결과
		
	}
	
}
/*
 * 3. 다음에 구현된 Circle 클래스를 참고로 다음을 만족하는 Cylinder 클래스를 작성하시오.
·          원통을 나타내는 Cylinder 클래스는 Circle형의 원과 실수형의 높이를 필드로 선언
·          메소드 getVolume()은 원통의 부피를 반환
·          Cylinder 클래스의 main() 메소드에서 반지름이 2.8, 높이가 5.6의 원통의 부피를 출력
·          다음은 원을 나타내는 클래스 Circle

4. 위에서 구현한 Cylinder를 다음 조건에 맞도록 기능을 추가하여 작성하시오.
·          다음과 같은 객체 생성이 가능하도록 생성자를 구현 
ㆍ     Cylinder cd = new Cylinder(new Circle(2.8), 5.6);
 */

문제 5

package Salary;

public class SalaryMan {
	
	int salary; 
	//월 급여 액 저장 초기값 1,000,000 저장
	SalaryMan(){
		this.salary = 1000000;
	}
	SalaryMan(int salary){ // 생성자에서 인자 월 급여액을 지정하기 위한 재정의
		this.salary=salary;
	}
	public int getAnnualGross() { // 연봉.. 연봉은 월 * 12 인데 
		// 보너스 500%를 수령해야 하니까 5.0을 곱해야하지만 int 정수형이므로 5만 곱하고
		
		return (this.salary*12)+(this.salary*5); // 연봉 + 500% 보너스 수령 
		// 연봉 리턴
	}
	public static void main(String[] args) {
		
		SalaryMan user = new SalaryMan();
		user.salary = 1500000;
		// 연봉 1800만 * 보너스 750만  // 최저도 못받는 인생인데 보너스가 쎄네
		System.out.println(user.getAnnualGross());
		System.out.println();
		System.out.println(new SalaryMan().getAnnualGross()); 
	    System.out.println(new SalaryMan(2000000).getAnnualGross()); 
	        
	        
	}

}
/*
5. 다음을 만족하는 클래스 SalaryMan을 작성하시오.
·          필드 salary는 월 급여액를 저장하며, int형으로 초기 값으로 1000000 저장 
·          메소드 getAnnualGross()는 연봉을 반환하는 메소드로 월급에 보너스 500%로 계산
·          기본 생성자에서 필드 salary의 초기 값을 사용하며, 정수형 인자인 생성자에서 인자가 월 급여액으로 지정  
·          다음과 같이 객체를 생성하여 메소드 getAnnualGrass()를 호출하여 출력 
       System.out.println(new SalaryMan().getAnnualGross()); 
	        System.out.println(new SalaryMan(2_000_000).getAnnualGross()); 
*/

문제 6 + 7 + 8

package Account;

import java.util.Scanner;

class Account {
	private String owner;
	private long balance; 
	
	Account(){ // default 생성자
	}
	Account(String owner){
		this.owner = owner;
	}
	Account(long balance){
		this.balance=balance;
	}
	Account(String owner, long balance){
		this.owner = owner; this.balance = balance;
	}
	void setOwner(String owner) {
		this.owner = owner;
	}
	String getOwner() {
		return this.owner;
	}
	void setBalance(long balance) {
		this.balance = balance;
	}
	long getBalance() {
		return this.balance;
	}
	public long deposit(long amount) {
		// 인자인 금액을 저축하는 기능
		System.out.println(amount+"만큼 입금 합니다.");
		return this.balance+=amount; //저장
	}
	public long withdraw(long amount) {
		// 입력한 금액만큼 인출한 기능
		Scanner sc = new Scanner (System.in);
		long money = amount; // 새로운 money 변수를 선언
		System.out.println("현재 고객님은 "+this.balance+"원 까지 인출할 수 있습니다.");
		if(this.balance < amount ) {  // 현재 값이 입력 값보다 적다면
			System.out.println("잔액이 부족하여 인출하실 수 없습니다.");
			System.out.println("가진 금액을 모두 출금 하시겠습니까? y/n");
			String se[] = {"y","n"};
			String sel = sc.next();
			if(sel.equals(se[0])) {
				money = this.balance; // 현재 돈을 money값에 대입
			}
		}
			System.out.println(money+"만큼 인출 합니다.");
			return this.balance -= money; // money값
	}
	void show() {
		System.out.println(this.owner+"님은 현재 "+this.balance+"원을 가지고 있습니다.");
	}
}
public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner (System.in);
			Account player = new Account(); // 생성자
			
			System.out.print("사용자 성함을 입력하세요 :");
			String owner = sc.next();
			player.setOwner(owner);
			System.out.print("사용자의 잔액은 얼만큼 있는지 적으세요. :");
			long balance = sc.nextLong();
			player.setBalance(balance);
			player.show();
			
			System.out.println("얼만큼 저축 하시겠습니까?");
			int amo = sc.nextInt();
			player.deposit(amo);
			player.show();
			
			System.out.println("얼마를 인출 하시겠습니까?");
			amo = sc.nextInt();
			player.withdraw(amo);
			player.show();
	}

}

 

9 -

package Rectangle;

public class Rectangle {
	double width;
	double length;
	Rectangle(double width, double length){
		this.width=width; this.length=length;
	}
	double getArea() { // 면적 (가로*세로)
		return this.width * this.length;
	}
	double getCircumference() { // 둘레 (가로+높이)*2
		return (this.width+this.length)*2 ;
	}
	public static void main(String[] args) {
		// 사각형의 가로와 세로로 객체를 생성하는 생성자
		// 면적을 반환하는 메소드 getArea(), 
		// 둘레를 반환하는 메소드 getCircumference(),
		// 다음과 같이 클래스 Rectangle 이용 
		// Rectangle rc = new Rectangle(3.82, 8.65);
//		System.out.println("면적 : "+rc.getArea());
//		System.out.println("둘레 : "+rc.getCircumference());
		Rectangle rc = new Rectangle(3.82,8.65);
		System.out.println("가로:3.82, 세로: 8.65");
		System.out.println("면적 : "+rc.getArea());
		System.out.println("둘레 : "+rc.getCircumference());

	}

}

10 -

package Computer;

class Computer{
	public static final String[] osType = {"윈도우7","애플 OS X","안드로이드"};
	private int OS; // 배열의 인덱스 i 역할을 할 것임 
	int Memory = 8;
	Computer(int OS,int Memory){
		this.OS = OS; this.Memory = Memory;
	}
	void show() {
		System.out.printf("운영체제: %s, 메인메모리: %d %n", osType[OS], Memory);
	}
}
public class Main {

	public static void main(String[] args) {
		
		Computer pc = new Computer(0,16);
			Computer apple = new Computer(1,32);
			Computer Android = new Computer(2, 16);
			pc.show();
			apple.show();
			Android.show();

	}

}

 

반응형

댓글