14 Replies - 5150 Views - Last Post: 05 July 2011 - 02:08 PM Rate Topic: -----

#1 Boeinglover24  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 05-July 11

Calling a method (totally newb question)

Posted 05 July 2011 - 12:21 PM

Hello everyone first post....
Thanks in advance and I tried my best to search the forum but I really am stuck :(

I just started taking JAVA classes and I'm having trouble with this assignment....
My professor gave us this

Create a class Rectangle Functions
that will have main () method
and getArea(), and getPerimeter()methods

In main () ask the user to enter with and height of a rectangle
Then CALL getArea (w,h)
get Perimeter (w,h)
those methods take two parameters and print the results (display)

and I came up with this

import java.util.scanner;

public class Rectangle
{
public static void main(strin arg[])
 {
  Scanner input = new Scanner(System.in);
   {  
     double width;
     double height;

     System.out.print( "Enter the width of the rectangle: ");
     width = input.nextDouble();
     System.out.print( "Enter the height of the rectangle: ");
     height = input.nextDouble();
     System.out.println( "The area of the rectangle is: " +ar);
     System.out.println( " and the perimeter is: " +per); 
     }
     public double getArea()
     {
     double ar;
     ar=width*height;
     return ar;
     }
     public double getPerimeter()
     {
     double per;
     per=Width+width+height+height;
     return per;
     }
  }
}
     



I am coming back with four errors

4 errors found:
File: C:\Users\John\Rectangle.java [line: 19]
Error: C:\Users\John\Rectangle.java:19: illegal start of expression
File: C:\Users\John\Rectangle.java [line: 19]
Error: C:\Users\John\Rectangle.java:19: ';' expected
File: C:\Users\John\Rectangle.java [line: 25]
Error: C:\Users\John\Rectangle.java:25: illegal start of expression
File: C:\Users\John\Rectangle.java [line: 25]
Error: C:\Users\John\Rectangle.java:25: ';' expected

Thanks a bunch...

Is This A Good Question/Topic? 0
  • +

Replies To: Calling a method (totally newb question)

#2 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 9498
  • View blog
  • Posts: 35,845
  • Joined: 12-June 08

Re: Calling a method (totally newb question)

Posted 05 July 2011 - 12:24 PM

Your methods should be OUTSIDE the closing curly brace from the main method. Right now you have methods shoved in a method and that angers java.
Was This Post Helpful? 1
  • +
  • -

#3 Kilorn  Icon User is offline

  • XNArchitect
  • member icon



Reputation: 1356
  • View blog
  • Posts: 3,528
  • Joined: 03-May 10

Re: Calling a method (totally newb question)

Posted 05 July 2011 - 12:25 PM

EDIT: modi123_1 types really fast.

Also, you've got two opening brackets inside your main method which is unnecessary. You should only have one open and close bracket per code block. Beyond taking those double methods out of the main, get rid of those extraneous brackets as well.

This post has been edited by Kilorn: 05 July 2011 - 12:26 PM

Was This Post Helpful? 1
  • +
  • -

#4 Boeinglover24  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 05-July 11

Re: Calling a method (totally newb question)

Posted 05 July 2011 - 12:38 PM

Thank you so much for the super fast help :)
import java.util.scanner;

public class Rectangle
{
public static void main(strin arg[])
  {
  Scanner input = new Scanner(System.in);
     {
     double width;
     double height;

     System.out.print( "Enter the width of the rectangle: ");
     width = input.nextDouble();
     System.out.print( "Enter the height of the rectangle: ");
     height = input.nextDouble();
     System.out.println( "The area of the rectangle is: " +ar);
     System.out.println( " and the perimeter is: " +per); 
     }
     public double getArea()
     {
     double ar;
     ar=width*height;
     return ar;
     }
     public double getPerimeter()
     {
     double per;
     per=Width+width+height+height;
     return per;
     }
  }
  public double getArea()
  {
  double ar;
  ar=width*height;
  return ar;
  }
  public double getPerimeter()
  {
  double per;
  per=Width+width+height+height;
  return per;
  }
}



