Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: Help with Arrays

  1. #1
    Junior Member
    Join Date
    Oct 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Help with Arrays

    Hey yall,

    This is my first Java Programming course and I'm a little stuck on the arrays section. Our assignment is to create multiple single and multidimensional arrays and also create multiple methods. The following is the assignment:

    1.A method that accepts an array of doubles and returns the highest double number in the array.(Highest Grade)
    2. A method that accepts an array of doubles and returns the lowest double number in the array.(Lowest Grade)
    3. A method that accepts an array of doubles and returns the average of the numbers in the array as a double.(Average Grade)
    4. A method that accepts a double number and returns a String letter grade.
    5. A method that has no input but returns a double array of student grades.
    6. A method that accepts an integer for input and returns the name equivalent for numbers 1 – 10 and adds the correct two letter extension to numbers after that. This should be used when asking for the student's name and grades like:
    "Please enter the first student's name: " and "Please enter the first grade: " ... second grade: ...
    7. Use a String Array to store the student names along with a parallel, double[][] multidimensional array to store all of the student grades

    The flow of the program should look like this:

    1.Ask for number of students
    a. Collect number of students
    2. Create student name array and multidimensional grades array
    3. Collect each student’s information
    a.Ask for first student’s name and assign it to students[]
    student[i] = input.next();
    b. Call the method that collects student grades and assign it to the grades array
    grades[i] = collectGrades();
    4. Do this for each student (loop back to 3)
    5. When all of the information is collected into the parallel arrays, for each student:
    a. System.out.print() a line that has the proper header
    b. For each student output the information collected in the proper format using the other required methods.

    The problem I'm having is getting it to loop back. I can collect the number of students, the name of that first student, the grades for that student and it will give the output - but I'm confused on how to get it to loop back to ask for the name and the grades again. I'm also confused on how to pass the data from grades to the multidimensional array. Attached is my code:

    Thanks so much for your help in advance!

    import java.util.*;
    public class Midterm {
     
    	public static void main(String[] args) {
    		Scanner input = new Scanner(System.in);
     
    		System.out.println("Enter the number of students: ");
    		int numofStudents = input.nextInt();
     
     
    		// define arrays
    		String[] studentName = new String[numofStudents];
    		double[][] scores = new double[numofStudents][];
     
     
    		// for loop for student's names
     
    		for (int n = 0;n < 1; n++){
    		System.out.println("Enter the " +  numName(n+1) + " student's name(s): ");
    		studentName[n] = input.next();
     
    		}// end for
     
    		//call collectGrades
    		double grades[] = collectGrades();
     
     
     
    		//output
    		System.out.println("Student Name\tHigh Score\tLow Score\tAverage\tGrade");
    	    System.out.println("----------------------------------------------------------");
    	    for(int s = 0;s < numofStudents;s++) {
    			System.out.println(studentName[0 + s] + "\t\t" +highGrade(grades) + "\t\t" +lowGrade(grades)  + "\t\t\t" +(averageGrade(grades)));
     
     
    	    }
    		}
     
     
     
    	public static double[] collectGrades() {
    		Scanner input = new Scanner(System.in);
    		System.out.println("How many grades will you be entering" + "\n for this student?");
    		int numGrades = input.nextInt();
     
    		double grades[] = new double[numGrades];
     
    		//collect grades
    		for(int g = 0;g < grades.length; g++) {
    			System.out.println("Please enter the " +numName(g+1) + " grade:");
    			grades[g] = input.nextDouble();
     
    		} // end for loop
     
    		return grades;
    	} // end collectGrades
     
    	public static double highGrade (double arrayIn[]) {
    		Arrays.sort(arrayIn);
    		return arrayIn[arrayIn.length -1];
     
    	} // end highGrade
     
    	public static double lowGrade (double arrayIn[]) {
    		Arrays.sort(arrayIn);
    		return arrayIn[0];
     
    	} // end highGrade
     
    	public static double averageGrade(double arrayIn[]) {
    		double avgGrade = 0.0;
    		for(int i = 0;i < arrayIn.length;i++) {
    			avgGrade += arrayIn[i];
     
    		} // end for
     
    		return avgGrade/arrayIn.length;
     
    	} // end averageGrade
     
     
    	public static String numName(int intIn) {
    		String numName = "";
    		switch(intIn) {
    		case 1:
    			numName = "first";
    			break;
    		case 2: 
    			numName = "second";
    			break;
    		case 3:
    			numName = "third";
    			break;
    		case 4: 
    			numName = "fourth";
    			break;
    		case 5:
    			numName = "fifth";
    			break;
    		case 6: 
    			numName = "sixth";
    			break;
    		case 7:
    			numName = "seventh";
    			break;
    		case 8: 
    			numName = "eight";
    			break;
    		case 9:
    			numName = "ninth";
    			break;
    		case 10: 
    			numName = "tenth";
    			break;
    		default:
    			numName = intIn + (intIn<20?"th" : (intIn%10==1?"st":
    			(intIn%10==2?"nd":(intIn%10==3?"rd":"rh"))));
    		} //end Switch
     
    		return numName;
     
    	}
     
    } // end public class Midterm


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Help with Arrays

    Whew! So much to get done.
    I could not keep the instructions in my head long enough to read them all.

    The first thing needed here is a plan. Break all of this up into smaller problems, and work on just one at a time. The 'flow' of the program there looks like a great starting point, but some of those steps look like they could be broken down into smaller steps as well. The more you break the steps down, the more they start looking like code (or at least something that translates into code).

    I see a question about getting a loop done. I suggest making a loop that just prints hi. Then get it to print hi for the number of times entered by the user. Get that basic loop working and then start adding the code to do something inside the loop besides printing hi. Keep the additions small and test at every stage.

  3. #3
    Junior Member
    Join Date
    Oct 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with Arrays

    Alright, so I figured out 95% of it. I'm getting the code to output correctly. I have one method left to conquer.

    I have to write a method that grabs the input from the Average Grade and translate that into a letter grade. So, is it possible to get the return value from method averageGrade and put it into method letterGrade? I was also thinking doing an if statement to make it a letter grade. Let me know how this looks i.e.:

    public static String letterGrade(double averageGrade) {
     
    if (averageGrade > 89)
    letterGrade = "A";
    if (averageGrade > 79)
    letterGrade = "B";
    if (averageGrade > 69)
    letterGrade = "C";
    if (averageGrade > 59)
    letterGrade = "D";
    else
    letterGrade = "F";
     
    return = letterGrade;
     
     
     
    }
    Last edited by djhunt90; October 13th, 2012 at 12:37 PM.

  4. #4
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Help with Arrays

    Quote Originally Posted by djhunt90 View Post
    Alright, so I figured out 95% of it. I'm getting the code to output correctly. I have one method left to conquer.

    I have to write a method that grabs the input from the Average Grade and translate that into a letter grade. So, is it possible to get the return value from method averageGrade and put it into method letterGrade? I was also thinking doing an if statement to make it a letter grade. Let me know how this looks i.e.:

    public static String letterGrade(double averageGrade) {
     
    if (averageGrade > 89)
    letterGrade = "A";
    if (averageGrade > 79)
    letterGrade = "B";
    if (averageGrade > 69)
    letterGrade = "C";
    if (averageGrade > 59)
    letterGrade = "D";
    else
    letterGrade = "F";
     
    return = letterGrade;
     
     
     
    }
    First let me say this is not to brush you off, but to get you thinking on the right path.
    Did you try it? Did it compile and run without errors? Are the results good?
    If so, great. If not, what seems to be wrong?

  5. #5
    Junior Member
    Join Date
    Oct 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with Arrays

    I changed it up a little:

    public static char letterGrade (double arrayIn[]) {
     
    		double abc = 0;
    		for(int i = 0;i < arrayIn.length;i++) {
    			abc += arrayIn[i];
     
    	} // end for
    		if (abc > 90)
    			return 'A';
    		if (abc > 80)
    			return 'B';
    		if (abc > 70)
    			return 'C';
    		if (abc > 60)
    			return 'D';
    		else 
    			return 'F';
    	}

    It's returning A for every one though. Is it because I'm use the arrayIn.length so it's pushing the highest to the top?

  6. #6
    Junior Member
    Join Date
    Oct 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with Arrays

    I actually believe I just answered my own question!

  7. #7
    Member
    Join Date
    Apr 2012
    Posts
    161
    Thanks
    0
    Thanked 27 Times in 27 Posts

    Default Re: Help with Arrays

    Just looking at your letterGrade method, it seems you are just adding up the grades in the array, not getting the average of the grades, which would mean that their grade should most definitely be higher than an 89.

  8. #8
    Junior Member
    Join Date
    Oct 2012
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Help with Arrays

    Could you please post your final solution. It may help someone out with future questions. Thanks!

Similar Threads

  1. Arrays
    By Emperor_Xyn in forum Java Theory & Questions
    Replies: 6
    Last Post: December 9th, 2011, 07:24 PM
  2. Arrays
    By ruffu054 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: September 7th, 2011, 11:18 PM
  3. arrays
    By dwamalwa in forum Java Theory & Questions
    Replies: 4
    Last Post: November 27th, 2010, 01:44 AM
  4. Arrays
    By mlan in forum Java Theory & Questions
    Replies: 2
    Last Post: February 26th, 2010, 10:23 AM
  5. 2d Arrays
    By mgutierrez19 in forum Collections and Generics
    Replies: 5
    Last Post: October 27th, 2009, 04:08 PM