Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

s painta) Q2) In your development environment, create a new directory, called Te

ID: 3606976 • Letter: S

Question

s painta) Q2) In your development environment, create a new directory, called Test 2. Write an interactive Java program, called "FileDisplay". The program prompts the user for the name of a text file,then read the name of the file, open the file and print its contents on the screen, Create a text ile in the same directory, test your program and ensure it functions as expected. See the "Submission Instructions on the last page for upload details 3) The figure on the next page shows the Animal, Plant classes and the Edible interface hierarchies. These classes are the parents of the classes described below getHeartRate,as shown in the The Animal class is non-abstract and defines two factions getLifespantis figure above. The Plant dass defines two abstract functions got Alane O ‘ g, tGroep O·This class also defines one not abstract function isSeeded), as shown in the figure The Animal class has two non-abstract child classes: Ant, and Shrimp. The Ant class deflnes a single function as in the figure, and has one child class, FireAnt. The Plant class has two non-abstract child classes, Okra and Poisonlvy Interface Edible defines a single function as in the figure. Interface Dangerous defines a single function as in the figure lnterface Poisonous is a child of Dangerous and defines a single function as in the figure. -The classes Shrimp & Okra implement the Edible interface Class FirAnt implements the Dangerous interface, and class Polsonivy implements the Polsonous interface From the description above, complete the following 1- g function and class names eact as given in the description and figures above, create Java classes to design and represent this hierarchy. Place these files inside the Test 2 directory. Besides the files created for this test, there should be no other files of any type in the Test 2 directory 2- For each class, define two constractors: a default, no-argument constructor, and a constructor that assign values to all the member variables, including the variables defined in the class's parent, If any 3- Adding getter and setter methods in addition to the ones in the figure is entirely optional; howee, you can only use constructors to assign values to class instance variables from your Driver java test program 4- Use function return types exactly as given in the figure

Explanation / Answer

Q2.

Code:

import java.io.FileNotFoundException;

import java.io.FileReader;

import java.util.Scanner;

public class FileDisplay {

public static void main(String[] args) throws FileNotFoundException {

Scanner in = new Scanner(System.in);

System.out.println("Enter the name of the file: ");

String fileName = in.nextLine();

FileReader reader = new FileReader(fileName);

String data;

in = new Scanner(reader);

while (in.hasNextLine()) {

data = in.nextLine();

System.out.println(data);

}

}

}

Sample output:

Enter the name of the file:
test.txt
I am from sample file.
Line 1
Line 2
Last Line

test.txt

I am from sample file.
Line 1
Line 2
Last Line

********************************************************************************************************************************

Q3. All the classes have been implemented as given in the question:

public class Animal {

private String lifeSpan;

private int heartRate;

public Animal() {

}

public Animal(String lifeSpan, int heartRate) {

this.lifeSpan = lifeSpan;

this.heartRate = heartRate;

}

public String getLifeSpan() {

return lifeSpan;

}

public int getHeartRate() {

return heartRate;

}

}

public class Ant extends Animal{

private int colonSize;

public Ant() {

}

public Ant(String lifeSpan, int heartRate, int colonSize) {

super(lifeSpan,heartRate);

this.colonSize = colonSize;

}

public int getColonySize() {

return colonSize;

}

}

public interface Dangerous {

boolean isFatal();

}

public interface Edible {

String getTaste();

}

public class FireAnt extends Ant implements Dangerous {

private boolean fatal;

public FireAnt() {

}

public FireAnt(String lifeSpan, int heartRate, int colonSize,boolean fatal) {

super(lifeSpan, heartRate, colonSize);

this.fatal = fatal;

}

@Override

public boolean isFatal() {

// TODO Auto-generated method stub

return fatal;

}

}

public class Okra extends Plant implements Edible {

private String name,group,taste;

public Okra() {

}

public Okra(String name, String group, String taste) {

this.name = name;

this.group = group;

this.taste = taste;

}

@Override

public String getName() {

// TODO Auto-generated method stub

return name;

}

@Override

public String getGroup() {

// TODO Auto-generated method stub

return group;

}

@Override

public String getTaste() {

// TODO Auto-generated method stub

return taste;

}

}

public abstract class Plant {

public abstract String getName();

public abstract String getGroup();

}

public class PoisonIvy extends Plant implements Poisonous {

private String name,group,treatment;

private boolean fatal;

public PoisonIvy() {

}

public PoisonIvy(String name, String group, String treatment, boolean fatal) {

this.name = name;

this.group = group;

this.treatment = treatment;

this.fatal = fatal;

}

@Override

public String getName() {

// TODO Auto-generated method stub

return name;

}

@Override

public String getGroup() {

// TODO Auto-generated method stub

return group;

}

@Override

public boolean isFatal() {

// TODO Auto-generated method stub

return fatal;

}

@Override

public String getTreatment() {

// TODO Auto-generated method stub

return treatment;

}

}

public interface Poisonous extends Dangerous {

String getTreatment();

}

public class Shrimp extends Animal implements Edible{

private String calories;

private String taste;

public Shrimp() {

}

public Shrimp(String lifeSpan, int heartRate,String calories,String taste) {

super(lifeSpan,heartRate);

this.calories = calories;

this.taste=taste;

}

public String getCalories() {

return calories;

}

@Override

public String getTaste() {

// TODO Auto-generated method stub

return taste;

}

}

public class Driver {

public static void main(String[] args) {

Okra okra = new Okra("Burnose Okra", "Malvaceae", "dull and salty");

Edible edible = okra;

System.out.println("Okra has a "+edible.getTaste()+" ..");

}

}

Sample output:

Okra has a dull and salty ..

*********************************************************************************************************************************

There are a lot of classes please go through very carefully. I hope explanation is not required, everything is according to the diagram. If you still have doubts, you can comment below.

I hope this helps you.
If you find my answer helpful,
Kindly rate the answer.
All the best :)