Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: swapping integers OOP

  1. #1
    Member
    Join Date
    Mar 2013
    Posts
    39
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default swapping integers OOP

    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;
    }
    }

     
    class DemoSwap {
    public static void main (String[] args){
    	Swapper x = new Swapper(9, 8);
    	Swapper y = new Swapper(7, 0);
     
    	System.out.println(x.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());
    }
    }


  2. #2
    Junior Member
    Join Date
    Apr 2013
    Posts
    25
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Default Re: swapping integers OOP

    Hi,
    In swap function
    temp = this.X;
    this.X = this.Y;
    this.Y = temp;

    and in the demoswap function the last two lines should display The value of X as tempZ.getX() and the value of Y as tempZ.getY()

    Hope this solves your problem.

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: swapping integers OOP

    Where does the testing method call the swap() method?
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. swapping nodes in a linked list
    By ueg1990 in forum Java Theory & Questions
    Replies: 3
    Last Post: September 10th, 2012, 02:37 PM
  2. OOP help!
    By imaznumkay in forum Object Oriented Programming
    Replies: 3
    Last Post: July 11th, 2011, 01:43 PM
  3. [SOLVED] Writing Integers to .txt File; Returning Random Characters Instead of Integers
    By verbicidalmaniac in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: March 8th, 2011, 09:42 PM
  4. Determine the two smallest integers from a set of user input integers
    By bpontin in forum Loops & Control Statements
    Replies: 4
    Last Post: October 17th, 2010, 06:38 PM
  5. OOP
    By mgutierrez19 in forum Object Oriented Programming
    Replies: 2
    Last Post: November 29th, 2009, 10:10 PM