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
The following is for a Java Program Create UML Class Diagram for these 4 java classes....
The following is for a Java Program Create UML Class Diagram for these 4 java classes. The diagram should include: 1) All instance variables, including type and access specifier (+, -); 2) All methods, including parameter list, return type and access specifier (+, -); 3) Include Generalization and Aggregation where appropriate. Java Classes description: 1. User Class 1.1 Subclass of Account class. 1.2 Instance variables __ 1.2.1 username – String __ 1.2.2 fullName – String __ 1.2.3 deptCode – int...
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 Java program that prompts the user to input a word (String). The program must...
Write a Java program that prompts the user to input a word (String). The program must print the reversed word with all consecutive duplicate characters removed. The program must contain the following classes: - The StackX class (you can use the Java Stack class). - The Reverse class which must contain a private data field called “word” of type string, a constructor, and a void method called revNoDup(). The revNoDup() method must reverse the word and remove the consecutive duplicate...
Below is the assignment, I have completed and tested every step except for the last one....
Below is the assignment, I have completed and tested every step except for the last one. "Print aLine with the first 'c' 'C' 'd' or 'D' removed." any help would be appreciated ASSIGNMENT Write a program using Scanner and its nextLine method. The following is an example of how to use nextLine Scanner kybd = new Scanner(System.in); System.out.println("Enter a line of text"); String aLine = kybd.nextLine(); If the user's input is shorter than 7 characters, print the message "The input...
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....
Write a Java class called Rectangle that represents a rectangular two-dimensional region. It should have following...
Write a Java class called Rectangle that represents a rectangular two-dimensional region. It should have following 4 fields: int x (x-coordinate for its top left corner) int y (y coordinate for its top left corner) double height (height of the rectangle) double width (width of the rectangle) Your rectangle objects should have the following methods: public Rectangle(int newx, int newy, int newwidth, int newheight) Constructor that initializes the x-coordinate, y-coordinate for the top left corner and its width and height....
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...
Java Programming COMP-228 LAB ASSIGNMENT #1 >> Apply the naming conventions for variables, methods, classes, and...
Java Programming COMP-228 LAB ASSIGNMENT #1 >> Apply the naming conventions for variables, methods, classes, and packages: - variable names start with a lowercase character for the first word and uppercase for every other word - classes start with an uppercase character of every word - packages use only lowercase characters - methods start with a lowercase character for the first word and uppercase for every other word Java Programming COMP-228 Exercise #1: [5 marks] Write a Java application using...
IN JAVA Inheritance Using super in the constructor Using super in the method Method overriding Write...
IN JAVA Inheritance Using super in the constructor Using super in the method Method overriding Write an application with five classes Vehicle, Car, Bus, Truck, and Tester class with main method in it. The following characteristics should be used: make, weight, height, length, maxSpeed, numberDoors, maxPassenges, isConvertable, numberSeats, maxWeightLoad, and numberAxels. Characteristics that are applicable to all vehicles should be instance variables in the Vehicle class. The others should be in the class(s) where they belong. Class vehicle should have...
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....
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT