Do it! 자바 프로그래밍 입문 (Chapter 5 ~ Chapter 10)

Jin·2022년 4월 23일
1

Chapter 5

pg.142


package chapter5;
import java.util.Scanner;
public class FunctionTest {

	public static void main(String[] args) {
		int num1, num2;
		Scanner sc = new Scanner(System.in);
		System.out.println("두 수를 입력하세요 :");
		num1 = sc.nextInt();
		num2 = sc.nextInt();

		int result = addNum(num1, num2);
		System.out.println(num1 + " + " + num2 + " = " + result + "입니다");

		result = substract(num1, num2);
		System.out.println(num1 + " - " + num2 + " = " + result + "입니다");

		result = times(num1, num2);
		System.out.println(num1 + " * " + num2 + " = " + result + "입니다");

		double value = divide(num1, num2);
		System.out.println(num1 + " / " + num2 + " = " + value + "입니다");

	}

	// 덧셈
	public static int addNum(int n1, int n2) {
		return n1 + n2;
	}

	// 뺄셈
	public static int substract(int n1, int n2) {
		return n1 - n2;
	}

	// 곱셈
	public static int times(int n1, int n2) {
		return n1 * n2;
	}

	// 나눗셈
	public static double divide(double n1, double n2) {
		return n1 / n2;
	}

}

pg.152 - 1번, 2번 문제

package chapter5;
public class Person1 {

	int age;
	String name;
	boolean marry;
	int decendent;

//	Person1(int age, String name, boolean marry, int decendent) {
//		this.age = age;
//		this.name = name;
//		this.marry = marry;
//		this.decendent = decendent;
//	}

	Person1() {
	}

	void showinfoPerson1(int age, String name, boolean marry, int decendent) {
		System.out.println("이 사람의 나이 : " + age);
		System.out.println("이 사람의 이름 : " + name);
		System.out.println("이 사람의 결혼 여부 : " + marry);
		System.out.println("이 사람의 자녀 수 : " + decendent);
	}

	public static void main(String[] args) {
//		Person1 jamesPerson1 = new Person1(40, "james", true, 3);

		Person1 jamesPerson1 = new Person1();
		jamesPerson1.showinfoPerson1(40, "james", true, 3);

	}

}
package chapter5;
public class Shop {

	long orderNum;
	String cusIDS;
	String orderDate;
	String orderName;
	String pruNum;
	String address;

	public Shop(long orderNum, String cusIDS, String orderDate, String orderName, String pruNum, String address) {
		this.orderNum = orderNum;
		this.cusIDS = cusIDS;
		this.orderDate = orderDate;
		this.orderName = orderName;
		this.pruNum = pruNum;
		this.address = address;
	}

	void showShop() {
		System.out.println("주문 번호 : " + orderNum);
		System.out.println("주문자 아이디 : " + cusIDS);
		System.out.println("주문 날짜 : " + orderDate);
		System.out.println("주문자 이름 : " + orderName);
		System.out.println("주문 상품 번호 : " + pruNum);
		System.out.println("배송 주소 : " + address);

	}

	public static void main(String[] args) {
		Shop hgsShop = new Shop(2018503120001L, "abc123", "2018년 3월 12일", "홍길순", "PD0345-12", "서울시 영등포구 여의도동 20번지");
		hgsShop.showShop();
	}

}

pg.157

package chapter5;

public class Person1 {
	String name;
	double tall;
	double weight;

	public Person1(String name, double tall, double weight) {
		name = name;
		tall = tall;
		weight = weight;
	}

	public static void main(String[] args) {
		Person1 testperson1 = new Person1("홍길동", 185.7, 78.5);
	}
}

pg.167

public class Student {
	int studentID;
	private String studentName;
	int grade;
	public String address;

	public String getStudentName() {
		return studentName;
	}
	public void setStudentName(String studentName) {
		this.studentName = studentName;
	}
}

*다른 클래스 파일 생성
public class StudentTest {
	public static void main(String[] args) {
		Student testStudent = new Student();
		testStudent.setStudentName("홍길동");
		System.out.println(testStudent.getStudentName());
	}
}

pg.168

  • 클래스를 생성할 때 호출하는 '생성자'는 멤버 변수를 초기화하는데 사용합니다.
  • 클래스를 생성하여 메모리에 있는 상태를 '인스턴스라' 하고 멤버 변수를 다른 말로 '인스턴스 변수'라고 합니다.
  • '메서드'는 일반 함수에 객체 지향의 개녕르 추가하여, 클래스 내부에서 선언하고 클래스 멤버 변수를 사용하여 클래스 기능을 구현합니다.
