Classes and Objects: Adding Letter Counts

  • Thread starter yitriana
  • Start date
  • Tags
    Classes
In summary, the conversation discusses creating a class that adds counts of letters in two Strings and creating an object representing the sum of the counts. The conversation also addresses confusion about how to construct objects in a class and the concept of objects being instances of a class. The main concern is how to create an object inside the class. The instructions for the class involve constructing and returning a new LetterInventory object that represents the sum of two given LetterInventory objects without changing the original objects.
  • #1
yitriana
36
0

Homework Statement



Create a class that adds counts of letters or two Strings, and create an object which represents sum of the counts.

Homework Equations



n/a

The Attempt at a Solution



i am confused about how to construct objects in a class itself, and not in client code.
as in this site,

http://math.hws.edu/javanotes/c5/s2.html

adding the roll produced by dice is done by client, but not by the class.

further, if objects are instances of the class, then the client must call the class and add whatever it returns or whatever from the methods.

but how can a class do this?

i am instructed to "Constructs and returns a new LetterInventory object that represents the sum of this letter inventory and the other given LetterInventory. The counts for each letter should be added together. The two LetterInventory objects being added together (this and other) should not be changed by this method"
 
Physics news on Phys.org
  • #2
so how can an object be created inside the class? any help will be much appreciated. public class LetterInventory { private int[] counts; private String alphabet; private int size; public LetterInventory(String data) { alphabet = "abcdefghijklmnopqrstuvwxyz"; counts = new int[alphabet.length()]; size = 0; data = data.toLowerCase(); for (int i=0; i<data.length(); i++) { int index = alphabet.indexOf(data.charAt(i)); if (index != -1) { counts[index]++; size++; } } } public LetterInventory add(LetterInventory other) { // constructs and returns a new LetterInventory object that represents the sum of this letter inventory and the other given LetterInventory. // The counts for each letter should be added together. The two LetterInventory objects being added together (this and other) should not be changed by this method }}
 
  • #3


In order to construct objects in a class, you can create a constructor method within the class. This method will be responsible for creating new instances of the class and initializing their properties. In this case, the constructor method would take in two String parameters, representing the two strings whose letter counts will be added together.

As for the class itself, it can have methods that perform the actual addition of the letter counts. For example, you could have a method called "addLetterCounts" which takes in two LetterInventory objects as parameters and returns a new LetterInventory object representing the sum of the two. This method would use the properties and methods of the LetterInventory class to add the letter counts together.

In terms of the client code, it would call the constructor method to create the LetterInventory objects and then call the "addLetterCounts" method to add their letter counts together. The client code would then have access to the new LetterInventory object representing the sum of the counts.

It's important to remember that the class itself is not responsible for calling its own methods or creating its own objects. This is the job of the client code. The class simply provides the structure and functionality for creating and manipulating objects of that type.
 

What is a class?

A class is a blueprint or template for creating objects in object-oriented programming. It defines the properties and behaviors that an object of that class will have.

What is an object?

An object is an instance of a class. It is a specific, tangible representation of the properties and behaviors defined in its class.

How do you add letter counts using classes and objects?

To add letter counts using classes and objects, you will first need to define a class with a method that takes a string as an argument. Within this method, you can use a loop to iterate through the characters in the string and increment a count for each letter. Then, you can create an object of this class and call the method on the desired string to get the total letter count.

What are the benefits of using classes and objects in this task?

Using classes and objects allows for a more organized and structured approach to adding letter counts. It also allows for reusability and scalability, as the same class and method can be used for any string input.

Are there any potential drawbacks to using classes and objects for this task?

One potential drawback is that it may be more complex and require a deeper understanding of object-oriented programming for someone who is not familiar with it. It may also be more time-consuming to set up compared to a simpler solution using basic functions.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
14
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
6K
  • Engineering and Comp Sci Homework Help
Replies
15
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
6
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
23
Views
5K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
Back
Top