This project focuses on demonstrating your understanding of classes and objects. Before attempting this project, be sure you have completed all of the reading assignments listed in the syllabus to date, participated in the weekly conferences, and thoroughly understand the examples throughout the chapters. The project requirements include:
Design and implement a stringed musical instrument violin class using the following guidelines:
Data fields for violin should include number of strings, an array of string names representing string names (e.g. E,A,D,G), and boolean fields to determine if the instrument is tuned, and if the instrument is currently playing. You are welcome to add additional data fields if you like.
A constructor method that set the tuned and currently playing fields to false.
Other methods
1) to tune the instrument
2) to start the instrument playing
3) to stop the instrument from playing.
4) return number of strings
5) return an array of string names representing string names (e.g. E,A,D,G)
Create a UML class diagram using a diagram tool (e.g. PPT, Visio) of your choice. Prepare the diagrams and place them in a word document along with a brief description of each of your classes.
Create Java classes for your instruments. Be sure that your code matches your design specifications and some minimal functionality is included. For example, if you called the violin.play() method, you should at least print that the violin is playing. Similar functionality should be supplied when you stop playing, tune or call any of your methods. For example:
public void playviolin() {
System.out.println("The violin is now playing.");
}
Write the output from your Instrument class methods to a text file that a user entered from the command line arguments (e.g. java violinOutput.txt). This allows your program to accept filenames from the user via a command line argument.
Finally, create a Java test class that simulates using your instrument class. In your test class be you should at a minimum: a) Construct 10 instances of your instrument, b) tune your instruments, c) Start playing your instrument, d) Call your unique method such as write string numbers and names, and e) Stop playing your instruments. (Hint: Arrays and Loops will make your job easier and result in more efficient code!)
Your programs should compile and run without errors.
Be sure
import java.io.*;
public class Test{
public static void main(String[] args) {
String outputFile = "";
if (0 < args.length) {
outputFile = args[0];
System.out.println("This program will write output to this file: " + outputFile + "\n");
try {
File file = new File(outputFile);
PrintWriter output = new PrintWriter(outputFile);
output.println("hello"); //to check if ir prints anything
Violin[] simpleViolin = new Violin[5];
//Create 5 violin objects
for (int i = 0; i < simpleViolin.length; i++){
simpleViolin = new Violin();
}
output.println("\nLet's tune " + Violin.getNumberOfViolins() + " violins.");
for(int i = 0; i < simpleViolin.length; i++){
output.print(i + 1);
simpleViolin.tuneOn();
}
output.println("\nNow let's start playing " + Violin.getNumberOfViolins() + " violins.");
for(int i = 0; i < simpleViolin.length; i++){
output.print(i + 1);
simpleViolin.startPlaying();
}
output.println("\nIt looks like " + Violin.getNumberOfViolins() + " violins have untuned.");
for(int i = 0; i < simpleViolin.length; i++){
output.print(i + 1);
simpleViolin.tuneOff();
}
output.println("\nThis music is terrible! Let's stop it!");
for(int i = 0; i < simpleViolin.length; i++){
output.print(i + 1);
simpleViolin.stopPlaying();
}
output.close();
}
catch (IOException io){
System.out.println("Sorry that file is not found " + io);
}
}//end if
}//end main
}//end Test
class Violin{
private final int numberOfStrings = 4;
private final char[] stringNames = {'E', 'A', 'D', 'G'};
private boolean isTuned = false;
private boolean isPlaying = false;
private static int numberOfViolins = 0;
private PrintWriter output;
public Violin(){
numberOfViolins++;
}
public void startPlaying() {
isPlaying = true;
System.out.println(" violin is now playing.");
}
public void stopPlaying() {
isPlaying = false;
System.out.println(" violin has stopped playing.");
}
public void tuneOn() {
isTuned = true;
System.out.println(" violin is now tuned.");
}
public void tuneOff() {
isTuned = false;
System.out.println(" violin is untuned.");
}
static int getNumberOfViolins(){
return numberOfViolins;
}
}