# Thread: Help With my Homework Please on Arrays :)

1. Member Join Date
Apr 2013
Posts
4
Rep Power
0

## Help With my Homework Please on Arrays :)

The assiment says

"Assignment #7 Chapter 6
This goal of this lab is to get you comfortable with 1D arrays and the syntax of how they work.
As with most programmers, often your code must work with existing code.
Please use the code listed below and complete where necessary.
Write the methods listed below that only show the signature and a comment.
public class Arrays
{
private int[] num = { 7, 8, 9, 9, 5, 7, 4,3};
private int[] num2 = new int ;
private char[] char1 = new char;
private char[] char2 = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i'};
public Arrays()
{
int i = 0;
for (i = 0; i < num2.length  (num2.length/2); i++)
num2[i] = i;
for (i = 0; i < char1.length; i++)
char1[i] = (char)(i+65);
}
}
//Create get methods for the instance variables
//Create the following methods, use the signature listed below.
public int sum(int[] a)
//sum the contents of array a that is passed
public int count(int[] a)
//count the number of elements in the array
public int countNonZero(int[] a)
//count the nonzero elements
public void printArray(String title, int[] a)
//first display a title for the array, then print the contents. The array is of type int
public void printArray(String title, char[] a)
//first display a title for the array, then print the contents. The array is of type char
public int average(int[] a)
//return the average of the elements in the array
public int findLast(int[] a, int j)
//return the index of the last location of the element j. if it is not found return -1
public int findLast(char[] char c)
//return the index of the last location of the element c. if it is not found return -1
public int findMax(int[] a)
//return the largest element
public int findIndexOfMax(int[] a)
//return the index of the largest element
public int range(int[] a)
//return a number that is the difference between the maximum and minimum values.
public int range(char[] c)
//return a number that is the difference between the maximum and minimum values.
public class ArrayDrv
{
public static void main(String args[])
{
// create the local array a1.
int[] a1 = { 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
Arrays useArray = new Arrays();
useArray.printArray(Array num1, useArray.getNum1());
useArray.printArray(Array a1,a1);
useArray.printArray(Array num2, useArray.getNum2());
useArray.printArray(Array char1, useArray.getChar1());
useArray.printArray(Array char2, useArray.getChar2());
System.out.println("The sum of a1 array is: " + useArray.sum(a1));
System.out.println("The sum of num2 array is: " + useArray.sum(useArray.getNum2()));
System.out.println("The sum of num1 array is: " + useArray.sum(useArray.getNum1()));
System.out.println("The count of num2 is: " + useArray.count(useArray.getNum2()));
System.out.println("The number of nonzero is: " +
useArray.countNonZero(useArray.getNum2()));
// continue with the same testing for all the methods you create.
}
}
}
2. Modify this program to add an array of strings. The array should be called names.
Fill the array with 10 different first names. This can be done in the constructor.
Create the following methods:
public boolean findName(String n1[], String newName)
//returns true if the newName is in the string array, false otherwise."

Here is my code. My question is if i'm doing this right and if not what do I need to do to correct it. Please give me examples on how to do it right :) THANK YOU!

Java Code:
```/**
* AWT Sample application
*
* @author
* Jared Wines
*/

import java.until.Arrays;
public class Arrays
{
private int[] num = { 7, 8, 9, 9, 5, 7, 4,3};
private int[] num2 = new int ;
private char[] char1 = new char;
private char[] char2 = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i'};

public Arrays()
{
int i = 0;
for (i = 0; i < num2.length  (num2.length/2); i++)
num2[i] = i;
for (i = 0; i < char1.length; i++)
char1[i] = (char)(i+65);
}
}
//Create get methods for the instance variables
//Create the following methods, use the signature listed below.
public int sum(int[i] Array.sum)
//sum the contents of array a that is passed

public int count(int[i] Array.count)
//count the number of elements in the array

public int countNonZero(int[i] Array.countNonZero)
//count the nonzero elements

public void printArray(String title, int[i] Array.printArray)
//first display a title for the array, then print the contents. The array is of type int

public void printArray(String title, char[i] Array.printArray)
//first display a title for the array, then print the contents. The array is of type char

public int average(int[] Array.average)
//return the average of the elements in the array

public int findLast(int[] a, int j)
//return the index of the last location of the element j. if it is not found return -1

public int findLast(char[] char c)
//return the index of the last location of the element c. if it is not found return -1

public int findMax(int[] a)
//return the largest element

public int findIndexOfMax(int[] a)
//return the index of the largest element

public int range(int[] a)
//return a number that is the difference between the maximum and minimum values.

public int range(char[] c)
//return a number that is the difference between the maximum and minimum values.

public class ArrayDrv
{

public static void main(String args[])
{

// create the local array a1.
int[] a1 = { 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
Arrays useArray = new Arrays();

useArray.printArray(Array num1, useArray.getNum1());
useArray.printArray(Array a1,a1);
useArray.printArray(Array num2, useArray.getNum2());
useArray.printArray(Array char1, useArray.getChar1());
useArray.printArray(Array char2, useArray.getChar2());

System.out.println("The sum of a1 array is: " + useArray.sum(a1));
System.out.println("The sum of num2 array is: " + useArray.sum(useArray.getNum2()));
System.out.println("The sum of num1 array is: " + useArray.sum(useArray.getNum1()));
System.out.println("The count of num2 is: " + useArray.count(useArray.getNum2()));
System.out.println("The number of nonzero is: " + useArray.countNonZero(useArray.getNum2()));

// continue with the same testing for all the methods you create.
}
}```  Reply With Quote

2. Moderator   Join Date
Apr 2009
Posts
13,541
Rep Power
26

## Re: Help With my Homework Please on Arrays :)

Does it compile?
If not then please supply the full error message(s) and highlight the line(s) on which they occur.

If it compiles, then does it run?
If not then please supply full exceptions, including stack traces, highlighting the line in your code it is thrown from.

If there is no exception then does it provide the correct output?
If not then please show the output you are getting and the output you expected.  Reply With Quote

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•