Please help me with this C code I need the answer by Tuesday

Please help me with this C++ code!! I need the answer by Tuesday 2/16

The assignment is to first create a class called TripleString. TripleString will consist of three private member strings as its basic data. There will be few instance methods to support that data.  

Once defined, we will use it to instantiate TripleString objects that can be used in our main()method. In a later assignment, the TripleString class will help us create a more involved application.

TripleString will contain three private member strings as its main data: string1, string2, and string3. We will also add a few public static member such as const int MAX_LEN and MIN_LEN. These represents the maximum and minimum length that our class will allow any of its strings to be set to. We can use these static members in the TripleString method whose job it is to test for valid strings (see below).

The Program Spec

Class TripleString Spec

Private Class Instance Members:

string string1

string string2

string string3

All legal strings should be between 1 and 50 characters.

As stated in the modules, we never want to see a literal in our methods. So the class should have static members to hold values for the limits described above, as well as default values for any field that isconstruct-ed using illegal arguments from the client. These are put in the public static section, but their initialization belongs in a different location. See modules for details.

Public Class Static Constants (declare to be const):

MIN_LEN = 1

MAX_LEN = 50

DEFAULT_STRING = \" (undefined) \"

Public Instance Methods

Default Constructor

TripleString() -- a default constructor that initializes all members to DEFAULT_STRING.

Paramteter-Taking Constructor

TripleString(string str1, string str2, string str3) -- a constructor that initializes all members according to the passed parameters. It has to to be sure each string satisfies the class requirement for a member string. It does this, as in the modules, by calling the mutators and taking possible action if a return value is false. If any passed parameter does not pass the test, a default string should be stored in that member.

Mutators/Accessor

set()s and get()s for these members. Mutators in our course are named using the convention as follows: setString1( ... ),  setString3( ... ), etc. Likewise with accessors:  getString2(). We need an accessor and mutator for each individual string member, so three pairs of methods in this category. Mutators make use of the private helper method validString().  When a mutator detects an invalid string, no action should be taken. The mutator returns false and the existing String stored in that member prior to the call remains in that member, not a new default string.

string toString() - a method that returns a string which contains all the information (three strings) of the TripleString object. This string can be in any format as long as it is understandable and clearly formatted.

Private Static Helper Methods

bool validString( string str ) -- a helper function that the mutators can use to determine whether a string is legal. This method returns true if the string\'s length is between MIN_LEN and MAX_LEN (inclusive). It returns false, otherwise.

Where it All Goes

There are now a variety of program elements, so let\'s review the order in which things appear in your .cpp file:

includes and namespace

class prototype(s)

static member initialization

global-scope method prototype(s) [You may not need them for this assignment.]

main() definition

global-scope method definition( ) [You may not need them for this assignment.]

class method definition(s)

In other words, Foothill.cpp will look like this:

As you see, TripleString class methods are defined after, not within, the TripleString class prototype.   Those methods are also defined after the main() method.

The Foothill main()

Instantiate four or more TripleString objects, some of them using the default constructor, some using the constructor that takes parameters.

Immediately display all objects.

Mutate one or more members of every object.

Display all objects a second time.

Do two explicit mutator tests. For each, call a mutator in an if/else statement which prints one message if the call is successful and a different message if the call fails.

Make two accessor calls to demonstrate that they work.

More:

Be sure that all output is descriptive. Use labels to differentiate your output sections and full sentences in your mutator/accessor tests.

No user input should be done in this program.

I am not supplying a sample output this week -- the above description is adequate.

Solution

import java.util.*;
import java.lang.*;
import java.io.*;

class TripleString
{
    public static int MIN_LEN = 1;
    public static int MAX_LEN = 50;
    public String DEFAULT_STRING = \" (undefined) \";
  
    private String string1;
    private String string2;
    private String string3;
  
  
    //Default constructor instatntiating all the string values to DEFAULT_STRING
    public TripleString(){
      
        this.string1 = this.DEFAULT_STRING;
        this.string2 = this.DEFAULT_STRING;
        this.string3 = this.DEFAULT_STRING;
      
    }
  
