•

Check out our Community Blogs

Register and join over 40,000 other developers!

### Recent Blog Entries

• phi

I love this community !

• JackJames

hi i am jack i am seo expert jack james would love you to read new post

# Help Needed with Rectangle "user input"

java user input

11 replies to this topic

### #1 Gman

Gman

CC Resident

• 97 posts
• Location:Donegal, Ireland
• Learning:Java, C#, PHP, (Visual) Basic, JavaScript, Assembly, Others

Posted 03 February 2012 - 02:12 PM

Hi all need some help with this question, have done question 1 but cant figure out part 2 of it.

Question:

1. Write a program which instantiates a new Rectangle object. Use appropriate methods to set its x and y coordinates to 15 and 35 respectively, and to set its width to 10 and its height to 20. Use appropriate methods to access the values of each instance variable, and output them to screen.

The x coordinate is: 15.0
The y coordinate is: 35.0
The height is: 10.0
The width is: 20.0

Here is my code:

```// gman
// 3/2/2012
// practical 2B

import java.awt.Rectangle;

public class rectangleQ1
{

public static void main(String []args)
{

Rectangle r1 = new Rectangle(15,35,20,10);

double x = r1.getX();
double y = r1.getY();
double height = r1.getHeight();
double width = r1.getWidth();

System.out.println("x is " + x);
System.out.println("y is " + y);
System.out.println("height is " + height);
System.out.println("width is " + width);

}

}
```

Here is the second part of question:

2. Adapt this program to allow the user to enter values for the width, height, x coordinate and y coordinate of the Rectangle. Use appropriate methods to set the instance variables of the Rectangle and output the values of all its instance variables.

Gman
• 0

### #2 chili5

chili5

CC Mentor

• Expert Member
• 3038 posts
• Programming Language:Java, C#, PHP, JavaScript, Ruby, Transact-SQL
• Learning:C, Java, C++, C#, PHP, JavaScript, Ruby, Transact-SQL, Assembly, Scheme, Haskell, Others

Posted 03 February 2012 - 03:24 PM

Why don't you just read the width, height, x and y from stdin into 4 variables and pass the appropriate variables to the constructor for rectangle?
• 0

### #3 Norm

Norm

• Senior Member
• 397 posts
• Location:Eastern Florida
• Programming Language:Java, C++, Assembly

Posted 03 February 2012 - 05:07 PM

See the Scanner class's methods for ways to read values from a user.
• 0

### #4 CurlyBonesHopkins

CurlyBonesHopkins

CC Lurker

• New Member
• 7 posts

Posted 03 February 2012 - 06:17 PM

JOptionPane (Java Platform SE 6)
That link will help with the input from the user.

In my opinion you should not have the main application(public static void main(String []args)) within the rectangle class, it should be in its own file separate from the rectangle class

Java: Static/Class methods
• 0

### #5 lethalwire

lethalwire

while(false){ ... }

• Senior Member
• 766 posts
• Programming Language:C, Java, PHP, JavaScript
• Learning:PHP

Posted 03 February 2012 - 08:33 PM

This shouldn't be anything new to you. You've already used Scanner before to read data from the user:
http://forum.codecal...r-initials.html

Get the data from the user and pass it into the Rectangle constructor like you did in part 1.
• 0

### #6 Gman

Gman

CC Resident

• 97 posts
• Location:Donegal, Ireland
• Learning:Java, C#, PHP, (Visual) Basic, JavaScript, Assembly, Others

Posted 04 February 2012 - 03:47 AM

yes its not new to me but getting error's, this is what i done and error is under.... i am just doing it for x so if i find how to do for x i will be able to find y , width and height.

```import java.awt.Rectangle;
import java.util.Scanner;

public class rectangleQ2
{

public static void main(String []args)
{
Scanner kb = new Scanner(System.in);
Rectangle r1 = new Rectangle();

System.out.println("Enter the coordinates for x");
r1 = kb.nextDouble();

double x = r1.getX();

System.out.println("x is " + x);

}

}
```

