Question

In: Computer Science

For Java Let's get more practice testing! Declare a public class TestRotateString with a single void...

For Java

Let's get more practice testing! Declare a public class TestRotateString with a single void static method named testRotateString. testRotateString receives an instance of RotateString as a parameter, which has a single method rotate. rotate takes a String and an int as arguments, and rotates the passed string by the passed amount. If the amount is positive, the rotation is to the right; if negative, to the left. If the String is null, rotate should return null.

Your testRotateString method should test the passed implementation using assert. Here's an example to get you started:\

assert rotate.String.rotate(null, 0) == null;

As you create your test suite, consider the various kinds of mistakes that you might make when implementing rotate. Maybe getting the direction wrong, or using the modulus improperly? There are lots of ways to mess this up! You need to catch all of them. Good luck!

Solutions

Expert Solution

First we declare the rotate method in RotateString class

public class RotateString
{
        public String rotate(String string, int rotations) {
                if (string == null)
                        return null;
                if (rotations == 0)
                        return string;
                int size = string.size();
                rotations %= size;
                if (rotations < 0) {
                        return string.substring(rotations) + string.substring(0, rotations);
                }
                return string.substring(size - rotations) + string.substring(0, size - rotations);
        }
}

Now we check all possible edge cases for rotate method

We check if passed argument is an instance of RotateString or not

We check outputs for null string, empty string

We validate rotation for positive and negative values

We vaildatte repeated rotations that is when rotations are greater than size of string

TestRotateString.java

public class TestRotateString
{
        static void testRotateString(RotateString rotateString) {
                assert (rotateString instanceof RotateString) == true;
                
                assert rotateString.rotate(null, 4) == null;
                
                assert rotateString.rotate("", 100) == "";
                
                assert rotateString.rotate("hello", 1) == "elloh";
                assert rotateString.rotate("hello", -1) == "ohell";
                
                assert rotateString.rotate("hello", 2) == rotateString.rotate("hello", 7);
                assert rotateString.rotate("hello", -2) == rotateString.rotate("hello", -7);
        }
}

Related Solutions

