Need help with this Exercise 21 .. So the first 2 pictures are the instruction.
ID: 3574857 • Letter: N
Question
Need help with this Exercise 21 .. So the first 2 pictures are the instruction. and the 3rd is what it looks like after opening the zip file
Here is the code for class Testing :
/*
* Class Testing
*
* Do NOT modify this file.
*
*/
public class Testing
{
public static void main (String[] args)
{
// ******************* DO NOT MODIFY THE MAIN METHOD*******************
testA();
testB();
testC();
testD();
}
public static void testA()
{
System.out.println(" **********************************************************");
System.out.println("TESTING SECTION A ");
YourCode a = new YourCode();
a.sectionA("WooHoo");
a.sectionA("HelloThere");
a.sectionA("abcdef");
a.sectionA("ab");
a.sectionA("0123456789");
a.sectionA("kitten");
}
public static void testB()
{
System.out.println(" **********************************************************");
System.out.println("TESTING SECTION B ");
YourCode b = new YourCode();
b.sectionB("Hello");
b.sectionB("java");
b.sectionB("coding");
b.sectionB("code");
b.sectionB("ab");
b.sectionB("Chocolate!");
b.sectionB("kitten");
b.sectionB("woohoo");
}
public static void testC()
{
System.out.println(" **********************************************************");
System.out.println("TESTING SECTION C ");
YourCode c = new YourCode();
c.sectionC("Hello");
c.sectionC("java");
c.sectionC("Hi");
c.sectionC("code");
c.sectionC("cat");
c.sectionC("12345");
c.sectionC("Chocolate");
c.sectionC("bricks");
}
public static void testD()
{
System.out.println(" **********************************************************");
System.out.println("TESTING SECTION G ");
YourCode d = new YourCode();
d.sectionD("abc hi ho");
d.sectionD("ABChi hi");
d.sectionD("hihi");
d.sectionD("hiHIhi");
d.sectionD("h");
d.sectionD("hi");
d.sectionD("Hi is no HI on ahI");
d.sectionD("hiho not HOHIhi");
}
}
And here is the code for class YourCode :
/*
* (name header)
*/
import java.util.Scanner;
public class YourCode
{
public static Scanner input = new Scanner(System.in);
public static void sectionB(String str)
{
// Your code here...
}
public static void sectionC(String str)
{
// Your code here...
}
public static void sectionD(String str)
{
// Your code here...
}
}
Explanation / Answer
Hi, Please find my code.
Please let me know in case of any issue.
/*
* (name header)
*/
import java.util.Scanner;
public class YourCode
{
public static Scanner input = new Scanner(System.in);
public static void sectionA(String str)
{
int length = str.length();
String A = str.substring(0, length/2);
System.out.println(A);
}
public static void sectionB(String str)
{
int length = str.length();
String B = str.substring(1, length-1);
System.out.println(B);
}
public static void sectionC(String str)
{
int length = str.length();
if(length == 2){
System.out.println(str);
}else{
String left = str.substring(0, 2);
String right = str.substring(2);
System.out.println(right+left);
}
}
public static void sectionD(String str)
{
int length = str.length();
int count = 0;
while(length >= 2){
String s = str.substring(0, 2);
if(s.equals("hi"))
count++;
str = str.substring(1);
length = str.length();
}
System.out.println(count);
}
}
/*
* Class Testing
*
* Do NOT modify this file.
*
*/
public class Testing
{
public static void main (String[] args)
{
// ******************* DO NOT MODIFY THE MAIN METHOD*******************
testA();
testB();
testC();
testD();
}
public static void testA()
{
System.out.println(" **********************************************************");
System.out.println("TESTING SECTION A ");
YourCode a = new YourCode();
a.sectionA("WooHoo");
a.sectionA("HelloThere");
a.sectionA("abcdef");
a.sectionA("ab");
a.sectionA("0123456789");
a.sectionA("kitten");
}
public static void testB()
{
System.out.println(" **********************************************************");
System.out.println("TESTING SECTION B ");
YourCode b = new YourCode();
b.sectionB("Hello");
b.sectionB("java");
b.sectionB("coding");
b.sectionB("code");
b.sectionB("ab");
b.sectionB("Chocolate!");
b.sectionB("kitten");
b.sectionB("woohoo");
}
public static void testC()
{
System.out.println(" **********************************************************");
System.out.println("TESTING SECTION C ");
YourCode c = new YourCode();
c.sectionC("Hello");
c.sectionC("java");
c.sectionC("Hi");
c.sectionC("code");
c.sectionC("cat");
c.sectionC("12345");
c.sectionC("Chocolate");
c.sectionC("bricks");
}
public static void testD()
{
System.out.println(" **********************************************************");
System.out.println("TESTING SECTION G ");
YourCode d = new YourCode();
d.sectionD("abc hi ho");
d.sectionD("ABChi hi");
d.sectionD("hihi");
d.sectionD("hiHIhi");
d.sectionD("h");
d.sectionD("hi");
d.sectionD("Hi is no HI on ahI");
d.sectionD("hiho not HOHIhi");
}
}
/*
Output:
**********************************************************
TESTING SECTION A
Woo
Hello
abc
a
01234
kit
**********************************************************
TESTING SECTION B
ell
av
odin
od
hocolate
itte
ooho
**********************************************************
TESTING SECTION C
lloHe
vaja
Hi
deco
tca
34512
ocolateCh
icksbr
**********************************************************
TESTING SECTION G
1
2
2
2
0
1
0
2
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.