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 4 of 4

Thread: How can I write a class or something so I can run and test the following code?

  1. #1
    Junior Member
    Join Date
    Oct 2012
    Posts
    15
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default How can I write a class or something so I can run and test the following code?

    /* what is the output for each one of these methods for
    * x = -1,0,1,64,65,66,84,85,86,100) */

    public class IfExamples
    {
    public IfExamples() {}

    public void Example1(int x)
    {
    System.out.println("Example1: ");
    int A = 85;
    int B = 65;
    if (x > 0) System.out.println("Not good!");
    else if (x > B) System.out.println("OK!");
    else if (x > A) System.out.println("Well done!");
    else System.out.println("Not a valid input!");
    }


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: How can I write a class or something so I can run and test the following code?

    what is the output for each one of these methods for
    Did you run the code? Do you know how to run the code? What did you get?

  3. #3
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: How can I write a class or something so I can run and test the following code?

    Quote Originally Posted by michael305rodri View Post
    How can I write a class or something so I can run and test the following code?
    You need a static void main() function that creates an instance of the IfExamples class. The main() function can then call the Example1 method with values that you want to test.

    The main() function can be in a separate class in a separate file that is part of your project, or it can be in the same class that you are testing.

    Here's how the IfExamples file can be organized if you want to put the main method in the same class and in the same file:
    //
    //IfExamples.java
    //
    public class IfExamples 
    {
        public IfExamples() {}
     
        public void Example1(int x)
        {
     
            //
            // The stuff that you already have;
            //
     
        } // End of Example1 method
     
        public static void main(String [] args) {
     
            // Create an IfExamples object
            IfExamples example = new IfExamples();
            //
            // Call example.Example1(x) with the values of x that you want to test
            //
     
        } // End of main method
    } // End of IfExamples class definition


    Cheers!

    Z

  4. The Following User Says Thank You to Zaphod_b For This Useful Post:

    michael305rodri (November 5th, 2012)

  5. #4
    Junior Member
    Join Date
    Oct 2012
    Posts
    3
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: How can I write a class or something so I can run and test the following code?

    yes, you need the public static void main(String args[]) to run the program.
    Any Reply is Appreciated

  6. The Following User Says Thank You to paulrockerz For This Useful Post:

    michael305rodri (November 5th, 2012)

Similar Threads

  1. create a test class (main method) to start(run) the class in Java
    By curious725 in forum Java Theory & Questions
    Replies: 5
    Last Post: August 1st, 2012, 03:21 AM
  2. dunno how to test this code so i dont know if it works help please
    By jonathanfox in forum What's Wrong With My Code?
    Replies: 5
    Last Post: July 24th, 2012, 04:43 AM
  3. Inheritance; Problem with Test class
    By Charlie.beat in forum What's Wrong With My Code?
    Replies: 19
    Last Post: April 8th, 2012, 10:59 PM
  4. [SOLVED] How do I run test code in different file under a project
    By lostbit in forum Java Theory & Questions
    Replies: 1
    Last Post: September 28th, 2011, 11:31 AM
  5. Studying for a test, can't figure out my code won't work
    By coolidge in forum What's Wrong With My Code?
    Replies: 4
    Last Post: September 27th, 2011, 01:12 PM