Connecting Tech Pros Worldwide Forums | Help | Site Map

ArrayList/Casting help: simple question

Newbie
 
Join Date: Nov 2007
Posts: 1
#1: Nov 10 '07
i'm stuck and i need a little direction. i'm only getting 2 error messages. i have 2 files:

Expand|Select|Wrap|Line Numbers
  1. import java.util.ArrayList;
  2. import java.util.*;
  3. //author 
  4.  
  5. public class StudentTester
  6.  {
  7.  
  8.   public static void main(String[] args)
  9.    {
  10.     ArrayList<Student> studentList = new ArrayList<Student>();
  11.  
  12.     Student a = new Student("john", "doe", "john@hotmail.com", 160, 85.5, 70.3, 95.6);
  13.     Student b = new Student("bill", "johnson", "bill@hotmail.com", 161, 81.5, 79.3, 90.6);
  14.     Student c = new Student("sally", "ray", "sally@hotmail.com", 162, 89.5, 77.3, 91.6);
  15.     Student d = new Student("eric", "davis", "eric@hotmail.com", 163, 80.5, 70.3, 99.6);
  16.     Student e = new Student("bruce", "lee", "bruce@hotmail.com", 164, 85.5, 74.3, 92.6);
  17.  
  18.     studentList.add(a);
  19.     studentList.add(b);
  20.     studentList.add(c);
  21.     studentList.add(d);
  22.     studentList.add(e);
  23.  
  24.     int elements = studentList.size();
  25.  
  26.     for(int i = 0; i < elements; i++)
  27.         {
  28.           System.out.print("My name is " + studentList.get(i).fullName());
  29.           System.out.print(" my total is " + studentList.get(i).total());
  30.           System.out.print(" so my average is " + studentList.get(i).myAverage());
  31.           System.out.println(" and my GPA is " + studentList.get(i).myGPA());
  32.         }
  33.  
  34.    }
  35.  }
and

Expand|Select|Wrap|Line Numbers
  1. /**
  2.  *
  3.  * @author
  4.  */
  5. public class Student {
  6.     private String first;     // first name
  7.     private String last;      // last name
  8.     private String email;     // email address
  9.     private int    section;   // section number
  10.     private double grade1;
  11.     private double grade2;
  12.     private double grade3;
  13.  
  14.     // construct a new student with given fields
  15.     public Student(String first, String last, String email, int section, double grade1, 
  16.     double grade2, double grade3) {
  17.         this.first   = first;
  18.         this.last    = last;
  19.         this.email   = email;
  20.         this.section = section;
  21.         this.grade1=grade1;
  22.         this.grade2=grade2;
  23.         this.grade3=grade3;
  24.     }
  25.  
  26.    // return full name
  27.    public String fullName(){
  28.            String full=first +"    "+last;
  29.       return full;
  30.  }
  31.  
  32.    // return the total of the 3 grades
  33.    public double total(){
  34.  
  35.     double totalGrades=grade1+grade2+grade3;
  36.         return totalGrades;
  37.     }
  38.    // return average for my grades
  39.  
  40.    public double myAverage(double myTotal){
  41.     double averageGrades=myTotal/3;
  42.     return averageGrades;
  43.     }
  44.  
  45.     // return GPA for my grades
  46.     public String myGPA( double myAverage ){
  47.     String GPA=null;
  48.     if(myAverage<= 50)
  49.     GPA="F";
  50.     else if(myAverage<=65)
  51.     GPA= "C";
  52.     else if(myAverage<=80)
  53.     GPA="B";
  54.     else if(myAverage <=100)
  55.     GPA="A";
  56.     return GPA;
  57.    }
  58.  
  59.  
  60. }
my 2 error messages are: myAverage(double) and myGPA(double) in Student cannot be applied to ().

i don't understand what it's saying is wrong. i know it has something to do with casting, but i have no clue on how to fix this problem. reading the api hasn't helped. please, i've been at this for hours and i feel this is a simple problem. i'm getting very frustrated.

humbly yours,
scott

JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#2: Nov 10 '07

re: ArrayList/Casting help: simple question


Quote:

Originally Posted by scottc

my 2 error messages are: myAverage(double) and myGPA(double) in Student cannot be applied to ().

i don't understand what it's saying is wrong. i know it has something to do with casting, but i have no clue on how to fix this problem. reading the api hasn't helped. please, i've been at this for hours and i feel this is a simple problem. i'm getting very frustrated.

humbly yours,
scott

You have two methods defined: myAverage(double) and myGPA(double). You're
calling those methods without a double type parameter though. That's what the
compiler is complaining about.

kind regards,

Jos
SammyB's Avatar
Moderator
 
