본문 바로가기

IT/Java73

Java (메소드 호출_주사위) 현재 클래스는 2개 public은 main이 있는 클래스에만 사용 가능 package p0528Dice; import java.util.Scanner; class Dice { int diceFace; //입력수 int RollDice; //랜덤수 public void startPlaying() { System.out.print("예상값을 입력하시오: "); Scanner scan = new Scanner(System.in); diceFace = scan.nextInt(); RollDice();//난수발생 메소드 } public void RollDice() { RollDice = (int) (Math.random() * 5) + 1; System.out.println(RollDice); compare();.. 2021. 5. 28.
Java (this생성자_Phonebook) package phonebook01; public class Phonebook { /*필드*/ //이름 전화번호 주소 String name; int tel; String add; /*생성자*/ Phonebook(String name, int tel, String add){ this.name = name; this.tel = tel; this.add = add; } Phonebook(String name, int tel){ this(name, tel, null); } /*메소드*/ void print() { System.out.printf("name: %s\n", name); System.out.printf("phone: %d\n", tel); if(add!=null) { System.out.printf(.. 2021. 5. 24.
Java (this생성자 공통메소드_Date) package p0524This; public class Date { int year; String month; int day; /*공통 메소드*/ Date(int year, String month, int day){ this.year = year; this.month = month; this.day = day; } Date(int year){ this(year, "1월", 1); //this(year, null, 0); } Date(){ this(2022); } public String toString() { return "[year=" + year +", " + "month=" + month +", " + "day=" + day +"]"; } } package p0524This; public clas.. 2021. 5. 24.
Java (객체지향메소드_Car) package p0524Overloading; public class Car { String company = "현대자동차"; String model; String color; int maxSpeed; Car(){ System.out.printf("car1.company : %s", company); } Car(String m){ model = m; System.out.printf("\n\ncar2.company : %s \ncar2.model : %s", company, model); } Car(String m, String c){ model = m; color = c; System.out.printf("\n\ncar3.company : %s \ncar3.model : %s \ncar3.color : .. 2021. 5. 24.