package chapter5;

public class MyDate {
	private int day;
	private int month;
	private int year;

	public MyDate(int day, int month, int year) {
		this.day = day;
		this.month = month;
		this.year = year;
	}

	public int getDay() {
		return day;
	}

	public void setDay(int day) {
		this.day = day;
	}

	public int getMonth() {
		return month;
	}

	public void setMonth(int month) {
		this.month = month;
	}

	public int getYear() {
		return year;
	}

	public void setYear(int year) {
		this.year = year;
	}

	public boolean isValid() {
		if ((this.day > 0 && this.day < 32) && (this.month > 0 && this.day < 13) && (this.day > 0 && this.day < 32) && (this.year > 0)) {
			System.out.println("유효한 날짜 입니다.");
			return true;
		} else {
			System.out.println("유효하지 않은 날짜입니다.");
			return false;
		}
	}

}

***다른 클래스 파일
package chapter5;

public class MyDateTest {
	public static void main(String[] args) {
		MyDate data1 = new MyDate(30, 2, 2000);
		System.out.println(data1.isValid());
		MyDate data2 = new MyDate(2, 10, 2006);
		System.out.println(data2.isValid());
	}

}

Chapter 6

pg.180

package cooper;
public class Student {
	String studentID;
	int grade;
	int money;

	public Student(String studentID, int money) {
		this.studentID = studentID;
		this.money = money;
	}
	public void takeTaxi(Taxi taxi) {
		taxi.taxiIncome(10000);
		this.money -= 10000;
	}
	public void showStudentInfo() {
		System.out.println("학생이름 : " + studentID + "\n돈 : " + money);
	}
}
----------------------------------------------------------------
package cooper;
public class Taxi {
	int taxiNum;
	int money;

	public Taxi(int taxiNum) {
		this.taxiNum = taxiNum;
	}

	public void taxiIncome(int money) {
		this.money += money;
	}

	public void showTaxiInfo() {
		System.out.println("택시이름 : " + taxiNum + "\n수입 :" + money);
	}

}
----------------------------------------------------------------
package cooper;
public class Test {
	public static void main(String[] args) {
		Student CEJstudent = new Student("CEJ", 30000);
		Taxi CEJtaxi = new Taxi(888);
		CEJstudent.takeTaxi(CEJtaxi);
		CEJstudent.showStudentInfo();
		CEJtaxi.showTaxiInfo();

	}
}

pg.190

package staticRengeTest;

public class Student {
	public int cardNum;
	private static int serialNum = 1000;
	int studentID;
	String studentName;
	int grad;
	String address;

	Student() {
		serialNum++;
		this.studentID = serialNum;
		cardNum = studentID + 100;
	}

	void showCardNumInfo() {
		System.out.println("카드번호 : " + cardNum + "\n학번 : " + studentID);
		System.out.println();
	}
}
----------------------------------------------------------------------------
package staticRengeTest;
public class StudentTest {
	public static void main(String[] args) {
		Student studentKim = new Student();
		Student studentPark = new Student();

		studentKim.showCardNumInfo();
		studentPark.showCardNumInfo();
	}
}

pg.196

package singleton;
public class CarFactory {
	public static int serialNum = 10001;
	public static int carNum;
	private static CarFactory instance = new CarFactory();

	private CarFactory() {
	}

	public static CarFactory getInstence() {
		serialNum++;
		carNum = serialNum;
		return instance;
	}

	public void getCarNum() {
		System.out.println("고유 번호 : " + carNum + "시리얼 번호 : " + serialNum);
	}
}

---------------------------------------------------------------------------------

package singleton;
public class CarFactoryTest {
	public static void main(String[] args) {
		CarFactory Ford = CarFactory.getInstence();
		Ford.getCarNum();

		CarFactory Benz = CarFactory.getInstence();
		Benz.getCarNum();
        
        System.out.println(Ford == Benz);
	}

}

고유 번호 : 10002시리얼 번호 : 10002
고유 번호 : 10003시리얼 번호 : 10003
true

pg.197

  • 클래스 내부에서 자신의 주소를 가리키는 예약어를 "this" 라고 합니다.

  • 클래스에서 여러 생성자가 오버로드되어 있을 경우에 하나의 생성자에서 다른 생성자를 호출할 때 "this"를 사용합니다.

  • 클래스 내부에서 선언하는 static 변수는 생성되는 인스턴스마다 만들어지는 것이 아닌 여러 인스턴스가 공유하는 변수입니다. 따라서 클래스에서 기반한 유일한 변수라는 의미로 "클래스 변수"라고도 합니다.

  • 지역 변수는 함수나 메서드 내부에서만 사용할 수 있고 스텍 메모리에 생성됩니다. 멤버 변수 중 static 예약어를 사용하는 "static 데이터 영역" 메모리에 생성됩니다.

