BlackSwan
BlackSwan

Reputation: 77

Arrays coresponding with each other

I have written a program which reads an input file and store the values in three arrays(a string and two double arrays). The class who do the task is given below:

package Assignments;

import java.util.Arrays;
import java.util.Scanner;


public class Unit {

static double[] fatContent;
static double[] protienContent;
static String[] nameOfElements;


Unit(int numberOfElements){
    fatContent=new double[numberOfElements];
    protienContent=new double[numberOfElements];
    nameOfElements=new String[numberOfElements];

}

static void start(String in,int counter){
     Scanner storingElements=new Scanner(in);
    double fat;
    double protien;
    String name;

    while(!storingElements.hasNext()){

        storingElements.nextLine();

    }
    name=storingElements.next();
    fat=storingElements.nextDouble();
    protien=storingElements.nextDouble();
    fatContent[counter]=fat;
    protienContent[counter]=protien;
    nameOfElements[counter]=name;

}


}

One of the input file I am reading is:

7
25
2
Mammal  fat_content(%)  protein_content(%)
Bison   7.9 5.9
Guinea_pig  3.9 8.1
Dolphin 14.0    10.4
Donkey  1.4 1.7
Goat    4.1 3.4
Deer    19.7    9.2
Dog         8.3 9.5
Yak     6.7 5.3
Camel   3.4 3.5
Cat     10.9    11.1
Rabbit  13.1    7.1
Llama   3.2 3.9
Human   43.9    7.4
Mule    1.8 2.0
Elephant    5.0 4.0
Horse   1.3 2.1
Rat     12.6    12.3
Reindeer    20.3    10.4
Sheep   6.4 5.6
Pig         5.1 6.6
Fox     5.9 7.4
Whale   42.3    10.9
Polar_bear  31.0    10.2
Zebra   4.8 3.0
Seal    53.2    11.2

and when I run this class from my main program it gives me output of:

[Bison, Guinea_pig, Dolphin, Donkey, Goat, Deer, Dog, Yak, Camel, Cat, Rabbit, Llama, Human,     Mule, Elephant, Horse, Rat, Reindeer, Sheep, Pig, Fox, Whale, Polar_bear, Zebra, Seal]

[7.9, 3.9, 14.0, 1.4, 4.1, 19.7, 8.3, 6.7, 3.4, 10.9, 13.1, 3.2, 43.9, 1.8, 5.0, 1.3, 12.6,     20.3, 6.4, 5.1, 5.9, 42.3, 31.0, 4.8, 53.2]

[5.9, 8.1, 10.4, 1.7, 3.4, 9.2, 9.5, 5.3, 3.5, 11.1, 7.1, 3.9, 7.4, 2.0, 4.0, 2.1, 12.3, 10.4, 5.6, 6.6, 7.4, 10.9, 10.2, 3.0, 11.2]

But my problem is that I want a connection between them like if I find the maximum value of fat array then it also gives me the corresponding value of protein and mammal name that belongs to it. I mean how can I make them relate to each other.... like (Bison 7.9 5.9) at one place.

I have searched online and I got answers like making a new class and setting array to that class or some thing like that but I really don't understand that.....I would highly appreciate an answer with a little detail because I am new to programming.

Thanks in advance and any ideas or help would be highly appreciated.....

Upvotes: 0

Views: 92

Answers (2)

Donvino
Donvino

Reputation: 2575

Here is the class, you need to improve it by yourself. But the main idea is in it, as I understood it.

class Mamal
{
    String Name;
    double fat;
    double protein;
    public Mamal(double fatContent,double proteinContent,String nameofElements)
    {
        Name=nameofElements;
        protein=proteinContent;
        fat=fatContent;
    }
    public String get_by_name(Mamal[] nameofElements,String name)
    {
        for(int i=0;i<nameofElements.length;i++)
        {
            if(nameofElements[i].getName().equalsIgnoreCase(name))
            {
                return nameofElements[i].getFat()+nameofElements[i].getName();
            }
        }
        return "";
    }
    public String getName() {
        return Name;
    }
    public double getFat() {
        return fat;
    }
    public double getProtein() {
        return protein;
    }
}

Function get_by_name helps you get the protein and fat of the mammal you need.

Upvotes: 1

Siddharth Kamaria
Siddharth Kamaria

Reputation: 2717

You can create a dedicated class named "Mammal" which would have properties of fat content, name of mammal and protein content. Make a array of Mammals and loop over to find the max fat content and easily you would find the corresponding protein content or any other query.

P.S.: You should have followed the object oriented paradigm. It's helpful.

Upvotes: 1

Related Questions