For Java Let's get some practice with maps! Create a public class CountLetters providing a single...
For Java Let's get some practice with maps! Create a public class CountLetters providing a single static method countLetters. countLetters accepts an array of Strings and returns a Map from Strings to Integers. (You can reject null arguments using assert.) The map should contain counts of the passed Strings based on their first letter. For example, provided the array {"test", "me", "testing"} your Map should be {"t": 2, "m": 1}. You should ignore empty Strings and not include any zero...
Let's get some practice with maps! Create a public class CountLetters providing a single static method...
Let's get some practice with maps! Create a public class CountLetters providing a single static method countLetters. countLetters accepts an array of Strings and returns a Map from Strings to Integers. (You can reject null arguments using assert.) The map should contain counts of the passed Strings based on their first letter. For example, provided the array {"test", "me", "testing"} your Map should be {"t": 2, "m": 1}. You should ignore empty Strings and not include any zero counts. As...
CODE: C# using System; public static class Lab6 { public static void Main() { // declare...
CODE: C# using System; public static class Lab6 { public static void Main() { // declare variables int hrsWrked; double ratePay, taxRate, grossPay, netPay=0; string lastName; // enter the employee's last name Console.Write("Enter the last name of the employee => "); lastName = Console.ReadLine(); // enter (and validate) the number of hours worked (positive number) do { Console.Write("Enter the number of hours worked (> 0) => "); hrsWrked = Convert.ToInt32(Console.ReadLine()); } while (hrsWrked < 0); // enter (and validate) the...
Let's get some practice implementing a known interface. Create a public class named MyString. MyString should...
Let's get some practice implementing a known interface. Create a public class named MyString. MyString should provide a public constructor that accepts a single String argument. You should reject null Strings in your constructor using assert. MyString should also implement the Java Comparable interface, returning 1 for a positive result and -1 for a negative result. Normally Strings are compared lexicographically: "aaa" comes before "z". MyString should compare instances based on the length of its stored String. So MyString("aaa") should...
public class sales_receipt { public static void main(String[] args) { //declare varables final double tax_rate =...
public class sales_receipt { public static void main(String[] args) { //declare varables final double tax_rate = 0.05; //tax rate String cashier_name = "xxx"; //sales person String article1, article2; //item name for each purchased int quantity1, quantity2; //number of quantities for each item double unit_cost1, unit_cost2; //unit price for each item double price1, price2; //calculates amounts of money for each item. double sub_total; //total price of two items without tax double tax; //amount of sales tax double total; //total price of...
Using the following in Java- package intersectionprinter; import java.awt.Rectangle; public class IntersectionPrinter { public static void...
Using the following in Java- package intersectionprinter; import java.awt.Rectangle; public class IntersectionPrinter { public static void main(String[] args) { Rectangle r1 = new Rectangle(0,0,100,150); System.out.println(r1);    Rectangle r2 = new Rectangle(50,75,100,150); System.out.println(r2);    Rectangle r3 = r1.intersection(r2);    } } Write a program that takes both Rectangle objects, and uses the intersection method to determine if they overlap. If they do overlap, then print it's coordinates along with its width and height. If there is no intersection, then have the...
Create a new Java file, containing this code public class DataStatsUser { public static void main...
Create a new Java file, containing this code public class DataStatsUser { public static void main (String[] args) { DataStats d = new DataStats(6); d.append(1.1); d.append(2.1); d.append(3.1); System.out.println("final so far is: " + d.mean()); d.append(4.1); d.append(5.1); d.append(6.1); System.out.println("final mean is: " + d.mean()); } } This code depends on a class called DataStats, with the following API: public class DataStats { public DataStats(int N) { } // set up an array (to accept up to N doubles) and other member...
Write program in Java import java.util.Scanner; public class Lab7Program { public static void main(String[] args) {...
Write program in Java import java.util.Scanner; public class Lab7Program { public static void main(String[] args) { //1. Create a double array that can hold 10 values    //2. Invoke the outputArray method, the double array is the actual argument. //4. Initialize all array elements using random floating point numbers between 1.0 and 5.0, inclusive    //5. Invoke the outputArray method to display the contents of the array    //6. Set last element of the array with the value 5.5, use...
Task 2/2: Java program Based upon the following code: public class Main {   public static void...
Task 2/2: Java program Based upon the following code: public class Main {   public static void main( String[] args ) {     String alphabet = "ABCDEFGHIJKLMNMLKJIHGFEDCBA";     for( <TODO> ; <TODO> ; <TODO> ) {       <TODO>;     } // Closing for loop   } // Closing main() } // Closing class main() Write an appropriate loop definition and in-loop behavior to determine if the alphabet string is a palindrome or not. A palindrome is defined as a string (or more generally, a token) which...
DESCRIBE WHAT THE FOLLOWING JAVA CODE DOES: public class Main{ public static void main(String[] args) {...
DESCRIBE WHAT THE FOLLOWING JAVA CODE DOES: public class Main{ public static void main(String[] args) { new MyFrame(); } } import javax.swing.*; public class MyFrame extends JFrame{ MyPanel panel; MyFrame(){ panel = new MyPanel(); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.add(panel); this.pack(); this.setLocationRelativeTo(null); this.setVisible(true); } } import java.awt.*; import javax.swing.*; public class MyPanel extends JPanel{ //Image image; MyPanel(){ //image = new ImageIcon("sky.png").getImage(); this.setPreferredSize(new Dimension(500,500)); } public void paint(Graphics g) { Graphics2D g2D = (Graphics2D) g; //g2D.drawImage(image, 0, 0, null); g2D.setPaint(Color.blue); g2D.setStroke(new BasicStroke(5)); g2D.drawLine(0, 0, 500,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT