Question: Using this code can you modify it to the intructions i have attached import java.io . File; import java.io . FileWriter; import java.io . IOException;

Using this code can you modify it to the intructions i have attached 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);
}
}
}
INSTRUCTIONS
This assignment has the following objectives:
1. apply inheritance to objects
2. implement an interface
3. apply polymorphism to the class hierarchy
4. read data from file and create new output files
Problem Description
Nintendos human resources data is disorganized, full of duplicates, and in metric! The information is stored in a database file, hr.txt and its your task to create two new versions of it:
One version will be in alphabetical order
One version will be converted from metric to imperial units
And both versions will have no duplicates
List of classes that you will write:
Main contains the main method.
Person stores HR information
PersonList an interface
PersonSet a class implementing the interface
PersonImperialSet a class inheriting from PersonSet
PersonOrderedSet another class inheriting from PersonSet
Instructions for Part 2
You need to write two new classes for part 2: PersonImperialSet and PersonOrderedSet.
1. Add a toString method to PersonSet that loops through the ArrayList, concatenating the Persons data to a String variable, which is then returned. The format needs to match the format of hr.txt.
2. Write a class named, PersonOrderedSet. This class should extend PersonSet and override the add method to add Persons in alphabetical order by name.
3. Write a class named, PersonImperialSet. This class should extend PersonSet and override the add method to convert the height measurement from centimeters to inches and the weight from kilograms to pounds. (Look up the conversions online.)
4. Modify Main to
A. instantiate a PersonOrderedSet and a PersonImperial

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!