Results 1 to 8 of 8
  1. #1
    luioliher is offline Member
    Join Date
    Apr 2012
    Location
    Phoenix
    Posts
    6
    Rep Power
    0

    Default Need help with my programming assignment.

    Hello

    I am new to this forum. I just created my account about 5 minutes ago. I would like to ask for your help. I currently have a programming assignment that I am stuck on. Here is what I am working with.

    Java Code:
    import java.util.Scanner;
    
    
    public class Assignment8 {
    
    	public static void main (String [] args) {
    
    		Scanner scan = new Scanner (System.in);
    
    		final int MAX = 10;
    
    		Numbers nums = new Numbers(MAX);
    
    		char command;
    		int value;
    
    		System.out.println ("To add an element into the array, type a.");
    		System.out.println ("To delete an element from the array, type d.");
    		System.out.println ("To print the current contents of the array, type p.");
    		System.out.println ("To exit this program, type x.\n");
    		System.out.print ("Add (a), delete (d), print (p) or exit (x)?: ");
    		command = scan.nextLine().charAt(0);
    
    		while (command != 'x') {
    
    			if (command == 'a' || command == 'd') {
    				System.out.print ("Enter a number: ");
    				value = scan.nextInt();
    				scan.nextLine();
    				if (command == 'a')
    					nums.add(value);
    				else
    					nums.delete(value);
    			}
    			else if (command == 'p')
    				nums.print();
    			else System.out.println ("Not a value input");
    
    			System.out.print ("Add (a), delete (d), print (p) or exit (x)?: ");
    
    			command = scan.nextLine().charAt(0);
    		}
    
    		System.out.println ("Program Complete");
    	}
    }
    
    -------------------------------------------------------------------------------------------------------------------------------------------------
    
    
    
    public class Numbers 
    {
    	final int MAX = 10;
    	int [] array = new int[MAX];
    	
    	public Numbers(int MAX)
    	{
    		
    	}
    	
    	public void add(int value)
    	{
    		for(int counter = 0; counter < array.length; counter++)
    			if(array[counter] == 0)
    	        {
    	           array[counter] = value;
    	        }
    	}
    	
    	public void delete(int value)
    	{
    		for(int counter = 0; counter < array.length; counter++)
    			if(array[counter] == value)
    				array[counter] = 0;
    	}
    	
    	public int[] print()
    	{
    		return array;
    	}
    
    }
    -------------------------------------------------------------------------------------------------------------------------------------------------

    I am not allowed to edit the main class. I wrote the Numbers class by myself. My problem is that I'm not sure if what I have done is correct. The main class will use the Numbers class's method add to add values input from the main class into an array that was created in the Numbers class. Sorry if I'm not being clear. The same thing will occur with the delete method, except it will obviously delete values from the array. I think what I have now is correct, but, when I run the program and press 'p' to print the array it stays blank. What is wrong with this?

    Thank you for your time and have a nice day :)
    Last edited by JosAH; 04-20-2012 at 07:40 PM. Reason: added [code] ... [/code] tags

  2. #2
    Diargg is offline Senior Member
    Join Date
    Feb 2012
    Posts
    117
    Rep Power
    0

    Default Re: Need help with my programming assignment.

    Java Code:
    else if (command == 'p')
    nums.print();
    ...
    public int[] print()
    {
    return array;
    }
    What does your print statement do?

    Also, you should put [CODE] tags around your work.

  3. #3
    luioliher is offline Member
    Join Date
    Apr 2012
    Location
    Phoenix
    Posts
    6
    Rep Power
    0

    Default Re: Need help with my programming assignment.

    Sorry about that. The print statement should return the content of the array. I've tried so many way to get it to display the array content but have failed

  4. #4
    Diargg is offline Senior Member
    Join Date
    Feb 2012
    Posts
    117
    Rep Power
    0

    Default Re: Need help with my programming assignment.

    What do you do with the array once it's returned though? The computer doesn't know what .print() means, it just runs the code found there. So it picks up the array (which to the computer, may as well be a rock), and hands it to your main program. Which then does nothing with it. Have you tried to System.out.println it?

  5. #5
    luioliher is offline Member
    Join Date
    Apr 2012
    Location
    Phoenix
    Posts
    6
    Rep Power
    0

    Default Re: Need help with my programming assignment.

    I have tried that but it tells me ' cannot return void result'. I just added the following.

    return: System.out.println(array);

  6. #6
    luioliher is offline Member
    Join Date
    Apr 2012
    Location
    Phoenix
    Posts
    6
    Rep Power
    0

    Default Re: Need help with my programming assignment.

    I changed my print method to the following and it works fine now. Thank you for your time, Diargg.

    public int[] print()
    {
    for(int counter = 0; counter < array.length; counter++)
    System.out.println(array[counter]);
    return array;
    }

  7. #7
    Diargg is offline Senior Member
    Join Date
    Feb 2012
    Posts
    117
    Rep Power
    0

    Default Re: Need help with my programming assignment.

    You actually don't need to return the array - print can easily be public void print();
    Also, is the behaviour exhibited by your .add and .delete functions what you expected?

  8. #8
    luioliher is offline Member
    Join Date
    Apr 2012
    Location
    Phoenix
    Posts
    6
    Rep Power
    0

    Default Re: Need help with my programming assignment.

    I won't be able to return the array if I put void. I want to display the array.

    Yes, the .add and .delete functions worked well. They did what I wanted them to do.

Similar Threads

  1. help..stuck w/ a java programming assignment
    By clemsontigers in forum New To Java
    Replies: 16
    Last Post: 02-19-2014, 05:16 AM
  2. Replies: 15
    Last Post: 04-21-2012, 02:38 AM
  3. need help with basic programming assignment (with arrays)
    By magictricks1020 in forum New To Java
    Replies: 11
    Last Post: 05-03-2011, 03:19 AM
  4. assistance w/ java programming assignment
    By clemsontigers in forum New To Java
    Replies: 4
    Last Post: 04-07-2011, 09:07 PM
  5. Replies: 1
    Last Post: 10-02-2009, 02:35 AM

Tags for this Thread

Posting Permissions

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