-1

I have to create an array of grades and perform operations. Now I have to use generics to do so and I pass the array to the generic function and try it using a conditional if else statement but the if else isn't even considered, the for loop works though.

Here is my code for the question.

import java.util.*;

class Calculation
{
    public static <T> void gpa(T[] elements)
    {
        for(T element:elements)
        {
            System.out.println(element);
        }
        int elem[]=new int[5];
        int i=0;
        int sum=0;
        for(T element:elements)
        {
            if(element=="A")
            {
                elem[i]=9;
            }
            if(element=="B")
            {
                elem[i]=8;
            }
             if(element=="C")
             {
                elem[i]=7;
             }    
            if(element=="D")
            {
                elem[i]=6;
            }
             if(element=="E")
            {
                elem[i]=5;
            }            
             if(element=="F")
            {
                elem[i]=2;
            }
             if(element=="2")
            {
                System.out.println("The grade is F.");
            }
             if(element=="5")
            {
                System.out.println("The grade is E.");
            }
             if(element=="6")
            {
                System.out.println("The grade is D.");
            }
             if(element=="7")
            {
                System.out.println("The grade is C.");
            }
             if(element=="8")
            {
                System.out.println("The grade is B.");
            }
             if(element=="9")
            {
                System.out.println("The grade is A.");
            }
            i++;
        }
        
        for(int j=0;j<5;j++)
        {
            sum+=elem[j]+sum;
        }
        int avg=sum/5;
        System.out.println("The average is " + avg);
    }
}

public class Calc
{
    public static void main(String args[])
    {   
        Calculation obj=new Calculation();
        Character arr1[]={'A','B','C','A','A'};
        Integer arr2[]={9,9,5,2,8};
        System.out.println("The CGPA will be");
        obj.gpa(arr1);
        System.out.println("The grade will be");
        obj.gpa(arr2);
    }
}

I expected the code to run considering the if else but that isn't working. I tried using a switch statement and that failed. The output with the code attached doesn't raise any errors but doesn't perform any comparisons with the conditional statements. Taking the values in conditional statements as characters/integers also gives an error.

  • in this case what does the "T" in for loop body stand for – kaay Nov 05 '22 at 07:00
  • T is the type of the given array elements, so String at first, Integer on second usage. – Christoph Dahlen Nov 05 '22 at 07:13
  • Hello and Welcome! Oracle provides a fairly good [*tutorial*](https://docs.oracle.com/javase/tutorial/java/generics/types.html) for zero cost. You'll find all the information that you on what are they meant for, and they are suitable. Also see [Compare two objects with .equals() and == operator](https://stackoverflow.com/questions/13387742/compare-two-objects-with-equals-and-operator) – Alexander Ivanchenko Nov 05 '22 at 11:39
  • 2
    Your method is filled with hard-coded logic for dialing with `String`. It's not clear why do you want it to be generic. – Alexander Ivanchenko Nov 05 '22 at 11:43
  • Does this answer your question? "[Java: Compare objects using >, < and ==](/q/27399712/90527)", "[Java Comparing Objects](/q/34125936/90527)" – outis Nov 06 '22 at 02:51
  • I just tried the .equals method and that doesn't work – Vansh Dalal Nov 06 '22 at 08:08
  • It is an assignment given and the question says we must use generics Here is the question. Create class "calculation " perform the calculation of grade for different marks if the mark is numeric assign the equivalent grade for it. if the grade in character make its as a numeric and calculate the CGPA of the student. In the driver class the array of mark can be send as parameter to the function in the calculation class. – Vansh Dalal Nov 06 '22 at 08:09

1 Answers1

1

Don’t compare Objects in Java by ==, use .equals(other) instead.

Christoph Dahlen
  • 826
  • 3
  • 15