Help in java????

Started by
4 comments, last by Fenrisulvur 14 years, 5 months ago
Hello i have a problem in java where i have to find the cgpa for a the given grades... i Have written the code and i am able to get the answer for float values . But if i give any values of type double i get an precision error... this is the code i have written

class CGPA
{
  int sem;
  public float calcCGPA(float[] sgpa,int[] credits, int noOfsem)
{

  
  sem=noOfsem;
  int sgplen=sgpa.length;
  int crdlen=credits.length;
  int len=sem;
  double res=0;
  //int i=0;
  int tot=0;
if(sgplen==len&&crdlen==len){
for(int i=0;i<len;i++)
{
  if(credits<=0){
    return -1;
    }
else
{
  tot=tot+credits;
}
}  
 for(int j=0;j<credits.length;j++)
{
  if(sgpa[j]<=0)
   return -1;
  res=res+(sgpa[j]*credits[j]);
}
res=res/tot;
return (float)res;
}

else{
  return -1;}}
  public static void main(String[] args)
{
  CGPA c=new CGPA();
  float[] s={22,23,24,25};
  int[] cr={50,100,50,100};
  int se=4;

float r=c.calcCGPA(s,cr,se);
System.out.println(r);
}
}

The problem requirements is that i must not change the return type of public float calcCGPA(float[] sgpa,int[] credits, int noOfsem) into a double neither should i change it in public static void main(String[] args) { CGPA c=new CGPA(); float[] s={22,23,24,25};// i must not change the float value here also!! Is there a way of casting so that i can include double variable and then cast it into a float array??? Please help me out.. i am sorry if i have posted in the wrong section as i am new here.... [Edited by - touches on October 26, 2009 11:36:33 PM]
Advertisement
what do you mean by precision error? it should print 94/300 = 0.31333... recursing 3 is the problem?

it will not be different even if you use double. if you want to print only a few digit after

round(num*100)/100.0

which will round the number to the two digits.

next time use [ source ] [ /source ] tags please
taytay
Since you asked a fairly to-the-point question, I won't bother reviewing your code. To cast from double to float:

    double d = 4.35235;    float f = (float)d;
public static void main(String[] args){CGPA c=new CGPA();float[] s={22,23,24,25};// how do i cast a double to a float in arrays???


when i give the values as float[]{22.3,45.6,22,55} as an example i am getting an precision loss error and i am not be to run the code???? I am able to run the code as long as i dont include any values like 2.34 etc.. How do i cast it so that i am able to include any value without changing the methods parameters or its return type???(this is specification of the problem setters)
1.23 is not a float, it's a double.
1.23f is a float.

so what you're looking for is:

float[] s = {1.23f, 2.34f, 3.45f};
Oh, sorry - you want the syntax for specifying float literals. Here you go:

    float f = 1.234f;    float [] f_array = {22.3f,45.6f,22f,55f};

http://java.sun.com/docs/books/tutorial/java/nutsandbolts/datatypes.html (see the "Literals" heading)

You could also use the float cast next to each number, ie (float)22.3 .
Doubles also have a suffix for literals, d as in 56.7d, but Java appears to parse all numerical literals with decimal points in them as doubles by default.

EDIT: bleh, too slow.

This topic is closed to new replies.

Advertisement