In: Computer Science
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());
}
}
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");
}
}
}