JAVA Answer




import java.io.*;
    import java.util.*;
    import java.text.*;
    import java.math.*;
    import java.util.regex.*;

    public class Solution
    {

    public static void main(String[] args)
    {
    Scanner sc = new Scanner(System.in);
    NavalVessel[] nv= new NavalVessel[4];
    for(int i=0; i<4;i++)
    {
        int a = sc.nextInt(); sc.nextLine();
        String b = sc.nextLine();
        int c = sc.nextInt();
        int d = sc.nextInt(); sc.nextLine();
        String e = sc.nextLine();
        nv[i] = new NavalVessel(a,b,c,d,e,"\0");
    }
    int f = sc.nextInt();sc.nextLine();
    String g = sc.nextLine();
    int res = findAvgVoyagesByPct(nv, f);
    if(res>0)
    System.out.println(res);
    else
    System.out.println("There are no voyages completed with this percentage");
    
    NavalVessel tem = findVesselByGrade(nv,g);
    if(tem == null)
    {
        System.out.println("No Naval Vessel is available with the specified purpose");
    }
    else
    System.out.println(tem.vn+"%"+tem.cn);

    

    }

    public static int findAvgVoyagesByPct(NavalVessel[] nv, int p)
    {
        int avg=0,per=0,s=0,c=0;
    for(int i=0;i<4;i++)
    {
        per = ((nv[i].vc*100)/nv[i].vp);
        if(per>=p)
        {
            s=s+nv[i].vc;
            c++; 
        }
    }
    if(c>0)
    {
        avg = s/c;
        return avg;
    }
    else
    return avg;
    }

    public static NavalVessel findVesselByGrade(NavalVessel[] nv, String purpose)
    {
    //method logic
        int rt;
    NavalVessel temp = new NavalVessel();
    for(int i=0;i<4;i++)
    {
        if((purpose.equalsIgnoreCase(nv[i].pp))&&(nv[i].vc<=nv[i].vp))
        {
            rt = ((nv[i].vc)*100)/(nv[i].vp);
            temp = nv[i];
            if(rt==100)
            {
                temp.cn = "Star";
            }
            else if (rt>=80 && rt<=99)
            {
                temp.cn = "Leader";
            }
            else if (rt>=55 && rt<=79)
            {
                temp.cn = "Inspirer";
            }
            else
            {
                temp.cn = "Striver";
            }
            return temp;
        }
    }
        return null;
    }

    }

    class NavalVessel 
    {
    //code to build the class
    int id,vp,vc;
    String vn,pp,cn;

    public NavalVessel()
    {

    }

    public NavalVessel(int id, String vn, int vp, int vc, String pp, String cn)
    {
        this.id = id;
        this.vn = vn;
        this.vp = vp;
        this.vc = vc;
        this.pp = pp;
        this.cn = cn;
    }

    }

No comments:

Post a Comment