[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Need help with homework & understanding!

Posted on 2012-09-19
28
Medium Priority
?
216 Views
Last Modified: 2012-12-31
myList.java is an interface. Create a class myArrayList.java that efficiently implements all the methods within your
myList.java interface. So far, this is what I came up with. The content of my myList.java. Under that is the content of myArrayList.java, I dont know a good way to implement the last two methods in myArrayList.java.

________________________
List.java
.......................................
package myInterface;

public interface myList<T> {
	public int size();
	public T get(int i); 
	public T set(int i, T item);
	public int indexOf(T item);
	public void add(T item);
	public void add(int i, T item);
	public T remove(int i);
	public Object[] toArray();
	public <K> K[] toArray(K[] a);
	public String toString();	
}

Open in new window




__________________________________
ArrayList.java
.......................................................
package myInterface;

public class myArrayList<T> implements myList<T> {

	private int size;
	private T[] Data;
	
	public myArrayList() {
		Data = (T[]) new Object[2];
		size=0;
	}

	public myArrayList(int capacity) {
		Data = (T[]) new Object[capacity];
		size=0;
	}
	

	public int size() {
		return this.size;
	}

	public T get(int i) {
		if (i < 0 || i >= size) 
			throw new ArrayIndexOutOfBoundsException(i);
		return Data[i];
	}

	public T set(int i, T item) {
		if (i < 0 || i >= size) 
			throw new ArrayIndexOutOfBoundsException(i);
		T old=Data[i];
		Data[i] = item;
		return old;
	}

	public int indexOf(T item) { 
		for (int i=0; i<size; i++) 
			if (Data[i].equals(item)) return i;
		return -1;
	}

	private void doubleCapacity() {
		int c =  Data.length*2;
		T[] newList = (T[]) new Object[c];
		for (int i =0; i< Data.length; i++)
			newList[i] = Data[i];  // This is OK?
		Data = newList;
	}


	public void add(T item) { 
		add(size,item);
	}


	public void add(int i, T item) { // add(insert) item to posion i
		if (i < 0 || i > size) 
			throw new ArrayIndexOutOfBoundsException(i);

		if (size == Data.length) // the Data array is full
			doubleCapacity();

		for (int k=size;k>i;k--) {
			Data[k] = Data[k-1];
		}
		size++;
		Data[i] = item;
	}


	public T remove(int i) {
		if (i < 0 || i >= size) 
			throw new ArrayIndexOutOfBoundsException(i);
		T item = Data[i];
		for (int k=i;k<size-1;k++)
			Data[k] = Data[k+1];
		size--;
		return item;
	}
	
	
	
	public Object[] toArray() {
		// needs work
		return null;
	}

	public <K> K[] toArray(K[] a) {
		// needs work
		return null;
	}
}

Open in new window

0
Comment
Question by:JavaBeginner123
  • 16
  • 12
28 Comments
 
LVL 36

Expert Comment

by:mccarl
ID: 38416208
You should just be able to create an Object[] (or a K[] in the second method, if the passed in array is not big enough), and then copy each item from Data[] over to the newly created array.

The above seems simple enough, have you tried it and are having issues?
0
 

Author Comment

by:JavaBeginner123
ID: 38416241
This is the only errors I'm getting. All three say "Unchecked cast from Object[] to T[]"
The errors so far
In addition, I wanted to finish implementing my last two methods before I stated testing.
I was given a class that is in a seperate package called Asg3.class.

________________________
Asg3.java
.......................................
package Asg3;
/* You should not modify this program  */
import myUtil.MyArrayList;

public class Asg3 {

	public static MyArrayList<Integer> addSome(int n) {
		MyArrayList<Integer> a =  new  MyArrayList<Integer>();
		// Add some Integers into to a
		for (int i=0; i<n; i++) {
			a.add(new Integer(i));
			a.add(a.size()-i, new Integer(i));
		}
		System.out.println(a.toString());
		// Try add's Exception
		try {
			a.add(a.size()+1, new Integer(999));
		} catch (ArrayIndexOutOfBoundsException e) {
			System.out.println("Can't add item at "+(a.size()+1));
		}
		return a;
	}
	
	public static void removeSome(MyArrayList<Integer> a) { 
		// Remove some Integers from a
		int n =a.size()/2;
		while (a.size()>n) {
			System.out.print("remove "+a.remove(a.size()-1)+", ");
		}
		System.out.println("\nAfter remove:"+a.toString());
		// Try remove's Exception
		try {
			System.out.print("remove "+a.remove(a.size()-1)+", "); 
			System.out.print("remove "+a.remove(a.size())+", "); ;
		} catch (ArrayIndexOutOfBoundsException e) {
			System.out.print("Can't remove item at "+a.size()+" from ");
			System.out.println(a.toString());
		}
	}
	
	public static void tryToArray(MyArrayList<Integer> a) {	
		Object[] o = a.toArray();
		System.out.print("Try toArray:");
		for (int i=0; i<o.length; i++)
			System.out.print(o[i].toString()+" ");
		System.out.print("\n");
	}
	
	public static void tryCopyConstructor(MyArrayList<Integer> a) {
		MyArrayList<Integer> b = new MyArrayList<Integer>(a);
		removeSome(b);
		System.out.println("Try copy constructor:"+b.toString());
	}
	
	public static void tryIndexOf(MyArrayList<Integer> a) {
		try {
			for (int i=0; i<=a.size(); i++) {
				Integer item= a.get(i);
				System.out.print("["+a.indexOf(item)+"]:"+item.toString()+" ");
			}
		}  catch (ArrayIndexOutOfBoundsException e) {
			System.out.print("["+a.indexOf(null)+"]:null ");
			System.out.println("["+a.indexOf(99)+"]:99");
		}
		
		for (int i=0; i<=a.size(); i++) {
			System.out.print("["+a.indexOf(i)+"]:"+i+" ");
		}
	}
	
	public static void main(String[] args) {
		if (args.length == 0)  {		
			System.out.println("Please provide an integer argument to the program.");
			System.exit(-1);
		}
		MyArrayList<Integer> a;
		a = addSome(Integer.parseInt(args[0]));
		removeSome(a);
		tryToArray(a);
		tryCopyConstructor(a);
		tryToArray(a);
		tryIndexOf(a);
	}
}

Open in new window

0
 
LVL 36

Expert Comment

by:mccarl
ID: 38416330
As to the errors that you are getting, that is easy, instead of creating new Object[]'s, you should be creating new T[]'s, (all three errors are the same issue).

As for your original question, you didn't answer what I asked you; have you tried to implement those 'last two methods'? And if so, what issue's did you get from that?


(Also, note, that in the Test class that was given to you, one of the test requires your array list to have a copy constructor which you will need to implement, in addition to the 2 constructors that you already have.)
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:JavaBeginner123
ID: 38416377
I'm confused with working with the the array as a parameter to implement the last two method; however, I just can't figure out a solution for them.

With the public Object[] toArray()...
.call the method toArray(K[] a) method and use Data a parameter.
.then return an object array

As for the toArray(K[] a) method
I not too sure what to do.
0
 

Author Comment

by:JavaBeginner123
ID: 38416392
I've been working with the second method and I came up with this:

public <K> K[] toArray(K[] a) {
	      if (a.length < size)
	  throw new NullPointerException();
        // Make a new array of a's runtime type, but my contents:
	      System.arraycopy(Data, 0, a, 0, size);
	        if (a.length > size)
              a[size] = null;
	          return a;
     }
}

