Results 1 to 4 of 4
  1. #1
    Propinquity is offline Member
    Join Date
    Oct 2013
    Posts
    63
    Rep Power
    0

    Default Please help? Difficulty with object-oriented programming concepts

    So, my initial exercise was to define a struct to represent a point in the Cartesian (xy) coordinate plane and write a client program that asks the user to type in two pairs of coordinates and finds the distance between them using a method. This I did with relative ease.
    I am having difficulty updating my point class to use getters, setters and constructors, and changing the client program accordingly to achieve the same purpose.
    I can't really figure out how to make a method that can process two different objects without defining additional fields in the point class definition, which rather defeats the purpose of having objects.

    Could someone please clarify what information I am missing that is making this so difficult for me? I am reading my textbook over and over and I am not finding anything that clarifies this.

    Class Definition:
    Java Code:
    public class point {
    	//fields
    	public int x;
    	public int y;
    	//constructors
    	public point(){
    		this(0, 0);
    	}
    	public point(int x, int y){
    		setLocation(x,y);
    	}
    	public void setLocation(int x, int y){
    		this.x = x;
    		this.y = y;
    	}
    	//methods
    	
    	//returns the distance between this point and another point
    	public double distanceBetween(double x1, double y1, double x2, double y2) {
    		double deltaX = (x1-x2);
    		double deltaY = (y1-y2);
    		return Math.pow(deltaX, 2) + Math.pow(deltaY, 2);
    	}
    	public String toString() {
    		return "(" + x + ", " + y + ")";
    		
    	}
    }
    Client Program:
    Java Code:
    import java.util.*;
    import java.awt.*;
    
    public class clientprogram {
    
    	public static void main(String[] args) {
    		Scanner input = new Scanner(System.in);
    		
    		point p1 = new point();
    		System.out.println("Enter your first point.");
    		Scanner input1 = new Scanner(input.nextLine());
    		input1.useDelimiter(",");
    		p1.x = input1.nextInt();
    		p1.y = input1.nextInt();
    		
    		point p2 = new point();
    		System.out.println("Enter your second point.");
    		Scanner input2 = new Scanner(input.nextLine());
    		input2.useDelimiter(",");
    		p2.x = input2.nextInt();
    		p2.y = input2.nextInt();
    
    		System.out.println("The distance between the two points is " + distanceBetween(p1.x, p1.y, p2.x, p2.y));
    	}
    }

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,041
    Rep Power
    10

    Default Re: Please help? Difficulty with object-oriented programming concepts

    Step 1: Use standard naming conventions. Classes start with an uppercase letter.
    Step 2: Make x and y in your Point class private.
    Step 3: Now that x and y are private, you need to add getters so your ClientProgram class can access them.
    Step 4: Now that you have the getters, you can modify your ClientProgram class to use them.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Propinquity is offline Member
    Join Date
    Oct 2013
    Posts
    63
    Rep Power
    0

    Default Re: Please help? Difficulty with object-oriented programming concepts

    Okay, so this is what I changed with my point class. I'm sorry if I'm being colossally, royally stupid, but I don't really understand why my ClientProgram still can't use the variables.
    I don't really know how to modify it to use the getters.
    It says that the constructor for point() is undefined...when I really don't think it is?
    Java Code:
    public class point {
    	//private fields
    	private int x;
    	private int y;
    	
    	//constructor method
    	public point(int x, int y){
    		this.x = x;
    		this.y = y;
    	}
    	// accessor methods
    	public int getX(){
    		return x;
    	}
    	public int getY(){
    		return y;
    	}
    	//mutator methods
    	public void setX(int x){
    		x = this.x;
    	}
    	public void setY(int y){
    		y = this.y;
    	}
    	//returns the distance between this point and another point
    	public double distanceBetween(int x1, int y1, int x2, int y2) {
    		double deltaX = (x1-x2);
    		double deltaY = (y1-y2);
    		return Math.pow(deltaX, 2) + Math.pow(deltaY, 2);
    	}
    	public String toString() {
    		return "(" + this.x + ", " + this.y + ")";
    		
    	}
    }

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,041
    Rep Power
    10

    Default Re: Please help? Difficulty with object-oriented programming concepts

    Again, I beg of you, use standard naming conventions.

    You haven't posted your updated ClientProgram class, so it's hard to tell you what's wrong with it. Sounds like you're trying to use a constructor that doesn't exist though. Compare the constructor you're calling in ClientProgram to the constructor you wrote in your Point class.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Object Oriented Programming
    By TylerJoeWatts in forum New To Java
    Replies: 11
    Last Post: 11-27-2013, 11:32 AM
  2. Object Oriented Programming Need Help!
    By JavaAddict in forum Java Applets
    Replies: 4
    Last Post: 11-14-2013, 05:02 PM
  3. new to object oriented programming and arrays
    By maximumammo in forum New To Java
    Replies: 11
    Last Post: 04-22-2012, 09:25 PM
  4. help for Object Oriented Programming RMI
    By clapton in forum New To Java
    Replies: 0
    Last Post: 03-20-2012, 07:42 PM
  5. Help in object oriented programming .
    By renu in forum New To Java
    Replies: 18
    Last Post: 10-26-2010, 08:57 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •