Create a class named Planets with a default constructor, a public method ReadFil
ID: 3577973 • Letter: C
Question
Create a class named Planets with a default constructor, a public method ReadFile, and a public data member distances, which is an array with 100 floating point values. Use -1.0 to initialize all the values of the array. The public method ReadFile will read each line from the given filename and store each value into the array. For each line of the file, print the line to the console and place the value into the array. This method does not return a value. You only need to write the class definition and any code that is required for that class. Place all code within the class definition. Our code will create and test your class.Explanation / Answer
Answer
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class Planets {
float distance[] = new float[100];
Planet(){
int i=0;
for(i = 0; i<100; i++)
{
distance[i] = -1.0;
}
}
public void ReadFile()
{
FileInputStream fis = new FileInputStream("provide the path of the file here");
Scanner scanner = new Scanner(fis);
String line;
int i = 0;
while(scanner.hasNextLine()){
line = scanner.nextLine();
System.out.println(line);
distance[i] = atof(line);
}
scanner.close();
}
public static void main(String args[]) throws FileNotFoundException{
ReadFile()
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.