Question
Need to use Java Eclipse. This question is about java recursion. Provide source codes to the following questions. (4 java files are given below.) E13.2
Need to use Java Eclipse.
This question is about java recursion. Provide source codes to the following questions. (4 java files are given below.)
E13.2 Given a class Square with an instance variable width, provide a recursive getArea method. Construct a square whose width is one less than the original and call its getArea method.
E13.8 Use recursion to implement a method public static boolean find(String text, String str) that tests whether a given text contains a string. For example, find("Mississippi", "sip") returns true. Hint: If the text starts with the string you want to match, then you are done. If not, consider the text that you obtain by removing the first character. Provide one source file (*.java) for one question. Hence, total two *.java files need to be submitted. To help your work, incomplete source files are attached. Use the provided source files to answer the questions. Square.java and Sentence.java are for E13.2 and E13.8, respectively. In modifying the source files, 1. Do not change the file (class) names. Points will be deducted if you have different names. 2. Fill in your name in the @author section of the comment in each of the files. If you do not, points will be deducted. Note that demo files (testing your classes) are provided. You do not need to submit these demo files (SentenceDemo.java and SquareDemo.java). Note also that your class should be able to give correct outputs with different inputs.
========================================================================
Square.java
/**
* Code for E13.2(HW8)
* @author
*/
/**
A square with integer dimensions.
*/
public class Square
{
private int width;
/**
Constructs a new square with integer dimensions.
@param w width
*/
public Square(int w)
{
width = w;
}
/**
Computes the area of the square.
@return the area
*/
public int getArea()
{
//implement a recursive method
}
}
========================================================================
SquareDemo.java
public class SquareDemo
{
public static void main(String[] args)
{
Square s1 = new Square(8);
Square s2 = new Square(14);
System.out.println(s1.getArea());
System.out.println("Expected: 64");
System.out.println(s2.getArea());
System.out.println("Expected: 196");
}
}
========================================================================
Sentence.java
/**
* Code for E13.8(HW8)
* @author
*/
public class Sentence
{
private String phrase;
/**
Constructs a Sentence object.
@param aPhrase a phrase entered by user
*/
public Sentence(String aPhrase)
{
phrase = aPhrase;
}
/**
Determines if a string exists in the phrase.
@param text the string to find
@return true if the string is found, false otherwise
*/
public boolean find(String text)
{
//implement a recursive method
}
}
======================================================================== SentenceDemo.java
/**
A tester class for Sentence.
*/
public class SentenceDemo
{
public static void main(String[] args)
{
Sentence s = new Sentence("Mississippi!");
boolean b = s.find("sip");
System.out.println(b);
System.out.println("Expected: true");
b = s.find("tip");
System.out.println(b);
System.out.println("Expected: false");
}
}
========================================================================
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started