Question

Fix Java Code Question: Coding AList Get Position Write a method for the AList class that...

Fix Java Code
Question:
Coding AList Get Position

Write a method for the AList class that returns the first position of a given object in the list with the method header below.

public int getPosition(T anObject)

Answer:

public int getPosition(T anObject) {

int position = 0;

for (int i = 0; i < length; i++)
if (list[i].equals(anObject)) {
position = i + 1;
}
return position;
}
0 0
Add a comment Improve this question Transcribed image text
Answer #1

JAVA CODE

============================================================================================

package hello;

import java.util.*;
public class listClass1 {

   //take two arguments list and object to search
   public int getPosition(List ob,Object anObject) {

       int position;
       //return -1 if not found
       position=ob.indexOf(anObject);
      
       return position;
       }
  
   public static void main(String[] args) {
       // TODO Auto-generated method stub
       List ob = new ArrayList();
       ob.add("abc");
       ob.add(23);
       ob.add(2);
       ob.add(2.1);
       ob.add("sunil");
       ob.add('c');

System.out.println("List is "+ob);
listClass1 o1=new listClass1();

int pos;
pos=o1.getPosition(ob,"dd");
if(pos==-1)
System.out.println("dd is not found in a list");
else
   System.out.println("dd is found in a list at index "+pos);  


pos=o1.getPosition(ob,2.1);
if(pos==-1)
System.out.println("2.1 is not found in a list");
else
   System.out.println("2.1 is found in a list at index "+pos);

   }

}

============================================================================================

Output

Add a comment
Know the answer?
Add Answer to:
Fix Java Code Question: Coding AList Get Position Write a method for the AList class that...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Consider the Rectangle2 java class definition below. Write the definition of an equals) method that checks...

    Consider the Rectangle2 java class definition below. Write the definition of an equals) method that checks if two Rectangle objects have the same dimensions Write a Java program to test the equals method public class Rectangle2 K private int width, length; public Rectangle2(int w, int 1) { setWidth(w); setLength(1); System.out.println("Inside parameterized!!!"); } public void setWidth(int w) { width = w;} public void setLength(int i) { length = 1;} int getWidth() { return width; } int getLength() { return length; }...

  • In Java programming language Please write code for the 6 methods below: Assume that Student class...

    In Java programming language Please write code for the 6 methods below: Assume that Student class has name, age, gpa, and major, constructor to initialize all data, method toString() to return string reresentation of student objects, getter methods to get age, major, and gpa, setter method to set age to given input, and method isHonors that returns boolean value true for honors students and false otherwise. 1) Write a method that accepts an array of student objects, and n, the...

  • I have a question in how to create a method. For example there is a LinkedList...

    I have a question in how to create a method. For example there is a LinkedList of Objects, say and Object is public class Dog; private String name; private int age I want my method to take a dog age, and return the position (int) of the dog with that age has in the list example; //takes a dog age and returns position in the list, if no age in the list of dogs with age, method returns -1 public...

  • Question B1 You are given the following Java classes: public class Queue { private static class...

    Question B1 You are given the following Java classes: public class Queue { private static class Node { Object object; Node next; Node () { object = null; next = null; } Node (Object object, Node next) { this.object = object; this.next = next; private Node header; private int size = 0; // size shows the no of elements in queue public Object dequeue () { if (size == 0 ) { return null; else { Object remove_object = header.object;...

  • Assume that you have a String "name" attribute in a Java class definition.  Write a public method...

    Assume that you have a String "name" attribute in a Java class definition.  Write a public method to return this "name" attribute. public String getName() { return name;         } 1.) Write a statement that throws an IllegalArgumentException with the error message “Argument cannot be negative”.​ 2.) The method getValueFromFile is public and returns an int. It accepts no arguments. The method is capable of throwing an IOException and a FileNotFoundException. Write the header for this method.

  • 5. Given the following class definition, write Java code that implements the addFirst method public class...

    5. Given the following class definition, write Java code that implements the addFirst method public class MyLinkedList<E> { private Node<E> head, tail; private int size 0; // Number of elements in the list /** Add an element to the beginning of the list */ public void addFirst (E e) { private static class Node<E> { E element; Node<E> next; public Node (E element) { = element; this.element

  • I need java code for the following problem. Lab 7: Methods 1. Write a Java program called Numbers that calls the following methods and displays the returned value: Write a method called cubelt that a...

    I need java code for the following problem. Lab 7: Methods 1. Write a Java program called Numbers that calls the following methods and displays the returned value: Write a method called cubelt that accepts one integer parameter and returns the value raised to the third power as an integer. o Write a method called randominRange that accepts two integer parameters representing a range. The method returns a random integer in the specified range inclusive. 2. o Write a method...

  • 2. Consider an ADT list of integers. Write a method public int computeMin (IntegerListinterface alist) that...

    2. Consider an ADT list of integers. Write a method public int computeMin (IntegerListinterface alist) that computes and returns the minimum of the integers in the given list alist or returns Integer.MAX VALUE if the given list is empty For example, if aList- <70, 65, 98, 94>, your method should return 65. Note: 1) Your method should work for any list of integers. 2) The definition of your method should be independent of the list's implementation (ie. your method should...

  • Java: Create a main method for your class, and then add another method to your class...

    Java: Create a main method for your class, and then add another method to your class (above the main method) named fallingDistance. This method accepts an integer into its parameter t, which is the amount of time, in seconds, that an object has been falling. This method returns the distance, in meters, that the object has fallen during the time interval. When an object s falling because of gravity, we use the following formula to determine the distance the object...

  • Given the Interface Code write a java class that implements this interface and show the working...

    Given the Interface Code write a java class that implements this interface and show the working functionality in the main method: public interface CustomList<T> { /** * This method should add a new item into the <code>CustomList</code> and should * return <code>true</code> if it was successfully able to insert an item. * @param item the item to be added to the <code>CustomList</code> * @return <code>true</code> if item was successfully added, <code>false</code> if the item was not successfully added (note: it...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT