8 Replies - 42409 Views - Last Post: 28 March 2009 - 01:10 PM Rate Topic: -----

#1 KrGmdeary   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 20-March 09

Returning elements of an ArrayList

Posted 26 March 2009 - 08:22 PM

So I am having trouble getting my code to return the elements and the book/internet isn't giving me any ideas on how to get me out of this hole...

The question:

Write a method that takes an ArrayList of Integer objects and returns an ArrayList of Character objects of the same size. The returned elements of the ArrayList are assigned a letter grade corresponding to the integer grade of the same index element of the ArrayList parameter( A if 90 or above,...,F if less than 60). Include code to test your method.


this is what I have so far and don't know what to do after this:

import java.util.ArrayList;
public class ReturningGrade 
{

	
	public static void main(String[] args) 
	{
		ArrayList grade = new ArrayList(); 
		System.out.println("Initial size of grade is: " +grade.size());
		grade.add("A");
		grade.add("B");
		grade.add("C");
		grade.add("D");
		grade.add("F");
		
		Object letterGrade[] = grade.toArray();
		int letgrade = 0;
		// sum the array
		if(letgrade => 90)
		System.out.println("Your letter grade is: "  +grade.toArray());
		}
		
}




Any help/comments will be greatly appreciated and will definitely help me better understand arraylists.

Is This A Good Question/Topic? 0
  • +

Replies To: Returning elements of an ArrayList

#2 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8381
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Returning elements of an ArrayList

Posted 26 March 2009 - 08:42 PM

No that is what your assignment says to do:
Return an ArrayList of characters that represents the marks of an ArrayList of Integer


ArrayList<Character> getEquivalen(ArrayList<Integer> mark) {
{
	  ArrayList<Character> cArray = new ArrayList<Character>();	  // create ArrayListr to return

	  // loop throught Int arrayList
	  for(int i = 0; i < mark.size(); i++) {
		 // extract value
		 int value = mark.get(i);
		 // convert value to char
		 if(ivalue> 90)
			   cArray.add(new Character('A'));
		 else if(ivalue> 80)
			   cArray.add(new Character('B'));
		 else if(value > 70)
			   cArray.add(new Character('C'));
		 else if(value > 60)
			   cArray.add(new Character('D'));
		 else
			   cArray.add(new Character('E'));
	  }
	  // return ArrayList of Char
	  return cArray;
}


Was This Post Helpful? 1
  • +
  • -

#3 KrGmdeary   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 20-March 09

Re: Returning elements of an ArrayList

Posted 27 March 2009 - 12:32 PM

Ok so that helped out but I'm reading errors:
import java.util.ArrayList;

public class ReturningGrade 
{



	public static ArrayList main(String[] args) 
	{
		ArrayList grade = new ArrayList(); 
		 // loop throught Int arrayList
		  for(int i = 0; i < grade.size(); i++) {
			
			 int value = grade.get(i);
			 
			 if(value>= 90)// convert value to char
				   grade.add(new Character('A'));
			 else if(value>= 80)
				   grade.add(new Character('B'));
			 else if(value >= 70)
				   grade.add(new Character('C'));
			 else 
				   grade.add(new Character('F'));
			
		  }
		  // return ArrayList of Char
		  return grade;
	}

		
}



I am reading an error under: int value = grade.get(i);



And I also having difficulties setting up a driver to test this...this is where I got till I got stuck:

public class ReturningGradeDriver 
{

	
	public static void main(String[] args) 
	{
		ReturningGrade grade = new ReturningGrade(85);
		
		System.out.println(value);
		
		
		
		

	}

}


Should I use a scanner to have the user input a number grade or use a default inside my driver?
Was This Post Helpful? 0
  • +
  • -

#4 KrGmdeary   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 20-March 09

Re: Returning elements of an ArrayList

Posted 28 March 2009 - 12:43 PM

I got it to read no errors, but I wasn't taught how to write a driver code to test my method, can anyone help me out with this?

This post has been edited by KrGmdeary: 28 March 2009 - 12:43 PM

Was This Post Helpful? 0
  • +
  • -

#5 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8381
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Returning elements of an ArrayList

Posted 28 March 2009 - 12:55 PM

View PostKrGmdeary, on 27 Mar, 2009 - 11:32 AM, said:

I am reading an error under: int value = grade.get(i);

grade is an ArrayList the get() method of an ArrayList returns an Object not an int
In your case you have an ArrayList that constains Integer objects and the other Character object
Was This Post Helpful? 0
  • +
  • -

#6 KrGmdeary   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 20-March 09

Re: Returning elements of an ArrayList

Posted 28 March 2009 - 12:57 PM

Yea I fixed that but I don't know how to test the class.
Was This Post Helpful? 0
  • +
  • -

#7 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8381
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Returning elements of an ArrayList

Posted 28 March 2009 - 12:58 PM

 ArrayList grade = new ArrayList(); 
		 // loop throught Int arrayList
		  for(int i = 0; i < grade.size(); i++) {



Kind of useless ... you just created grade : a new ArrayList()
for sure it is empty no need to try to loop through its elements
Was This Post Helpful? 0
  • +
  • -

#8 KrGmdeary   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 20-March 09

Re: Returning elements of an ArrayList

Posted 28 March 2009 - 01:04 PM

I'm not really understanding..
Was This Post Helpful? 0
  • +
  • -

#9 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8381
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Returning elements of an ArrayList

Posted 28 March 2009 - 01:10 PM

There are no requirement in your assignment to have all that in different classes

class TestArray {
	 
	TestArray() {

		ArrayList<Integer> mark = new ArrayList<Integer>();
		// fill it with 0, 3, 6, 9, 12, ...
		for(int i = 0; i < 100; i = i + 3) {
			mark.add(i);
		}

		ArrayList<Character> charac = getEquivalen(mark);

		// display both ArrayList side by side
		for(int i = 0; i < mark.size(); i++)
		   System.out.println(mark.get(i) + " = " + charac.get(i));
	}

	public static void main(String[] args) {
	   new TestArray();
	}
}


Was This Post Helpful? 1
  • +
  • -

Page 1 of 1