Question

In: Computer Science

1. Given a string of at least 3 characters as input, if the length of the...

1. Given a string of at least 3 characters as input, if the length of the string is odd return the character in the middle as a string. If the string is even return the two characters at the midpoint.

public class Class1 {
  
public static String midString(String str) {
//Enter code here
}
}

-----------

2. Given an array of integers return the sum of the values stored in the first and last index of the array. The array will have at least 2 elements in it

public class Class1 {
  
public static int endSum(int[] values) {
//Enter code here
}
  
}

-----------

3. The method takes a string as a parameter. The method prints treat if the string is candy or chocolate (of any case, such as CaNdY) Otherwise print trick

import java.util.Scanner;

public class Class1 {
public static void trickOrTreat(String str) {
//Enter code here
}
  
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
trickOrTreat(s.nextLine());
}
}

Solutions

Expert Solution

Part 1

Source code of the Java Program: save the below code as Class1.java

//Class1.java

//Program to display the middle characters of the inputted string

import java.util.Scanner;

public class Class1 {

//method to find the middlecharacters

public static String midString(String str) {

String str1="";

int length=str.length();

//checking the legth of the string is even

if(length%2==0){

int mid=length/2;

//If the length of the string is even return the

//two characters at the midpoint as a string

str1=Character.toString(str.charAt(mid-1))+(str.charAt(mid));

return str1;

}else{

int mid=length/2;

//if the length of the string is odd return the

//character in the middle as a string

str1=Character.toString(str.charAt(mid));

return str1;

}

}

//main function

public static void main(String args[])

{

Scanner scanner = new Scanner(System.in);

System.out.println("Enter Any String.");

String str=scanner.next();

int length=str.length();

//checking that the minimum value of length should be 3

if(length>=3){

//calling the function midString

String string=midString(str);

//displaying the midddle characters

System.out.println("Middle Characters of the String : "+string);

}else

System.out.println("Enter a string with length greater than 3.");

}

}

Part 3

Create "Scanner" class object in main() function for taking input from user.

prompt user to enter a string.

Take user input string in any string variable and call trickOrTreat() function passing string variable as a parameter to this function like "trickOrTreat(str)".

In "trickOrTreat()" this function, first declare two string variables and initialize with "candy" and "chocolate".

compare the string which is passed by the "main" function with above two string variables i.e "candy" or "chocolate".

The string is passed by the "main" function to "trickOrTreat()" function as a parameter.

There is one method named "equalsIgnoreCase()". This method ignoring the case of the string. So, use this method in "if" condition for ignoring the case.

If condition is true i.e user input string is "candy" or "chocolate" in any case then print "treat".

At last, If condition is false i.e user input string is not "candy" or "chocolate" in any case then print "trick".

Program

import java.util.Scanner;

public class Class1 {

public static void main(String[]args){

Scanner sc= new Scanner(System.in);

System.out.println("Enter string");

String str=sc.nextLine();

trickOrTreat(str);

}

public static void trickOrTreat(String str) {

String str1="candy";

String str2="chocolate";

if(str.equalsIgnoreCase(str1) || str.equalsIgnoreCase(str2)){

System.out.println("treat");

}

else{

System.out.println("trick");

}

}

}


Related Solutions

Given a string of at least 3 characters as input, if the length of the string...
Given a string of at least 3 characters as input, if the length of the string is odd return the character in the middle as a string. If the string is even return the two characters at the midpoint. -------------------------------------------------------------- public class Class1 { public static String midString(String str) {     //Enter code here } }
Java:    Find a pattern that will match any string that is --  at least 6 characters...
Java:    Find a pattern that will match any string that is --  at least 6 characters long, -- and begins with a letter or number (\w) -- and contains at least one non-letter and non-number (\W).
Suppose you are given a string containing only the characters ( and ). In this problem,...
Suppose you are given a string containing only the characters ( and ). In this problem, you will write a function to determine whether the string has balanced parentheses. Your algorithm should use no more than O (1) space beyond the input; any algorithms that use space not in O (1) will receive half credit at most. Any solutions that always return true (or always return false) or otherwise try to game the distribution of test cases will receive zero...
IN PYTHON Given a string with duplicate characters in it. Write a program to generate a...
IN PYTHON Given a string with duplicate characters in it. Write a program to generate a list that only contains the duplicate characters. In other words, your new list should contain the characters which appear more than once. Suggested Approach Used two nested for loops. The first for loop iterates from 0 to range(len(input_str)). The second for loop iterates from first_loop_index + 1 to range(len(input_str)). The reason you want to start at first_loop_index + 1 in the nested inner loop...
Suppose you are given a string containing only the characters ( and ). In this problem,...
Suppose you are given a string containing only the characters ( and ). In this problem, you will write a function to determine whether the string has balanced parentheses. Your algorithm should use no more than O (1) space beyond the input; any algorithms that use space not in O (1) will receive half credit at most. Any solutions that always return true (or always return false) or otherwise try to game the distribution of test cases will receive zero...
Given a string, write a method called removeRepthat returns another string where adjacent characters that are...
Given a string, write a method called removeRepthat returns another string where adjacent characters that are the same have been reduced to a single character. Test the program by calling the method from the main method. For example: removeRep(“yyzzza”) à “yza” removeRep(“aabbbccd”) à “abcd” removeRep(“122333”) à “123”
Project: Given a string s and an integer array indices of the same length. The string...
Project: Given a string s and an integer array indices of the same length. The string s will be shuffled such that the character at the i th position moves to indices[i] in the shuffled string. Return the shuffled string. Example: Input: s = "codeleet", indices = [4,5,6,7,0,2,1,3] Output: "leetcode" Explanation: As shown, "codeleet" becomes "leetcode" after shuffling. You need to do: Create a class called ShuffledStringApp. Keeping class StringOperation and IO in util package. In this project, you will...
Do not use c-string, arrays, and read input characters one by one. Also use : s.push_back(ch);....
Do not use c-string, arrays, and read input characters one by one. Also use : s.push_back(ch);. Code a program that reads a sequence of characters from the keyboard (one at a time) and creates a string including the distinct characters entered .The input terminates once the user enters a white-space character or the user has entered 50 distinct characters. Eg: Enter a sequence of characters: abAa1121X+&$%$dc[space] Distinct characters are: abA12X+&$%dc. Use C++
A string of length L = 1 is held fixed at both ends. The string is...
A string of length L = 1 is held fixed at both ends. The string is initially deformed into a shape given by u(x, t = 0) = sin^2(πx) and released. Assume a value of c2 = 1. Find the solution u(x, t) for the vibration of the string by separation of variables.
Assume that bits is a string that only contains the characters "0" and "1". n is...
Assume that bits is a string that only contains the characters "0" and "1". n is an integer variable that is less than the length of bits. Fill in the blanks below to replace bits with a new string that consists of all of the characters of bits from index n through the end, followed by the first n characters of bits. For example, if bits was "1101010" and n was 3, the new value of bits would be "1010110"....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT