so my class has ust begun oop. We are asked to make a class called Swapper with two integer instances variables x, y and a constructor with two parameters that inirtialize the parameters. Also three methods a getX that returns x a getY that returns y and a void swap that swaps the values of x and y then create a SwapperDemo class that tests all methods. Im stuck and having problems grasping how to send between two filrs and all that.
here is what I got:
public class Swapper { int X; int Y; public Swapper(int i, int j) { // TODO Auto-generated constructor stub } int getX(){ return X; } int getY(){ return Y; } void swap(){ int temp; temp = this.X; this.X = this.Y; this.Y = this.X; } }
im obviously way off but not sure how this all works.
--- Update ---
so i think this works does this look right?
public class Swapper { int X; int Y; public Swapper(int i, int j) { X=i; Y=j; // TODO Auto-generated constructor stub } int getX(){ return X; } int getY(){ return Y; } void swap(){ int temp; temp = this.X; this.X = this.Y; this.Y = this.X; } }class DemoSwap { public static void main (String[] args){ Swapper tempZ = new Swapper(9,5); System.out.println("The value of X is "+tempZ.X); System.out.println("The value of Y is "+tempZ.Y); System.out.println(); System.out.println("When the values are swapped "); System.out.println(); System.out.println("The value of X is "+ tempZ.getY()); System.out.println("The value of Y is "+ tempZ.getX()); } }