简体   繁体   中英

Creating accessors in Java

I'm trying to write a class and create accessors in Eclipse that I will have to use later on, however I'm having trouble doing so. I have the directions listed below but keep getting stuck on the last two.

Directions:

Write a class StringSet . A StringSet object is given a series of String objects. It stores these Strings (or a reference to them, to be precise) and can perform limited calculations on the entire series. A StringSet class has the following specification:

  1. a single instance variable of type ArrayList<String>

  2. a single default constructor

  3. mutator that adds a String newStr to the StringSet object

    void add(String newStr)

  4. accessor that returns the number of String objects that have been added to this StringSet object

    int size()

  5. accessor that returns the total number of characters in all of the Strings that have been added to this StringSet object

    int numChars()

  6. accessor that returns the number of Strings in the StringSet object that have exactly len characters

    int countStrings(int len)

My code so far:

import java.util.ArrayList;

public class StringSet {

    ArrayList<String> StringSet = new ArrayList<String>();

    public StringSet() {
    }

    public void add(String newStr) {
        StringSet.add(newStr);
    }

    public int getsize() {
        return StringSet.size();
    }

    public int getnumChars() {
        return StringSet.length();
    }

    public int countStrings(int len) {
        if (StringSet.equals(len)) {
            return StringSet.size();
        }
    }
}

Your string set is an array of string objects. Think of it as though you've added each of the following to stringSet (indexes are to the left of the value).

0[Hello]
1[My]
2[name]
3[is]
5[Keith]

For simplicity I'm going to use a primitive String[] rather than an ArrayList

Question 5

Create a variable that will increment its value by the size of each String. Then use a for loop to evaluate each individual String 's length and add it to that variable:

int totalCharsInStringSet = 0;
for (int i = 0; i < stringSet.size(); i++) { // loop through each index
    // add the length of the string at this index to your running tally
    totalCharsInStringSet += stringSet[i].length; 
}
// you've read through all of the array; return the result
return totalCharsInStringSet;

Question 6

Calling stringSet.size() is just going to count how many elements are in the array; ie, 5. So you need to create a tally of how many individual strings match the target length. Craete a variable to keep that tally. And again, use a for loop to iterate through the array, and compare each string's length to the target value. If it matches, increment your tally:

int numberOfMatches = 0;
for (int i = 0; i < stringSet.size(); i++) {
   if (string[i].length == len) {  // "len" is your input target length
        numberOfMatches ++;
   }
}
return numMatches;

Number 5: You iterate through your ArrayList and add up the length of the strings stored there. Number 6: You iterate through you ArrayList and check if the length of the String matches the desired length, and keep track of the number of matching strings.

Well ArrayLists don't have a length() method, so what you need to do to count total number of chars in the string set is iterate through the StringSet list and find the length of each string and add it to a running total.

int sum = 0;
for(int i = 0; i < StringSet.size(); i++){
   sum = sum + StringSet.get(i).length();
}
return sum;

For the countStrings you need to place this if statement in a for loop and increment a sum each time the if statement is triggered and return the sum

int sum = 0;
for(int i = 0; i < StringSet.size(); i++){
    if(StringSet.get(i).length() == len){
        sum = sum + 1;
    }
}
return sum;

For Question 5:

public int getnumChars()
{
    int countChar = 0;
    for(String strSet: StringSet){
        countChar += strSet.length();
    }
    return countChar;
}

For Question 6:

public int countStrings(int len)
{
    int count = 0;
    for(String strSet: StringSet){
        if(strSet.length() == len){
            count++;
        };
    }

    return count;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM