有道java题:
Write a program that prints the values
1
10
100
1000
10000
100000
1000000
10000000
100000000
1000000000
10000000000
100000000000
Implement a class
public class PowerGenerator
{
/**
Constructs a power generator.
@param aFactor the number that will be multiplied by itself
*/
public PowerGenerator(int aFactor) { . . . }
/**
Computes the next power.
*/
public double nextPower() { . . . }
. . .
}
Then supply a test class PowerGeneratorRunner that calls
System.out.println(myGenerator.nextPower()) twelve times.
Use the following class as your main class:
public class PowerGeneratorRunner
{
public static void main(String[] args)
{
PowerGenerator myGenerator = new PowerGenerator(10);
for (int i = 1; i <= 12; i++)
System.out.println(myGenerator.nextPower());
}
}
请给个答案,谢谢
Write a program that prints the values
1
10
100
1000
10000
100000
1000000
10000000
100000000
1000000000
10000000000
100000000000
Implement a class
public class PowerGenerator
{
/**
Constructs a power generator.
@param aFactor the number that will be multiplied by itself
*/
public PowerGenerator(int aFactor) { . . . }
/**
Computes the next power.
*/
public double nextPower() { . . . }
. . .
}
Then supply a test class PowerGeneratorRunner that calls
System.out.println(myGenerator.nextPower()) twelve times.
Use the following class as your main class:
public class PowerGeneratorRunner
{
public static void main(String[] args)
{
PowerGenerator myGenerator = new PowerGenerator(10);
for (int i = 1; i <= 12; i++)
System.out.println(myGenerator.nextPower());
}
}
请给个答案,谢谢
{
private long result; /**
Constructs a power generator.
@param aFactor the number that will be multiplied by itself
*/
public PowerGenerator(int aFactor) { result = aFactor}
/**
Computes the next power.
*/
public double nextPower() { result=result*result }
. . .
}
Then supply a test class PowerGeneratorRunner that calls
System.out.println(myGenerator.nextPower()) twelve times.
Use the following class as your main class:
public class PowerGeneratorRunner
{
public static void main(String[] args)
{
PowerGenerator myGenerator = new PowerGenerator(10);
for (int i = 1; i <= 12; i++)
System.out.println(myGenerator.nextPower());
}
}
final static double FT1=0.1;
double aFactor;
/**
Constructs a power generator.
@param aFactor the number that will be multiplied by itself
*/
public PowerGenerator(int aFactor){
this.aFactor=aFactor*FT1*FT1;
}
/**
Computes the next power.
*/
public long nextPower(){
aFactor=aFactor/FT1;
return (long)aFactor;
}
}public class PowerGeneratorRunner {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
PowerGenerator myGenerator = new PowerGenerator(10);
for (int i = 1; i <= 12; i++){
System.out.println(myGenerator.nextPower());
}
}
}