Open in new window

0
 
LVL 36

Expert Comment

by:mccarl
ID: 38416525
Ok, firstly, the interface can't tell you the full specification of what a method must do, one can sometimes work out the required functionality, but in the case of the toArray methods, it is not 100% clear. I don't know if your teacher gave you any reference info on what the methods should do but I am guessing that the idea is to implement the same functionality as the built-in Java ArrayList. Therefore, I would recommend reading the Javadoc for it here, http://docs.oracle.com/javase/6/docs/api/java/util/ArrayList.html#toArray%28%29

From the above Javadoc you will learn some detail about what each method should do, in terms of whether a new array news to be allocated and items copied in, or if you can just copy items into the array that was already passed in.

A couple of points about what you have already stated developed...

With the public Object[] toArray()...
.call the method toArray(K[] a) method and use Data a parameter.


Not quite. It is as simple as calling the toArray(K[] a) method but the parameter that you pass is different. (Hint: the Javadoc linked above gives you a big clue!)

if (a.length < size)
        throw new NullPointerException();


You don't need to throw an exception in this case. This is a valid use case of the method and so you should do something else. Again, what to do is detailed in the Javadoc


Hopefully after reading the Javadocs some of this will become much clearer. Have a go, and post back your latest code and issues that you are having. We will get there! (Note: I realise that I could just give you code for these methods but since this is homework, you will get much more value out of this exercise if you are able to come up with the code yourself)
0
 

