Writing a computer game in java that has graphics, motion, and sound effects

Word and line count. Modify WordCount.java so that reads in text from standard input and prints out the number of characters, words, and lines in the text.

Rainfall problem. Write a program Rainfall.java that reads in nonnegative integers (representing rainfall) one at a time until 999999 is entered, and then prints out the average of value (not including 999999).

Remove duplicates. Write a program Duplicates.java that reads in a sequence of integers and prints back out the integers, except that it removes repeated values if they appear consecutively. For example, if the input is 1 2 2 1 5 1 1 7 7 7 7 1 1, your program should print out 1 2 1 5 1 7 1.

Run length encoding. Write a program RunLengthEncoder.java that encodes a binary input using run length encoding. Write a program RunLengthDecoder.java that decodes a run length encoded message.

Head and tail. Write programs Head.java and Tail.java that take an integer command line input N and print out the first or last N lines of the given file. (Print the whole file if it consists of <= N lines of text.)

Print a random word. Read a list of N words from standard input, where N is unknown ahead of time, and print out one of the N words uniformly at random. Do not store the word list. Instead, use Knuth's method: when reading in the ith word, select it with probability 1/i to be the selected word, replacing the previous champion. Print out the word that survives after reading in all of the data.

  • Caesar cipher. Julius Caesar sent secret messages to Cicero using a scheme that is now known as a Caesar cipher. Each letter is replaced by the letter k positions ahead of it in the alphabet (and you wrap around if needed). The table below gives the Caesar cipher when k = 3.
    Original:  A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
    Caesar:    D E F G H I J K L M N O P Q R S T U V W X Y Z A B C
    

    For example the message "VENI, VIDI, VICI" is converted to "YHQL, YLGL, YLFL". Write a program Caesar.java that takes a command-line argument k and applies a Caesar cipher with shift = k to a sequence of letters read from standard input. If a letter is not an uppercase letter, simply print it back out.

  • Caesar cipher decoding. How would you decode a message encrypted using a Caesar cipher? Hint: you should not need to write any more code.
  • Parity check. A Boolean matrix has the parity property when each row and each column has an even sum. This is a simple type of error-correcting code because if one bit is corrupted in transmission (bit is flipped from 0 to 1 or from 1 to 0) it can be detected and repaired. Here's a 4 x 4 input file which has the parity property:
    1 0 1 0
    0 0 0 0
    1 1 1 1
    0 1 0 1
    

    Write a program ParityCheck.java that takes an integer N as a command line input and reads in an N-by-N Boolean matrix from standard input, and outputs if (i) the matrix has the parity property, or (ii) indicates which single corrupted bit (i, j) can be flipped to restore the parity property, or (iii) indicates that the matrix was corrupted (more than two bits would need to be changed to restore the parity property). Use as little internal storage as possible. Hint: you do not even have to store the matrix!

  • Takagi's function. Plot Takagi's function: everywhere continuous, nowhere differentiable.
  • Hitchhiker problem. You are interviewing N candidates for the sole position of American Idol. Every minute you get to see a new candidate, and you have one minute to decide whether or not to declare that person the American Idol. You may not change your mind once you finish interviewing the candidate. Suppose that you can immediately rate each candidate with a single real number between 0 and 1, but of course, you don't know the rating of the candidates not yet seen. Devise a strategy and write a program AmericanIdol that has at least a 25% chance of picking the best candidate (assuming the candidates arrive in random order), reading the 500 data values from standard input.

    Solution: interview for N/2 minutes and record the rating of the best candidate seen so far. In the next N/2 minutes, pick the first candidate that has a higher rating than the recorded one. This yields at least a 25% chance since you will get the best candidate if the second best candidate arrives in the first N/2 minutes, and the best candidate arrives in the final N/2 minutes. This can be improved slightly to 1/e = 0.36788 by using essentially the same strategy, but switching over at time N/e.

    Java Sound API also include a software sound mixer that supports up to 64 channels for sound effect and background music.

    Java Media Framework (JMF), which is not part of JavaSE, is needed to support MP3 and advanced features. JOAL (Java Bindings on OpenAL) supports 3D sound effect.

    Sampled Audio

    To play sampled audio, you create an instance of a

    if (clip.isRunning()) clip.stop();
    
    2 or a
    if (clip.isRunning()) clip.stop();
    
    3, which acts as a source to the software audio mixer. Audio samples are then loaded into it, and delivered to the mixer. The mixer may mix the samples with those from other sources and then deliver the mix to a target (usually an audio output device on a sound card).

    if (clip.isRunning()) clip.stop();
    
    4

    Code Example:

    The steps of playing sounds via

    if (clip.isRunning()) clip.stop();
    
    3 are:

    1. Allocate a
      if (clip.isRunning()) clip.stop();
      
      6 piped from a file or URL, e.g.,
    2. Allocate a sound
      if (clip.isRunning()) clip.stop();
      
      3 resource via the static method
      if (clip.isRunning()) clip.stop();
      
      8:
      Clip clip = AudioSystem.getClip();
      
    3. Open the clip to load sound samples from the audio input stream opened earlier:
    4. You can now play the clip by invoking either the
      if (clip.isRunning()) clip.stop();
      
      9 or
      Sequence song = MidiSystem.getSequence(new File("song.mid"));
      
      0 method
    5. You can stop the player by invoking
      Sequence song = MidiSystem.getSequence(new File("song.mid"));
      
      1, which may be useful to stop a continuous
      Sequence song = MidiSystem.getSequence(new File("song.mid"));
      
      0.
      if (clip.isRunning()) clip.stop();
      

    Playing Sound Effects for Java Games

    A typical game requires various sound effects, e.g., move, eat, shoot, kill, gameover, and etc. The following enumeration encapsulates all the sound effects in a single class, to simplify game programming.

    Dissecting the Program

    [PENDING]

    Writing a computer game in java that has graphics, motion, and sound effects

    Dissecting the Program

    [PENDING]

    (optional)
    Sequence song = MidiSystem.getSequence(new File("song.mid"));
    
    3

    A source data line acts as a source to the audio mixer. Unlike Clip (which pre-load the audio samples), an application writes audio samples to a source data line, which handles the buffering of the bytes and delivers them to the mixer, in a streaming manner.

    MIDI Synthesized Sound

    In a computer game, MIDI can be used for providing the background music, as it save you quite a bit of bandwidth if you are downloading the file from a slow connection. Java Sound API supports three types of MIDI: MIDI Type 1, MIDI Type 2 and Rich Music Format (RMF).

    The steps are:

    1. Allocate a Sequence piped from a MIDI file:
      Sequence song = MidiSystem.getSequence(new File("song.mid"));
      
    2. Allocate a Sequencer to play a MIDI sequence:
      Sequencer player = MidiSystem.getSequencer();
      
    3. Set the current sequence for the sequencer to play:
      player.setSequence(song);
      
    4. Open the sequencer to load the sequence:
      player.open();
      
    5. You could set the loop count via
      Sequence song = MidiSystem.getSequence(new File("song.mid"));
      
      4 and start playing the music by invoking
      if (clip.isRunning()) clip.stop();
      
      9:
    6. You can set the tempo (rate of play) via
      Sequence song = MidiSystem.getSequence(new File("song.mid"));
      
      6, which is useful in a computer game when the difficulty level is increased.
    7. You can stop the play via
      Sequence song = MidiSystem.getSequence(new File("song.mid"));
      
      1:
      if (player.isRunning()) player.stop();
      

    Code Example

    The method is declared

    Sequence song = MidiSystem.getSequence(new File("song.mid"));
    
    8, as we only need one global copy, and no instance variable involved in the operation.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    

    Notes: Windows Vista seems to have lot of problems with Midi.

    MP3 & Java Media Framework (JMF)

    Java Media Framework (JMF) is not part of JavaSE, but can be downloaded from http://java.sun.com/javase/technologies/desktop/media/jmf. JMF, among other things, provides support for playing MP3, AAC music. Download JMF and run the installation. Check to ensure that "

    Sequence song = MidiSystem.getSequence(new File("song.mid"));
    
    9" is installed into "
    Sequencer player = MidiSystem.getSequencer();
    
    0".

    What kind of act does programming perform in computer programs?

    Computer programming is the process of writing code to facilitate specific actions in a computer, application or software program, and instructs them on how to perform.

    Which of the following must first be applied to programs written in the Java high

    High-level language programs must be translated into machine language before they can be executed.

    What step should you take to run the same program on a computer that has a different processor?

    Answer: "There is need to compile the program again on a different processor." is the correct answer for the above scenerio.

    What is one of the benefits of using a high

    One of the most significant advantages of Java is its ability to move easily from one computer system to another. The ability to run the same program on many different systems is crucial to World Wide Web software, and Java succeeds at this by being platform-independent at both the source and binary levels.