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
8 2487
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
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.
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
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)?
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
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
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.
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
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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 =...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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,...
|
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...
|
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...
|
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,...
|
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...
| |