Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 13 of 13

Thread: Error: Main method not found! Please help!

  1. #1
    Junior Member
    Join Date
    Nov 2012
    Posts
    22
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Error: Main method not found! Please help!

    My Assignment: Write a class named Rectangle to represent rectangles. The data fields are width, height, and color. Use double for width and height, and String for color. Suppose that all the rectangles are the same color. You need to provide the accessor methods for the properties and a findArea() method for computing the area of the rectangle.
    Write a client program(test class) to test the class Rectangle. In the client program, create two Rectangle objects. Assign any width and height to the two objects. Assign the first object the color red, and the second, yellow. Display the properties of both objects and find their areas.

    Some of the codes of the main class rectangle were already provided by my instructor. I did the rest but still getting the main method not found error.


    public class Rectangle {
    private double width = 1;
    private double height = 1;
    private static String color = "white";
     
                    public Rectangle( ) {
     
                            }
     
                    public Rectangle ( double widthParam, double heightParam, String colorParam ) { }
     
                    public double getWidth ( ) { 
                    return width;  
                    }  
     
     
     
     
                    public void setWidth ( double widthParam ) {
                                        if ((width >= 0) || (width <0))
                                                {
                                                    System.out.println("Fatal error");
                                                    System.exit(0);
                                                }
                                        else {
                                              this.width = width;
                                          }
                                      }
     
     
     
     
                    public double getHeight ( ) { 
                                return height;
                                }
     
     
     
                    public void setHeight ( double heightParam ) { 
                                    if ((height >= 0) || (height <0))
                                              {
                                                  System.out.println("Fatal error");
                                                  System.exit(0);
                                              }
                                      else {
                                            this.height = height;
                                        }}
     
     
     
     
                    public String getColor ( ) {
                    return color;
                    }
     
                    public void setColor ( String colorParam ) { }
     
                    public double findArea ( ) {
                    double area = width * height;    
                    return area;
                    }
     
        @Override
                    public String toString ( ) { 
                    return ("Deafult height " + height + "Default width " + width + "Default color " + color);
                    }
     
     
    }      
    class Test{
    public static void main(String[] args){
    //invoking no-arg constructor
    Rectangle mr = new Rectangle();
    mr.setHeight(2.0);
    mr.setWidth(3.0);
    double area1 = mr.findArea();
     
    //invoking parameterized constructor
    Rectangle mr2 = new Rectangle(2.0,3.0,"yellow");
    double area2=mr.findArea();
     
    //printing areas
    System.out.println(area1);
    System.out.println(area1);
    }
     
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Error: Main method not found! Please help!

    getting the main method not found error.
    What is the commandline you are using to execute the program?

    The formatting of the Test class does not following coding standards. The nested statements should be indented 3-4 spaces. See the Rectangle class for how to do it.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Nov 2012
    Posts
    22
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Error: Main method not found! Please help!

    Quote Originally Posted by Norm View Post
    What is the commandline you are using to execute the program?

    The formatting of the Test class does not following coding standards. The nested statements should be indented 3-4 spaces. See the Rectangle class for how to do it.
    Thanks, I know. Will change that.

    Got hold of the error, I was creating two classes in 1 file, separated the Test class. But I cannot dsiplay color and area. Output is 1.0 and 1.0 in the first two lines respectively.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Error: Main method not found! Please help!

    Is the program executing now?
    I cannot dsiplay color and area. Output is 1.0 and 1.0 in the first two lines respectively.
    Copy and paste here the full contents of the program's output.

    If the output is wrong, please explain what it should be and provide some example of correct output.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Error: Main method not found! Please help!

    Please don't cross-post the same question in multiple forum without notifying all forums that you post in that you're doing this. This is not fair to us or the volunteers in the other fora, and can get you placed on do-not-help lists if it continues.

  6. #6
    Junior Member
    Join Date
    Nov 2012
    Posts
    22
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Error: Main method not found! Please help!

    Quote Originally Posted by Norm View Post
    Is the program executing now?

    Copy and paste here the full contents of the program's output.

    If the output is wrong, please explain what it should be and provide some example of correct output.
    Okay, this is my rectangle.java file code,

    public class Rectangle {
    private double width = 1;
    private double height = 1;
    private static String color = "white";
     
                    public Rectangle( ) {
     
                            }
     
                    public Rectangle ( double widthParam, double heightParam, String colorParam ) { }
     
                    public double getWidth ( ) { 
                    return width;  
                    }  
     
     
     
     
                    public void setWidth ( double widthParam ) {
                                        if (width <= 0) 
                                                {
                                                    System.out.println("Fatal error");
                                                    System.exit(0);
                                                }
                                        else {
                                              this.width = width;
                                          }
                                      }
     
     
     
     
                    public double getHeight ( ) { 
                                return height;
                                }
     
     
     
                    public void setHeight ( double heightParam ) { 
                                    if (height <= 0) 
                                              {
                                                  System.out.println("Fatal error");
                                                  System.exit(0);
                                              }
                                      else {
                                            this.height = height;
                                        }}
     
     
     
     
                    public String getColor ( ) {
                    return color;
                    }
     
                    public void setColor ( String colorParam ) { }
     
                    public double findArea ( ) {
                    double area = width * height;    
                    return area;
                    }
                    public String toString ( ) { 
                    return ("Deafult height " + height + "Default width " + width + "Default color " + color);
                    }
     
     
     
     
    }


    and this is the Test class,

    public class TestRectangle {
     
    public static void main(String[] args){
                //invoking no-arg constructor
                Rectangle mr = new Rectangle();
                mr.setHeight(2.0);
                mr.setWidth(3.0);
                mr.setColor("Green");
                double area1 = mr.findArea();
     
                //invoking parameterized constructor
                Rectangle mr2 = new Rectangle(2.0,3.0,"yellow");
                double area2=mr.findArea();
     
                //printing areas
                System.out.println(area1);
                System.out.println(area2);
                }
     
                }

    There are no errors but what I want to output is area1 and area2 as you see in the Test class that has different values and also a way to set a different color for mr1 and mr2 in Test class

    But what I am getting is 1.0(1st line) and 1.0(2nd line)

    Thanks!

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Error: Main method not found! Please help!

    what I am getting is 1.0(1st line) and 1.0(2nd line)
    What values does the findArea() method compute and return? Look at that method to see what it does.
    If you don't understand my answer, don't ignore it, ask a question.

  8. #8
    Junior Member
    Join Date
    Nov 2012
    Posts
    22
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Error: Main method not found! Please help!

    Quote Originally Posted by Norm View Post
    What values does the findArea() method compute and return? Look at that method to see what it does.
    Tried this code System.out.println(findArea); error comes up saying "Cannot find symbol"

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Error: Main method not found! Please help!

    The findArea() I was talking about is a method. Notice the () I added at the end of the name.
    Use your editor's Find or Search function to find where the findArea() method is defined and where it is called.
    If you don't understand my answer, don't ignore it, ask a question.

  10. #10
    Junior Member
    Join Date
    Nov 2012
    Posts
    22
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Error: Main method not found! Please help!

    Got it, its the same value 1.0. Tried with different initial values in height and width, findarea is good but my test class is not giving me the values I assigned in that class; all what I am getting is the default findarea value!

  11. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Error: Main method not found! Please help!

    Where does the test class pass values to the Rectangle class? What does the Rectangle class do with the values it receives?
    If you don't understand my answer, don't ignore it, ask a question.

  12. #12
    Junior Member
    Join Date
    Nov 2012
    Posts
    22
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Error: Main method not found! Please help!

    Quote Originally Posted by Norm View Post
    Where does the test class pass values to the Rectangle class? What does the Rectangle class do with the values it receives?
    Sorry I am not getting you here. Did you find an error in my code? I have no idea what I should change in my code!

  13. #13
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Error: Main method not found! Please help!

    Sorry I am not getting you here
    You need to look at the code. Where are the Rectangle class objects created? Hint: objects are created with the new statement.
    See the tutorial on constructors:
    Providing Constructors for Your Classes (The Java™ Tutorials > Learning the Java Language > Classes and Objects)
    Passing Information to a Method or a Constructor (The Java™ Tutorials > Learning the Java Language > Classes and Objects)
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replies: 26
    Last Post: February 10th, 2013, 12:59 PM
  2. Why am I getting a <No Main Classes Found> error message?? Help please!!
    By Rick Sebastian in forum What's Wrong With My Code?
    Replies: 13
    Last Post: February 9th, 2013, 07:04 PM
  3. Symbol not found error
    By ayuda96 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: July 25th, 2012, 04:48 PM
  4. Replies: 3
    Last Post: October 31st, 2011, 12:42 AM
  5. Main Class Not Found Problem
    By shadow in forum Java Theory & Questions
    Replies: 3
    Last Post: September 29th, 2009, 09:42 AM