Results 1 to 8 of 8
- 04-20-2012, 07:28 PM #1
Member
- Join Date
- Apr 2012
- Location
- Phoenix
- Posts
- 6
- Rep Power
- 0
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
- 04-20-2012, 07:42 PM #2
Senior Member
- Join Date
- Feb 2012
- Posts
- 117
- Rep Power
- 0
Re: Need help with my programming assignment.
Java Code:else if (command == 'p') nums.print(); ... public int[] print() { return array; }
Also, you should put [CODE] tags around your work.
- 04-20-2012, 07:52 PM #3
Member
- Join Date
- Apr 2012
- Location
- Phoenix
- Posts
- 6
- Rep Power
- 0
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
- 04-20-2012, 07:55 PM #4
Senior Member
- Join Date
- Feb 2012
- Posts
- 117
- Rep Power
- 0
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?
- 04-20-2012, 08:03 PM #5
Member
- Join Date
- Apr 2012
- Location
- Phoenix
- Posts
- 6
- Rep Power
- 0
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);
- 04-20-2012, 09:35 PM #6
Member
- Join Date
- Apr 2012
- Location
- Phoenix
- Posts
- 6
- Rep Power
- 0
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;
}
- 04-20-2012, 10:25 PM #7
Senior Member
- Join Date
- Feb 2012
- Posts
- 117
- Rep Power
- 0
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?
- 04-21-2012, 05:51 AM #8
Member
- Join Date
- Apr 2012
- Location
- Phoenix
- Posts
- 6
- Rep Power
- 0
Similar Threads
-
help..stuck w/ a java programming assignment
By clemsontigers in forum New To JavaReplies: 16Last Post: 02-19-2014, 05:16 AM -
Desperately Need Help With Java Programming Assignment
By Hadsvich in forum New To JavaReplies: 15Last Post: 04-21-2012, 02:38 AM -
need help with basic programming assignment (with arrays)
By magictricks1020 in forum New To JavaReplies: 11Last Post: 05-03-2011, 03:19 AM -
assistance w/ java programming assignment
By clemsontigers in forum New To JavaReplies: 4Last Post: 04-07-2011, 09:07 PM -
Help with implement class programming assignment
By ALH813 in forum EclipseReplies: 1Last Post: 10-02-2009, 02:35 AM
Bookmarks