Author Comment

by:JavaBeginner123
ID: 38416575
I still don't understand. I have been reading the same line for hours and can't make sense out of it.

In the toArray(K[] a)  method, the a is where all the elements from the list is begin stored if a is big enough. I'm guessing from the context of the Javadoc that if it is not big enough in size then I must create a new array of K. I believe my prof wants me to add an exception just in case the specified array was null.

As for storing the elements in the new array, I expected the System.arraycopy(Data, 0, a, 0, size); to achieve that.



PS. Sorry for creating another question on Experts. I didn't hear a response from you in a few hours so I thought that you were done helping me; however, I still want to work towards a solution, if your patient.
0
 
LVL 36

Expert Comment

by:mccarl
ID: 38416625
You seem to be on the right track then, in terms of what the methods are supposed to be doing.

I believe my prof wants me to add an exception just in case the specified array was null.
That's fine but you were throwing the Exception when the passed in array, a, was too small. You should be testing if it is null and then throwing the Exception.

However, you are correct in the fact that you DO have to test for the passed in array being not big enough, and in fact what you stated above is exactly correct, in that you need to create a new array of K (and obviously, you have to create it with a size big enough to fit the number of items in your list). Note, that at this point you can just assign this newly created array back into the variable, a, as you no longer need the reference to the original array that was passed in.

As for storing the elements in the new array, I expected the System.arraycopy(Data, 0, a, 0, size); to achieve that.
Yes, that is exactly correct, so this part of your method (and the lines after it) are fine, no need to change them. So that is all for this method, it is pretty close now to being finished.

How are you going with the no-arg toArray() method? As I said above, it should just be a one liner, that calls the toArray(K[] a) method, but you have to work out what the correct parameter should be to pass to it.


PS. Sorry for creating another question on Experts. I didn't hear a response from you in a few hours
No worries, but it was only an hour and a half before I responded, and I do have a day job that takes priority to EE! ;)
I still want to work towards a solution, if your patient.
Agreed, if you're patient also!
0
 

Author Comment

by:JavaBeginner123
ID: 38416678
Ok!! thanks for the clarification.  

For reference this page explains the gist of of my program:
http://www.itk.ilstu.edu/faculty/chungli/MyJavaDoc/

Here is my code so far....

package myUtil;

import java.util.Arrays;

public class MyArrayList<T> extends java.lang.Object implements MyList<T>{
	
	private int size;
	private T[] Data;
	
	public MyArrayList() {
		Data = (T[]) new Object[10];
		size = 0;
	}

	public MyArrayList(int capacity) {
		Data = (T[]) new Object[capacity];
		size=0;
	}
	
	public MyArrayList(MyArrayList<T> ma) {
		Data = (T[]) new Object[ma.Data.length];
		size=ma.size;
		for (int i=0;i<size;i++) {
			Data[i] = ma.Data[i];
		}

	}

