aspose file tools*
The moose likes Beginning Java and the fly likes arrays Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "arrays" Watch "arrays" New topic
Author

arrays

mike hengst
Ranch Hand

Joined: Oct 19, 2002
Posts: 43
What is the best way to pass arrays. For example I have to pass into a method the index of one array and as a second arg another array.


Thank you Oh Lord<br />For the white blind light<br />A city rises from the sea<br />I had a splitting headache<br />from which the future's made<br />--morrisson
Pauline McNamara
Sheriff

Joined: Jan 19, 2001
Posts: 4012
    
    6
Howdy "lizard king"
Could you please change your display name to comply with JavaRanch's naming policy? (It's the only rule 'round these parts.) You can make the change quickly right here. Thanks, and welcome to JavaRanch!
Pauline
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671

Is this what you mean?


"I'm not back." - Bill Harding, Twister
mike hengst
Ranch Hand

Joined: Oct 19, 2002
Posts: 43
What I am trying to do is to have a method that receives two arguments. The first one is an index for grades and the second one argument is the grade to be placed in the index. I am setting the grade into grades array and setting an equivalent gp. The gp is set by using a parallel array to find the gp for the grade.
What I am starting out with:
public void setGrade(char[] stuG, char[] valGrade)
{

}
here are my variables
int stuNum;
char[] valGrade;
int[] valPoint;


int studNum;
char[] stuG;
double[] stuP;
Any help getting this started is appreciated.

Mike
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9053
    
  12
Mike H,

Thank you for changing your name from lizard king.
I'm sorry to inform you that your name still does not comply with the requirements we have at JavaRanch. 'H' is not considered to be a last name. Please change your displayed name again. Thanks a lot for your co-operation.


JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
"mike h" - Err, close. Now if you could just make the "h" into something a little more like a full last name. (We allow an initial for the first name, but not the last name.)
What I am trying to do is to have a method that receives two arguments. The first one is an index for grades and the second one argument is the grade to be placed in the index.
OK. Neither of those are arrays themselves - they are ints (or for the grade, perhaps float or double). You'll need to modify the method declaration appropriately.
The question now is - where is/are the array(s) you need to store the data in? If you've created a class which has the arrays as instance fields (grades and gp), and you've already initialized the fields prior to calling this method, then the arrays are already available for your use. Alternately, you may need to pass the arrays themselves into the method, in addition to the index and value - for a total of four method parameters, rather than two. Probably the first method is better in the long run - but it depends how you're setting up the rest of the class, and what you want the class to do.
By the way, what's the "gp" you refer to?
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9053
    
  12
public void setGrade(char[] stuG, char[] valGrade)
{

}
here are my variables
int stuNum; // a student ID?
char[] valGrade; // letter grades A thru F
int[] valPoint; // point values of the grades (i.e. A=100 B=90 etc)?

int studNum; // is this the same as stuNum?
char[] stuG; // the letter grade the student got on a test/assignment?
double[] stuP; // Why is this a double when valPoint is an int?

What if you just put the numeric value in the array and translate it to a letter afterwards.
mike hengst
Ranch Hand

Joined: Oct 19, 2002
Posts: 43
I guess there is anonymity here, not that I am worried about. Anyways back to the program at hand. Here is my code:
public class Student
{
int stuNum;
char[] valGrade;
int[] valPoint;


int studNum;
char[] stuG;
double[] stuP;
Student()
{
stuNum = stuNum + 1;
}

public void setGrade(char[] stuG,char[] valGrade)
{
}

}
I am a little new at this java stuff that is why I am looking for help here. The text of the instructions go as follow. setGrade receives 2 arguments. The first is the index for the array of the students grades. The first arg is the position of the grade you are setting for the student. The second arg is the letter grade. The method should set the letter grade into student grades array and also set the grade point equal for the letter grade into the grade points array. look up the grade point in the parallel array of grades.
Now I am not looking for a completely coded method as I am supposed to be learning this stuff. Any help again is appreciated and I apologize for the naming thing. I thought I was in compliance with lizard king, hopefully my full name is what will work. Thanks
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9053
    
  12


Here's a start ...
mike hengst
Ranch Hand

