POLAYOR

学校Java Week9

Week9

W9L1

Static Variable

the particular member belongs to a type itself, rather than to an instance of that type.

Array of Objects

Just like int or String, we can create an array of Person objects

Constructors Overloading

we can call other constructors by using this()

Inheritance

  • The idea is to define a new class (subclass or child class) that inherits instance variables (attribute, state) and instance methods (operation, behavior) from another class (superclass or parent class)
    • enabling code reuse
  • Furthermore, we will learn to make the subclass redefines or overrides some of the methods in the superclass
// Student is a subclass(child class) of Person, and Person is the superclass( parent class) of Student
public class Student extends Person(){
    
}
  • A class can only be a subclass of one superclass, but a superclass can be extended by many subclasses

Final

If the instance variable will never be changed after it is initialized, we can declare it to be final

Constructor

We use super(), that means to call the equivalent constructor in our superclass

Override/Overload

  • Overriding: implementing or replacing an inherited method
  • Overloading: multiple implementations of the same name

Converting between Subclass and Superclass (Polymorphism)

  • superclass variable calls the overridden method of each specific subclass
Person p2 = new Student("A Student", "M", "China", "1954-4-
7", 193, 3, "Digital Media"); // correct

Student t1 = new Person("James Bond", "M", "London", "1921-11-
11", 007); // wrong

Object Type Casting

  • Upcasting
// in inexplicit way
Person p2 = new Student("A Student", "M", "China", "1954-4-7", 193, 3, "Digital
Media");

// in explicit way
Student s1 = new Student("A Student", "M", "China", "1954-4-7", 193, 3, "Digital
Media");
p2 = (Person)s1;

  • Downcasting
// need to be explicit
Person ps = new Student("A Student", "M", "China", "1954-4-7", 193, 3,
"Digital Media");
s1 = (Student) ps;

W9L2

Skeleton

public class Clock {

    private int hours;
    private int minutes;

    // CW1 #7.1
    // Creates a clock whose initial time is h hours and m minutes.
    public Clock(int h, int m) {

        this.hours = h;
        this.minutes = m;

    }

    // CW1 #7.2
    // Creates a clock whose initial time is specified as a string, using the format HH:MM.
    public Clock(String s) {

        String[] t = s.split(":");
        this.hours = Integer.parseInt(t[0]);
        this.minutes = Integer.parseInt(t[1]);

    }

    // CW1 #7.3
    // Returns a string representation of this clock, using the format HH:MM.
    public String toString() {

        if (this.hours<10) {
            if (this.minutes<10) {
                return "0" + this.hours + ":0" + this.minutes;
            } else {
                return "0" + this.hours + ":" + this.minutes;
            }
        } else {
            if (this.minutes<10) {
                return this.hours + ":0" + this.minutes;
            } else {
                return this.hours + ":" + this.minutes;
            }
        }

    }

    // CW1 #7.4
    // Is the time on this clock earlier than the time on that one?
    public boolean isEarlierThan(Clock that) {

        if (this.hours < that.hours) {
            return true;
        } else if (this.hours == that.hours) {
            if (this.minutes < that.minutes) {
                return true;
            } else {
                return false;
            }
        } else {
            return false;
        }

    }

    // CW1 #7.5
    // Adds 1 minute to the time on this clock.
    public void tick() {

        this.minutes += 1;
        if (this.minutes == 60) {
            this.minutes = 0;

            if (this.hours == 23) {
                this.hours = 0;
            } else {
                this.hours += 1;
            }
        }

    }

    // CW1 #7.6
    // Adds delta minutes to the time on this clock.
    public void tock(int delta) {

        int addhours = 0;

        while (delta < 0 || delta >= 60) {
            delta -= 60;
            addhours ++;
        }

        if ((this.minutes + delta) >= 60) {
            delta -= 60;
            addhours++;
        }
        this.minutes += delta;

        while ((this.hours + addhours) >= 24) {
            addhours -= 24;
        }
        this.hours += addhours;

    }

    // Getters Added:

    public int getHours() {
        return hours;
    }
    public int getMinutes() {
        return minutes;
    }

}

ex

public class AlarmClock extends Clock {
    
    // Alarm's hours, minutes, sound
    private int alarmHours;
    private int alarmMinutes;
    String alarmSound;
    
    
    // Exercise #9.1
    // Creates an Alarm clock whose initial time is h hours and m minutes,
    //     sounds an alarm at alarmHours hours and alarmMinutes minutes,
    //     with the default sound "Beep beep beep beep!"
    public AlarmClock(int h, int m, int alarmHours, int alarmMinutes) {
		
		super(h, m);
        this.alarmHours = alarmHours;
        this.alarmMinutes = alarmMinutes;
        this.alarmSound = "Beep beep beep beep!";

        if (h==alarmHours && m==alarmMinutes) {
            System.out.println(this.alarmSound);
        }
    }
    
    
    // Exercise #9.2
    // Creates an Alarm clock whose initial time is h hours and m minutes,
    //     sounds an alarm at alarmHours hours and alarmMinutes minutes,
    //     and sets the sound to alarmSound
    public AlarmClock(int h, int m, int alarmHours, int alarmMinutes, String alarmSound) {

        super(h, m);
        this.alarmHours = alarmHours;
        this.alarmMinutes = alarmMinutes;
        this.alarmSound = alarmSound;

        if (h==alarmHours && m==alarmMinutes) {
            System.out.println(this.alarmSound);
        }
		
    }
    
    
    // Exercise #9.3
    // Adds 1 minute to the time on this Alarm clock.
    // In addition, it sounds (prints) the alarm at the specified time.
    @Override
    public void tick() {
		
		super.tick();

        if (this.getHours() == this.alarmHours && this.getMinutes() == this.alarmMinutes) {
            System.out.println(alarmSound);
        }
		
    }
    
    
    // Test Client
    public static void main(String[] args) {
        AlarmClock ac1 = new AlarmClock(5, 58, 6, 0);
        ac1.tick();
        ac1.tick();  // Beep beep beep beep!
        System.out.println(ac1);  // 06:00
        
        AlarmClock ac2 = new AlarmClock(14, 29, 14, 30, "Wake Up! The Hero! Kamen Rider!");
        ac2.tick();  // Wake Up! The Hero! Kamen Rider!
    }
    
}

CW1

/*
 * Exercise and CW1 Week #9
 */
public class CuckooClock extends Clock {
    
    
    // Exercise #9.4
    // Creates a Cuckoo clock whose initial time is h hours and m minutes.
    public CuckooClock(int h, int m) {

        super(h, m);
		
    }
    
    
    // CW1 #9.1
    // Adds 1 minute to the time on this Cuckoo clock.
    // In addition,  it prints "Cuckoo!" at the start of every hour
    // It prints one time for each hour
    // Whether it is morning or night does not change the number of times it prints
    // For example, for 14:00, it prints "Cuckoo!" two times;
    //              and for 00:00 and 12:00, it prints "Cuckoo!" twelve times.
    @Override
    public void tick() {
		
		super.tick();

        if (this.getMinutes() != 0) {
            return;
        }

        int temp_hours = getHours() % 12;

        if (temp_hours==0) {
            for (int i=0; i<12; i++) {
                System.out.println("Cuckoo!");
            }
        }

        for (int i=0; i<temp_hours; i++) {
            System.out.println("Cuckoo!");
        }
		
    }
    
    
    // Test Client
    public static void main(String[] args) {
        CuckooClock cc1 = new CuckooClock(0, 58);
        cc1.tick();
        cc1.tick();  // Cuckoo!
        System.out.println(cc1);  // 01:00

        System.out.println();
        CuckooClock cc2 = new CuckooClock(13, 59);
        cc2.tick();  // Cuckoo! 
		             // Cuckoo!

        System.out.println();
		CuckooClock cc3 = new CuckooClock(23, 59);
        cc3.tick();

        System.out.println();
        CuckooClock cc4 = new CuckooClock(24, 0);
        cc4.tick();

        System.out.println();
        CuckooClock cc5 = new CuckooClock(11, 59);
        cc5.tick();

        System.out.println();
        CuckooClock cc6 = new CuckooClock(12, 0);
        cc6.tick();

        System.out.println();
        CuckooClock cc7 = new CuckooClock(12, 59);
        cc7.tick();

        System.out.println();
        CuckooClock cc8 = new CuckooClock(13, 58);
        cc8.tick();

        System.out.println();
        CuckooClock cc9 = new CuckooClock(13, 59);
        cc9.tick();
    }
}
/*
 * CW1 Week #9
 */
public class HalloweenClock extends Clock {
    
    // class variable to store number of ticks
    // of any instances of HalloweenClock so far
    private static int numOfTicks = 0;
    
    
    // CW1 #9.2
    // Creates a Halloween clock whose initial time is h hours and m minutes.
    public HalloweenClock(int h, int m) {

        super(h, m);
      
    }
    
    
    // CW1 #9.3
    // Adds 1 minute to the time on this Halloween clock.
    // In addition, if any Halloween clocks have ticked three times,
    //     print "Halloween!"
    @Override
    public void tick() {

      super.tick();

        numOfTicks += 1;

        if (numOfTicks == 3) {
            System.out.println("Halloween!");
            numOfTicks = 0;
        }

    }
    
    
    // Test Client
    public static void main(String[] args) {
        HalloweenClock hc1 = new HalloweenClock(1, 0);
        HalloweenClock hc2 = new HalloweenClock(2, 0);
        hc1.tick();
        hc2.tick();
        hc2.tick();  // Halloween!
        
        HalloweenClock hc3 = new HalloweenClock(3, 30);
        hc1.tick();
        hc2.tick();
        hc3.tick();  // Halloween!
        System.out.println(hc3);  // 03:31
    }
   
}

posted on 2022-11-10 11:40  POLAYOR  阅读(11)  评论(0编辑  收藏  举报

导航