	public int size() {
		return this.size;
	}

	public T get(int i) {
		if (i < 0 || i >= size) 
			throw new ArrayIndexOutOfBoundsException(i);
		return Data[i];
	}

	public T set(int i, T item) {
		if (i < 0 || i >= size) 
			throw new ArrayIndexOutOfBoundsException(i);
		T old=Data[i];
		Data[i] = item;
		return old;
	}


	public int indexOf(T item) { // -1 if item can't be found
		for (int i=0; i<size; i++) 
			if (Data[i].equals(item)) return i;
		return -1;
	}

	public T remove(int i) {
		if (i < 0 || i >= size) 
			throw new ArrayIndexOutOfBoundsException(i);
		T item = Data[i];
		for (int k=i;k<size-1;k++)
			Data[k] = Data[k+1];
		size--;
		return item;
	}

private void doubleCapacity() {
	int c =  Data.length*2;
	T[] newList = (T[]) new Object[c];
	for (int i =0; i< Data.length; i++)
		newList[i] = Data[i];  // This is OK?
	Data = newList;
}

public void add(T item) { // add to the end
	add(size,item);
}


public void add(int i, T item) { // add(insert) item to posion i
	if (i < 0 || i > size) 
		throw new ArrayIndexOutOfBoundsException(i);

	if (size == Data.length) // the Data array is full
		doubleCapacity();

	for (int k=size;k>i;k--) {
		Data[k] = Data[k-1];
	}
	size++;
	Data[i] = item;
}


//----------------------------------------------//

	
	public Object[] toArray() {
                 toArray(Data);
	}

	
	public <K> K[] toArray(K[] a) {
		      if(a.length < size)
                             *****
		      System.arraycopy(Data, 0, a, 0, size);
		      if(a.length > size)
		            a[size] = null;
		      return a;
		}  
	
	public String toString(){
		// TODO Auto-generated method stub
		return String;
	}
}
//-------------------------------------------------//

Open in new window



I just added a copyconstructor and imported a java class.
0
 

Author Comment

by:JavaBeginner123
ID: 38416693
Since the return type for the toArray() w/ no argument is an Object Array, I assume that the method is more than a one-liner.
0
 
LVL 36

Expert Comment

by:mccarl
ID: 38416763
Ok, since it is a one-liner, I can't really think of anymore hints with out giving you the code...

return toArray(new Object[size]);

Hopefully, you can at least understand what that is doing... The two main points about the toArray() method are that it returns an Object[] and that the array is a newly allocated array. So what the above is doing is allocating a new Object[], using the other toArray(K[] a) method to do the element copying (it will only copy since we are passing in an array that is big enough), and then returning that array back to the caller.
0
 

Author Comment

by:JavaBeginner123
ID: 38416816
Ok, it now makes sense. Since the toArray() needed to return a type Object array, and the toArray() w/ argument needed an Array of a generic type, we could pass a new Object Array to satisfy both conditions.

As for the toString() method, I have to return a String.
In that String I must concatenate the elements of the array into a list  ["0","1","2","3"] in that manner. While making sure the last element is not preceded by a comma.

I could see creating a tokenizer variable and using a for loop to create tokens of the elements with the hasNext() method. From there I could us concatenate those tokens into a listed array; yet, it seems like a bad approach.
0
 
LVL 36

Expert Comment

by:mccarl
ID: 38417522
elements with the hasNext() method
No, there is no hasNext() method. All your elements are in an array, so you just access them like... Data[ i ]... where i is a loop variable of a for loop, say. I would recommend looking at Java's StringBuilder class; it will be very useful to you here. The only other point is whether to put a comma in the generated String. As you say, you append one after every element except the last. This is easy to do because in the for loop, you know the current index that you are processing (probably in variable, i) and you know the index of the last element (Data.size - 1), so you can just append a comma on every loop iteration except where i == (Data.size - 1).


