자바 스터디 노트 #4

Examples of Class in Java

Member Functions in a Class

  • constructor/destructor
  • operations
  • get/set methods
  • printStates()

Object Examples

  • Savings account
  • Vending machine
  • Elevator

Possible Project Ideas

  • 비디오 대여 관리 프로그램(DVD/Book rental store management application)
  • 뱅킹 시스템 구축(Banking system)

Bicycle class

class Bicycle {
    int cadence = 0;
    int speed = 0;
    int gear = 1;

    void changeCadence(int newValue) {
        cadence = newValue;
    }
    void changeGear(int newValue) {
        gear = newValue;
    }
    void speedUp(int increment) {
        speed = speed + increment;
    }
    void applyBrakes(int decrement) {
        speed = speed - decrement;
    }
    void printStates() {
        System.out.println("cadence: "+cadence+" speed: "+speed+" gear: "+gear);
    }
}

Lamp class

class Lamp {
    bool light = false;

    void turnOn() {
        light = true;
    }
    void turnOff() {
        light = false;
    }
    void printStates() {
        System.out.println("Light: "+light? "on":"off");
    }
}

Radio class

class Radio {
    bool power = false;
    int volume = 4;
    float frequency = 100.0;
    // AM = 0, FM = 1
    int amfm = 1;

    void turnOn() {
        power = true;
    }
    void turnOff() {
        power = false;
    }
    void increaseVolume(int increment) {
        volume = volume + increment;
    }
    void decreaseVolume(int decrement) {
        volume = volume - decrement;
    }
    float getFreqency() {
        return frequency;
    }
    int getAMFM() {
        return amfm;
    }
    void setFrequency(float freq) {
        frequency = freq;
    }
    void setAMFM(bool switch) {
        if(switch == false)
            amfm = 0;
        else
            amfm = 1;
    }
    void printStates() {
        System.out.println("Power: "+power? "on":"off");
        System.out.println("Volume: "+volume);
        System.out.println("Frequency: "+frequency);
        System.out.println("AM/FM: "+amfm? "FM":"AM");
    }
}

Leave a Reply