Can you please finish the last method... package prac; import java.util.Scanner;
ID: 3859677 • Letter: C
Question
Can you please finish the last method...
package prac;
import java.util.Scanner;
import java.io.*;
public class Prac {
public static plant[] myFlowers = new plant[10];
public static void main(String[] args) throws IOException {
//create a domain with two attributes
//create an array of objects that the file has in it
//call methods in main
createArrayOfFlowers();
findTallestFlower();
printAllFlowers();
}
//create methods
public static void createArrayOfFlowers()throws IOException
{
File myFile = new File("plants.txt");
Scanner inFile = new Scanner(myFile);
int i = 0;
String name;
double height;
plant aPlant;
while (inFile.hasNext() && i < myFlowers.length)
{
name = inFile.next();
height = inFile.nextDouble();
aPlant = new plant(name, height);
myFlowers[i] = aPlant;
i++;
}
inFile.close();
}
//create methods
public static void findTallestFlower()
{
double tallestHeight = myFlowers[0].getHeight();
int indexOfTallest = 0;
//getting first entry of my arrays of flowers
for (int i = 1; i < myFlowers.length; i++)
{
if(myFlowers[i].getHeight() > tallestHeight)
{
tallestHeight = myFlowers[i].getHeight();
indexOfTallest = i;
}
}
System.out.println("The tallest plant had a height of "+tallestHeight);
System.out.println("The tallest plant was "+myFlowers[indexOfTallest]);
}
//create methods
public static void printAllFlowers()
{
//find average height
}
}
Explanation / Answer
Note:Could u plz check the method.If u want me to do any modification.I will do it thank You.
__________________
plant.java
package prac;
public class plant {
private String name;
private double height;
public plant(String name, double height) {
super();
this.name = name;
this.height = height;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
}
______________________
Prac.java
package prac;
import java.util.Scanner;
import java.io.*;
public class Prac {
public static plant[] myFlowers = new plant[10];
public static void main(String[] args) throws IOException {
//create a domain with two attributes
//create an array of objects that the file has in it
//call methods in main
createArrayOfFlowers();
findTallestFlower();
printAllFlowers();
}
//create methods
public static void createArrayOfFlowers() throws IOException {
File myFile = new File("plants.txt");
Scanner inFile = new Scanner(myFile);
int i = 0;
String name;
double height;
plant aPlant;
while (inFile.hasNext() && i < myFlowers.length) {
name = inFile.next();
height = inFile.nextDouble();
aPlant = new plant(name, height);
myFlowers[i] = aPlant;
i++;
}
inFile.close();
}
//create methods
public static void findTallestFlower() {
double tallestHeight = myFlowers[0].getHeight();
int indexOfTallest = 0;
//getting first entry of my arrays of flowers
for (int i = 1; i < myFlowers.length; i++) {
if (myFlowers[i].getHeight() > tallestHeight) {
tallestHeight = myFlowers[i].getHeight();
indexOfTallest = i;
}
}
System.out.println("The tallest plant had a height of " + tallestHeight);
System.out.println("The tallest plant was " + myFlowers[indexOfTallest]);
}
//create methods
public static void printAllFlowers() {
double total = 0, avg = 0.0;
for (int i = 1; i < myFlowers.length; i++) {
System.out.println(myFlowers[i].getName());
total += myFlowers[i].getHeight();
}
avg = (total / myFlowers.length);
System.out.println("Average Height is :" + avg);
}
}
_________________________Thank You