Also, I note that in the last code that you posted you haven't fixed the 3 "Unchecked cast" warnings. I gave you directions on how to fix that in my second post.
0
 

Author Comment

by:JavaBeginner123
ID: 38439697
I've been watching more tutorials & reading the java docs to become more understanding with the syntax in java. The "Unchecked cast" warnings that I was concerned with are supposedly negligible in this program. This is my complete program at this time:

Asg.java
.......................................
package myUtil;

public interface MyList<T> {
	public int size();
	public T get(int i); 
	public T set(int i, T item);
	public int indexOf(T item);
	public void add(T item);
	public void add(int i, T item);
	public T remove(int i);
	public Object[] toArray();
	public <K> K[] toArray(K[] a);
	public String toString();	
}

Open in new window


MyArrayList.java
.......................................
package myUtil;

import java.util.Arrays;

public class MyArrayList<T> extends java.lang.Object implements MyList<T>{
	
	private int size;
	private T[] Data;
	
	public MyArrayList() {
		Data = (T[]) new Object[10];
		size = 0;
	}

	public MyArrayList(int capacity) {
		Data = (T[]) new Object[capacity];
		size = 0;
	}
	
	public MyArrayList(MyArrayList<T> ma) {
		Data = (T[]) new Object[ma.Data.length];
		size=ma.size;
		for (int i=0; i < size; i++) {
			Data[i] = ma.Data[i];
		}

	}

	public int size() {
		return this.size;
	}

	public T get(int i) {
		if (i < 0 || i >= size) 
			throw new ArrayIndexOutOfBoundsException(i);
		return Data[i];
	}

	public T set(int i, T item) {
		if (i < 0 || i >= size) 
			throw new ArrayIndexOutOfBoundsException(i);
		T old=Data[i];
		Data[i] = item;
		return old;
	}


	public int indexOf(T item) { // -1 if item can't be found
		for (int i=0; i<size; i++) 
			if (Data[i].equals(item)) return i;
		return -1;
	}

	public T remove(int i) {
		if (i < 0 || i >= size) 
			throw new ArrayIndexOutOfBoundsException(i);
		T item = Data[i];
		for (int k=i;k<size-1;k++)
			Data[k] = Data[k+1];
		size--;
		return item;
	}

private void doubleCapacity() {
	int c =  Data.length*2;
	T[] newList = (T[]) new Object[c];
	for (int i =0; i< Data.length; i++)
		newList[i] = Data[i];  // This is OK?
	Data = newList;
}

public void add(T item) { // add to the end
	add(size,item);
}


public void add(int i, T item) { // add(insert) item to posion i
	if (i < 0 || i > size) 
		throw new ArrayIndexOutOfBoundsException(i);

	if (size == Data.length) // the Data array is full
		doubleCapacity();

	for (int k=size;k>i;k--) {
		Data[k] = Data[k-1];
	}
	size++;
	Data[i] = item;
}

	
	public Object[] toArray() {
		return toArray(new Object[size]);
	}
	
	public <K> K[] toArray(K[] a) {
		if(a.length < size)
	return (K[]) Arrays.copyOf(Data, size, a.getClass());
		System.arraycopy(Data, 0, a, 0, size);
		if(a.length > size)
	    a[size] = null;
   		return a;
		}  
	
@Override
public String toString() {
	String s1 = "[";
	StringBuilder sb = new StringBuilder(s1);
		
		for(int i = 0; i < Data.length; i++){
			sb.append(Data[i]);
			if(i < (Data.length - 1)){
				sb.append(",");	
			}else{
				sb.append("]");	
			}
		}	
	return super.toString();
}
}

Open in new window


Asg.java
.......................................
package Asg3;
/* You should not modify this program  */
import myUtil.MyArrayList;

public class Asg3 {