Joined: Oct 19, 2002
Posts: 43
so is this going in the right direction
public class Student
{
static char[] studentGrades = new char[5];
static char[] letterGrades = { 'F' , 'D' , 'C' , 'B' , 'A' };
static double[] grades = { 0.0 , 1.0 , 2.0 , 3.0 , 4.0 };
static double[] gradePoints = new double[ 5 ];
int studentNumber;
public Student()
{
int stuNum = stuNum +1;
}

public void setGrade( int index , char letterGrade )
{
// set studentGrades[ index ] = ?? // set gradePoints[ index ] = ??}
for (int i = 0;i < studentGrades[].length; ++i)
{
//setting letter grade into studentGrades array
studentGrades[i] = letterGrade;
//setting the index of the letterGrade into letterGrades array
studentGrades[i].index = letterGrades[i];
//setting gradePoints into grades
gradePoints[i].index = grades[i];






// studentGrades[i] = letterGrade;


}

}
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9053
    
  12
Originally posted by mike hengst:
so is this going in the right direction

Since studentGrades is an array of char, I would think you would want to assign a char into each array slot (defined by the array index).

You could also throw in a couple of System.out.println statements to see what you're getting each time. Try compiling it (maybe even one statement at a time by commenting the others out) and see what happens.

Also note that indentation (and code tags to preserve that indentation) helps readability which helps other people's ability to help you.
Andrew Lit
Ranch Hand

Joined: Jul 01, 2002
Posts: 135
i have question according arrays to. i decided not to make new topic.
i wonder how should i pass double array to a constructor.
Should i do it by iterating all the elements or there is a better way?
thank you
[ October 20, 2002: Message edited by: Andrew Lit ]
mike hengst
Ranch Hand

Joined: Oct 19, 2002
Posts: 43
I have worked a little more on this. So in my setGrade method I have passed index and letterGrade in to the method. Is this how you use them to set the positions in the arrays equal to the index and letterGrade. Also when I look up the gradepoint do I(and how) loop through studentgrades and assign the gradepoint....Thanks for all the help...

import java.text.*;

public class Student
{
static char[] studentGrades = new char[5];
static char[] letterGrades = { 'F' , 'D' , 'C' , 'B' , 'A' };
static double[] grades = { 0.0 , 1.0 , 2.0 , 3.0 , 4.0 };
static double[] gradePoints = new double[ 5 ];
static int studentNumber;
public Student()
{
int stuNum += ;
}

public void setGrade( int index , char letterGrade )
{
// set studentGrades[ index ] = ?? // set gradePoints[ index ] = ??}
for (int i = 0;i < studentGrades.length; ++i)
{
if (letterGrade == studentGrades[i])
{
studentGrades[i] = letterGrade;
studentGrades[index] = letterGrades[i];
gradePoints[index] = grades[i];
}
// studentGrades[i] = letterGrades;
}

}
public void setStudentNumber(int val)
{
studentNumber = val;
}

public int getStudentNumber()
{
return studentNumber;
}
public String toString(int stuNum)
{
DecimalFormat df = new DecimalFormat("0.00");
StringBuffer sb = new StringBuffer("Student " + stuNum);
sb.append("/n" + "Grades");
sb.append("/n" + "Course1: " + studentGrades[0] + df.format(gradePoints[0]));
sb.append("/n" + "Course2: " + studentGrades[1] + df.format(gradePoints[1]));
sb.append("/n" + "Course3: " + studentGrades[2] + df.format(gradePoints[2]));
sb.append("/n" + "Course4: " + studentGrades[3] + df.format(gradePoints[3]));
sb.append("/n" + "Course5: " + studentGrades[4] + df.format(gradePoints[4]));
}
}

ps I hope that the indentation comes through this time. The last time when I pasted it in the formatting was lost.
[ October 20, 2002: Message edited by: mike hengst ]
mike hengst
Ranch Hand

Joined: Oct 19, 2002
Posts: 43
I can see that the indents did not come thru. How are getting your code to stay indented. I noticed that the font is smaller , are youusing a screenshot instead of copy and paste?
Sorry for the 'barely readable code'
Mike
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9053
    
  12
When you post your reply, see the buttons below the text box? Press the one named "code". This will give you a pair of code tags. You need to put your code between the code tags.
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9053
    
  12

Does this code compile? Is is called by another class? Where is main()?
mike hengst
Ranch Hand

Joined: Oct 19, 2002
Posts: 43
Thanks for the info on the code tags. I am new to this site. Here is my code again. This compiles but I do not know if it works. Someone with experience may spot something that needs change. Thanks
mike hengst
Ranch Hand

Joined: Oct 19, 2002
Posts: 43
Yes it does compile. It is going to be called by another class GradePoint. GradePoint instantiates Student twice for two student objects and assigns the grades and also displays the student objects. If you would like I could post what I have for that class as well. It is still in progress so...
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9053
    
  12
Looks like you're on the right track.
mike hengst
Ranch Hand

Joined: Oct 19, 2002
Posts: 43
I think that I will get it all today. Thanks for your time and patience. I appreciate it.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: arrays