Question

In: Computer Science

Class Design – IntList (A.k.a. ArrayList (v0.0)) Let’s build a useful class that wraps up an...

Class Design – IntList (A.k.a. ArrayList (v0.0))

Let’s build a useful class that wraps up an array of integers. This class will store two data items; one an array of integers, and the other a counter to track the number of actual elements in the list. Start by defining a new class called IntList and copying the main driver code included below or in IntList.java. Then, add each of the following functions and data items indicated in the member sections. The IntList.java file shows an example of a char list, be sure to change it to a list of integers in order to complete the assignment.

Data Members

  • Declare an integer array called “data”
    • Make this private and able to store up to 100 integers
  • Declare an integer called numElements
    • Make this private, and set it to zero.

Method Members

  • public void add(int n) {
    • this should add the value n to our array and add one to num_elements
  • public String toString() {
    • String retVal=””;
      • In a loop, append all the elements in our data array to this string, separated by a comma
    • return retVal;
  • Copy the main driver below and run it to test your IntList so far
    • Do you see output for each integer added to your list?
    • “95 100 58 ” //for example
  • Next, uncomment each of the commented lines in main and build the corresponding function
    • Build a function to sum the integer elements stored in your IntList.
    • public int sum() {
      • This function takes no parameters; uses the class-level data[] array instead
  • Since the next function is similar to sum(), copy and paste sum() and rename it to “indexOf”
    • Also, add an input parameter to the method signature called target, which is an int that we’re looking for in our list of integers.
    • public int indexOf(int target) {
      • takes a target parameter that we’re looking for
      • This returns -1 if not found
public static void main(String[] args) { 
    IntList a = new IntList();
    a.add(95); a.add(100); a.add(58);
    System.out.println(a.toString() );
    //System.out.println(a.sum() );
    //System.out.println(a.indexOf(100)); //uncomment these to work on next
    //System.out.println(a.indexOf(20));
    //System.out.println(a.save() );
}

Solutions

Expert Solution

 class IntList 
 {
    //Declare a private integer array called “data ” with size 100
     private int[] data=new int[100];
     //Declare an integer called numElements
     private int numElements=0;
     //add() method add the value n to array data and add one to num_elements
     public void add(int n)
     {
         //add the value n to array data
        data[numElements]=n;
        //add one to num_elements
        numElements++;

     }
     //In a loop, append all the elements in our data array to this string, separated by a comma
     public String toString() {
         //Declaring loop variable i
        int i;
        //Declaring a string retVal to store the output string
        String retVal="";
        //Loop to access elements in array data
        for(i=0;i<numElements;i++)
        //Concatinating array elements to string
        retVal+=" "+data[i];
        //Return resulting string
        return retVal;
     }
     //function to sum the integer elements stored in IntList
     public int sum() 
     {
         //Declaring integer variable sumElements to store sum of array elements
         int sumElements=0;
         //Declaring loop variable i
         int i=0;
         //Loop to access elements in array data
         while(i<numElements)
         {
             //Add each element to sumElements
             sumElements+=data[i];
             //Incrementing loop variable
             i++;
         }
         //Returning sum of array elements
         return sumElements;
     }
     public int indexOf(int target) 
     {
         //Declaring integer variable to store index of target variable
        int index=-1;
        //Declaring loop variable i
        int i=0;
        //Loop to access elements in array data
        while(i<=numElements)
        {
            //Checking if array element is equal to value of target if yes set index=i and break from loop 
            if(data[i]==target)
            {
               index=i;
                break;
            }
             //Incrementing loop variable 
            i++;
        }
        //Return index value
        return index;
     }
public static void main(String[] args)
 { 
    IntList a = new IntList();
    a.add(95); a.add(100); a.add(58);
    System.out.println(a.toString() );
    System.out.println(a.sum() );
    System.out.println(a.indexOf(100)); //uncomment these to work on next
    System.out.println(a.indexOf(20));
    //System.out.println(a.save() );
 }
}

Sample Output

Since there were no instructions for save() method it is not implemented


Related Solutions

Develop the getSuggestions(ArrayList wordCountList) method as the base method of the class. Develop the countWords(ArrayList wordList)...
Develop the getSuggestions(ArrayList wordCountList) method as the base method of the class. Develop the countWords(ArrayList wordList) method to find the frequencies of all words in the text file. Develop the getWordList(File[] fileArray) method to get all words in the text file. Ignore the words that have 3 or less characters. Your customer wants you to develop a method that will find the sentences that contain a specific word. This is basically a word search, but your customer needs the list...
class Company uses an ArrayList of class Employee to manage its employees. Your job is to...
class Company uses an ArrayList of class Employee to manage its employees. Your job is to create two classes , Company and Employee, with appropriate instance fields, constructors, and methods as been described in the set of questions that follows . A sample use case of the classes is shown below: public class CompanyTester{ public static void main(String[] args){ Company myCompany = new Company(); myCompany.add( new Employee("james","gasling")); myCompany.add( new Employee("bill","gate")); myCompany.add( new Employee("dennis","ritchie")); System.out.println(myCompany); } } The sample output of...
Explain Design Build method
Explain Design Build method
A Design-Bid-Build project delivery approach overlaps the design and construction phases, whereas the Design-Build approach requires...
A Design-Bid-Build project delivery approach overlaps the design and construction phases, whereas the Design-Build approach requires each phase to be completed before moving on to the next one. True False Identify ALL that apply: Under the Design-Bid-Build delivery method, there are legal contacts between which of the following. Owner-Engineer Owner-Contractor Engineer-Contractor Identify ALL that apply: The CM at Risk is responsible for: Means and Methods Quality Assurance Payment to Subcontractors Safety A General Contractor must be the employer of the...
Step 4: Create a class called BabyNamesDatabase This class maintains an ArrayList of BabyName objects. Instance...
Step 4: Create a class called BabyNamesDatabase This class maintains an ArrayList of BabyName objects. Instance Variables Declare an ArrayList of BabyName objects. Constructor public BabyNamesDatabase () - instantiate the ArrayList of BabyName objects. You will not insert the items yet. This method will be one line of code. Mutator Methods public void readBabyNameData(String filename) - open the provided filename given as input parameter, use a loop to read all the data in the file, create a BabyName object for...
Implement a class named Parade using an ArrayList, which will manage instances of class Clown. Each...
Implement a class named Parade using an ArrayList, which will manage instances of class Clown. Each Clown only needs to be identified by a String for her/his name. Always join a new Clown to the end of the Parade. Only the Clown at the head of the Parade (the first one) can leave the Parade. Create a test application to demonstrate building a parade of 3 or 4 clowns (include your own name), then removing 1 or 2, then adding...
Advanced Digital System Design Build a 1-bit subtractor and scale it up to become an 8-bit...
Advanced Digital System Design Build a 1-bit subtractor and scale it up to become an 8-bit subrtactor. Include truth tables and gate level diagrams for the 1-bit version.
import java.util.Stack; import java.util.ArrayList; import java.util.Scanner; class TreeNode{ int data; ArrayList<TreeNode> children = new ArrayList<>(); TreeNode...
import java.util.Stack; import java.util.ArrayList; import java.util.Scanner; class TreeNode{ int data; ArrayList<TreeNode> children = new ArrayList<>(); TreeNode parent = null;    public TreeNode(int d){ data = d; }    public TreeNode addChild(int d){ TreeNode n = new TreeNode(d); n.setParent(this); children.add(n); return n; }    public ArrayList<TreeNode> getChildren(){ return children; }    public void setParent(TreeNode p){ parent = p; }    public TreeNode getParent(){ return parent; } } class Main { public static void main(String[] args)    {        Scanner scan...
import java.util.Stack; import java.util.ArrayList; import java.util.Scanner; class TreeNode{ int data; ArrayList<TreeNode> children = new ArrayList<>(); TreeNode...
import java.util.Stack; import java.util.ArrayList; import java.util.Scanner; class TreeNode{ int data; ArrayList<TreeNode> children = new ArrayList<>(); TreeNode parent = null;    public TreeNode(int d){ data = d; }    public TreeNode addChild(int d){ TreeNode n = new TreeNode(d); n.setParent(this); children.add(n); return n; }    public ArrayList<TreeNode> getChildren(){ return children; }    public void setParent(TreeNode p){ parent = p; }    public TreeNode getParent(){ return parent; } } class Main { public static void main(String[] args)    {        Scanner scan...
import java.util.Stack; import java.util.ArrayList; import java.util.Scanner; class TreeNode{ int data; ArrayList<TreeNode> children = new ArrayList<>(); TreeNode...
import java.util.Stack; import java.util.ArrayList; import java.util.Scanner; class TreeNode{ int data; ArrayList<TreeNode> children = new ArrayList<>(); TreeNode parent = null;    public TreeNode(int d){ data = d; }    public TreeNode addChild(int d){ TreeNode n = new TreeNode(d); n.setParent(this); children.add(n); return n; }    public ArrayList<TreeNode> getChildren(){ return children; }    public void setParent(TreeNode p){ parent = p; }    public TreeNode getParent(){ return parent; } } class Main { public static void main(String[] args)    {        Scanner scan...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT