Question

Complete the class LoopyText. The constructor has been completed and provided to you in Codecheck. • public LoopyText(String

Given code:

/**
* Provides some methods to manipulate text
*
*/
public class LoopyText
{
private String text;
  
/**
* Creates a LoopyText object with the given text
* @param theText the text for this LoopyText
*/
public LoopyText(String theText)
{
text = theText;
}
  
//Your methods here
}

Given tester code:

/**
* Tests the methods of LoopyText.
* @author Kathleen O'Brien
*/
public class LoopyTextTester
{
public static void main(String[] args)
{
LoopyText loopy = new LoopyText("Java for All");
System.out.println(loopy.getEverySecondCharacter());
System.out.println("Expected: Jv o l");
  
loopy = new LoopyText("Today is the First Day of the Rest of your LIFE");
System.out.println(loopy.getEverySecondCharacter());
System.out.println("Expected: Tdyi h is a fteRs fyu IE");
  
loopy = new LoopyText("");
System.out.println(loopy.getEverySecondCharacter());
System.out.println("Expected: ");
}
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

LoopyText.java

public class LoopyText
{
private String text;
  
/**
* Creates a LoopyText object with the given text
* @param theText the text for this LoopyText
*/
public LoopyText(String theText)
{
text = theText;
}
  
//Your methods here

public String getEverySecondCharacter() {
   String s = "";
   for(int i=0;i<text.length();i++) {
       if(i%2 == 0) {
           s = s + text.charAt(i);
       }
   }
   return s;
}

}

LoopyTextTester.java

/**
* Tests the methods of LoopyText.
* @author Kathleen O'Brien
*/
public class LoopyTextTester
{
public static void main(String[] args)
{
LoopyText loopy = new LoopyText("Java for All");
System.out.println(loopy.getEverySecondCharacter());
System.out.println("Expected: Jv o l");
  
loopy = new LoopyText("Today is the First Day of the Rest of your LIFE");
System.out.println(loopy.getEverySecondCharacter());
System.out.println("Expected: Tdyi h is a fteRs fyu IE");
  
loopy = new LoopyText("");
System.out.println(loopy.getEverySecondCharacter());
System.out.println("Expected: ");
}
}

Output:

Jv o l
Expected: Jv o l
Tdyi h is a fteRs fyu IE
Expected: Tdyi h is a fteRs fyu IE

Expected:

Add a comment
Know the answer?
Add Answer to:
Given code: /** * Provides some methods to manipulate text * */ public class LoopyText {...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • You will write three static methods to manipulate an input String in different ways using various...

    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...

  • Design and implement a class Q that uses Q.java as a code base. The queue ADT...

    Design and implement a class Q that uses Q.java as a code base. The queue ADT must use class LinkedList from Oracle's Java class library and its underlying data structure (i.e. every Q object has-a (contains) class LinkedList object. class Q is not allowed to extend class LinkedList. The methods that are to be implemented are documented in Q.java. Method comment blocks are used to document the functionality of the class Q instance methods. The output of your program must...

  • Solve this using Java for an Intro To Java Class. Provided files: Person.java /** * Models...

    Solve this using Java for an Intro To Java Class. Provided files: Person.java /** * Models a person */ public class Person { private String name; private String gender; private int age; /** * Consructs a Person object * @param name the name of the person * @param gender the gender of the person either * m for male or f for female * @param age the age of the person */ public Person(String name, String gender, int age) {...

  • given the following two classes Within a file named Rational.java, define a public class named Rational...

    given the following two classes Within a file named Rational.java, define a public class named Rational such that … the Rational class has two private instance variables, numerator and denominator, both of type int the Rational class defines two public accessor methods, numerator() and denominator() the Rational class defines a constructor that has one String parameter a throws clause indicates that this constructor could potentially throw a MalformedRationalException this constructor throws a MalformedRationalException in the following circumstances … When the...

  • Define the Bag class in the file Bag.java with the following structure. Define the public methods...

    Define the Bag class in the file Bag.java with the following structure. Define the public methods listed, including the constructors. You may add fields to the class as necessary. // A class representing a bag of items public class Bag { } // Construct a bag, initially empty, with the indicated weight that it can hold (in ounces) public Bag ( int weight ) { ... } // Construct a bag, initially empty, with an assumed weight of 20 ounces....

  • What is wrong with the following Java Code. public class TestEdible { abstract class Animal {...

    What is wrong with the following Java Code. public class TestEdible { abstract class Animal { /** Return animal sound */ public abstract String sound(); } class Chicken extends Animal implements Edible { @Override public String howToEat() { return "Chicken: Fry it"; } @Override public String sound() { return "Chicken: cock-a-doodle-doo"; } } class Tiger extends Animal { @Override public String sound() { return "Tiger: RROOAARR"; } } abstract class Fruit implements Edible { // Data fields, constructors, and methods...

  • Hi I really need help with the methods for this lab for a computer science class....

    Hi I really need help with the methods for this lab for a computer science class. Thanks Main (WordTester - the application) is complete and only requires uncommenting to test the Word and Words classes as they are completed. The needed imports, class headings, method headings, and block comments are provided for the remaining classes. Javadocs are also provided for the Word and Words classes. Word The Word class represents a single word. It is immutable. You have been provided...

  • For Questions 1-3: consider the following code: public class A { private int number; protected String...

    For Questions 1-3: consider the following code: public class A { private int number; protected String name; public double price; public A() { System.out.println(“A() called”); } private void foo1() { System.out.println(“A version of foo1() called”); } protected int foo2() { Sysem.out.println(“A version of foo2() called); return number; } public String foo3() { System.out.println(“A version of foo3() called”); Return “Hi”; } }//end class A public class B extends A { private char service; public B() {    super();    System.out.println(“B() called”);...

  • For Questions 1-3: consider the following code: public class A { private int number; protected String...

    For Questions 1-3: consider the following code: public class A { private int number; protected String name; public double price; public A() { System.out.println(“A() called”); } private void foo1() { System.out.println(“A version of foo1() called”); } protected int foo2() { Sysem.out.println(“A version of foo2() called); return number; } public String foo3() { System.out.println(“A version of foo3() called”); Return “Hi”; } }//end class A public class B extends A { private char service; public B() {   super();   System.out.println(“B() called”); } public...

  • Java class quiz need help~ This is the Backwards.java code. public class Backwards { /** *...

    Java class quiz need help~ This is the Backwards.java code. public class Backwards { /** * Program starts with this method. * * @param args A String to be printed backwards */ public static void main(String[] args) { if (args.length == 0) { System.out.println("ERROR: Enter a String on commandline."); } else { String word = args[0]; String backwards = iterativeBack(word); // A (return address) System.out.println("Iterative solution: " + backwards); backwards = recursiveBack(word); // B (return address) System.out.println("\n\nRecursive solution: " +...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT