Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I need hepl with this code. import java.util.Scanner; public class CharacterMain { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println(Please enter

I need hepl with this code.

import java.util.Scanner;

public class CharacterMain {

public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("Please enter a character for sequence 1:"); sequence1(sc.nextLine().charAt(0)); System.out.println(); System.out.println("Please enter a character for sequence 2:"); sequence2(sc.nextLine().charAt(0)); sc.close(); }

/* * Exercise 1: * print true if ch is equal to 'a' and false otherwise * print true if ch is a digit and false otherwise * print true if ch is in lower-case and false otherwise */ public static void sequence1(char ch) { // write code /* Hint: if statement in java if(){ //true condition statement } else{ //alternative statement } */ }

/* * Exercise 2: * print true if ch is equal to 'Z' and false otherwise * print true if ch is an alphabet letter and false otherwise * print the lower case version of ch * print true if ch is in upper-case and false otherwise * print the upper case version of ch * print true if upper-case ch is equal to lower-case ch and false otherwise */ public static void sequence2(char ch) { // write code } }

Here is the JUnit Code to verify:

 

import static org.junit.Assert.*; import java.io.ByteArrayOutputStream; import java.io.PrintStream; import java.lang.reflect.Method; import org.junit.Before; import org.junit.Test;

public class CharacterMainTest {

private ByteArrayOutputStream byteArrayOutputStream; private PrintStream console; private String expected1a = "false false false "; private String expected1b = "false false true "; private String expected1c = "false true false "; private String expected1d = "true false true ";

private String expected2a = "false true e true E false "; private String expected2b = "false true e false E false "; private String expected2c = "false false 4 false 4 true "; private String expected2d = "true true z true Z false "; private String notExpected = "";

@Before public void setup(){ byteArrayOutputStream = new ByteArrayOutputStream(); console = System.out; }

@Test public void testSequence1() throws Exception{ runTest('E',"CharacterMain","sequence1"); String result1 = byteArrayOutputStream.toString().replaceAll(" ", ""); byteArrayOutputStream = new ByteArrayOutputStream(); runTest('e',"CharacterMain","sequence1"); String result2 = byteArrayOutputStream.toString().replaceAll(" ", ""); byteArrayOutputStream = new ByteArrayOutputStream(); runTest('4',"CharacterMain","sequence1"); String result3 = byteArrayOutputStream.toString().replaceAll(" ", ""); byteArrayOutputStream = new ByteArrayOutputStream(); runTest('a',"CharacterMain","sequence1"); String result4 = byteArrayOutputStream.toString().replaceAll(" ", ""); assertFalse("Remember to output the first sequence results into the console.",notExpected.equals(result1));

assertTrue("Your first sequence did not passed the test with 'E'. Please review it.",expected1a.equals(result1)); assertTrue("Your first sequence did not passed the test with 'e'. Please review it.",expected1b.equals(result2)); assertTrue("Your first sequence did not passed the test with '4'. Please review it.",expected1c.equals(result3)); assertTrue("Your first sequence did not passed the test with 'a'. Please review it.",expected1d.equals(result4)); } @Test public void testSequence2() throws Exception{ runTest('E',"CharacterMain","sequence2"); String result1 = byteArrayOutputStream.toString().replaceAll(" ", ""); byteArrayOutputStream = new ByteArrayOutputStream(); runTest('e',"CharacterMain","sequence2"); String result2 = byteArrayOutputStream.toString().replaceAll(" ", ""); byteArrayOutputStream = new ByteArrayOutputStream(); runTest('4',"CharacterMain","sequence2"); String result3 = byteArrayOutputStream.toString().replaceAll(" ", ""); byteArrayOutputStream = new ByteArrayOutputStream(); runTest('Z',"CharacterMain","sequence2"); String result4 = byteArrayOutputStream.toString().replaceAll(" ", ""); assertFalse("Remember to output the second sequence results into the console.",notExpected.equals(result1)); assertTrue("Your second sequence did not passed the test with 'E'. Please review it.",expected2a.equals(result1)); assertTrue("Your second sequence did not passed the test with 'e'. Please review it.",expected2b.equals(result2)); assertTrue("Your second sequence did not passed the test with '4'. Please review it.",expected2c.equals(result3)); assertTrue("Your second sequence did not passed the test with 'Z'. Please review it.",expected2d.equals(result4)); }

private void runTest(final char data, final String className, final String methodName) throws Exception{ try{ System.setOut(new PrintStream(byteArrayOutputStream));

final Class cls = Class.forName(className); final Method meth = cls.getDeclaredMethod(methodName, char.class); final char params = data; meth.invoke(null, (Object) params);

} finally { System.setOut(console); } } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Design Application And Administration

Authors: Michael Mannino, Michael V. Mannino

2nd Edition

0072880678, 9780072880670

Students also viewed these Databases questions

Question

4. Identify cultural variations in communication style.

Answered: 1 week ago