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 2 of 2

Thread: Help With my Homework Please :)

  1. #1
    Junior Member surfelijo's Avatar
    Join Date
    Feb 2013
    Location
    Fallbrook, CA
    Posts
    14
    My Mood
    Nerdy
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Help With my Homework Please :)

    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 [10];
    private char[] char1 = new char[26];
    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. THANK YOU!




    /**
     * 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 [10]; 
    		private char[] char1 = new char[26]; 
    		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. 
      } 
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Help With my Homework Please :)

    My question is if i'm doing this right
    Does it compile without errors? If there are errors, copy the full text and paste it here.
    Are there errors when you execute it? If there are errors, copy the full text and paste it here.

    BTW You should not name your class with the same name as a Java SE class.

    The code is poorly formatted. All methods should start in the same column.
    Many methods in the post are indented too far.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Help with my homework?
    By mshem in forum Java Theory & Questions
    Replies: 2
    Last Post: November 16th, 2012, 03:15 AM
  2. Homework help?
    By regi.dg in forum Object Oriented Programming
    Replies: 0
    Last Post: October 16th, 2012, 08:17 PM
  3. HELP WITH HOMEWORK!!
    By jmillernc01 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 10th, 2012, 11:13 AM
  4. Homework help
    By hockey15 in forum Object Oriented Programming
    Replies: 3
    Last Post: September 23rd, 2012, 11:28 PM
  5. Homework help
    By cd247 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 11th, 2009, 05:56 PM