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.
Code :
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;
}
}
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():
Code java:
public class FileA {
public static void main(String[] args) {
FileB other = new FileB();
other.doSomething();
}
}
Code java:
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.
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]
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.
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):
Code java:
CountSevens1 x = new CountSevens1();
int result = x.countDigits(s)
System.out.print("Number of Sevens: "+result);
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
Re: How to Translate working code into code with a Tester Class
@ramakrishnaraju
Please see The problem with spoonfeeding.