Do it/Practice

Java) class연습

develop_mii 2025. 7. 24. 03:47

1. Product 클래스 구현

- 다음 필드를 포함합니다 : String name (상품명) ,  int price (가격),  int quantity (수량)
- getTotalPrice() 메서드를 작성하여 총 가격(가격 × 수량)을 반환하세요.
- main 메서드에서 상품을 생성하고 총 가격을 출력하세요.

 

#Product

// 클래스 내에 선언된 변수 - 속성, 필드, 멤버변수
String name;
int price;
int quantity;

//생성자
public Product(String name, int price, int quantity){
this.name = name;
this.price = price;
this.quantity = quantity;
}

//클래스 내에 선언된 함수 - 메소드
public int getTotalPrice(){
return price*quantity;
}

#Main

Product p1 = new Product("가방",10000,2);

System.out.println(p1.getTotalPrice());

//혹은 int result = p1.getTotalPrice();
//          System.out.println(result);

 

 

2. Score 클래스 구현

- 필드: int kor, int eng, int math
- printScore() 메서드 작성: 총점, 평균을 출력
평균이 90점 이상이면 "우수", 60점 미만이면 "재시험",
그 외는 "보통" 출력

 

#Score

int kor;
int eng;
int math;

public Score (int kor, int eng, int math){
this.kor =  kor;
this.eng = eng;
this.math = math;
}

public void printScore(){
int sum = kor + eng + math;
double avg = (doduble)sum / 3;          //혹은 double avg = sum/3.0
if (avg>=90){
System.out.println("우수")
} else if (avg<60){
System.out.println("재시험")
} else {
System.out.println("보통")
}
}

#Main

Score score = new Score(90,80,70);

score. printScore();

 

 

3. Temperature 클래스 구현

- 필드: double celsius
- double toFahrenheit() 메서드 작성:  섭씨 온도를 화씨로 변환하여 반환
                                                           변환 공식: 화씨 = 섭씨 * 1.8 + 32

#Temperature

double celius;


public Tempertature(double celius){
this.celius = celius;
}

public double toFahrenheit(){
double f1 = celius * 1.8 +32;
return f1;
}

#Main

Temperature t1 = new Temperature(22);

double t2 = t1.  toFahrenheit();
System.out.println("화씨 온도 : " + t2);

 

 

4.Movie 클래스 구현

- 필드: String title, String genre, int runtime

- boolean isLongMovie() 메서드 작성: 상영 시간이 120분 이상이면 true, 아니면 false 반환
- main에서 결과 출력

 

#Movie 

String title;
String genre;
int runtime;

public Movie ( String title, String genre, int runtime){
this.title = title;
this.genre = genre;
this.runtime = runtime;
}

public boolean isLongMovie(){
if (runtime>=120){              
return true;
} else{
return false;
}
// 더 간단하게
// return runtime>=120;
}

#Main

Movie m1 = new Movie( "영화제목","장르",130);

System.out.println("긴 영화인가요?:" + m1.isLongMovie());
//혹은 boolean run = m1.isLongMovie();
          System.out.println( "긴 영화인가요?:" +  run);




'Do it > Practice' 카테고리의 다른 글

Java) String 클래스 연습  (1) 2025.08.04
Java) scanner 속성 예제  (0) 2025.07.28
Java) class연습 02  (0) 2025.07.28
Java) 배열 연습  (1) 2025.07.23
Java) 반복문 for 연습  (0) 2025.07.23