The statements below shows the declaration of an array:
String[] Municipality ={”Steve Tshwete”, “Nkomazi”, “City of Mbombela”, “Govan Mbeki”};
1) Make use of a loop of your choice and write statement(s) that will display all array elements indicating the Municipality’s name as follows:
Municipality 1: Steve Tshwete
Municipality 2: Nkomazi
Municipality 3: City of Mbombela
Municipality 4: Govan Mbeki
1) Write a statement that will replace array element Nkomazi with White River.
3) What is the array length?
4) Make use of a loop of your choice and write statement(s) that will display all Municipalities, considering effects of question (2), starting with the last Municipality as shown below:
Municipality 4: Govan Mbeki
Municipality 3: City of Mbombela
Municipality 2: Nkomazi
Municipality 1: Steve Tshwete
public class Main
{
public static void main(String[] args) {
//1
String[] Municipality ={"Steve Tshwete", "Nkomazi", "City of Mbombela", "Govan Mbeki"};
for(int i=0;i<4;i++){
System.out.println("Municipality "+(i+1)+":"+Municipality[i]);
}
System.out.println();
//4
for(int i = 3; i >= 0; i--){
System.out.println("Municipality "+(i+1)+":"+Municipality[i]);
}
// 1) Write a statement that will replace array element Nkomazi with White River.
Municipality[1]="White River";
// What is the array length?
int length=Municipality.length;
System.out.println(length);
}
}
Comments
Leave a comment