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: Help requested - testing a class with a tester class, no methods allowed.

  1. #1
    Junior Member
    Join Date
    Feb 2011
    Posts
    5
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Help requested - testing a class with a tester class, no methods allowed.

    I have an assignment where I am to test a class I've written by writing another class called Tester. Tester can have no instance variables or methods, everything has to be done in one constructor.

    If I need to share the main class I will, but I'd rather just try to describe my issue so I can get the methodology, and try to figure out what to do on my own.

    Here's the two methods of interest that I am trying to test:

    public void setTime(int hour, int minute)
        {
            hours.setValue(hour);
            minutes.setValue(minute);
            updateDisplay();
            updateDisplayUS();
        }
     
    public String getUSTime()
        {
            return displayStringUS;
        }

    What Tester needs to do is print out what is stored in displayStringUS to check it against the time I have entered into clock.setTime() in my Tester class:

    public Tester()
        {
            ClockDisplay clock = new ClockDisplay();
     
            clock.setTime(00, 00);
            clock.getUSTime();
            System.out.println(*****);
         }

    In a nutshell, I'm trying to test what is entered in clock.setTime manually against what the code from the ClockDisplay class generates. I need to do this for 8 separate time tests.

    I have no idea how to do this. I've Googled for an hour now, but I guess I just simply lack the vocabulary at this point to search for the proper syntax.


  2. #2
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Help requested - testing a class with a tester class, no methods allowed.

    Ok, so I guess my question would be: Is tester supposed to be an object or the executing class (the main)?


    As for this code:
    public Tester()
        {
            ClockDisplay clock = new ClockDisplay();
     
            clock.setTime(00, 00);
            clock.getUSTime();
            System.out.println(*****);
         }
    Consider your ClockDisplay.getUSTime() method. This method returns a String, and that is all it does. You call this method in the code above, but you do not capture the value returned by this method.

    To capture the value, then print it out, you want to say:
    String value = clock.getUSTime();
    System.out.println(value);

    If your teacher wants you to not set the result to a variable (ignore that this is bad practice) and instead just print out the result, you would say:
    System.out.println(clock.getUSTime());


    Am I anywhere near what you are asking or did I miss the mark?
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  3. The Following 2 Users Say Thank You to aussiemcgr For This Useful Post:

    javapenguin (February 22nd, 2011), miketeezie (February 21st, 2011)

  4. #3
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Help requested - testing a class with a tester class, no methods allowed.

    Hmmmmm....well I compiled and that settles it.

    Your Tester class won't do much.

    I tried to run a class I quickly made up that just printed something out and it refused to run without a main method.

    Are you making an applet by an chance?

    If not, you must have at least 1 method to get Tester to do anything other than return

    java.lang.NoSuchMethodError: main:
    Exception in thread "main"


    public Tester()
        {
    public static void main(String[] args)
    {
            ClockDisplay clock = new ClockDisplay();
     
            clock.setTime(00, 00);
          String str =   clock.getUSTime();
            System.out.println(str);
    }
         }
    Last edited by javapenguin; February 21st, 2011 at 10:04 PM.

  5. #4
    Junior Member
    Join Date
    Feb 2011
    Posts
    5
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Help requested - testing a class with a tester class, no methods allowed.

    System.out.println(clock.getUSTime());

    That's it exactly! I didn't realize the compiler would allow you to put the all that into a print statement.

    My Tester class now verifies that my methods from my main ClockDisplay class do indeed work, and prints them out to the terminal.

    Thanks so much! I'm learning a ton here.

Similar Threads

  1. Use of Exceptions and Methods of The String Class
    By cagataylina in forum Exceptions
    Replies: 1
    Last Post: April 26th, 2011, 01:56 AM
  2. [SOLVED] Help with Class Methods(Probably simple)
    By ShakeyJakey in forum Object Oriented Programming
    Replies: 9
    Last Post: May 27th, 2010, 09:53 AM
  3. Methods in java class files are not recognized.
    By Girish in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 18th, 2010, 05:44 AM
  4. Replies: 0
    Last Post: April 11th, 2010, 08:56 AM
  5. using the setSize and setLocation methods of Rectangle class
    By etidd in forum Java Theory & Questions
    Replies: 1
    Last Post: March 6th, 2010, 04:03 PM