Homework 8 - Blender

跳女舞最魅的安翔想要一台攪拌機。這攪拌機的功能很單純,能夠倒入東西,選擇不同強度來攪拌,然後倒出。

請幫助安翔,以物件導向的模式來設計一隻攪拌機的程式吧!

輸入說明

  • insert 表示要倒入東西,再輸入一串文字(String)為要倒入的東西。
  • mix 表示要攪拌,再輸入一個整數(int)n 為攪拌強度。
  • drop 表示倒出,倒出攪拌機內的東西。

輸出說明

  • 對於 insert 要判斷攪拌機是否為空,否則不能放。
  • 對於 mix 要判斷攪拌機內是否有物品,否則不能攪拌。攪拌後要偷看一下攪拌的結果。
  • 對於 drop 只要把攪拌機內的物品印出即可。

額外說明

  • mix 需依照強度(多洗幾次牌)打亂倒入的東西,強度為 n 時代表整個字串隨機排列 n 次。
  • 需以物件導向方式設計,請依此為基礎撰寫:
public class Blender {
    private String stuff;

    public void mount(String something) {

    }

    public String unmount() {

    }

    public void blend(int strength) {

    }

    public boolean isEmpty() {

    }

    public String watch() {

    }
}

輸入輸出範例

mix 強度為 0 時,不做攪拌,可攪拌很多次。

mix 要判斷是否有物品可攪拌。

不可重複放入物品。

Hint

Class String

  • public char[] toCharArray()

an array of chars copied from the input data.

  • public static String valueOf(char[] data)

Returns the string representation of the char array argument. The contents of the character array are copied; subsequent modification of the character array does not affect the returned string.

Class Math from java.lang.Math

  • public static double random()

Returns a double value with a positive sign, greater than or equal to 0.0 and less than 1.0. Returned values are chosen pseudorandomly with (approximately) uniform distribution from that range. When this method is first called, it creates a single new pseudorandom-number generator, exactly as if by the expression

new java.util.Random() This new pseudorandom-number generator is used thereafter for all calls to this method and is used nowhere else. This method is properly synchronized to allow correct use by more than one thread. However, if many threads need to generate pseudorandom numbers at a great rate, it may reduce contention for each thread to have its own pseudorandom-number generator.

Returns: a pseudorandom double greater than or equal to 0.0 and less than 1.0.

Class Scanner from java.util.Scanner

  • public Scanner(InputStream source)

    Constructs a new Scanner that produces values scanned from the specified input stream. Bytes from the stream are converted into characters using the underlying platform's default charset.

    ex.Scanner input = new Scanner( System.in );

  • public String next()

Finds and returns the next complete token from this scanner. A complete token is preceded and followed by input that matches the delimiter pattern. This method may block while waiting for input to scan, even if a previous invocation of hasNext() returned true.

  • public int nextInt()

Scans the next token of the input as an int. An invocation of this method of the form nextInt() behaves in exactly the same way as the invocation nextInt(radix), where radix is the default radix of this scanner.