I'm still returning the same errors.
I'm a total newb so please bear with me :)
like above modi? and kilorn which brackets?
Thank you so much again guys!
Was This Post Helpful? 0
  • +
  • -

#5 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 9498
  • View blog
  • Posts: 35,845
  • Joined: 12-June 08

Re: Calling a method (totally newb question)

Posted 05 July 2011 - 12:41 PM

No. Why do you have copies of "public double getArea()" and "public double getPerimeter()" still inside your main brackets? Get rid of them. Hell get rid of that other set of brackets internal to the main method.

import java.util.scanner;

public class Rectangle
{

public static void main(strin arg[])
  {
  Scanner input = new Scanner(System.in);

     double width;
     double height;

     System.out.print( "Enter the width of the rectangle: ");
     width = input.nextDouble();
     System.out.print( "Enter the height of the rectangle: ");
     height = input.nextDouble();
     System.out.println( "The area of the rectangle is: " +ar);
     System.out.println( " and the perimeter is: " +per); 
  }
  public double getArea()
  {
  double ar;
  ar=width*height;
  return ar;
  }
  public double getPerimeter()
  {
  double per;
  per=Width+width+height+height;
  return per;
  }
}


This post has been edited by modi123_1: 05 July 2011 - 12:42 PM

Was This Post Helpful? 1
  • +
  • -

#6 giggly kisses  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 80
  • View blog
  • Posts: 391
  • Joined: 29-March 09

Re: Calling a method (totally newb question)

Posted 05 July 2011 - 12:44 PM


import java.util.scanner;

public class Rectangle
{
public static void main(strin arg[])
  {
  Scanner input = new Scanner(System.in);
     { //You need to get rid of this bracket
     double width;
     double height;

     System.out.print( "Enter the width of the rectangle: ");
     width = input.nextDouble();
     System.out.print( "Enter the height of the rectangle: ");
     height = input.nextDouble();
     System.out.println( "The area of the rectangle is: " +ar);
     System.out.println( " and the perimeter is: " +per); 
     } //And this bracket.
     
     //This should not be in the main method
     //public double getArea()
     //{
     //double ar;
     //ar=width*height;
     //return ar;
     //}
     //public double getPerimeter()
     //{
     //double per;
     //per=Width+width+height+height;
     //return per;
     //}
  }
  public double getArea()
  {
  double ar;
  ar=width*height;
  return ar;
  }
  public double getPerimeter()
  {
  double per;
  per=Width+width+height+height;
  return per;
  }
}


Was This Post Helpful? 1
  • +
  • -

#7 Mila  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 34
  • View blog
  • Posts: 193
  • Joined: 28-October 06

Re: Calling a method (totally newb question)

Posted 05 July 2011 - 01:00 PM

Just pointing out that it is not semantically wrong to include standalone curly braces. That just provides scope limitation within a code block.

Example:
public void foo() {
  System.out.println("Hi!");
  {
    int x = 99;
    System.out.println(x);
  }
  // x does not exist outside of the code block
}

Was This Post Helpful? 0
  • +
  • -

#8 Boeinglover24  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 05-July 11

Re: Calling a method (totally newb question)

Posted 05 July 2011 - 01:14 PM

oopps forgot to delete those in the main method....
Thank you very much guys.....
But i'm still getting a few errors.
But that was a great leap forward for me :)
import java.util.scanner;

public class Rectangle
{

public static void main(strin arg[])
  {
  Scanner input = new Scanner(System.in);
     
     double width;
     double height;

     System.out.print( "Enter the width of the rectangle: ");
     width = input.nextDouble();
     System.out.print( "Enter the height of the rectangle: ");
     height = input.nextDouble();
     System.out.println( "The area of the rectangle is: " +ar);
     System.out.println( " and the perimeter is: " +per); 
     
   }
   public double getArea()
   {
   double ar;
   ar=width*height;
   return ar;
   }
   public double getPerimeter()
   {
   double per;
   per=width+width+height+height;
   return per;
   }
}



