학교/컴프1

컴프 1 실습 코드 정리 (Week11)

발빠진 쥐 2024. 5. 26. 17:31

Week11

ColorPointTest

package Week11;
/**
* 컴퓨터프로그래밍1
* 학번:202402697
* 이름:엄태은
*/

import java.util.Scanner;

public class ColorPointTest {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
		
		int x = sc.nextInt();
		int y = sc.nextInt();
		
		ColorPoint zeroPoint = new ColorPoint(); //ColorPoint 클래스 객체 생성
		System.out.println(zeroPoint.toString()); //toString메소드 return값 출력
		
		ColorPoint cp = new ColorPoint(10,10);
		cp.move(x, y);
		cp.setColor("red");
		System.out.println(cp.toString());
	}

}

class Point {
	private int x,y;
	
	 public Point (int x, int y) {
		this.x=x;
		this.y=y;
	}
	
	public int getX() {
		return this.x;
	}
	
	public int getY() {
		return this.y;
	}
	
	public void move(int x, int y) {
		this.x = x;
		this.y = y;
	}
	
}

class ColorPoint extends Point {
	
	String color ;
	
	
	public ColorPoint (int x, int y) { //매개변수 있을 때 생성자
		super(x,y);
		this.color = "black";
	}
	
	public void setColor(String color) {
		this.color = color; //color = "red" <--setColor메소드 괄호안에 ""써있음
	}


	public ColorPoint() { //매개변수 없을 때 생성자 
		
		super(0,0);
		this.color = "black";
		
	}


	public void move(int x, int y) { 
		super.move(x, y);
	}
	
	
	
	public String toString() {
		return this.color + "(" + getX() + "," + getY() + ")";
	}
	
	
	
}

-출력값

black(0,0)

red(x,y)

 

 

 

PersonTest

package Week11;

import java.util.Scanner;

public class PersonTest {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Person person = new Person("Hong kildong", 25);
		System.out.println(person.show());

		Student student = new Student("Chang kilsan", 20, "202350806");
		System.out.println(student.show());

		ForeignStudent foreignStudent = new ForeignStudent("Peter", 27, "201702085", "USA");
		System.out.println(foreignStudent.show());

	}

}

class Person {
	String name;
	int age;
	
	
	public Person(String name, int age) {
		this.name = name;
		this.age = age;
	}
	
	public String getName(){
		return this.name;
		
	}
	
	public int age(){
		return this.age;
		
	}
	

	public String show() {
		
		System.out.print(getClass().getSimpleName());
		
		return " name: " + name + ", age" + ": " + age;

	}

}

class Student extends Person {
	String studentNumber;

	public Student(String name, int age, String studentNumber) {
		super(name, age);
		this.studentNumber = studentNumber;
	}

	public void getstudentNumber(){
		this.studentNumber=studentNumber;
		
	}
	
	
	
	public String show() {
		
		return super.show()+ ", studentNumber: " + studentNumber;
	}

}

class ForeignStudent extends Student {

	String nationality;

	public ForeignStudent(String name, int age, String studentNumber, String nationality) {
		super(name, age, studentNumber);
		this.nationality = nationality;
	}
	
	public void getnationality(){
		this.nationality=nationality;
		
	}
	

	public String show() {
		
		return super.show()+ ", nationality: " + nationality;
	}
}

 

 

-출력값

Person name: Hong kildong, age: 25

Student name: Chang kilsan, age: 20, studentNumber: 202350806

ForeignStudent name: Peter, age: 27, studentNumber: 201702085, nationality: USA

 

출력값이

클래스 이름 + name (+ age (+ studentNumber ))형식이므로

 

super를 사용해 하나씩 추가해감

System.out.print(getClass().getSimpleName()); 

로 클래스 이름 출력...!!!!