Error message;

----jGRASP exec: javac rectangleQ2.java

rectangleQ2.java:17: error: incompatible types
r1 = kb.nextDouble();
^
required: Rectangle
found: double
1 error

The little up arrow is between " e & ( " Tell me what i'm doing wrong......

Thanks

Gman
• 0

### #7 chili5

chili5

CC Mentor

• Expert Member
• 3038 posts
• Programming Language:Java, C#, PHP, JavaScript, Ruby, Transact-SQL
• Learning:C, Java, C++, C#, PHP, JavaScript, Ruby, Transact-SQL, Assembly, Scheme, Haskell, Others

Posted 04 February 2012 - 04:10 AM

r1 is a Rectangle object. You can't read a double into it. What I meant was create 4 double variables and read 4 doubles into those with Scanner. Then pass those to the Rectangle constructor.
• 0

### #8 Gman

Gman

CC Resident

• 97 posts
• Location:Donegal, Ireland
• Learning:Java, C#, PHP, (Visual) Basic, JavaScript, Assembly, Others

Posted 04 February 2012 - 04:20 AM

Hey chili5
thanks for getting in on help, no sure what you mean can you show i tried this but not working.
```import java.awt.Rectangle;
import java.util.Scanner;

public class rectangleQ2
{

public static void main(String []args)
{
Scanner kb = new Scanner(System.in);
Rectangle r1 = new Rectangle();

double x =0;

System.out.println("Enter the coordinates for x");
x = kb.nextDouble();

double xv = r1.getX();

System.out.println("x is " + xv);

}

}
```

can you show me
• 0

### #9 chili5

chili5

CC Mentor

• Expert Member
• 3038 posts
• Programming Language:Java, C#, PHP, JavaScript, Ruby, Transact-SQL
• Learning:C, Java, C++, C#, PHP, JavaScript, Ruby, Transact-SQL, Assembly, Scheme, Haskell, Others

Posted 04 February 2012 - 04:29 AM

Sure.

First off, this code:
```
double xv = r1.getX();
```

will always give back 0 since you haven't set the x value in r1.

```public static void main(String []args)
{
Scanner kb = new Scanner(System.in);
Rectangle r1 = new Rectangle();

System.out.println("Enter the coordinates for x");
int x = kb.nextInt();

System.out.println("Enter the coordinates for y");
int y = kb.nextInt();

System.out.println("Enter height:");
int height = kb.nextInt();

System.out.println("Enter width:");
int width = kb.nextInt();

r1 = new Rectangle(x, y, width, height);

double xv = r1.getX();

System.out.println("x is " + xv);
}

```

• 0

### #10 Gman

Gman

CC Resident

• 97 posts
• Location:Donegal, Ireland
• Learning:Java, C#, PHP, (Visual) Basic, JavaScript, Assembly, Others

Posted 04 February 2012 - 04:41 AM

Thank you just need to keep at it now till it sinks in. great help.

Gman
• 0

### #11 chili5

chili5

CC Mentor

• Expert Member
• 3038 posts
• Programming Language:Java, C#, PHP, JavaScript, Ruby, Transact-SQL
• Learning:C, Java, C++, C#, PHP, JavaScript, Ruby, Transact-SQL, Assembly, Scheme, Haskell, Others

Posted 04 February 2012 - 04:42 AM

Yep! Any questions just shout. Does that code I posted make sense to you?
• 0

### #12 Gman

Gman

CC Resident

• 97 posts
• Location:Donegal, Ireland
• Learning:Java, C#, PHP, (Visual) Basic, JavaScript, Assembly, Others

Posted 04 February 2012 - 05:00 AM

Yep! Any questions just shout. Does that code I posted make sense to you?

100% yes i just kept getting errors and it was getting worse than better but you saved the day. Thanks really appreciate your input and i think codecall is great for help to. happy man till next problem

P.s i hope it will help other's too.

Gman
• 0

### Also tagged with one or more of these keywords: java, user input

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download