Join Date: Mar 2007
Location: Springfield, Ohio
Posts: 729
#3: Nov 11 '07

re: ArrayList/Casting help: simple question


Also, a Student object already knows the average and the total, so there is no need to pass those values in again. You can remove the input parameters for the total and myAverage methods of the Student object. Then with a few changes, your code works fine.
JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#4: Nov 11 '07

re: ArrayList/Casting help: simple question


This reply doesn't answer your question but it's a little tip: I saw you initialize
your ArrayList<Student> list in three steps:

1) first you constructed your ArrayList<Student>, next
2) you constructed a few Students a, b, c, etc.
3) finally you added a, b, c etc. to your list. and you don't need a, b, c ... anymore.

Note that a class, even a local one can have initialization blocks. That's the
place where you add your students; you effectively create a (local) class that
extends from an ArrayList<Student> but you don't care because your extending
class *is a* ArrayList<Student>. This is how it's done:

Expand|Select|Wrap|Line Numbers
  1. ArrayList<Student> students= new ArrayList<Student>() { // extended class
  2.    { // initialization block
  3.       add(new Student( ... ));
  4.       add(new Student( ... ));
  5.       ...
  6.    } // end of initialization block of extending (anonymous local) class
  7. }; // end of class definition and instantiation
  8.  
I hope you like this trickery dickery; not many folks realize that you can use
initialization blocks like this.

kind regards,

Jos
SammyB's Avatar
Moderator
 
Join Date: Mar 2007
Location: Springfield, Ohio
Posts: 729
#5: Nov 11 '07

re: ArrayList/Casting help: simple question


Quote:

Originally Posted by JosAH

This reply doesn't answer your question but it's a little tip: I saw you initialize
your ArrayList<Student> list in three steps:

1) first you constructed your ArrayList<Student>, next
2) you constructed a few Students a, b, c, etc.
3) finally you added a, b, c etc. to your list. and you don't need a, b, c ... anymore.

Note that a class, even a local one can have initialization blocks. That's the
place where you add your students; you effectively create a (local) class that
extends from an ArrayList<Student> but you don't care because your extending
class *is a* ArrayList<Student>. This is how it's done:

Expand|Select|Wrap|Line Numbers
  1. ArrayList<Student> students= new ArrayList<Student>() { // extended class
  2. { // initialization block
  3. add(new Student( ... ));
  4. add(new Student( ... ));
  5. ...
  6. } // end of initialization block of extending (anonymous local) class
  7. }; // end of class definition and instantiation
  8.  
I hope you like this trickery dickery; not many folks realize that you can use
initialization blocks like this.

kind regards,

Jos

I like it and we've not even had array lists yet! Is the init block a part of Array Lists or are there other places where it can be used (like a new array)?
JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#6: Nov 11 '07

re: ArrayList/Casting help: simple question


Quote:

Originally Posted by SammyB

I like it and we've not even had array lists yet! Is the init block a part of Array Lists or are there other places where it can be used (like a new array)?

The init blocks are part of every class; most of the time they're not there though.

When you do new X() this is what happens:

1) first the superclass ctor of X gets executed;
2) next, from top to bottom all the initializer (blocks) are executed;
3) only then is the ctor of class X is executed.

the little trickery dickery I described uses step 2), i.e. the super class is fully
constructed and then step 2) sets in.

For ArrayLists this step can be usefull, i.e.add those things to that list in that
particular step. Even for anonymous classes (which extend another class) the
iniitializer blocks are *the* place to initialize stuff. It's not special to ArrayLists;
I mean there's no special syntax or whatever that takes care of that; those blocks
simply are executed before any ctor gets executed. For Lists (ArrayLists etc.)
they are just usefull (see my previous example),

kind regards,

Jos
JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#7: Nov 11 '07

re: ArrayList/Casting help: simple question


ps. arrays are quite stupid when it comes to this but the language itself helps here:

Expand|Select|Wrap|Line Numbers
  1. Student[] students = {
  2.    new Student( ... ),
  3.    new Student( ... ),
  4.    new Student( ... ),
  5.    ... 
  6. };
  7.  
kind regards,

Jos
Member
 
Join Date: Nov 2007
Location: ZIMBABWE
Posts: 118
#8: Nov 14 '07

re: ArrayList/Casting help: simple question


A method with a parameter in its signature and another without a signature are different. You are calling a method without a signature but the methods you have created in your classes have fgot signatures.
JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#9: Nov 14 '07

re: ArrayList/Casting help: simple question


Quote:

Originally Posted by heat84

A method with a parameter in its signature and another without a signature are different. You are calling a method without a signature but the methods you have created in your classes have fgot signatures.

Please also read the previous replies: the OP's question has already been answered.

kind regards,

Jos
Reply