	public static MyArrayList<Integer> addSome(int n) {
		MyArrayList<Integer> a =  new  MyArrayList<Integer>();
		// Add some Integers into to a
		for (int i=0; i<n; i++) {
			a.add(new Integer(i));
			a.add(a.size()-i, new Integer(i));
		}
		System.out.println(a.toString());
		// Try add's Exception
		try {
			a.add(a.size()+1, new Integer(999));
		} catch (ArrayIndexOutOfBoundsException e) {
			System.out.println("Can't add item at "+(a.size()+1));
		}
		return a;
	}
	
	public static void removeSome(MyArrayList<Integer> a) { 
		// Remove some Integers from a
		int n =a.size()/2;
		while (a.size()>n) {
			System.out.print("remove "+a.remove(a.size()-1)+", ");
		}
		System.out.println("\nAfter remove:"+a.toString());
		// Try remove's Exception
		try {
			System.out.print("remove "+a.remove(a.size()-1)+", "); 
			System.out.print("remove "+a.remove(a.size())+", "); ;
		} catch (ArrayIndexOutOfBoundsException e) {
			System.out.print("Can't remove item at "+a.size()+" from ");
			System.out.println(a.toString());
		}
	}
	
	
	
	public static void tryToArray(MyArrayList<Integer> a) {	
		Object[] o = a.toArray();
		System.out.print("Try toArray:");
		for (int i=0; i<o.length; i++)
			System.out.print(o[i].toString()+" ");
		System.out.print("\n");
	}
	
	
	
	public static void tryCopyConstructor(MyArrayList<Integer> a) {
		MyArrayList<Integer> b = new MyArrayList<Integer>(a);
		removeSome(b);
		System.out.println("Try copy constructor:"+b.toString());
	}
	
	public static void tryIndexOf(MyArrayList<Integer> a) {
		try {
			for (int i=0; i<=a.size(); i++) {
				Integer item= a.get(i);
				System.out.print("["+a.indexOf(item)+"]:"+item.toString()+" ");
			}
		}  catch (ArrayIndexOutOfBoundsException e) {
			System.out.print("["+a.indexOf(null)+"]:null ");
			System.out.println("["+a.indexOf(99)+"]:99");
		}
		
		for (int i=0; i<=a.size(); i++) {
			System.out.print("["+a.indexOf(i)+"]:"+i+" ");
		}
	}
	
	public static void main(String[] args) {
		if (args.length == 0)  {		
			System.out.println("Please provide an integer argument to the program.");
			System.exit(-1);
		}
		MyArrayList<Integer> a;
		a = addSome(Integer.parseInt(args[0]));
		removeSome(a);
		tryToArray(a);
		tryCopyConstructor(a);
		tryToArray(a);
		tryIndexOf(a);
	}
}

Open in new window



So, I fixed the code and now it runs; yet, this is my output when I compile it in Unix:

Exception in thread "main" java.lang.NoClassDefFoundError: myUtil/MyArrayList
      at java.lang.Class.getDeclaredMethods0(Native Method)
      at java.lang.Class.privateGetDeclaredMethods(Class.java:2442)
      at java.lang.Class.getMethod0(Class.java:2685)
      at java.lang.Class.getMethod(Class.java:1620)
      at sun.launcher.LauncherHelper.getMainMethod(LauncherHelper.java:484)
      at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:476)
Caused by: java.lang.ClassNotFoundException: myUtil.MyArrayList
      at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
      at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
      at java.security.AccessController.doPrivileged(Native Method)
      at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
      at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
      at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
      at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
      ... 6 more


I don't know what I am doing wrong at this point.
0
 
LVL 36

Expert Comment

by:mccarl
ID: 38439722
The "Unchecked cast" warnings that I was concerned with are supposedly negligible in this program.
That may be the case, but that still doesn't change the fact that they are so easily fixed. Also, warnings are there for a reason; your program may compile and run now, but things may break in the future. All you need do is change MyArrayList.java, lines 11, 16, 21 from this form...

Data = (T[]) new Object[capacity];

to this form...

Data = new T[capacity];

(There is no point creating an Object array and then casting it to type T. Just create arrays of type T in the first place)



