Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

- the program needs to be written in Java -I will include the necessary code cla

ID: 671244 • Letter: #

Question

- the program needs to be written in Java
-I will include the necessary code classes below

-----------------------------------------------------

package stringAndStringBuilder;

public class RoundTrip {
   public static void main(String[] args) {
      
       String[] cities = {"Berlin", "Weimar", "Heidelberg", "Muenchen"};
      
   }
}

RoundTrip.java Use a StringBuilder and the String array cities to produce a String of the form described below. When you are done print the newly created String. From city1 to city2 to . . . to cityn and back to city1 Note: if I change the cities in the array before running the code the output needs to change accordingly Sample Output: If the String array cities includes the cities "Berlin", “Weimar", "Heidelberg", "Muenchen" From Berlin to Weimar to Heidlberg to Muenchen and back to Berlin.

Explanation / Answer


public class RoundTrip{
public static void main(String[] main){
      
   String[] cities={"Berlin","Weimar","Heidelberg","Muenchen"};
   StringBuilder s=new StringBuilder("From ");
   for(int i=0;i<cities.length;i++ ){
       s.append(cities[i]);
       s.append(" to ");
   }
   s.append(" and back to ");
   s.append(cities[0]);
   System.out.println(s);
}

}