    //Constructor passing three strings and it will instantiate the string1, string2, string3 values
    public TripleString(String string1, String string2, String string3){
      
        if(validString(string1)){
            this.string1 = string1;
        }else{
            this.string1 = this.DEFAULT_STRING;
        }
      
        if(validString(string2)){
            this.string2 = string2;
        }else{
            this.string2 = this.DEFAULT_STRING;
        }
      
        if(validString(string3)){
             this.string3 = string3;
        }else{
             this.string3 = this.DEFAULT_STRING;
        }
      
      
    }
  
    //valid String function to check whether the passed argument is true or false.
    public boolean validString(String string){
       
         int length = string.length();
       
         if(length >= 1 && length <=50){
             return true;
         }
         return false;
       
    }
  
    //Accessor Functions
    public String getString1(){
        return this.string1;
    }
  
    public String getString2(){
        return this.string2;
    }
  
    public String getString3(){
        return this.string3;
    }
  
    //Mutator Functions
    public boolean setString1(String string){
        if(validString(string)){
            this.string1 = string;
            return true;
        }
        return false;
    }
  
    public boolean setString2(String string){
        if(validString(string)){
            this.string2 = string;
            return true;
        }
        return false;
    }
  
    public boolean setString3(String string){
        if(validString(string)){
            this.string3 = string;
            return true;
        }
        return false;
    }
  
    //override the toString() method to print the values in the current object.
    public String toString(){
      
        String toPrint = \"[\" + this.string1 + \" \" + this.string2 + \" \" + this.string3 + \"]\";
      
        return toPrint;
    }
  
  
   public static void main (String[] args) throws java.lang.Exception
   {
       // your code goes here
      
       //Instatntiating 4 objects of TripleString and printing all the objects at once.
       TripleString ts1 = new TripleString();
       System.out.println(ts1);
      
       TripleString ts2 = new TripleString(\"How\",\"are\",\"you\");
       System.out.println(ts2);
      
       TripleString ts3 = new TripleString(\"Are\",\"You\",\"There?\");
       System.out.println(ts3);
      
       TripleString ts4 = new TripleString(\"I\",\"Love\",\"You\");
       System.out.println(ts4);
      
       //Mutator calls
      
      
       ts1.setString1(\"Hello\");
       ts1.setString2(\"Guys\");
       ts1.setString2(\"onlineJudgeisbetterwayofcheckingcodewhennocompileyoucaninstallinsystem\");
      
       //Print again to check whether the mutator calls are successful or failure.
       System.out.println(ts1);
       System.out.println(ts2);
       System.out.println(ts3);
       System.out.println(ts4);
      
      
  
       ts2.setString1(\"Where\");
       ts4.setString2(\"Hate\");
       ts3.setString3(\"here\");
      
       System.out.println(ts1);
       System.out.println(ts2);
       System.out.println(ts3);
       System.out.println(ts4);
      
      
       //Checking whether the Mutator calls are successful or not.Print Success if The string is valid.
      
       if(ts1.setString2(\"onlineJudgeisbetterwayofcheckingcodewhennocompileyoucaninstallinsystem\")){
          
            System.out.println(\"Call Successfull\");
       }
       else{
            System.out.println(\"Call Fails\");
       }
      
      
       //Checking whether the Mutator calls are successful or not.Print Success if The string is valid.
       if(ts4.setString2(\"worship\")){
          
            System.out.println(\"Call Successfull\");
       }
       else{
            System.out.println(\"Call Fails\");
       }
      
      
       //Checking the accessor calls are working fine or not.
      
       ts1.getString1();
       ts2.getString2();
       ts3.getString3();
      
      
   }
}

Please help me with this C++ code!! I need the answer by Tuesday 2/16 The assignment is to first create a class called TripleString. TripleString will consist o
Please help me with this C++ code!! I need the answer by Tuesday 2/16 The assignment is to first create a class called TripleString. TripleString will consist o
Please help me with this C++ code!! I need the answer by Tuesday 2/16 The assignment is to first create a class called TripleString. TripleString will consist o
Please help me with this C++ code!! I need the answer by Tuesday 2/16 The assignment is to first create a class called TripleString. TripleString will consist o
Please help me with this C++ code!! I need the answer by Tuesday 2/16 The assignment is to first create a class called TripleString. TripleString will consist o

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site