and the errors are...
I reckon height and width needs to be somewhere else..

13 errors found:
File: C:\Users\John\Rectangle.java [line: 1]
Error: C:\Users\John\Rectangle.java:1: cannot find symbol
symbol : class scanner
location: package java.util
File: C:\Users\John\Rectangle.java [line: 6]
Error: C:\Users\John\Rectangle.java:6: cannot find symbol
symbol : class strin
location: class Rectangle
File: C:\Users\John\Rectangle.java [line: 8]
Error: C:\Users\John\Rectangle.java:8: cannot find symbol
symbol : class Scanner
location: class Rectangle
File: C:\Users\John\Rectangle.java [line: 8]
Error: C:\Users\John\Rectangle.java:8: cannot find symbol
symbol : class Scanner
location: class Rectangle
File: C:\Users\John\Rectangle.java [line: 17]
Error: C:\Users\John\Rectangle.java:17: cannot find symbol
symbol : variable ar
location: class Rectangle
File: C:\Users\John\Rectangle.java [line: 18]
Error: C:\Users\John\Rectangle.java:18: cannot find symbol
symbol : variable per
location: class Rectangle
File: C:\Users\John\Rectangle.java [line: 24]
Error: C:\Users\John\Rectangle.java:24: cannot find symbol
symbol : variable width
location: class Rectangle
File: C:\Users\John\Rectangle.java [line: 24]
Error: C:\Users\John\Rectangle.java:24: cannot find symbol
symbol : variable height
location: class Rectangle
File: C:\Users\John\Rectangle.java [line: 30]
Error: C:\Users\John\Rectangle.java:30: cannot find symbol
symbol : variable width
location: class Rectangle
File: C:\Users\John\Rectangle.java [line: 30]
Error: C:\Users\John\Rectangle.java:30: cannot find symbol
symbol : variable width
location: class Rectangle
File: C:\Users\John\Rectangle.java [line: 30]
Error: C:\Users\John\Rectangle.java:30: cannot find symbol
symbol : variable height
location: class Rectangle
File: C:\Users\John\Rectangle.java [line: 30]
Error: C:\Users\John\Rectangle.java:30: incompatible types
found : java.lang.String
required: double
File: C:\Users\John\Rectangle.java [line: 30]
Error: C:\Users\John\Rectangle.java:30: cannot find symbol
symbol : variable height
location: class Rectangle

Thanks in advance...
i know this is extremely newbish but i'm really lost...
if you're getting tired of i'll understand i can just bother my professor later
: )
Was This Post Helpful? 0
  • +
  • -

#9 Kilorn  Icon User is offline

  • XNArchitect
  • member icon



Reputation: 1356
  • View blog
  • Posts: 3,528
  • Joined: 03-May 10

Re: Calling a method (totally newb question)

Posted 05 July 2011 - 01:16 PM

The second error is the easiest. You've typo'd the datatype inside your Main signature. It should be string instead of strin.

EDIT: The last few errors are because you're using variables that you have not yet declared. ar and per aren't declared before they are used in the Main method. You've got them declared and used inside methods that aren't called from the Main method.

This post has been edited by Kilorn: 05 July 2011 - 01:18 PM

Was This Post Helpful? 0
  • +
  • -

#10 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 9498
  • View blog
  • Posts: 35,845
  • Joined: 12-June 08

Re: Calling a method (totally newb question)

Posted 05 July 2011 - 01:21 PM

@Mila: Well yes, that is true. I took an educated guess the OP wasn't caring about crazy scope limitations now. ;)

@OP/#8: Are you even debugging this program or reading the errors? It's starting to feel like you are debugging by proxy.

