Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Need help to complete my assignment.

  1. #1
    Junior Member
    Join Date
    Jul 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Need help to complete my assignment.

    This is what I have so far. Below are the requirements
    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:
    1.Design and implement a stringed musical instrument class using the following guidelines:

    a.Data fields for your instrument 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.

    b. A constructor method that set the tuned and currently playing fields to false.

    c.Other methods 1) to tune the instrument, 2) to start the instrument playing, and 3) to stop the instrument from playing.

    d.Other methods as you see fit (Add at least one unique method).

    2.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.

    3.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.");
    }

    4.Write the output from your Instrument class methods to a text file that a user entered from the command line arguments (e.g. java Mynamep3tst myfilename.txt). This allows your program to accept filenames from the user via a command line argument.

    5.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, and e) Stop playing your instruments. (Hint: Arrays and Loops will make your job easier and result in more efficient code!)

    6.Your programs should compile and run without errors.

    7.Be sure to test your program carefully. Provide a list of comprehensive test cases used to validate your application and include these test cases in your word document containing your UML diagrams and descriptions. Similar to Project 1, your test data can be shown in a table that includes input data, expected output, actual output and pass/fail results from the test.

    This is what I have so far

    import java.io.FileWriter;

    import java.io.IOException;

    import java.io.PrintWriter;

    import java.util.Formatter;







    public class Violin {




    //Class Violin constructor

    public Violin()

    {

    tunedViolin = false;

    playViolin = false;

    }



    //Class Violin methods

    public boolean setTuneViolin()

    {

    tunedViolin = true;

    return tunedViolin;

    }



    public boolean setViolin()

    {

    playViolin = true;

    return playViolin;

    }



    public void playViolin(int choice) throws IOException //throw exception to append file

    {

    //true = append file

    FileWriter writeFile = new FileWriter("myText.text", true);

    PrintWriter outputFile = new PrintWriter(writeFile);



    //set array limit

    int limit = stringArray.length;



    switch (choice)

    {

    case 1:

    //output violin number in console and external file

    String tuned1 = "Violin 1 is Tuned.";

    String play1 = "Playing Violin 1 strings:";

    System.out.println(tuned1);

    System.out.println(play1);

    outputFile.println(tuned1);

    outputFile.println(play1);



    //loop through array

    for(int index = 0; index < limit ; index++)

    {

    System.out.println(stringArray[index]);

    outputFile.println(stringArray[index]);

    }



    break;



    case 2:

    //output violin number in console and external file

    String tuned2 = "Violin 2 is Tuned.";

    String play2 = "Playing Violin 2 strings:";

    System.out.println(tuned2);

    System.out.println(play2);

    outputFile.println(tuned2);

    outputFile.println(play2);



    //loop through array

    for(int index = 3; index >= 0; index--)

    {

    System.out.println(stringArray[index]);

    outputFile.println(stringArray[index]);

    }



    break;



    case 3:

    //output violin number in console and external file

    String tuned3 = "Violin 3 is Tuned.";

    String play3 = "Playing Violin 3 strings:";

    System.out.println(tuned3);

    System.out.println(play3);

    outputFile.println(tuned3);

    outputFile.println(play3);



    //loop through array

    for(int index = 0; index < limit; index+=2)

    {

    System.out.println(stringArray[index]);

    outputFile.println(stringArray[index]);

    }



    break;



    case 4:

    //output violin number in console and external file

    String tuned4 = "Violin 4 is Tuned.";

    String play4 = "Playing Violin 4 strings:";

    System.out.println(tuned4);

    System.out.println(play4);

    outputFile.println(tuned4);

    outputFile.println(play4);



    //access array

    System.out.println(stringArray[0]);

    outputFile.println(stringArray[0]);

    System.out.println(stringArray[3]);

    outputFile.println(stringArray[3]);

    System.out.println(stringArray[1]);

    outputFile.println(stringArray[1]);

    System.out.println(stringArray[2]);

    outputFile.println(stringArray[2]);



    break;



    case 5:

    //output violin number in console and external file

    String tuned5 = "Violin 5 is Tuned.";

    String play5 = "Playing Violin 5 strings:";

    System.out.println(tuned5);

    System.out.println(play5);

    outputFile.println(tuned5);

    outputFile.println(play5);



    //access array

    System.out.println(stringArray[3]);

    outputFile.println(stringArray[3]);

    System.out.println(stringArray[0]);

    outputFile.println(stringArray[0]);

    System.out.println(stringArray[2]);

    outputFile.println(stringArray[2]);



    break;



    case 6:

    //output violin number in console and external file

    String tuned6 = "Violin 6 is Tuned.";

    String play6 = "Playing Violin 6 strings:";

    System.out.println(tuned6);

    System.out.println(play6);

    outputFile.println(tuned6);

    outputFile.println(play6);



    //access array

    System.out.println(stringArray[0]);

    outputFile.println(stringArray[0]);



    break;



    case 7:

    //output violin number in console and external file

    String tuned7 = "Violin 7 is Tuned.";

    String play7 = "Playing Violin 7 strings:";

    System.out.println(tuned7);

    System.out.println(play7);

    outputFile.println(tuned7);

    outputFile.println(play7);



    //loop through array

    for(int index = 0; index < limit ; index++)

    {

    //loop through array

    for(int index1 = 0; index1 < limit ; index1++)

    {

    System.out.println(stringArray[index1]);

    outputFile.println(stringArray[index1]);

    }

    }



    break;



    case 8:

    //output violin number in console and external file

    String tuned8 = "Violin 8 is Tuned.";

    String play8 = "Playing Violin 8 strings:";

    System.out.println(tuned8);

    System.out.println(play8);

    outputFile.println(tuned8);

    outputFile.println(play8);



    //access array

    System.out.println(stringArray[0]);

    outputFile.println(stringArray[0]);

    System.out.println(stringArray[0]);

    outputFile.println(stringArray[0]);

    System.out.println(stringArray[1]);

    outputFile.println(stringArray[1]);

    System.out.println(stringArray[3]);

    outputFile.println(stringArray[3]);



    break;



    case 9:

    //output violin number in console and external file

    String tuned9 = "Violin 9 is Tuned.";

    String play9 = "Playing Violin 9 strings:";

    System.out.println(tuned9);

    System.out.println(play9);

    outputFile.println(tuned9);

    outputFile.println(play9);



    //access array

    System.out.println(stringArray[0]);

    outputFile.println(stringArray[0]);



    break;



    case 10:

    //output violin number in console and external file

    String tuned10 = "Violin 10 is Tuned.";

    String play10 = "Playing Violin 10 strings:";

    System.out.println(tuned10);

    System.out.println(play10);

    outputFile.println(tuned10);

    outputFile.println(play10);



    //access array

    System.out.println(stringArray[3]);

    outputFile.println(stringArray[3]);

    break;



    }

    //close file

    outputFile.close();





    //loop through array



    }



    public boolean stopViolinPlay()

    {

    playViolin = false;

    return playViolin;

    }





    //Class Violin attributes

    private int strings;

    private boolean tunedViolin;

    private boolean playViolin;

    private String stringArray[] = {"E", "A", "D","G"};//Violin array

    private Formatter x; //variable use to create file





    }

    How should I stop the violin to play


  2. #2
    Member
    Join Date
    Jul 2012
    Posts
    83
    My Mood
    Cynical
    Thanks
    3
    Thanked 9 Times in 9 Posts

    Default Re: Need help to complete my assignment.

    You're asking about a "mutator" or "setter" method, one that sets the value of the boolean playViolin field. The standard way to do this is to create a public method that allows outside classes to set the value of this field:

    public void setPlayViolin(boolean play) {
       // I'll let you decide what to put in here
    }

    On an un-related note, consider editing your original post and adding [code] [/code] tags around your code so that it will retain its formatting and be readable by us.

  3. #3
    Member
    Join Date
    Jul 2012
    Posts
    83
    My Mood
    Cynical
    Thanks
    3
    Thanked 9 Times in 9 Posts

    Default Re: Need help to complete my assignment.

    For example here's your program reposted with code tags wrapped around it (and with removal of some of your excess white-space). See how much easier it is to read?

     
    import java.io.FileWriter;
    import java.io.IOException;
    import java.io.PrintWriter;
    import java.util.Formatter;
     
    public class Violin {
       // Class Violin constructor
       public Violin() {
          tunedViolin = false;
          playViolin = false;
       }
     
       // Class Violin methods
       public boolean setTuneViolin() {
          tunedViolin = true;
          return tunedViolin;
       }
     
       public boolean setViolin() {
          playViolin = true;
          return playViolin;
       }
     
       public void playViolin(int choice) throws IOException // throw exception to
                                                             // append file
       {
          // true = append file
          FileWriter writeFile = new FileWriter("myText.text", true);
          PrintWriter outputFile = new PrintWriter(writeFile);
          // set array limit
          int limit = stringArray.length;
          switch (choice) {
          case 1:
             // output violin number in console and external file
             String tuned1 = "Violin 1 is Tuned.";
             String play1 = "Playing Violin 1 strings:";
             System.out.println(tuned1);
             System.out.println(play1);
             outputFile.println(tuned1);
             outputFile.println(play1);
             // loop through array
             for (int index = 0; index < limit; index++) {
                System.out.println(stringArray[index]);
                outputFile.println(stringArray[index]);
             }
             break;
          case 2:
             // output violin number in console and external file
             String tuned2 = "Violin 2 is Tuned.";
             String play2 = "Playing Violin 2 strings:";
             System.out.println(tuned2);
             System.out.println(play2);
             outputFile.println(tuned2);
             outputFile.println(play2);
             // loop through array
             for (int index = 3; index >= 0; index--) {
                System.out.println(stringArray[index]);
                outputFile.println(stringArray[index]);
             }
             break;
          case 3:
             // output violin number in console and external file
             String tuned3 = "Violin 3 is Tuned.";
             String play3 = "Playing Violin 3 strings:";
             System.out.println(tuned3);
             System.out.println(play3);
             outputFile.println(tuned3);
             outputFile.println(play3);
             // loop through array
             for (int index = 0; index < limit; index += 2) {
                System.out.println(stringArray[index]);
                outputFile.println(stringArray[index]);
             }
             break;
          case 4:
             // output violin number in console and external file
             String tuned4 = "Violin 4 is Tuned.";
             String play4 = "Playing Violin 4 strings:";
             System.out.println(tuned4);
             System.out.println(play4);
             outputFile.println(tuned4);
             outputFile.println(play4);
             // access array
             System.out.println(stringArray[0]);
             outputFile.println(stringArray[0]);
             System.out.println(stringArray[3]);
             outputFile.println(stringArray[3]);
             System.out.println(stringArray[1]);
             outputFile.println(stringArray[1]);
             System.out.println(stringArray[2]);
             outputFile.println(stringArray[2]);
             break;
          case 5:
             // output violin number in console and external file
             String tuned5 = "Violin 5 is Tuned.";
             String play5 = "Playing Violin 5 strings:";
             System.out.println(tuned5);
             System.out.println(play5);
             outputFile.println(tuned5);
             outputFile.println(play5);
             // access array
             System.out.println(stringArray[3]);
             outputFile.println(stringArray[3]);
             System.out.println(stringArray[0]);
             outputFile.println(stringArray[0]);
             System.out.println(stringArray[2]);
             outputFile.println(stringArray[2]);
             break;
          case 6:
             // output violin number in console and external file
             String tuned6 = "Violin 6 is Tuned.";
             String play6 = "Playing Violin 6 strings:";
             System.out.println(tuned6);
             System.out.println(play6);
             outputFile.println(tuned6);
             outputFile.println(play6);
             // access array
             System.out.println(stringArray[0]);
             outputFile.println(stringArray[0]);
             break;
          case 7:
             // output violin number in console and external file
             String tuned7 = "Violin 7 is Tuned.";
             String play7 = "Playing Violin 7 strings:";
             System.out.println(tuned7);
             System.out.println(play7);
             outputFile.println(tuned7);
             outputFile.println(play7);
             // loop through array
             for (int index = 0; index < limit; index++) {
                // loop through array
                for (int index1 = 0; index1 < limit; index1++) {
                   System.out.println(stringArray[index1]);
                   outputFile.println(stringArray[index1]);
                }
             }
             break;
          case 8:
             // output violin number in console and external file
             String tuned8 = "Violin 8 is Tuned.";
             String play8 = "Playing Violin 8 strings:";
             System.out.println(tuned8);
             System.out.println(play8);
             outputFile.println(tuned8);
             outputFile.println(play8);
             // access array
             System.out.println(stringArray[0]);
             outputFile.println(stringArray[0]);
             System.out.println(stringArray[0]);
             outputFile.println(stringArray[0]);
             System.out.println(stringArray[1]);
             outputFile.println(stringArray[1]);
             System.out.println(stringArray[3]);
             outputFile.println(stringArray[3]);
             break;
          case 9:
             // output violin number in console and external file
             String tuned9 = "Violin 9 is Tuned.";
             String play9 = "Playing Violin 9 strings:";
             System.out.println(tuned9);
             System.out.println(play9);
             outputFile.println(tuned9);
             outputFile.println(play9);
             // access array
             System.out.println(stringArray[0]);
             outputFile.println(stringArray[0]);
             break;
          case 10:
             // output violin number in console and external file
             String tuned10 = "Violin 10 is Tuned.";
             String play10 = "Playing Violin 10 strings:";
             System.out.println(tuned10);
             System.out.println(play10);
             outputFile.println(tuned10);
             outputFile.println(play10);
             // access array
             System.out.println(stringArray[3]);
             outputFile.println(stringArray[3]);
             break;
          }
          // close file
          outputFile.close();
          // loop through array
       }
     
       public boolean stopViolinPlay() {
          playViolin = false;
          return playViolin;
       }
     
       // Class Violin attributes
       private int strings;
       private boolean tunedViolin;
       private boolean playViolin;
       private String stringArray[] = { "E", "A", "D", "G" };// Violin array
       private Formatter x; // variable use to create file
    }

Similar Threads

  1. Complete Noob -- A little help would be appreciated...
    By echmiel in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 5th, 2012, 05:31 PM
  2. assignment troubles polymorphism (guide for assignment included)
    By tdawg422 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 8th, 2011, 10:01 AM
  3. A little help need to complete my tutorial question
    By hengchuen in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 17th, 2011, 07:51 AM
  4. How to complete code
    By Shay in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 5th, 2010, 01:59 PM
  5. How to delete and add elements to an array dynamically?
    By k_w_r2007 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: April 21st, 2009, 11:31 AM