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

Thread: How to Translate working code into code with a Tester Class

  1. #1
    Junior Member
    Join Date
    May 2012
    Posts
    17
    My Mood
    Stressed
    Thanks
    15
    Thanked 0 Times in 0 Posts

    Default How to Translate working code into code with a Tester Class

    I have this code: it works and it properly runs. I just do not know how to make it have 2 classes and make a call. Sorry for the beginner question. Thank you for your help.

    import java.util.Scanner;
     
    /**
        Counts the number of digits with value 7 in the 
         decimal representation of the integer n
    */
     
    public class CountSevens1
    {
        public static void main(String[] args)
        {
            String s = "";    
            Scanner in = new Scanner(System.in);
            System.out.print("Enter digits: ");
            int n = in.nextInt();
            s = Integer.toString(n);
     
            //your work here
            System.out.println(countDigits(s));
        }
     
        public static int countDigits(String s) 
        {
            int count = 0;
     
            for (int i = 0; i < s.length(); i++)
            {
                if (s.charAt(i) == '7') 
                {
                    count++;
                }
            }
            return count;
        }
    }


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

    Default Re: How to Translate working code into code with a Tester Class

    I think your question is about how to make two class files communicate with each other. At this basic level, simply create two files, put them in the same folder, compile both of them, and run the class that contains the main(String[] args).

    For example, let's say we have two files: FileA and FileB. FileA contains just the main, and FileB contains method: doSomething():
    public class FileA {
         public static void main(String[] args) {
              FileB other = new FileB();
              other.doSomething();
         }
    }
    public class FileB {
         public void doSomething() {
              return; //does nothing
         }
    }

    Now, as long as both of those files are in the same folder, are both compiled, and you run FileA, it will work with no worries.
    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 User Says Thank You to aussiemcgr For This Useful Post:

    bankoscarpa (May 3rd, 2012)

  4. #3
    Junior Member
    Join Date
    May 2012
    Posts
    17
    My Mood
    Stressed
    Thanks
    15
    Thanked 0 Times in 0 Posts

    Default Re: How to Translate working code into code with a Tester Class

    I did what you said and i keep getting the same error. this is what i have in the tester class.[CODE]import java.util.Scanner;
    public class CountSevens1Tester
    {
    public static void main(String[] args)
    { String s = "";
    Scanner in = new Scanner(System.in);
    System.out.print("Enter digits: ");
    int n = in.nextInt();
    s = Integer.toString(n);
    CountSevens1 x = new CountSevens1();
    x.countDigits();
    }
    }[CODE]

  5. #4
    Junior Member
    Join Date
    May 2012
    Posts
    17
    My Mood
    Stressed
    Thanks
    15
    Thanked 0 Times in 0 Posts

    Default Re: How to Translate working code into code with a Tester Class

    I now have the tester down to this:

    import java.util.Scanner;
    public class CountSevens1Tester
    {
    public static void main(String[] args)
    { String s = "";
    Scanner in = new Scanner(System.in);
    System.out.print("Enter digits: ");
    int n = in.nextInt();
    s = Integer.toString(n);
    CountSevens1 x = new CountSevens1();
    x.countDigits(s);
    System.out.print("I do not know what to put here to return the number of sevens.");
    }
    }

    I am not sure what to put in the last line.

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

    Default Re: How to Translate working code into code with a Tester Class

    What error were you getting earlier?

    If you want to print out the result, you can do this (assuming CountSevens1.countDigits(s) returns an int):
    CountSevens1 x = new CountSevens1();
    int result = x.countDigits(s)
    System.out.print("Number of Sevens: "+result);
    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/

  7. The Following User Says Thank You to aussiemcgr For This Useful Post:

    bankoscarpa (May 4th, 2012)

  8. #6
    Junior Member
    Join Date
    Oct 2012
    Posts
    2
    My Mood
    Cool
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to Translate working code into code with a Tester Class

    code removed by moderator
    //this is rama krishna raju .if u like this reply me
    Last edited by jps; October 15th, 2012 at 02:13 PM. Reason: removed code

  9. #7
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: How to Translate working code into code with a Tester Class

    @ramakrishnaraju
    Please see The problem with spoonfeeding.

Similar Threads

  1. Dog Tester Class
    By mwardjava92 in forum Object Oriented Programming
    Replies: 4
    Last Post: February 11th, 2012, 05:33 PM
  2. Translate Turbo Pascal code in Java
    By Mercenaire in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 4th, 2011, 04:52 AM
  3. Replies: 3
    Last Post: April 13th, 2011, 03:30 PM
  4. Help requested - testing a class with a tester class, no methods allowed.
    By miketeezie in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 21st, 2011, 10:40 PM
  5. Translate Java Code Into English
    By drkossa in forum Java Theory & Questions
    Replies: 4
    Last Post: November 27th, 2009, 02:52 PM