Why not use something simple like System.In? Scanner seems like an odd choice with your current question set..
Was This Post Helpful? 0
  • +
  • -

#11 Mila  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 34
  • View blog
  • Posts: 193
  • Joined: 28-October 06

Re: Calling a method (totally newb question)

Posted 05 July 2011 - 01:35 PM

View PostBoeinglover24, on 05 July 2011 - 02:14 PM, said:

File: C:\Users\John\Rectangle.java [line: 1]
Error: C:\Users\John\Rectangle.java:1: cannot find symbol
symbol : class scanner

This is because your input statement is not capitalized correctly. It should be import java.util.Scanner; (note that Scanner is capitalized.)

View PostBoeinglover24, on 05 July 2011 - 02:14 PM, said:

location: package java.util
File: C:\Users\John\Rectangle.java [line: 6]
Error: C:\Users\John\Rectangle.java:6: cannot find symbol
symbol : class strin

You spelled String wrong. And forgot to capitalize it.

View PostBoeinglover24, on 05 July 2011 - 02:14 PM, said:

File: C:\Users\John\Rectangle.java [line: 17]
Error: C:\Users\John\Rectangle.java:17: cannot find symbol
symbol : variable ar
location: class Rectangle

You have no variable "ar". I think you forgot a double ar = height * width; or double ar = getArea();.

View PostBoeinglover24, on 05 July 2011 - 02:14 PM, said:

File: C:\Users\John\Rectangle.java [line: 18]
Error: C:\Users\John\Rectangle.java:18: cannot find symbol
symbol : variable per
location: class Rectangle

Same as "ar" except you have no "per" method.

The rest of the errors are because you are trying to use variables that you have not yet declared within the getArea and getPerimeter methods.

Mila
Was This Post Helpful? 0
  • +
  • -

#12 giggly kisses  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 80
  • View blog
  • Posts: 391
  • Joined: 29-March 09

Re: Calling a method (totally newb question)

Posted 05 July 2011 - 01:55 PM

You are getting these errors because you are trying to access a variable that is not in the scope of the main method. You aren't using the methods that you created either and you don't have a instance of your Rectangle class.

I'm not sure if you're understanding object oriented programming and how it works. This Rectangle class that you created is a blueprint of a class. Everything is the Rectangle class tells an instance of the Rectangle class how to behave. Think of a Rectangle as a special data type that you created to hold specific data in a specific way. A Rectangle has two important attributes, they are the height and the width. When you create a Rectangle class you want to store this data to be accessed later, you do this by creating a constructor or creating setters (special methods used to set a field in your Rectangle class).

You have two options to make your code work, one is to create an instance of your Rectangle class and modify it a bit (which I will show you how after this) and the second option is to make your getArea() and getPerimeter() methods static so you don't need an instance of the Rectangle class to use them. This is how you would do the latter:

import java.util.Scanner;

public class Rectangle
{

public static void main(string arg[])
  {
     Scanner input = new Scanner(System.in);
     double width;
     double height;

     System.out.print( "Enter the width of the rectangle: ");
     width = input.nextDouble();
     System.out.print( "Enter the height of the rectangle: ");
     height = input.nextDouble();
     System.out.println( "The area of the rectangle is: " + Rectangle.getArea(width, height));
     System.out.println( " and the perimeter is: " + Rectangle.getPerimeter(width, height)); 
     
   }

   //You didn't allow for your getArea() method to get any data from the main method.
   //By adding getArea(double width, double height) you can now pass data from the main
   //method to the getArea() method for it to do the calculation on.
   public static double getArea(double width, double height)
   {
   double ar;
   ar=width*height;
   return ar;
   }


   //This method had the same problem.
   public static double getPerimeter(double width, double height)
   {
   double per;
   per=width+width+height+height;
   return per;
   }
}



