Question: please help make this code run import java.io . File; import java.io . FileWriter; import java.io . IOException; import java.util.ArrayList; import java.util.Scanner; class Person {

please help make this code run
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
class Person {
private String name;
private double height; // in meters
private double weight; // in kilograms
public Person(String name, double height, double weight){
this.name = name;
this.height = height;
this.weight = weight;
}
public double getHeight(){
return height;
}
public double getWeight(){
return weight;
}
public void setHeight(double height){
this.height = height;
}
public void setWeight(double weight){
this.weight = weight;
}
@Override
public String toString(){
return name +""+ height +""+ weight;
}
@Override
public boolean equals(Object o){
if (o == null) return false;
if (this == o) return true;
if (!(o instanceof Person)) return false;
Person p =(Person) o;
return this.name.equals(p.name) && this.height == p.height && this.weight == p.weight;
}
}
interface PersonList {
void add(Person p);
Person get(int index);
}
class PersonSet implements PersonList {
protected ArrayList people = new ArrayList<>();
@Override
public void add(Person p){
if (!people.contains(p)){
people.add(p);
}
}
@Override
public Person get(int index){
if (index >=0 && index < people.size()){
return people.get(index);
}
return null;
}
@Override
public String toString(){
StringBuilder sb = new StringBuilder();
for (Person p : people){
sb.append(p.toString()).append("
");
}
return sb.toString();
}
}
public class Main {
public static void main(String[] args){
// Test the Person and PersonSet classes
Person person1= new Person("Mario",1.55,65.0); // Example data
PersonSet personSet = new PersonSet();
personSet.add(person1);
// Reading from the hr.txt file
try {
File inputFile = new File("hr.txt");
Scanner fileReader = new Scanner(inputFile);
// Skip the first row (header)
if (fileReader.hasNextLine()){
fileReader.nextLine();
}
// Reading and adding data to the PersonSet
while (fileReader.hasNext()){
String name = fileReader.next();
double height = fileReader.nextDouble();
double weight = fileReader.nextDouble();
Person person = new Person(name, height, weight);
personSet.add(person);
}
fileReader.close();
// Displaying the list of persons
System.out.println(personSet.toString());
// Writing the list to an output file
FileWriter fileWriter = new FileWriter("outputfile.txt");
fileWriter.write(personSet.toString());
fileWriter.close();
} catch (IOException e){
e.printStackTrace();
System.out.println(e);
System.exit(1);
}
}
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!