package p197_Q6;

public class CardCompany {
	private static int serialNum = 1000;
	public int cardNum;
	public String name;

	public CardCompany(String name) {
		this.name = name;
		cardNum = ++serialNum;
	}

	public int getCardNum() {
		return cardNum;
	}

	public void cardInfo() {
		System.out.println("이름:" + name + "\n카드넘버:" + cardNum);

	}
}
-----------------------------------------------------------
public class Test {

	public static void main(String[] args) {
		CardCompany HGD = new CardCompany("홍길동");
		HGD.cardInfo();
		CardCompany HGS = new CardCompany("홍길순");
		HGS.cardInfo();

	}

}
package p197_Q7;

public class CardCompany {
	private static CardCompany instance = new CardCompany();
	
	CardCompany(){	
	}
	
	public static CardCompany getInstance() {
		if(instance == null) {
			instance = new CardCompany();
		}
		return instance;
	}
	
	public Card creatCard(String name){
		Card card = new Card(name);
		return card;
	}
    }
    -----------------------------------------------------------
    public class Card {
		private static int serialNum = 1000;
		private int cardNum;
		String name;

	Card(String name){
		this.name = name;
		this.cardNum = ++serialNum;
	}

	String cardInfo(){
		String result ="카드이름 :" + name + "\n카드번호 : " + cardNum;
		return result;
	}	
	}
	-----------------------------------------------------------
	public class CardTest {

	public static void main(String[] args) {
		CardCompany cardCompany = CardCompany.getInstance();
		
		Card KBCard1 = cardCompany.creatCard("국민카드1");	
		Card KBCard2 = cardCompany.creatCard("국민카드2");
		
		System.out.println(KBCard1.cardInfo());
		System.out.println(KBCard2.cardInfo());
	}
	}

Chapter 7

pg.211


package Chapter7;

public class Student {

	int studentID;
	String name;

	public Student(int studentID, String name) {
		this.studentID = studentID;
		this.name = name;
	}

	void showStudentInfo() {
		System.out.println(studentID + ", " + name);
	}

}

---------------------------------------------------------------------

public class StudentArray {

	public static void main(String[] args) {
		Student[] arrStudent = new Student[3];

		arrStudent[0] = new Student(1001, "James");
		arrStudent[1] = new Student(1002, "Tomas");
		arrStudent[2] = new Student(1003, "Edward");

		for (Student student : arrStudent) {
			student.showStudentInfo();
		}
		
	}

}

pg.221

package Chapter7;

public class Alphabet {

	public static void main(String[] args) {

		char[][] alphabet = new char[13][2];
		char num = 'a';
		for (int i = 0; i < alphabet.length; i++) {
			for (int j = 0; j < alphabet[i].length; j++) {
				alphabet[i][j] = num;
				System.out.print(alphabet[i][j]);
				num++;

			}
			System.out.println("");
		}

	}

}

pg.225

package Chapter7;

public class Student {

	int studentID;
	String name;

	public Student(int studentID, String name) {
		this.studentID = studentID;
		this.name = name;
	}

	void showStudentInfo() {
		System.out.println(studentID + ", " + name);
	}

}
----------------------------------------------------------------
import java.util.ArrayList;

public class StudentArrayList {
	public static void main(String[] args) {

		ArrayList<Student> arrayList = new ArrayList<Student>();

		arrayList.add(new Student(1001, "James"));
		arrayList.add(new Student(1002, "Tomas"));
		arrayList.add(new Student(1003, "Edward"));

		// 방법 1
		for (Student student : arrayList) {
			student.showStudentInfo();
		}

		System.out.println("");

		// 방법 2
		for (int i = 0; i < arrayList.size(); i++) {
			arrayList.get(i).showStudentInfo();
		}

		System.out.println("");

		// 방법 3
		for (int i = 0; i < arrayList.size(); i++) {
			Student student = arrayList.get(i);
			student.showStudentInfo();
		}

	}
}

pg.

Chapter 8

pg.

pg.

pg.

pg.

Chapter 9

pg.

pg.

pg.

pg.

Chapter 10

pg.

pg.

pg.

pg.

profile
Hi, there?

0개의 댓글