By adding the keyword static in front of the keyword public and behind the keyword double you are now able to call these methods without creating an instance of the Rectangle class. The second option you have is to modify your code so it encapsulates the data into an Object that you can create as many instances of as you would like. Let me show you an example:


public class Rectangle {
	//These are the fields used to store the width and height of your Rectangle object.
	private double width, height;
	
	//Constructor for the class, used to set up all data when a 
	//Rectangle object is created. This default constructor creates
	//a Rectangle object with a width and height of 1.0
	public Rectangle() {
		width = 1.0;
		height = 1.0;
	}
	
	
	//This is another constructor for the Rectangle class that accepts
	//two arguments that are of the type double. This is what the you
	//will use when you know what the width and height of the Rectangle
	//object is when you create it. The constructor takes w and h and 
	//sets them equal to width and height respectivley.
	public Rectangle(double w, double h) {
		width = w;
		height = h;
	}
	
	
	//This is a setter method, it is used to change the value of width
	//to whatever w is.
	public void setWidth(double w) {
		width = w;
	}
	
	//This is a setter method, it is used to change the value of height
	//to whatever h is.
	public void setHeight(double h) {
		height = h;
	}
	
	//This is a getter method, it is used to retrieve the value of width
	//at the current time.
	public double getWidth() {
		return width;
	}
	
	//This is a getter method, it is used to retrieve the value of height
	//at the current time.
	public double getHeight() {
		return height;
	}
	
	
	//Here is the getArea method that you created before. It uses the 
	//width and height field that is stored in your rectangle object to
	//calculate the area.
	public double getArea(){
	   double ar;
	   ar=width*height;
	   return ar;
	}
	
	
	//Here is the getPerimeter method that you created before. It uses the 
	//width and height field that is stored in your rectangle object to
	//calculate the perimeter.
	public double getPerimeter(){
	   double per;
	   per=width+width+height+height;
	   return per;
	}
}




Now that we have a Rectangle class we can start to create instances of it. Here is an example of how you would go about creating a few instances of the Rectangle class and use its methods:

import java.util.*;

public class test {
	
	public static void main(String[] args) {
		double area, perimeter;
		
		//This instance of Rectangle, called rec1, uses the default
		//constructor for our class, thus it sets width and height
		//to 1.0
		Rectangle rec1 = new Rectangle();
		
		//This instance of Rectangle, called rec2, uses the second
		//constructor from our class it sets the width to 2.3 and the
		//height to 1.3
		Rectangle rec2 = new Rectangle(2.3, 1.3);
		
		Scanner keyboard = new Scanner(System.in);
		
		
		//This asks the user to enter a double for the width of rec1
		//and sets rec1's width equal to the input by using the setWidth() method
		System.out.println("Enter the width for rec1:");
		rec1.setWidth(keyboard.nextDouble());
		
		
		//This asks the user to enter a double for the height of rec1
		//and sets rec1's height equal to the input by using the setHeight() method
		System.out.println("Enter the height for rec1:");
		rec1.setHeight(keyboard.nextDouble());
		
		//Now that the width and height have been set in rec1 were going to calculate the area
		//and Perimeter of that Rectangle object.
		area = rec1.getArea();
		perimeter = rec1.getPerimeter();
		
		System.out.println("The area for rec1 is: " + area);
		System.out.println("The pereimeter for rec1 is: " + perimeter);
		
		//We can do the same thing with rec2 and get different results since they are different
		//Rectangle objects and hold different data.
		System.out.println("The area for rec2 is: " + rec2.getArea());
		System.out.println("The pereimeter for rec2 is: " + rec2.getPerimeter());
	}
}



Here is what the program looks like when it runs and I enter 2 and 3 for input.

Quote

Enter the width for rec1:
2
Enter the height for rec1:
3
The area for rec1 is: 6.0
The pereimeter for rec1 is: 10.0
The area for rec2 is: 2.9899999999999998
The pereimeter for rec2 is: 7.199999999999999

