Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Need help with the input
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
nelsonkkyy




PostPosted: Sun Oct 31, 2010 12:03 am   Post subject: Need help with the input

This is the original one:
Quote:

class TestPoint
{
public static void main (String[] args)
{

Point p1 = new Point ();
Point p2 = new Point ();
Point p3 = new Point ();
Point p4 = new Point ();

p1.setx (2);
p1.sety (4);
p2.setx (3);
p2.sety (5);
p3.setx (9);
p3.sety (7);
p4.setx (6);
p4.sety (2);
System.out.println ("Point1(" + p1.getx () + ", " + p1.gety () + ")");
System.out.println ("Point2(" + p2.getx () + ", " + p2.gety () + ")");
System.out.println ("Point3(" + p3.getx () + ", " + p3.gety () + ")");
System.out.println ("Point4(" + p4.getx () + ", " + p4.gety () + ")");
System.out.println (" ");



p2.add (p3);
System.out.println ("p2 + p3 = (x, y)");
System.out.println ("(" + p2.getx () + ", " + p2.gety () + ")");
System.out.println (" ");

System.out.println ("p4 + p1 = (x, y)");
System.out.println ("(" + (p4.getx () + p1.getx ()) + ", " + (p4.gety () + p1.gety ()) + ")");
System.out.println (" ");

p2.subtract (p1);
System.out.println("p2 - p1 = (x, y)");
System.out.println("("+ (p2.getx() - p1.getx()) +", "+ (p2.gety() - p1.gety()) +")");
System.out.println(" ");

p1.dist (p2);
System.out.println ("Distance between p1 and p2 is: " + p1.dist (p2));
System.out.println ("Distance between p2 and p3 is: " + p2.dist (p3));
System.out.println ("Distance between p3 and p4 is: " + p3.dist (p4));
System.out.println ("Distance between p3 and p1 is: " + p3.dist (p1));

}
}

Quote:

class Point
{
int x;
int y;
void setx (int x)
{
this.x = x;
}


void sety (int y)
{
this.y = y;
}


int getx ()
{
return this.x;
}


int gety ()
{
return this.y;
}


void add (Point p)
{
x = x + p.x;
y = y + p.y;
}


void subtract (Point p)
{
x = x - p.x;
y = y - p.y;
}



double dist (Point p)
{
return Math.sqrt ((Math.pow ((x - p.x), 2) + Math.pow ((y - p.y), 2)));
}



}



Im trying to make it as an input way but i cant.

Quote:

import java.io.*;



class TestPoint
{
public static void main (String[] args) throws IOException

{
int rounds = 1;
String input;
Point p1 = new Point ();
Point p2 = new Point ();
Point p3 = new Point ();
Point p4 = new Point ();


BufferedReader stdin = new BufferedReader (
new InputStreamReader (System.in));
for (int i = 1 ; i <= 5 ; i++)
{
System.out.println ("Enter x: ");
input = stdin.readLine ();
p1.setx = new Point ();

System.out.println ("Enter y: ");
input = stdin.readLine ();
p1.sety = new Point ();
}

p2.setx (1);
p2.sety (5);
p3.setx (1);
p3.sety (2);
p4.setx (-2);
p4.sety (1);
System.out.println ("Point1(" + p1.getx () + ", " + p1.gety () + ")");
System.out.println ("Point2(" + p2.getx () + ", " + p2.gety () + ")");
System.out.println ("Point3(" + p3.getx () + ", " + p3.gety () + ")");
System.out.println ("Point4(" + p4.getx () + ", " + p4.gety () + ")");
System.out.println (" ");



p2.add (p3);
System.out.println ("p2 + p3 = (x, y)");
System.out.println ("(" + p2.getx () + ", " + p2.gety () + ")");
System.out.println (" ");

System.out.println ("p4 + p1 = (x, y)");
System.out.println ("(" + (p4.getx () + p1.getx ()) + ", " + (p4.gety () + p1.gety ()) + ")");
System.out.println (" ");

p2.subtract (p1);
System.out.println ("p2 - p1 = (x, y)");
System.out.println ("(" + (p2.getx () - p1.getx ()) + ", " + (p2.gety () - p1.gety ()) + ")");

p1.dist (p2);
System.out.println ("Distance between p1 and p2 is: " + p1.dist (p2));
System.out.println ("Distance between p2 and p3 is: " + p2.dist (p3));
System.out.println ("Distance between p3 and p4 is: " + p3.dist (p4));
System.out.println ("Distance between p3 and p1 is: " + p3.dist (p1));

}
}
Sponsor
Sponsor
Sponsor
sponsor
S_Grimm




PostPosted: Tue Nov 02, 2010 1:53 pm   Post subject: Re: Need help with the input

Quote:


BufferedReader stdin = new BufferedReader (
new InputStreamReader (System.in));
for (int i = 1 ; i <= 5 ; i++)
{
System.out.println ("Enter x: ");
input = stdin.readLine ();
p1.setx = new Point ();

System.out.println ("Enter y: ");
input = stdin.readLine ();
p1.sety = new Point ();
}



you have no variables being assigned from the input. you need to assign the variables to the input
Barbarrosa




PostPosted: Sun Nov 14, 2010 10:32 pm   Post subject: Re: Need help with the input


    You're not using the system input for any point values.
    Java:

    input = stdin.readLine ();
    p1.setx = new Point (); //you did nothing with your input. I recommend using the Integer.parseInt(String) method.

    You're using methods like they're variables, and assigning the wrong types to them.
    Java:

    void setx (int x)
    {
    this.x = x;
    }
    ...
    p1.setx = new Point (); //setx is a method that takes an int; you're treating it like an object's member that's a Point object.
    //the appropriate way would be p1.setx(2); or something like that

    You're only using the default package scope for everything. This is more optional, but it's usually a bad idea not to set it to public or private.
    Java:


    /*missing "private" */int x;
    ...
    /*missing "public" */void setx (int x)

    You should use the [syntax="java"][/syntax] tag, not the quote tag.

Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 3 Posts ]
Jump to:   


Style:  
Search: