Each person in the group will be responsible for rewriting the code to solve one of the original parts (celebrity names), without using arrays or loops. The purpose of this assignment is to practice Strings manipulation and String methods so full credit will only be given if you are utilizing the code for Strings (substring, charAt, compareTo, equals, etc). program Java
this was my last assigment
Take the 4th and the 5th words from part 1 and print out both of their celebrity couple names. Celebrity couples are made from taking half of the first name and combining it with the other half of the other name. The first half of the first name combines with the second half of the second name and the first half of the second name combines with the second half of the first name. Odd length names can be handled any way you wish, but all letters of both original words must be used between the two new words.
For example - Joey and Amanda would become Jonda and Amaey
// Part4- function to mix words to get celebrity couple names
public static void mix(String a[]){
System.out.print("Part4: ");
// first half of 1st word + second half of 2nd word to get celebrity name of word 1
String celeb=a[0].substring(0,a[0].length()/2)+a[1].substring(a[1].length()/2,a[1].length());
System.out.print(celeb+" ");
// fisrt half of 2nd word + second half of 1st word for celebrity name of word 2
celeb=a[1].substring(0,a[1].length()/2)+a[0].substring(a[0].length()/2,a[0].length());
System.out.print(celeb+" ");
}
// driver code
public static void main(String[] args) {
Scanner s=new Scanner(System.in);
// words entered by user
String words[]=new String[5];
System.out.println("Enter 5 words:");
for(int i=0;i<5;i++){
System.out.print("Word "+(i+1)+": ");
words[i]=s.next();
}
// part1 for ordering
order(words);
//part 2 for pig latin conversion
String a[]={words[0],words[1]};
pigLatin(a);
//part 3 for reverse words
a[0]=words[1];
a[1]=words[2];
reverse(a);
//part 4 for mixing words
a[0]=words[3];
a[1]=words[4];
mix(a);
}
}
PROGRAM :
Note: Replace the function given below with the function in your code.
public static void mix(String a[]) {
System.out.println("Part 4:");
String first = a[0];
String second = a[1];
String first_half, second_half;
int l1 = first.length();
int l2 = second.length();
// First Half
first_half = first.substring(0, l1/2) + second.substring(l2/2);
//Second Half
second_half = second.substring(0, l2/2) +
first.substring(l1/2);
System.out.println(final + " " + second_half);
}
Get Answers For Free
Most questions answered within 1 hours.