-
February 20th, 2013, 08:59 PM
#1
need help soon on error.
People your help would be appreciate very much. I just started working 15 hours a day and dont have alot of time for coding, so your help will be very much appreciate. below is the code i have now and i cant figure out what i have done wrong.
The error i am gett is below the code.............
Code:
import java.util.Scanner;
public class SquareObject {
// This is an example of how to return an object FROM a method
// and pass that same object TO another method
public static void main(String[] args) {
Square s; // declare an object reference variable of type Square
int area;
SquareObject Square = new SquareObject();
// Call a method, getSquare(), to create and return a new Square object
s = getSquare();
// Pass the new Square to a method to calculate the area
area = sqArea(Square);
System.out.println("The Area of the new Square is: " + area);
} // end of main method
// Other methods in the driver class
public static Square getSquare() {
// This method prompts the user for the size of a Square, and
// instantiates and returns a new Square object of that size
int sideLen;
s UserSquare; // declare an object reference variable of type Square
Scanner scan = new Scanner(System.in);
System.out.println("What size square do you want -- i.e. the length " +
"of each side? ") ;
sideLen = scan.nextInt(); // get the user's entry
userSquare = new s(sideLen); //found error on new usersquare //instantiate a Square of requested size
return ; // return the new object
} // end of getSquare method
public static int sqArea(SquareObject Square) {
// This method receives a Square object as a parameter
int area;
area = s.len * s.len;
return area;
} // end of sqArea method
} // end of the first (driver) class
class Square { // start of the Square (instance) class
int len;
// Constructor method - creates a square of a given size
public Square(int side) {
len = side;
System.out.println("Creating a square of size " + len);
}
} // end of Square class
I am fairly new to Java and i am a little confused on this
output: errors
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
s cannot be resolved to a type
userSquare cannot be resolved to a variable
s cannot be resolved to a type
This method must return a result of type Square
at SquareObject.getSquare(SquareObject.java:23)
at SquareObject.main(SquareObject.java:11)
Thank you all in advance for helping me
-
February 21st, 2013, 04:25 AM
#2
Re: need help soon on error.
First problem is in the sqArea function. You pass it a SquareObject argument, but it looks as if the argument should be a Square ... and not the Square class itself but an instance of the Square class ... i.e.
Code:
/**
* This method receives a Square object as a parameter.
*/
public static int sqArea(Square s) {
int area;
area = s.len * s.len;
return area;
} // end of sqArea method
Line 9
Code:
SquareObject Square = new SquareObject();
A variable named "Square" of type SquareObject is never used, plus it could be confusing whether Square is an instance of SquareObject or the class Square defined later on. Get rid of this line.
Line 13
Code:
s = getSquare();
area = sqArea(Square);
You need to be passing an instance of Square to the sqArea function, not the type itself ... i.e.
Code:
s = getSquare();
area = sqArea(s);
In the getSquare method,
Code:
s UserSquare; // declare an object reference variable of type Square
is trying to create a variable names UserSquare of type s ... s is not defined. That is what the compiler is telling you.
You probably want a variable named userSquare (note the lower case u ... good convention). Then when you construct it having got the side length, create a new Square(sideLen) not a new s(sideLen). i.e.
Code:
sideLen = scan.nextInt(); // get the user's entry
userSquare = new Square(sideLen);
Finally, the getSquare function needs to return an instance of the Square class - the one that has just been initialised.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|