ArrayList/Casting help: simple question | Newbie | | Join Date: Nov 2007
Posts: 1
| |
i'm stuck and i need a little direction. i'm only getting 2 error messages. i have 2 files: - import java.util.ArrayList;
-
import java.util.*;
-
//author
-
-
public class StudentTester
-
{
-
-
public static void main(String[] args)
-
{
-
ArrayList<Student> studentList = new ArrayList<Student>();
-
-
Student a = new Student("john", "doe", "john@hotmail.com", 160, 85.5, 70.3, 95.6);
-
Student b = new Student("bill", "johnson", "bill@hotmail.com", 161, 81.5, 79.3, 90.6);
-
Student c = new Student("sally", "ray", "sally@hotmail.com", 162, 89.5, 77.3, 91.6);
-
Student d = new Student("eric", "davis", "eric@hotmail.com", 163, 80.5, 70.3, 99.6);
-
Student e = new Student("bruce", "lee", "bruce@hotmail.com", 164, 85.5, 74.3, 92.6);
-
-
studentList.add(a);
-
studentList.add(b);
-
studentList.add(c);
-
studentList.add(d);
-
studentList.add(e);
-
-
int elements = studentList.size();
-
-
for(int i = 0; i < elements; i++)
-
{
-
System.out.print("My name is " + studentList.get(i).fullName());
-
System.out.print(" my total is " + studentList.get(i).total());
-
System.out.print(" so my average is " + studentList.get(i).myAverage());
-
System.out.println(" and my GPA is " + studentList.get(i).myGPA());
-
}
-
-
}
-
}
and - /**
-
*
-
* @author
-
*/
-
public class Student {
-
private String first; // first name
-
private String last; // last name
-
private String email; // email address
-
private int section; // section number
-
private double grade1;
-
private double grade2;
-
private double grade3;
-
-
// construct a new student with given fields
-
public Student(String first, String last, String email, int section, double grade1,
-
double grade2, double grade3) {
-
this.first = first;
-
this.last = last;
-
this.email = email;
-
this.section = section;
-
this.grade1=grade1;
-
this.grade2=grade2;
-
this.grade3=grade3;
-
}
-
-
// return full name
-
public String fullName(){
-
String full=first +" "+last;
-
return full;
-
}
-
-
// return the total of the 3 grades
-
public double total(){
-
-
double totalGrades=grade1+grade2+grade3;
-
return totalGrades;
-
}
-
// return average for my grades
-
-
public double myAverage(double myTotal){
-
double averageGrades=myTotal/3;
-
return averageGrades;
-
}
-
-
// return GPA for my grades
-
public String myGPA( double myAverage ){
-
String GPA=null;
-
if(myAverage<= 50)
-
GPA="F";
-
else if(myAverage<=65)
-
GPA= "C";
-
else if(myAverage<=80)
-
GPA="B";
-
else if(myAverage <=100)
-
GPA="A";
-
return GPA;
-
}
-
-
-
}
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
|  | Expert | | Join Date: Mar 2007
Posts: 10,611
| | | 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
|  | Moderator | | Join Date: Mar 2007 Location: Springfield, Ohio
Posts: 729
| | | 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.
|  | Expert | | Join Date: Mar 2007
Posts: 10,611
| | | 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: -
ArrayList<Student> students= new ArrayList<Student>() { // extended class
-
{ // initialization block
-
add(new Student( ... ));
-
add(new Student( ... ));
-
...
-
} // end of initialization block of extending (anonymous local) class
-
}; // end of class definition and instantiation
-
I hope you like this trickery dickery; not many folks realize that you can use
initialization blocks like this.
kind regards,
Jos
|  | Moderator | | Join Date: Mar 2007 Location: Springfield, Ohio
Posts: 729
| | | 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: -
ArrayList<Student> students= new ArrayList<Student>() { // extended class
-
{ // initialization block
-
add(new Student( ... ));
-
add(new Student( ... ));
-
...
-
} // end of initialization block of extending (anonymous local) class
-
}; // end of class definition and instantiation
-
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)?
|  | Expert | | Join Date: Mar 2007
Posts: 10,611
| | | 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
|  | Expert | | Join Date: Mar 2007
Posts: 10,611
| | | re: ArrayList/Casting help: simple question
ps. arrays are quite stupid when it comes to this but the language itself helps here: -
Student[] students = {
-
new Student( ... ),
-
new Student( ... ),
-
new Student( ... ),
-
...
-
};
-
kind regards,
Jos
| | Member | | Join Date: Nov 2007 Location: ZIMBABWE
Posts: 118
| | | 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.
|  | Expert | | Join Date: Mar 2007
Posts: 10,611
| | | 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
|  | | | | Forums
Visit our community forums for general discussions and latest on Bytes
/bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 229,155 network members.
|