A primary advantage of organizing code into classes is thatwe can break a proble
ID: 3613633 • Letter: A
Question
A primary advantage of organizing code into classes is thatwe can break a problem up into more manageable
subproblems. You don't have worry howVector2D will be used in the wholeprogram|you can and should
test it independently before moving on to the nextsubproblem.
The second class you write tests yourVector2D implementation. Write a classnamed Vector2DTest
that consists only of a mainmethod thatcreates vector instances, executes methods on these instances,and
displays the results to verify your code is workingproperly. For instance, to test the setXmethod, onemight
use:
Vector2D a = new Vector2D();
a.setX(-5.7);
System.out.println("Expected: -5.7.");
System.out.println("Actual: " +a.getX());
Test all constructors andmethods and make sure they produce the intended results. Displayboth the
expected and actual results. Comment this class if it ishelpful to you.
Explanation / Answer
I assume you have a constructor that takes an x and y value. publicclass Vector2dtest { publicstatic void main(String[]args) { //Test all constructors and methods and make sure they produce theintended results. //default constructor Vector2D test= newVector2D(); test.setX(-2.4); test.setY(3.9); System.out.println("Expected: -2.4"); System.out.println("Actual:"+a.getX()); System.out.println("Expected: 3.9"); System.out.println("Actual:"+a.getY()); //2 param constructor test = newVector2D(-2.4, 3.9); System.out.println("Expected: -2.4"); System.out.println("Actual:"+a.getX()); System.out.println("Expected: 3.9"); System.out.println("Actual:"+a.getY()); } }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.