Question

Java Programming In this lab students continue to write programs using multiple classes. By the end...

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

Homework Answers

Answer #1

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));
      
   }
}

Know the answer?
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
Similar Questions
Exercise 2 Write a class named Vehicle Holds make and model (strings) and year (int) Contains...
Exercise 2 Write a class named Vehicle Holds make and model (strings) and year (int) Contains this method: Vehicle(String make, String model, int year) { this.make = make; this.model = model; this.year = year; } Add getters and setters for each variable Write a class called FordMustang that extends the Vehicle class               Create instance of object               Pass variables               Print
THIS IS JAVA PROGRAMMING Write a method that finds the shortest word in an array of...
THIS IS JAVA PROGRAMMING Write a method that finds the shortest word in an array of String values and returns the length, in characters, of that word. The method should use the following header. public static int minLength(String[] array) Write a test program that prompts the user to enter ten strings, store them in an array and invokes this method to return the shortest length, and displays that value. (A near complete program can be found attached to the dropbox)
write a c++ program A class called car (as shown in the class diagram) contains: o...
write a c++ program A class called car (as shown in the class diagram) contains: o Four private variables: carId (int), carType (String), carSpeed (int) and numOfCars (int). numOfCars is a static counter that should be  Incremented whenever a new Car object is created.  Decremented whenever a Car object is destructed. o Two constructors (parametrized and copy constructor) and one destructor. o Getters and setters for the Car type, ID, and speed. And static getter function for numOfCars....
1- Use inheritance to implement the following classes: A: A Car that is a Vehicle and...
1- Use inheritance to implement the following classes: A: A Car that is a Vehicle and has a name, a max_speed value and an instance variable called the number_of_cylinders in its engine. Add public methods to set and get the values of these variables. When a car is printed (using the toString method), its name, max_speed and number_of_cylinders are shown. B: An Airplane that is also a vehicle and has a name, a max_speed value and an instance variable called...
4. Programming Problem: In this problem we will keep the Mechanism interface and the two classes...
4. Programming Problem: In this problem we will keep the Mechanism interface and the two classes Car and Computer from the previous problem. In addition, we will add a new class called Mechanic. We will also create a new TestMechanic class that will contain the main method. Create a class called Mechanic with following instance variable and constants: private int cost; public static int TEST_PRICE = 10; public static int REPAIR_PRICE = 50; Have a default constructor that will set...
Write the Game class, Java lanuage. A Game instance is described by three instance variables: gameName...
Write the Game class, Java lanuage. A Game instance is described by three instance variables: gameName (a String), numSold (an integer that represents the number of that type of game sold), and priceEach (a double that is the price of each of that type of Game). I only want three instance variables!! The class should have the following methods: A constructor that has two parameter – a String containing the name of the Game and a double containing its price....
1) Write a java programming using a while loop where you will add numbers and when...
1) Write a java programming using a while loop where you will add numbers and when you press number 0 it will add all your numbers and display the results. Use Scanner object. Steps: 1) Declare variables integer called sum which is equal to zero   2) Declare Scanner object   3) Declare while condition where is true   4) Inside of that condition prompt the user to enter the numbers   5) Declare variable integer called number and input the number statement   6)...
This is in java and you are not allowed to use Java API classes for queues,...
This is in java and you are not allowed to use Java API classes for queues, stacks, arrays, arraylists and linkedlists. You have to write your own implementations for them. You should construct a BST by inserting node values starting with a null tree. You can re-use the code for the insert method given in the sample code from the textbook. -insert method is provided below Your code should have a menu driven user interface at the command line with...
is there anything wrong with the solution. the question are from java course Write a main...
is there anything wrong with the solution. the question are from java course Write a main method that will request the user to enter Strings using a JOptionPane input dialog. The method should continue accepting strings until the user types “STOP”.       Then, using a JOptionPane message dialog, tell the user how many of the strings begin and end with a digit. Answer: import javax.swing.*; public class IsAllLetters {     public static void main(String[] args) {         String input;         int count =...
Practice manipulating an object by calling its methods. Complete the class Homework1 class using String methods....
Practice manipulating an object by calling its methods. Complete the class Homework1 class using String methods. Remember that all the String methods are accessors. They do not change the original String. If you want to apply multiple methods to a String, you will need to save the return value in a variable. Complete the class by doing the following: + Print the word in lowercase + Replace "e" with "3" (Use the unmodified variable word) + Print the changed word...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT