KeyLimePi
KeyLimePi

Reputation: 3

Changing things from an Array to an ArrayList

Have this thing for Homework and the code was given to us in an array and we have to convert it to an arraylist.

here is the HW question: modify the Programmer class so that the technologies array data member is an ArrayList of String. Also, make addTechnology just a bit more sophisticated: if we try to add Technology ‘tech’ to a Programmer that already has ‘tech’ in its array list, then no changes should be made to the array list. (I.e., the addTechnology operation does nothing in that case.) Furthermore, modify the getSalary method so that each Programmer who knows Java gets an extra $3000 bonus. Thus, for example, knowing C++ earns a $5000 bonus, but knowing Java earns $8000

here is the original code:

public class Programmer extends Employee
{
    private String[] technologies;

    public Programmer(String name, String ssn)
    {
        super(name, ssn, 65000.00);
        technologies = new String[0];
    }

    public void addTechnology(String tech)
    {
        String[] newArray = new String[technologies.length + 1];
        for (int i = 0; i < technologies.length; i++)
            newArray[i] = technologies[i];
        newArray[technologies.length] = tech;
        technologies = newArray;
    }

    public double getSalary()
    {
        return super.getSalary() + technologies.length * 5000.00;
    }

    /*public String toString()
    {
        String returnVal = "Programmer " + super.toString() + " and knows";
        for (String tech : technologies)
        {
            returnVal += " " + tech;  // Note: Inefficient due to String concatenation.
                                      // Also lacks punctuation.
        }
        return returnVal;
    }*/

    public String toString()
    // This version inserts commas between the technologies
    // It also generates the string efficiently, using a StringBuilder object.
    {
        StringBuilder returnVal = new StringBuilder("Programmer ");
        returnVal.append(super.toString());
        if (technologies.length > 0)
        {
            returnVal.append(" and knows ");
            if (technologies.length == 1)
            {
                returnVal.append(technologies[0]);
            }
            else if (technologies.length == 2)
            {
                returnVal.append(technologies[0]);
                returnVal.append(" and ");
                returnVal.append(technologies[1]);
            }
            else
            {
                for (int i = 0; i < technologies.length - 1; i++)
                    returnVal.append(technologies[i] + ", ");
                returnVal.append("and ");
                returnVal.append(technologies[technologies.length - 1]); 
            }
        }
        return returnVal.toString();
    }
}

and here is what i currently have:

import java.util.*;

public class Programmer extends Employee
{
    ArrayList<String> technologies = new ArrayList<>();

    public Programmer(String name, String ssn)
    {
        super(name, ssn, 65000.00);

    }

    public void addTechnology(String tech)
    {
        if (technologies.contains(tech))
            System.out.println("The technology is already contained in the array");
        else
            technologies.add(tech);
        //
    }

    public double getSalary()
    {
        double salary = super.getSalary() + technologies.size() * 5000.00;
        if(technologies.contains("Java")){
            salary = salary + 3000;
            return salary;
        }
        else
            return salary;

    }

    /*public String toString()
    {
        String returnVal = "Programmer " + super.toString() + " and knows";
        for (String tech : technologies)
        {
            returnVal += " " + tech;  // Note: Inefficient due to String concatenation.
                                      // Also lacks punctuation.
        }
        return returnVal;
    }*/

    public String toString()
    // This version inserts commas between the technologies
    // It also generates the string efficiently, using a StringBuilder object.
    {
        StringBuilder returnVal = new StringBuilder("Programmer ");
        returnVal.append(super.toString());
        if (technologies.size() > 0)
        {
            returnVal.append(" and knows ");
            if (technologies.size() == 1)
            {
                returnVal.append(technologies.indexOf());
            }
            else if (technologies.size() == 2)
            {
                returnVal.append(technologies[0]);
                returnVal.append(" and ");
                returnVal.append(technologies[1]);
            }
            else
            {
                for (int i = 0; i < technologies.size()- 1; i++)
                    returnVal.append(technologies[i] + ", ");
                returnVal.append("and ");
                returnVal.append(technologies[technologies.length - 1]);
            }
        }
        return returnVal.toString();
    }
}

my issue is at the bottom of the code, in the toString method, how do i change those for the arraylist?

Upvotes: 0

Views: 162

Answers (3)

clcto
clcto

Reputation: 9648

To retrieve a value from an ArrayList, use the get(int) method.

When using an array, the notation array[i] is used both to get and set the value:

int[] array = new int[10];
array[0] = 1;
int i;

i = array[0];
array[0] = i;

When using an array list, each of these has its own method:

ArrayList<int> array_list = new ArrayList<int>();
array_list.add( 1 );
int i;

i = array_list.get( 0 );
array_list.set( 0, i );

Upvotes: 1

Amit Bhati
Amit Bhati

Reputation: 5659

My issue is at the bottom of the code, in the toString method, how do i change those for the arraylist?

You can write your toString() method like below:-

 public String toString()
    // This version inserts commas between the technologies
    // It also generates the string efficiently, using a StringBuilder object.
    {
        StringBuilder returnVal = new StringBuilder("Programmer ");
        returnVal.append(super.toString());
        if (technologies.size() > 0)
        {
            returnVal.append(" and knows ");
          for(String technology:technologies){
              returnVal.append(technology).append(",");
          }
        }
        return returnVal.substring(0, returnVal.lastIndexOf(",")).toString();
    }

Upvotes: 0

eric.m
eric.m

Reputation: 1612

Just use the ArrayList.get(int index) method to get the object at that index. Is the array[i] equivalent.

technologies.get(i);

Upvotes: 0

Related Questions