As for why your program isn't running on Unix, it will be an issue with classpath settings or locations of the class files. Can you post the directory structure showing where all you relevant files are located and also the command line that you are using to execute your program?
0
 

Author Comment

by:JavaBeginner123
ID: 38439741
Will do, one moment plz
0
 

Author Comment

by:JavaBeginner123
ID: 38439792
I made the changes to the "Unchecked cast" and got this error
After correcting the "Unchecked cast"

My directory is this:   home/rechand/IT179/Asg3
The directory my program is in

This is what i typed in and the errors i got when I ran the javac:
My errors after running javac
0
 
LVL 36

Expert Comment

by:mccarl
ID: 38439812
Firstly, my apologies about the issue with the warnings, I learn something new everyday myself! I was just assuming the you could create a T[].


As for the errors, nothing in those screenshots shows me where your MyList.java and MyArrayList.java files are located, and if they are already compiled, etc.
0
 

Author Comment

by:JavaBeginner123
ID: 38439827
The MyList.java and MyArrayList.java files are located in the  directory home/rechand/IT179/Asg3/myUtil
My set upThe files
0
 
LVL 36

Expert Comment

by:mccarl
ID: 38439842
Ok try this then, all these commands are with the ~/IT179 directory as the Current directory...

javac -classpath . myUtil/MyList.java myUtil/MyArrayList.java Asg3/Asg3.java

and then...

java -classpath . Asg3.Asg3
0
 

Author Comment

by:JavaBeginner123
ID: 38439860
It gave me an exception in my main
Exceptionthrown
0
 
LVL 36

Expert Comment

by:mccarl
ID: 38439885
Have you edited Asg3.java since you last posted it above? I believe the error is telling me that you removed (or commented out) the 'package Asg3;' line from the top of Asg3.java. You need to keep that line in there!
0
 

Author Comment

by:JavaBeginner123
ID: 38439953
I believe I still have it as an import.

import
Package
I added that part & I get this...
still Error
0
 
LVL 36

Expert Comment

by:mccarl
ID: 38440669
I believe I still have it as an import.
I can't see where I said anything about 'imports'. However, it appears that you went back and actually read my comment about the 'package declaration' and edited your comment, to add this...
I added that part & I get this...[screenshot]
So WHY did you go back to using the (wrong) command that you were using earlier. It didn't cross your mind to put the 'package Asg3;' line back AND use the commands that I gave you?

To be absolutely CLEAR... make sure Asg3.java has

package Asg3;

Open in new window

as it's first line. And then from the ~/IT179 directory, do these two commands... (if 10 is the Integer that you want to pass to the program)

javac -classpath . myUtil/MyList.java myUtil/MyArrayList.java Asg3/Asg3.java

java -classpath . Asg3.Asg3 10

Open in new window

0
 

Author Comment

by:JavaBeginner123
ID: 38440712
My apologies. So, with the package declaration and while in the ***/IT179 directory, this is my output.

Output
0
 
LVL 36

Accepted Solution

by:
mccarl earned 2000 total points
ID: 38440771
So it runs!!

There appears to still be a problem in your MyArrayList.toString() method. It is 99% correct, the loop is doing all the right things, the only problem is with the 'return' statement. You have to change something in the line...
return super.toString();

Open in new window

As for the rest of the printout, you would have to go through what Asg3 is trying to do with your MyArrayList to determine if the output of the program is what is expected. Did your professor give you what the expected output should be?
0
 

Author Comment

by:JavaBeginner123
ID: 38440864
I thought that was the correct way to override a method. Would returning the result of the StringBuilder as a String representation be correct.

 As I was checking over the Asg3,java, it seemed as that in the output, I was being thrown a few ArrayOutOfBounds exceptions.
0
 

Author Comment

by:JavaBeginner123
ID: 38440997
I changed the following code:

return super.toString();

Open in new window

now it read the following:
return sb.toString();

Open in new window



Output
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses
Course of the Month19 days, 9 hours left to enroll

873 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question