Hey guys and gals. Like I said in my last post I am in college. Well I wrote a program and a friend who majored in computer science said that my program will work... Well it does but doesnt do like I want. I am in need of advice using arrays... I am writing a program that has to use 10 grades. This program is suppost to show the class average, lowest grade, and highest grade. It will not do that put the program functions... As you can tell I'm here for advice not being lazy and doing my homework. Thanks for any help here is what I have wrote so far.
public class array1
{
public static void main(String[] args){

int[] gradeArray = {80,90,91,92,93,94,95,96,97,98};
{

System.out.println("gradeArray/10");
}
{
System.out.println("lowest grade");
}
{
System.out.println("Highest grade");
}
}
}

Thanks for any help it is appreciated and thanks for the help on my last post.

Recommended Answers

All 3 Replies

public class array1
{
    public static void main(String[] args) {
        int[] gradeArray = {80,90,91,92,93,94,95,96,97,98}; //creates a new array called "gradeArray" that stores grades in it
        {
            System.out.println("gradeArray/10"); //literally prints out the words "gradeArray/10"
        }
        {
            System.out.println("lowest grade"); //literally prints out the words "lowest grade"
        }
        {
            System.out.println("Highest grade"); //literally prints out the words "Highest grade"
        }
    }
}

You can't just tell the computer to print this stuff and expect it to do all the work for you. The computer doesn't know how to find the highest grade, the lowest grade or the average. You need to create code that will manipulate the array through loops to do this.

Furthermore, you don't use {...} in general -- you only use it when you want to define a block. For instance, the line "public class array1" says "I am making a class called array1." We would want {...} to tell us in between the braces "this what is in that class I am making called array1." So too, the line "public static void main(String[] args)" says "I am making a method called main." We would want {...} here also to tell us in between the braces "this is what is in that method I am making called main." However, there is no reason to have all those blocks in the middle with no heading -- they don't have any purpose. Your code as it stands should really look like:

public class array1
{
    public static void main(String[] args)
    {
        int[] gradeArray = {80,90,91,92,93,94,95,96,97,98}; //creates a new array called "gradeArray" that stores grades in it
        System.out.println("gradeArray/10"); //literally prints out the words "gradeArray/10"
        System.out.println("lowest grade"); //literally prints out the words "lowest grade"
        System.out.println("Highest grade"); //literally prints out the words "Highest grade"
    }
}

Of course, that code still has the issue of just literally printing those words in the quotes. You still need to make code to fix it.

Whats your segustion on how to fix this? I was never given any real good explanation on how to do arrays other than figure it out yourself...

Well I can't give you the solution to your problem, but these are notes that I made on arrays for my AP Comp Sci test, so take a look at them and maybe they will help you out:
---------------------------------------------------------------
Definition and Usefulness of an Array
○ Arrays are an efficient tool for storing information that would need to be stored in a series of variables in an otherwise inefficient and confusing way.
○ It is basically a set of elements stored together in a linear formation according to index
§ Imagine an answer sheet -- you have a question number (index) and an answer (element value)
○ Arrays are particularly useful when dealing with long lists of data, such as grades for a student

Declaration
DataType[] name = new DataType[ArraySize];
DataType name[] = new DataType[ArraySize];

Assignment
○ To access a value, use the syntax name[index] in place of where you would use a variable name.

Initialization
○ Values in an array are all empty values that need to be initialized
○ Arrays can be initialized with curly braces containing the contents after the declaration statement:
DataType[] name = new DataType[ArraySize] {element0, element1, element2, etc};

Arrays & Loops Combo
○ Since arrays use a counter to access the data, it is easy to perform functions with them such as initialization of values or adding the values together with a for loop
○ Counter for the for loop functions as an index counter

String[] args
○ Another look at the main method should give us understanding of what String[] args represents.
○ An array of arguments can be passed into the method as strings.

Typecasting Array Contents
Syntax: (DataType[])arr;

Shortcomings of Arrays
○ Size is immutable -- does not shrink or expand
○ Values cannot be removed or added, only changed.

Array Index Out Of Bounds Exception
○ If you try to retrieve a value from the array from a non existent index (a negative one or one that exceeds the array size), the program will crash because that index does not exist.
○ Known as an infamous error message that frustrates everyone

Matrices and the XY Plane
○ Arrays can have multiple dimensions
○ To add a new dimension, simply add another bracket set in the array usage:
DataType[][] name = new DataType[first dimension size][second dimension size];
○ Usually the first bracket represents the row, and the second bracket represents the column
○ You can add array dimensions to an array even if you can't map it (like a 4D array).
Warning: the memory space consumed is the volume of the figure, so the size of every measurement has to be multiplied together.
○ In order to properly use methods of arrays, such as the .length property, treat the matrix as an "array of arrays." In this sense:
arr.length returns the length of the first dimension
arr[0].length returns the length of the second dimension
----------------------------------------------
This sample usage of arrays with loops might help you:

int[] arr = new int[5];
for (int i=0;i<arr.length;i++)
{
    arr[i] = i+1;
}

//At this point, arr will look like {1,2,3,4,5}

See if you can use any of this to help you. Good luck!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.