This post has been edited by giggly kisses: 05 July 2011 - 01:58 PM

Was This Post Helpful? 0
  • +
  • -

#13 mahi.aw  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 114
  • Joined: 21-May 11

Re: Calling a method (totally newb question)

Posted 05 July 2011 - 02:00 PM

View PostBoeinglover24, on 05 July 2011 - 12:21 PM, said:

Hello everyone first post....
Thanks in advance and I tried my best to search the forum but I really am stuck :(

I just started taking JAVA classes and I'm having trouble with this assignment....
My professor gave us this

Create a class Rectangle Functions
that will have main () method
and getArea(), and getPerimeter()methods

In main () ask the user to enter with and height of a rectangle
Then CALL getArea (w,h)
get Perimeter (w,h)
those methods take two parameters and print the results (display)

and I came up with this

import java.util.scanner;

public class Rectangle
{
public static void main(strin arg[])
 {
  Scanner input = new Scanner(System.in);
   {  
     double width;
     double height;

     System.out.print( "Enter the width of the rectangle: ");
     width = input.nextDouble();
     System.out.print( "Enter the height of the rectangle: ");
     height = input.nextDouble();
     System.out.println( "The area of the rectangle is: " +ar);
     System.out.println( " and the perimeter is: " +per); 
     }
     public double getArea()
     {
     double ar;
     ar=width*height;
     return ar;
     }
     public double getPerimeter()
     {
     double per;
     per=Width+width+height+height;
     return per;
     }
  }
}
     



I am coming back with four errors

4 errors found:
File: C:\Users\John\Rectangle.java [line: 19]
Error: C:\Users\John\Rectangle.java:19: illegal start of expression
File: C:\Users\John\Rectangle.java [line: 19]
Error: C:\Users\John\Rectangle.java:19: ';' expected
File: C:\Users\John\Rectangle.java [line: 25]
Error: C:\Users\John\Rectangle.java:25: illegal start of expression
File: C:\Users\John\Rectangle.java [line: 25]
Error: C:\Users\John\Rectangle.java:25: ';' expected

Thanks a bunch...


Hie,,,here is the way to go!!!

import java.util.Scanner;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

public class rectangle {

    public double getArea(double width, double height) {
        double ar;
        ar = width * height;
        return ar;
    }

    public double getPerimeter(double Width, double height) {
        double per;
        per = Width + Width + height + height;
        return per;
    }

    public static void main(String args[]) {

        /*get the input from user*/
        System.out.print("Enter the variables on same line with spaces in between: width & Heigth");


        Scanner input = new Scanner(System.in);

        double width = Double.valueOf(input.next());
        double heigth = Double.valueOf(input.next());

        System.out.println(width);
        System.out.println(heigth);

        rectangle ob1 = new rectangle();
        double area = ob1.getArea(width, heigth);
        double peri = ob1.getPerimeter(width, heigth);

        System.out.println("area is " + area);
        System.out.println("peri is " + peri);


    }
}




just copy paste whole code in one of the file:::do not forget to rename the name of the file as the class name..
enjoy
Was This Post Helpful? -1
  • +
  • -

#14 Mila  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 34
  • View blog
  • Posts: 193
  • Joined: 28-October 06

Re: Calling a method (totally newb question)

Posted 05 July 2011 - 02:03 PM

@mahi.aw - Maybe instead of just giving away the answer, you could help the OP understand why it's the right answer?
Was This Post Helpful? 0
  • +
  • -

#15 mahi.aw  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 114
  • Joined: 21-May 11

Re: Calling a method (totally newb question)

Posted 05 July 2011 - 02:08 PM

View PostMila, on 05 July 2011 - 02:03 PM, said:

@mahi.aw - Maybe instead of just giving away the answer, you could help the OP understand why it's the right answer?


hie mila,,
yes i think you are right,,
from the next time onward will do the way you suggested!!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1