S
S
Sawayadi2020-10-01 20:11:13
Java
Sawayadi, 2020-10-01 20:11:13

How to display an array of objects with the maximum value?

I have to output an array of the object with the longest time (the 5th value of the arrays):

package com.company;

import java.util.*;

public class Main {
    public static void main(String[] args) {

        Scanner in = new Scanner(System.in);
        System.out.println("Введите тип самолета: ");
        String coty = in.nextLine();
        AviaRejs [] adv = new AviaRejs [4];
        adv[0] = new AviaRejs("Италия", "Ту-154", "BA 456", 2234, 8);
        adv[1] = new AviaRejs("Япония", "ИЛ-621", "РО 331", 3309, 9);
        adv[2] = new AviaRejs("Россия", "Ту-134", "ТА 601", 1445, 6);
        adv[3] = new AviaRejs("Бразилия", "ИЛ-965", "НС 527", 6776, 10);
        }
        }
    }

I tried with reduce but complains about ">":
for(int i=0; i<4; i++) {
            double max = Arrays.stream(adv[i]).reduce(adv[i], (x, y) -> x > y ? x : y);
            System.out.println(max);
        }

I will be glad if you tell me how to solve this.
AviaRejs class code:
package com.company;

import java.util.Scanner;

public class AviaRejs {
    private String name;
    private String race;
    private String type;
    private int r;
    private int t;

    public AviaRejs(String name, String type, String race, int r, int t) {
        this.name = name;
        this.type = type;
        this.race = race;
        this.r = r;
        this.t = t;
    }

    public AviaRejs() {
    }

    public String getName() {

        return name;
    }

    public void setName(String name) {

        this.name = name;
    }

    public String getType() {

        return type;
    }

    public void setType(String type) {

        this.type = type;
    }

    public String getRace() {

        return race;
    }

    public void setRace(String race) {

        this.race = race;
    }

    public int getR() {

        return r;
    }

    public void setR(int r) {

        this.r = r;
    }

    public int getT() {

        return t;
    }

    public void setT(int t) {

        this.t = t;
    }

    public static AviaRejs input() {
        Scanner in = new Scanner(System.in);
        System.out.println("Ведите название пункта назначения: ");
        String name = in.nextLine();
        System.out.println("Ведите тип самолета: ");
        String type = in.nextLine();
        System.out.println("Ведите номер рейса: ");
        String race = in.nextLine();
        System.out.println("Ведите расстояние до пункта назначения(км): ");
        int r = in.nextInt();
        System.out.println("Ведите время полета(час): ");
        int t = in.nextInt();

        return new AviaRejs(name, race, type, r, t);
    }

    public static int checkMid(int r, int t) {
        int c=r/t;
        return c;
    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
Therapyx, 2020-10-01
@Sawayadi

int max = 0;
for (AviaRejs obj : adv) {
     if (max < obj.getVremya()) {
           max = obj.getVremya();
     }
}
System.out.println(max);

v classe AviaRejs make a function that returns the Time attribute back. You can do better, but it's clearer

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question