Java代写:CST8132GuitarSimulation


根据提供的UML图,实现一个吉他模拟器,练习OOP的基本编程方法。

Purpose

Think about Java Objects and how they communicate. Work with multiple classes
and their constructors. Work with examples of the has-a relationship,
Association, Aggregation, Composition between objects.

Description

For this exercise, we will be considering the implementation of a simple
guitar simulation. Simulation is the basis of many games, but it also has
important industrial applications.
Our goal is to become familiar and comfortable with the notion that Object
Oriented Programming is about Objects communicating and interacting. Recall
that Java Objects in general have fields (also called attributes, instance
variables, or properties) which constitute their state, and methods which
constitute their behaviors. These Objects are instances of Java Classes, which
have fields (and constructors) to specify the Object’s state, and methods to
specify the Object’s behaviors. When a property of one object is a reference
to yet another object, we say the first object is in a has-a relationship with
the second, and there is an Association between them. More specifically, there
is an Aggregation relationship.

Background

Consider a piece of wire or cord or wire with one end tied or fastened to a
wooden board. The other end of the wire is wrapped around a nail or peg
embedded in the same board, so the wire is tight. If the peg is somehow turned
with fingers or a wrench, the wire wraps further around the peg and the wire
becomes tighter. We can simulate this situation with Java code.
The peg will be represented by a simple Java object that can be turned.
Therefore, we have a Peg class that has a turn method. A Peg has a wire
wrapped around it. Note the phrase, “has a”, in that sentence. A Peg has-a
Wire. In other words, a Peg has an attribute which is an instance variable
that holds a reference to a Wire object.
A Wire object can tighten. Hence the Wire class has a changeTension method
that increases the Wire’s tension attribute by a certain amount (or decreases
it if the amount is negative). So we can change the tension of a wire, but
what else can we do with it? We can pluck the wire, or in other words, we can
tweak it with our finger to make it vibrate. So the Wire class has a pluck
method. What else do we know about a Wire? The Wire is attached to the board.
This attachment is an attribute rather than a behavior. To understand why,
think of a person who is married to their spouse. We say the person has a
spouse, and we speak of their marital status. The behavior or act of having a
wedding and getting married is a different concept. Marital status is a
property. So we will say the Wire has an attachment to the board, or even
better, a Wire has-a Board, the same way we might say a person has a spouse.
This means one of the instance variables of the Wire class will be a reference
to a Board object.
Now, it turns out the part of the Board where the Wire is attached is thin
like the membrane of a drum.
So when the Wire is plucked and it vibrates, the Board makes a sound that
corresponds to the tension of the Wire. The Board class has a soundNote method
that takes a parameter corresponding to the tension property of the wire.
What should a Peg’s turn method do? We need to specify the amount by which the
Peg is turned, so the turn method needs a parameter to correspond to that
amount, which can be positive or negative. The Peg’s turn method would contain
one line of Java code that simply calls its Wire’s changeTension method to
change the Wire’s tension by that amount. We could say that when the Peg’s
turn method is called, the Peg sends a message to the Wire that it should
change its tension (the Peg calls one of the methods of the Wire).
Our simple guitar has (is Composed of) six Peg, Wire pairs (one pair shown
below) and a Board. The Composition relationship is a form of Aggregation.
Composition means consists-of, which is a stronger form of has-a. So the
Guitar class has an array of Wires, an array of Pegs, and a Board as
attributes.
The Wire vibrates at a certain frequency corresponding to its tension, and
when the corresponding Peg is turned, the wire’s frequency changes. We will
keep things really simple and consider the unit of turning a Peg the same as a
unit of tension. For example, one of the behaviors of the Guitar will be that
turn(1,-2) will mean that the first Peg (that is, Peg 1, the first argument)
is turned just enough in the loosening direction to decrease the wire’s
tension or vibrating frequency by two units (a decrease of 2, the -2 which is
the second argument). We will call this second argument the change-in-tension,
or in other words, delta-tension. For now, our wires can have any tension, but
wires with tensions less than zero will be floppy, and when plucked they will
sound like they have a tension of zero (no sound).
There are not many lines of code to write. The main work is to figure out what
goes where, and which methods call which other methods.
You are given the following items:

  1. UML for the objects in this guitar simulation (see below). The constructors (whose parameters are often used to initialize properties of the object being constructed) are not mentioned in the UML so you will need to determine those yourself. Ask for help if you get stuck.
  2. Java source code for the Board class, which you will download from blackboard. This will be the mechanism through which our simulation activates the sound card on your computer. You will use this source code (your Wire object’s vibrate method will call the Board object’s soundNote method) but you are NOT expected to understand how the Board class works. You only need to know the soundNote method is there, and you need to use that method at the appropriate place in the code.
  3. A Java source code file called SimpleSong.java containing a main method for testing your guitar simulation. Download this file from Blackboard. When you run this file, you are playing a simple song on your guitar simulation. Feel free to change this file to make different sound patterns, but note that at this point our guitar doesn’t yet have frets, so we will be limited in the notes we can produce, although you could turn the Peg to create different sounds. We will work on removing those limitations in a future assignment.
  4. Constant declarations in a file called GuitarConstants.txt. You will copy and paste these constants into the appropriate place in your Guitar.java file. These constants are just integers that correspond to the tensions and therefore vibrating frequencies of the six wires.
    Your assignment is to implement the simulation by writing Java Classes
    according to the UML. Format your code with proper indentation and other
    formatting, using the coding conventions of the first Hybrid Activity for
    guidance. Test plan and external documentation are not required for this
    exercise, but in future labs they will be required.

Hints

  1. You will create only one Board object. One attribute (instance variable) of the Guitar class is a reference to the one Board object. The constructor of the Guitar class will pass the reference of this Board object as a parameter to the Wire constructor as each wire is created a total of six times, so that in its constructor, each Wire can copy this reference into its Board property. Your Guitar constructor will create (that is, new Board(), new Wire(…), etc, all of the objects the guitar is composed of.
  2. You will have only six Wire objects, and six Peg objects, all created in the constructor for the Guitar class. The Wire objects are created one by one, and each is passed as an argument to the corresponding Peg constructor as the pegs are created, so that each Peg object in its constructor can set its Wire attribute.
  3. In Guitar.java, the methods take an integer N as a parameter, and then those methods need to do something with the Nth object (that is call a method on the Nth object). You will probably be using N as the index into an array. You will discover that often your methods are implemented with a single method call on another object. The main point of this assignment is for you to think about which objects should invoke which methods on other objects.

Submission

The submission process for this assignment is the same as before:

  1. demonstrate your simulation to your lab instructor.
  2. submit a zip archive of your deliverables folder: Lastname_Firstname_CST8132_Lab3.zip

文章作者: SafePoker
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 SafePoker !
  目录