Add the following method to the Point class: public void flip() Negates and swap
ID: 3680310 • Letter: A
Question
Add the following method to the Point class:
public void flip()
Negates and swaps the x/y coordinates of the Point object. For example, if the object initially represents the point (5, -3), after a call to flip, the object should represent (3, -5). If the object initially represents the point (4, 17), after a call to flip, the object should represent (-17, -4).
Explanation / Answer
public class Point { private int x; private int y; // your code goes here /*logic: 1. First we will swap both values. 2. after that we will negate both values */ public void flip() { int temp; //swapping //this pointer is used to fetch values coresponding to current object temp=this->x; // third temp variable is used to swap two values this->x=this->y; this->y=temp; //negation this->x=-(this->x); this->y=-(this->y); } }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.