Results 1 to 6 of 6
Like Tree2Likes
  • 1 Post By Fubarable
  • 1 Post By DarrylBurke

Thread: Testing a method

  1. #1
    CoryBrown is offline Member
    Join Date
    Jun 2012
    Location
    Jacksonville, FL
    Posts
    3
    Rep Power
    0

    Default Testing a method

    I have the following code and need to test my methods in class Violin. I only keep getting one line of output. I'm trying to test each element in the violins [] array and get output for each. This is for my first java programming class so I will apologize upfront for asking elementary questions.

    Thanks
    - Cory


    Java Code:
    package corybrownp3;
    
    public class Corybrownp3 {
    
        
            public static void main(String[] args) throws Exception{
                 
                //declare violin object
                Violin violin1 = new Violin();
                           
                
                java.io.File file = new java.io.File("Corybrownp3test.txt");
                 
                //check to see if file already exists
                if (file.exists())
                {
                    System.out.println("File already exists");
                    //System.exit(0);
                }
          
                //create a file
                java.io.PrintWriter output = new java.io.PrintWriter(file);
          
                
                 
                //write test results to file
                output.println(violin1.playViolin());
                output.println(violin1.stopPlaying());
                output.println(violin1.tuneViolin());
            
                if (Violin.isTuned && Violin.isPlaying)
                    System.out.println("The violin is tuned and playing!");
                else 
                    System.out.println("The violin is not playing, or it is not tuned.");
                
            
            } 
            
            
    }
            
          
    class Violin 
     {
         static boolean isTuned; //Violin starts off not tuned
         static boolean isPlaying; //Violin is not playing at start
          
         //array for Violin strings
         char [] violinStrings = {'E','A','D','G'};
          
         //default Violin object
         public Violin()
         {
             isTuned = false;
             isPlaying = false;
             //System.out.println("The violin is not playing, and it is not tuned.");
         }
          
         public Violin(boolean T, boolean P)
         {
             isTuned = T;
             isPlaying = P;
         }
          
         public boolean playViolin()
         {
            //System.out.println("The violin is playing!");
            return isPlaying = true;
         }
          
         public boolean stopPlaying()
         {
             //System.out.println("The violin has stopped playing.");
             return isPlaying = false;
         }
          
         public boolean tuneViolin()
         {
             //System.out.println("The violin is being tuned!");
             return isTuned = true;
         }
          
     
    
    public class ViloinTest{
      public void main(String[] args){
        
            Violin[] violins;
            
            violins = new Violin[10];  
            
                for(int i = 0; i < violins.length; i++){
                    violins[i] = new Violin();
    	   
    	}
                    
            //Test Violin methods    
            for (int i = 0; i < violins.length; i++)
                System.out.println(Violin.isPlaying + " ");
              
       }
    }
    }
    Last edited by CoryBrown; 06-25-2012 at 10:18 PM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Testing a method

    Don't forget to close your PrintWriter object, output, after you're done writing to it.
    CoryBrown likes this.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,254
    Rep Power
    19

    Default Re: Testing a method

    CoryBrown likes this.
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    CoryBrown is offline Member
    Join Date
    Jun 2012
    Location
    Jacksonville, FL
    Posts
    3
    Rep Power
    0

    Default Re: Testing a method

    Thanks, db.

  5. #5
    CoryBrown is offline Member
    Join Date
    Jun 2012
    Location
    Jacksonville, FL
    Posts
    3
    Rep Power
    0

    Default Re: Testing a method

    Any takers on helping me out with this?

  6. #6
    Ksharp is offline Banned
    Join Date
    Jun 2012
    Location
    Beijing,China
    Posts
    34
    Rep Power
    0

    Default Re: Testing a method

    Your code looks good, except you need get rid of public from "public class ViloinTest", since there is only allowed PUBLIC class in a package.

    Java Code:
    
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package corybrownp3;
    
    public class Corybrownp3 {
     
         
            public static void main(String[] args) throws Exception{
                  
                //declare violin object
                Violin violin1 = new Violin();
                            
                 
                java.io.File file = new java.io.File("Corybrownp3test.txt");
                  
                //check to see if file already exists
                if (file.exists())
                {
                    System.out.println("File already exists");
                    //System.exit(0);
                }
           
                //create a file
                java.io.PrintWriter output = new java.io.PrintWriter(file);
           
                 
                  
                //write test results to file
                output.println(violin1.playViolin());
                output.println(violin1.stopPlaying());
                output.println(violin1.tuneViolin());
             
                if (Violin.isTuned && Violin.isPlaying)
                    System.out.println("The violin is tuned and playing!");
                else
                    System.out.println("The violin is not playing, or it is not tuned.");
                 
             
            }
             
             
    }
             
           
    class Violin
     {
         static boolean isTuned; //Violin starts off not tuned
         static boolean isPlaying; //Violin is not playing at start
           
         //array for Violin strings
         char [] violinStrings = {'E','A','D','G'};
           
         //default Violin object
         public Violin()
         {
             isTuned = false;
             isPlaying = false;
             //System.out.println("The violin is not playing, and it is not tuned.");
         }
           
         public Violin(boolean T, boolean P)
         {
             isTuned = T;
             isPlaying = P;
         }
           
         public boolean playViolin()
         {
            //System.out.println("The violin is playing!");
            return isPlaying = true;
         }
           
         public boolean stopPlaying()
         {
             //System.out.println("The violin has stopped playing.");
             return isPlaying = false;
         }
           
         public boolean tuneViolin()
         {
             //System.out.println("The violin is being tuned!");
             return isTuned = true;
         }
      
    }
      
     
     class ViloinTest{
      public void main(String[] args){
         
            Violin[] violins;
             
            violins = new Violin[10]; 
             
                for(int i = 0; i < violins.length; i++){
                    violins[i] = new Violin();
            
        }
                     
            //Test Violin methods   
            for (int i = 0; i < violins.length; i++)
                System.out.println(Violin.isPlaying + " ");
               
       }
    }
    
    
    
    
    
    
    The violin is not playing, or it is not tuned.
    成功生成(总时间:6 秒)

    Ksharp

Similar Threads

  1. Junit testing, testing list<e> interface
    By mackavelirip in forum New To Java
    Replies: 0
    Last Post: 10-05-2011, 06:08 AM
  2. Counting Method Execution, Testing Tool
    By PataRican in forum Advanced Java
    Replies: 3
    Last Post: 05-01-2011, 11:24 AM
  3. Testing if it is an Int and if not ask again
    By Alexander_003 in forum New To Java
    Replies: 2
    Last Post: 11-27-2010, 12:09 AM
  4. Testing boolean method
    By SteroidalPsycho in forum New To Java
    Replies: 7
    Last Post: 10-23-2009, 04:12 AM
  5. Array Method Testing
    By Suzanne1187 in forum Java Applets
    Replies: 1
    Last Post: 04-15-2009, 08:23 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •