Purpose: Once you have completed and tested the functionality of your MyStringBuilder class, you will do a rough comparative test to see how long some of the operations take relative to the predefined StringBuilder class and the predefined String class.
Details: You will complete a simple simulation to compare the times required for three operations: append(char c) , insert(int loc, char c) and delete(int start, int stop) These methods are defined in the StringBuilder and MyStringBuilder classes, but you will have to approximate them with the String class, since it is immutable [part of the assignment is to figure out how to approximate these with a String - as a hint look at the substring() method in the String class].
You will test these operations in the following way:
- Write a program called Assig2B.java, which will be the main program for the rest of this description.
- Your main program will be invoked with a command line argument, which is the number of characters that you will be appending / inserting into your string objects. Call this value N. Since the value of the character does not matter here, you can just use all character 'A's for your operations.
- In your main program, you will iterate 3 times, once for each of the three test classes (StringBuilder, MyStringBuilder and String). For each iteration in your main program, you will do the following:
- Create a new object of the class that is being tested
- Time the operations as described below. For more details on the procedure for timing, see below.
- Time append(): Use the append() method (or String approximation thereof) to add each of the N characters to the end of the current testing object.
- Time delete(0,1): Remove the first character of the current testing object repeatedly until the object contains no more characters (a total of N characters should be removed).
- Time insert(): Process the N characters again. However, this time you will insert() each character into the middle of the testing object. The "middle" of the object can be determined by dividing the length by 2. Be careful to test for special cases!
- For each of your Time operations above you should proceed in the following way:
- Mark the start "time" of your test with a call to System.nanoTime() - Perform ALL of the operations on the current testing object (i.e. append / insert all N characters into or remove all N characters from the object)
- Mark the stop "time" of your test with a call to System.nanoTime()
- Calculate the elapsed time for all of the operations by subtracting the start time from the stop time
- Calculate the average time per operation by dividing the elapsed time by the number of characters processed (either appended / inserted or deleted)
- Output your results to the display, both for the total time and the time per operation
Run your program with each of the following input values for N: 10000, 20000, 40000, 80000, 160000
Qo.
ANS:
Java program to append a string using StringBuilder
JAVA Code:
/** USING StringBuilder append */
import java.lang.*;
import java.util.*;
public class Assig2B{
public static void main(String args[])
{
int choice;
System.out.println("*********
LEGENDS ****************");
System.out.println("Enter 1 for
Appending");
System.out.println("Enter 2 for
Inserting");
System.out.println("Enter 3 for
Deleting");
System.out.println("1. APPEND(N)
\n2. INSERT(pos,char) \n3. DELETE (startPosition,
endPostion)");
Scanner ch=new
Scanner(System.in);
System.out.print("What Do You Want?
Enter Your Choice: ");
choice=ch.nextInt();
switch(choice)
{
//Append n chars
long string
case 1: String
userStr;
int n;
Scanner s = new
Scanner(System.in);
System.out.println("Intial
String:");
StringBuilder SBinput = new
StringBuilder("I Like Chegg India "); //Let's consider this is
initial string
System.out.println(SBinput);
System.out.print("How Many
Character(s) You want to append? ");
n=s.nextInt();
Scanner iptstr = new
Scanner(System.in);
System.out.print("Enter "+n+"
Charcter Long String : ");
userStr =
iptstr.nextLine();
String snipName =
userStr.substring(0, n); //automatically snip string when you'll
enter the string greater than limit
if
(userStr.length()>n)
{
userStr=snipName;
}
System.out.print("You've
entered: " +userStr);
System.out.println();
//Appending Operation of n
char long string
SBinput.append(userStr);
System.out.println("After
Appending: ");
System.out.println(SBinput);
break;
//inserting a
string at desired position
case 2: char
ins;
int pos;
Scanner ob=new
Scanner(System.in);
StringBuilder initStr = new
StringBuilder("I Like Chegg India ");
System.out.println("Intial
String = " + initStr);
System.out.print("Which
charcter you want to insert? Enter that: ");
ins=ob.next().charAt(0);
System.out.print("On Which
Position You want to Insert? ");
pos=ob.nextInt();
//insert char at offset value
on the user given position
initStr.insert(pos,ins);
// prints StringBuilder after
insertion
System.out.print("After
insertion = ");
System.out.println(initStr.toString());
break;
//DELETION a
string from start position to end position
case 3: int
startPos, endPos;
StringBuilder str = new
StringBuilder("I Like Chegg India ");
System.out.println("Intial
String = " + str);
Scanner obj=new
Scanner(System.in);
System.out.println(" DELETING
A STRING ");
System.out.println("Enter
Start Position: ");
startPos=obj.nextInt();
System.out.println("Enter End
Position: ");
endPos=obj.nextInt();
//deleting chars from start
index to end index
str.delete(startPos,
endPos);
System.out.println("After
deletion = "+str);
break;
default:
System.out.println("Your Choice is Wrong!!");
}
}
}
OUTPUT IMAGE1:
APPEND OPERATION
OUTPUT IMAGE2:
INSERT OPERATION
OUTPUT IMAGE3:
DELETE OPERATION
-------------------------------------------------
THANKS!!
Purpose: Once you have completed and tested the functionality of your MyStringBuilder class, you will do...
You have to use a h file for class definitions and two (as specified below) cpp file for the C++ implementation. You have to generate the list from scratch and check for overflow I will be giving extra points (up to 2 points this time) to students that exceed the requirements posed in the assignments. For example, extensive testing, checking for exceptions, and usable user interface. Please submit enough screenshots showing compiling and execution under Linux. Write a main program...
DIRECTIONS FOR THE WHOLE PROJECT BELOW BigInt class The purpose of the BigInt class is to solve the problem using short methods that work together to solve the operations of add, subtract multiply and divide. A constructor can call a method called setSignAndRemoveItIfItIsThere(). It receives the string that was sent to the constructor and sets a boolean variable positive to true or false and then returns a string without the sign that can then be processed by the constructor to...
Assume the only console output method available is display(c), as shown below, which you include in your solution. This means you are not permitted to use System.out.print(), System.out.println(), nor System.out.printf() in your methods. public static void display (charc) { System.out.print (c); Implement the following methods in a program class, that has a main() method that simply tests each. Apply the concept of overloading, where best, and that the methods can call other new methods to reduce repeating the same code....
!!!!!!!Java!!!!! When you are confident that your methods work properly and that you can generate random text with my generateText method, you can move on to the second step. Create a third class called Generator within the cs1410 package. Make class. This class should have a main method that provides a user interface for random text generation. Your interface should work as follows: Main should bring up an input dialog with which the user can enter the desired analysis level...
Please help me. package a1; public class Methods { /* * Instructions for students: Use the main method only to make calls to the * other methods and to print some testing results. The correct operation of * your methods should not depend in any way on the code in main. * * Do not do any printing within the method bodies, except the main method. * * Leave your testing code...
You will write three static methods to manipulate an input String in different ways using various String methods. You need to provide the code for each of the three static methods in class StringPlay (requirements for each listed below). You will also change the control statement in the test harness to allow for variations in the sentinel value. You need to modify the loop control condition in Lab09.java so that user inputs of ‘finish’, “FINISH”, “FiniSH”, “fINISH”, etc. will end...
1. Analyze the following code: public class Test implements Runnable { public static void main(String[] args) { Thread t = new Thread(this); t.start(); } public void run() { System.out.println("test"); } } 1. The code compiles but will not print anything since t does not invoke the run method. 2. The code will not compile since you cannot invoke "this" in a static method. 3. The program compiles, runs, and prints tests on the console. 2. What will the following example...
Once your UML diagram is complete implement the class naming the Java file MyInteger.java. Create another Java file, a program named MyIntegerTester.java that tests all methods in the class. One technique is to write both classes at the same time. As much as possible you implement and test one method at a time. This minimized the amount of new code you have to look at when debugging a method. Only the Version 4.0 “tester” file will have main() method in...
In this question you have to write a C++ program to convert a date from one format to another. You have to write a complete program consisting of a main() function and a function called convertDate(). The function receives a string of characters representing a date in American format, for example December 29, 1953. The function has to convert this date to the international format. For example: If the string December 29, 1953 is received, the string that the function...
Using loops with the String and Character classes. You can also use the StringBuilder class to concatenate all the error messages. Floating point literals can be expressed as digits with one decimal point or using scientific notation. 1 The only valid characters are digits (0 through 9) At most one decimal point. At most one occurrence of the letter 'E' At most two positive or negative signs. examples of valid expressions: 3.14159 -2.54 2.453E3 I 66.3E-5 Write a class definition...