728x90
반응형
📂프로젝트 파일
💾소스코드
public class TvTest {
public static void main(String[] args) {
Tv t;
t = new Tv();
t.power();
t.setChannel(11); // t.channel = -1; (X) 메소드와 변수 사용하기
// t.channelDown();
// System.out.println(t.channel);
t.showInfo();
Tv t2;
t2 = new Tv();
t2.power();
t2.setChannel(7);
t2.showInfo();
}
}
class Tv {
String color;
int channel;
boolean power;
void power() {
power = !power;
}
// 입력받은 정수 값으로 channel(멤버변수)를 설정
void setChannel(int n) {
if (n<0 || n>100) {
System.out.println("잘못된 채널 입력입니다.");
return;
}
channel = n;
}
// 멤버변수 channel을 반환
int getChannel() {
return channel;
}
void channelUp() {
channel++;
}
void channelDown() {
channel--;
}
// 전원의 on/off 여부와 현재 채널을 출력
void showInfo() {
if(power == false) {System.out.println("전원 상태 : OFF");}
else {System.out.println("전원 상태 : ON");}
System.out.println("현재 채널 : " + channel);
}
}
💾소스코드
public class CardTest {
public static void main(String[] args) {
Card c1 = new Card();
c1.width = 200;
c1.height = 300;
c1.shape = "heart";
c1.number = 1;
Card c2 = new Card();
// width는 클래스 변수여서 값을 바꾸게 되면 c1에도 적용된다.
c2.width = 100;
c2.shape = "diamond";
c2.number = 7;
c1.showInfo();
c2.showInfo();
}
}
class Card {
// 클래스 변수
static int width;
static int height;
// 인스턴스 변수
String shape;
int number;
void showInfo() {
System.out.println("너비 : " + width + "\n높이 : " + height);
System.out.println("모양 : " + shape + "\n숫자 : " + number);
System.out.println("");
}
}
💾소스코드
public class CardTest2 {
public static void main(String[] args) {
Card2 c1 = new Card2();
c1.kind = "Heart";
c1.number = 7;
Card2 c2 = new Card2();
c2.kind = "Spade";
c2.number = 4;
System.out.println("c1은 " + c1.kind + ", " + c1.number +
"이며, 크기는 (" + c2.width + ", " + c2.height + ")");
System.out.println("c2는 " + c2.kind + ", " + c2.number +
"이며, 크기는 (" + c2.width + ", " + c2.height + ")");
c1.width = 50;
c1.height = 80;
System.out.println("c1은 " + c1.kind + ", " + c1.number +
"이며, 크기는 (" + c2.width + ", " + c2.height + ")");
System.out.println("c2는 " + c2.kind + ", " + c2.number +
"이며, 크기는 (" + c2.width + ", " + c2.height + ")");
}
}
class Card2 {
String kind;
int number;
static int width = 100;
static int height = 250;
}
💾소스코드
public class StudentText {
public static void main(String[] args) {
// Student s1 = new Student();
// s1.name = "김태희";
// s1.score_c = 90;
// s1.score_java = 80;
// s1.avg = s1.getAvg();
//
//
// Student s2 = new Student();
// s2.name = "정지훈";
// s2.score_c = 80;
// s2.score_java = 70;
// s2.avg = s2.getAvg();
//
// s1.showInfo();
// s2.showInfo();
Car c1 = new Car();
c1.gearType = "a";
c1.door = 4;
c1.color = "green";
c1.showInfo();
Car c2 = new Car();
c2.gearType = "b";
c2.door = 3;
c2.color = "red";
c2.showInfo();
}
}
class Student {
static String track = "디지털 컨버전스";
String name;
int score_c;
int score_java;
float avg;
int rank;
void showInfo() {
System.out.println("[ " + track + " ]" + "이름 : " + name + "| C점수 : " + score_c
+ "| JAVA점수 : " + score_java + "| 평균 " + avg);
}
float getAvg() {
avg = (score_c + score_java) / 2f;
return avg;
}
}
class Car {
static String product = "KIA";
String gearType;
int door;
String color;
void showInfo() {
System.out.println("차종 : " + product + " | 기어 종류 : " + gearType + " | 문 : " + door + " | 색상 : " + color);
}
}
728x90
반응형
'Language > JAVA' 카테고리의 다른 글
JAVA의 정석 3판 - 객체지향 프로그래밍 연습문제 (0) | 2022.06.23 |
---|---|
JAVA 4일차 - 객체 지향 프로그래밍(2) (0) | 2022.06.23 |
JAVA 3일차 - 다차원 배열 (0) | 2022.06.23 |
명품 자바 프로그래밍 에센셜 실습문제 3장 (0) | 2022.06.21 |
JAVA 2일차(2) - 배열 (0) | 2022.06.21 |
댓글