• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

arrays

 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 4012
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Is this what you mean?
 
mike hengst
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Jim Yingst
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"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
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Here's a start ...
 
mike hengst
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.

 
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Does this code compile? Is is called by another class? Where is main()?
 
mike hengst
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Looks like you're on the right track.
 
mike hengst
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think that I will get it all today. Thanks for your time and patience. I appreciate it.
 
Of course, I found a very beautiful couch. Definitely. And this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic