Java Programming
In this lab students continue to write programs using multiple classes. By the end of this lab, students should be able to
Write classes that use arrays and ArrayLists of objects as instance variables
Write methods that process arrays and ArrayLists of objects
Write getter and setter methods for instance variables
Write methods using object parameters and primitive types
Question-
class StringSet. A StringSet object is given a series of up to 10 String objects. It stores these Strings (or a reference to them, to be precise) and can perform limited calculations on the entire series.
The StringSet class has the following specification:
// an instance variable of type String[] // an int instance variable that indicates the number of String objects that the StringSet currently contains // a single no-argument constructor // a mutator that adds a String newStr to the StringSet object void add(String newStr)
// an accessor that returns the number of String objects that have been added to this StringSet object int size() // an accessor that returns the total number of characters in all of the Strings that have been added to this StringSet object int numChars() // an accessor that returns the number of Strings in the StringSet object that have exactly len characters int countStrings(int len) Write a class StringSetTester that has a main method. It should ask the user for the number of Strings to add to a StringSet object. Afterward, use StringSet's size and numChars methods to print information about the collection of Strings entered. Also print the number of Strings that are exactly 5 and 7 characters long. Hint: because Scanner's nextInt and nextLine process whitespace differently, you may want to use code similar to the following Scanner kybd = new Scanner(System.in); System.out.print("How many strings will you enter? "); int numStr = kybd.nextInt(); // stops after the number, leaves end of line or other whitespace kybd.nextLine(); // "eats" everything up to and including the next newline // the next kybd.nextLine() will read user input
import java.io.*;
import java.util.Scanner;
class StringSet
{
private String []strList;
private int strCount;
public StringSet()
{
strList = new String[10];
strCount = 0;
}
public void add(String newStr)
{
if(strCount == 10){
System.out.println("List full. Cannot insert more string");
return;
}
strList[strCount] = newStr;
strCount++;
}
public int size()
{
return strCount;
}
public int numChar()
{
int i=0;
int length = 0;
for(;i<strCount;i++)
{
length +=
strList[i].length();
}
return length;
}
public int countStrings(int len)
{
int i=0;
int count = 0;
for(;i< strCount;i++)
{
if(strList[i].length() == len)
{
count++;
}
}
return count;
}
}
public class StringSetTester
{
public static void main(String []args)
{
int numStr = 0, i=0;
Scanner in = new
Scanner(System.in);
StringSet list = new StringSet();
System.out.print("How many strings
will you enter? ");
numStr = in.nextInt();
in.nextLine();
for(;i < numStr ;i++)
{
System.out.println("Enter String " + (i+1));
list.add(in.nextLine());
}
System.out.println("Number of
string in the list : " + list.size());
System.out.println("Number of
characters in the list : " + list.numChar());
System.out.println("Number of strings having 5 characters : " +
list.countStrings(5));
System.out.println("Number of
strings having 7 characters : " + list.countStrings(7));
}
}
Get Answers For Free
Most questions answered within 1 hours.