• 0

Java Code help


Question

First the Java Program:

public class TestDriverA4
{
	public static void main(String[] args)
	{
        
  boolean testResult;

  // Create a new VCR with default values.
  VCR vcr1 = new VCR(); 
  
  // Test playing the video.  
  vcr1.rewind();
  showVCR(vcr1.getCurrent());

  testResult = true;
  for (int counter = 0; counter < 6; counter++)
  {
 	 if (vcr1.play() == false)
 	 {
    testResult = false;
 	 }
 	 else
 	 {
    showVCR(vcr1.getCurrent());
 	 }
  } 
  System.out.println("TestDriverA4: Expect: true. Actual: " + testResult );

  // Test going past the range of video
  System.out.println("TestDriverA4: try playing some more. Should get nothing" );
  if (vcr1.play() == false)
  {
 	 testResult = false;
  }
  else
  {
 	 showVCR(vcr1.getCurrent());
  }
  System.out.println("TestDriverA4: Expect: false. Actual: " + testResult );


  // Create a new VCR with a video in length of 120 minutes.
  VCR vcr2 = new VCR(120);

  // Test play 
  System.out.println("TestDriverA4: Expect to see [1] [3] [12] on 3 lines");
  vcr2.rewind();
  showVCR(vcr2.getCurrent());

  testResult = true;
  if (vcr2.play(2) == false)
  {
 	 testResult = false;
  }
  else
  {
 	 showVCR(vcr2.getCurrent());
  }

  if (vcr2.play(9) == false)
  {
 	 testResult = false;
  }
  else
  {
 	 showVCR(vcr2.getCurrent());
  }
  System.out.println("TestDriverA4: Expect: true. Actual: " + testResult );

         
  // Test changing length by changing the first vcr to have a video of 120 minutes
  vcr1.setLength(120);
  System.out.println("TestDriverA4: Expect: 120. Actual: " + vcr1.getLength() );

  // Test reverse and fastReverse by showing the the last, next to last, and first elements
  System.out.println("TestDriverA4: Expect to see [120] [119] [1] on 3 lines");
  if (vcr1.play(119) == false)
  {
 	 testResult = false;
  }
  else
  {
 	 showVCR(vcr1.getCurrent());
  }
  if (vcr1.reverse() == false)
  {
 	 testResult = false;
  }
  else
  {
 	 showVCR(vcr1.getCurrent());
  }
  if (vcr1.fastReverse(118) == false)
  {
 	 testResult = false;
  }
  else
  {
 	 showVCR(vcr1.getCurrent());
  }
  System.out.println("TestDriverA4: Expect: true. Actual: " + testResult );
        
  // Test going past the begining and going plays a negative amount
  System.out.println("TestDriverA4: try reversing some more. Should get nothing" );
  if (vcr1.fastReverse(300) == false)
  {
 	 testResult = false;
  }
  else
  {
 	 showVCR(vcr1.getCurrent());
  }
  System.out.println("TestDriverA4: Expect: false. Actual: " + testResult );

  testResult = true;
  if (vcr1.play(-100) == false)
  {
 	 testResult = false;
  }
  else
  {
 	 showVCR(vcr1.getCurrent());
  }
  System.out.println("TestDriverA4: Expect: false. Actual: " + testResult );

  // Attempt to reset the first VCR to 60 minutes
  vcr1.setLength(60);
  System.out.println("TestDriverA4: Expect: 60. Actual: " + vcr1.getLength() );

  // Attempt to reset the first VCR to 0 minute
  vcr1.setLength(0);
  System.out.println("TestDriverA4: Expect: 60. Actual: " + vcr1.getLength() );

  // Play the last minute
  System.out.println("TestDriverA4: Expect to see [60] on the next line");
       
  if (vcr1.play(59) == false)
  {
 	 testResult = false;
  }
  else
  {
 	 showVCR(vcr1.getCurrent());
  }

  // Attempt to create a new VCR with 0 minute.
  VCR vcr3 = new VCR(0);
  System.out.println("TestDriverA4: Expect: 180. Actual: " + vcr3.getLength() );

	} // end main

	private static void showVCR(int position)
	{
  System.out.println("[ "+ position + " ]");
    }

}//end class



then the CLASS

THEN THE CLASS

import java.util.Random;

    public class VCR
   
   {
     
      public final static int TAPE_START = 1;
  public final static int DEFAULT_TAPE_LENGTH = 180;
      public final static int DEFAULT_REVERSE_LENGTH = 1;
      public final static int DEFAULT_PLAY_DURATION = 30;
   
  private int sequenceLength;
  private int currentLength;

   
       public VCR()
      {
    this(DEFAULT_TAPE_LENGTH);
  }  
  
       public VCR(int length)
      {
 	 if(length > 0)
 	 {
    sequenceLength = length;
 	 }
 	 else
 	 {
    sequenceLength = DEFAULT_TAPE_LENGTH;
 	 }
 	 currentLength = 1;
      }
   
       public boolean play() 
      {
 	 return play(DEFAULT_PLAY_DURATION);
      }
   
       public boolean play(int time)
      {
 	 if (currentLength < sequenceLength && time > 0)
 	 {
    currentLength += time;
    if(currentLength == sequenceLength);
 	 }
 	 return true;
      }
   
   
       public boolean reverse()
      {
 	 return fastReverse(DEFAULT_REVERSE_LENGTH);
      }
   
       public boolean fastReverse(int jump)
      {
 	 if(currentLength > 1 && jump > 0)
 	 {
    currentLength -= jump;
    if(currentLength < 1)
    {
   	 currentLength = 1;
    }
    
 	 }
    return true;
      }
  
   
       public void rewind()
      {
 	 currentLength = TAPE_START;
      }
   
       public void setLength(int length)
  {
    if(length > 0 && length < sequenceLength)
    {
   	 sequenceLength = length;
    }
    currentLength = TAPE_START;	
      }
   
       public int getLength()
      {
 	 return sequenceLength;
      }
   
       public int getCurrent()
      {
 	 return currentLength;
      }
                                                     
}

the output is

----jGRASP exec: java TestDriverA4

[ 1 ]
[ 31 ]
[ 61 ]
[ 91 ]
[ 121 ]
[ 151 ]
[ 181 ]
TestDriverA4: Expect: true. Actual: true
TestDriverA4: try playing some more. Should get nothing
[ 181 ]
TestDriverA4: Expect: false. Actual: true
TestDriverA4: Expect to see [1] [3] [12] on 3 lines
[ 1 ]
[ 3 ]
[ 12 ]
TestDriverA4: Expect: true. Actual: true
TestDriverA4: Expect: 120. Actual: 120
TestDriverA4: Expect to see [120] [119] [1] on 3 lines
[ 120 ]
[ 119 ]
[ 1 ]
TestDriverA4: Expect: true. Actual: true
TestDriverA4: try reversing some more. Should get nothing
[ 1 ]
TestDriverA4: Expect: false. Actual: true
[ 1 ]
TestDriverA4: Expect: false. Actual: true
TestDriverA4: Expect: 60. Actual: 60
TestDriverA4: Expect: 60. Actual: 60
TestDriverA4: Expect to see [60] on the next line
[ 60 ]
TestDriverA4: Expect: 180. Actual: 180

 ----jGRASP: operation complete.

I need the tape to go to 180 and not 181, what should i fix it, thanks

Link to comment
Share on other sites

1 answer to this question

Recommended Posts

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.