In: Computer Science
In Java
Problem 1
Create an Array of your top four vacation spots. Print the first
spot and the last spot in your array.
Problem 2
Create an Array to hold the names of 5 people. Print the first
letter of each of their names.
Problem 3
Create a mini console game that asks for the number of words you
want to enter into an Array. Enter each word and then a letter.
Print out feedback that says "Yep its got one of those" or "Sorry
that letter is not in the word" depending on whether on not the
word entered contains the letter specified.
Stretch Task (Optional)
Create a String representing a list of things that uses some
delimiter (commas,dashes, etc) Split that String into an Array of
Strings, then print each of the Array Elements. You will need to
research (How to Split a String using a Delimiter).
Answer 1:
public class VacationSpot {
public static void main(String[] args) {
// creating array with 4 places
String
arr[]={"Hyderebad","Chicago","Delhi","Kerala"};
// printing the first spot
System.out.println("First Spot : "+arr[0]);
// printing the last spot
System.out.println("Last Spot : "+arr[3]);
}
}
Answer 2:
public class NamesOfPeople {
public static void main(String[] args) {
// created array with 5
values
String names[] = { "Uday", "Koti",
"Smith", "Virat", "Chotu" };
// iterating through the
array
for (String str : names) {
// printing the
first char of each name
System.out.println(str + " : " + str.charAt(0));
}
}
}
Answer 3:
import java.util.Scanner;
public class WordsCheck {
public static void main(String[] args) {
Scanner sc = new
Scanner(System.in);
System.out.println("Enter number of
words");
int n = sc.nextInt();
sc.nextLine();
String words[] = new
String[n];
char c[] = new char[n];
System.out.println("Enter " + n + "
words ");
for (int i = 0; i < n; i++)
{
words[i] =
sc.nextLine();
c[i] =
sc.nextLine().charAt(0);
if
(words[i].contains(c[i] + "")) {
System.out.println("Yep its got one of
those");
} else {
System.out.println("Sorry that letter is not in
the word");
}
}
}
}