473,396 Members | 2,029 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

ArrayList/Casting help: simple question

1
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
Nov 10 '07 #1
8 2487
JosAH
11,448 Expert 8TB
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
Nov 10 '07 #2
SammyB
807 Expert 512MB
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.
Nov 11 '07 #3
JosAH
11,448 Expert 8TB
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
Nov 11 '07 #4
SammyB
807 Expert 512MB
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)?
Nov 11 '07 #5
JosAH
11,448 Expert 8TB
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
Nov 11 '07 #6
JosAH
11,448 Expert 8TB
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
Nov 11 '07 #7
heat84
118 100+
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.
Nov 14 '07 #8
JosAH
11,448 Expert 8TB
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
Nov 14 '07 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: JJ | last post by:
Hi, I have created an Arraylist object from an Arraylist class. I added rows to the arraylist object and I need to find a particular record in my arraylist. How do I do this? Also if I was in...
10
by: C Downey | last post by:
Hello: I have an arraylist storing some very basic objects. The object is very basic, it has 2 properties : ID, and COUNT Before I add an object to the arraylist, I want to check if an...
6
by: gane kol | last post by:
Hi, I have a code that creates a datatable from an arraylist, but i am getting an error in casting in for (int intRow = 0; intRow < alLCPlist.Count; intRow++) { DataRow drow =...
6
by: GrandpaB | last post by:
While writing this plea for help, I think I solved my dilemma, but I don't know why the problem solving statement is necessary. The inspiration for the statement came from an undocumented VB...
18
by: JohnR | last post by:
From reading the documentation, this should be a relatively easy thing. I have an arraylist of custom class instances which I want to search with an"indexof" where I'm passing an instance if the...
8
by: lgbjr | last post by:
Hi All, I have 10 arraylists in a VB.NET app. Based on the state of various checkboxes and radiobuttons, a set of data is read into these arraylists. I don't know how many of the arraylists will...
6
by: Iapain | last post by:
Hello, I've created an arraylist which consist arraylist ..like ArrayList myAL = new ArrayList(); // Main Array List ArrayList tmpAL = new ArrayList(); //Temp Array List tmpAL.Add(2); //Add...
5
by: Tony | last post by:
Hello! I have a class called Item as follows. I use CompareTo to be able to sort an ArrayList containg Items on heatNumber. This works fine. Now to my question. I also want to be able to sort a...
12
by: Justin | last post by:
Ok, I give up. I can't seem to construct a decent (productive) way of sorting my arraylist. I have a structure of two elements: Structure TabStructure Dim TabName As String Dim FullFilePath...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.