Kelompok 12 SI-40-02
Nama:
Ben Hughie Rezanda (1202164286)
Ian Dzillan Malik (1202160194)
BAB 7
Soal:
14. Revise the class OneWayNoRepeatsList, as given in Listing 7.9, so that is it allocates an extra element in the array entry and ignores entry [0], as suggested earlier near the end of the section entitled “More About Array Indices”.
Penjelasan soal:
Jadi dalam soal ini, kita akan merevisi class OneWayNoRepeatsList seperti pada Listing 7.9 agar elemennya bisa di tambah dan mengabaikan indeks [0]-nya.
Scirpt code java:
OneWayNoRepeatList.class:
public class OneWayNoRepeatsList {
public static int START_POSITION = 1;
public static int DEFAULT_SIZE = 50;
public int countOfEntries;
public String [] entry;
public OneWayNoRepeatsList(int maximumNumberOfEntries){
entry = new String [maximumNumberOfEntries];
countOfEntries = 0;
}
public OneWayNoRepeatsList(){
entry = new String[DEFAULT_SIZE];
countOfEntries = DEFAULT_SIZE;
}
public boolean isFull(){
return countOfEntries == entry.length;
}
public boolean isEmpty(){
return countOfEntries == 0;
}
public void addItem(String item){
if (!isOnList(item)){
if (countOfEntries == entry.length){
System.out.println("kazuma");
System.exit(0);
}else{
entry[countOfEntries] = item;
countOfEntries++;
}
}
}
public String getEntryAt(int position){
String result = null;
if ((1<= position) && (position <= countOfEntries))
result = entry[position -1];
return result;
}
public boolean atLastEntry(int position){
return position == countOfEntries;
}
public boolean isOnList(String item){
boolean found = false;
int i=0;
while (!found && (i<countOfEntries)){
if (item.equalsIgnoreCase(entry[i]))
found = true;
else
i++;
}
return found;
}
public int getMaximumNumberOfEntries(){
return entry.length;
}
public int getNumberOfEntries(){
return countOfEntries;
}
public void eraseList(){
countOfEntries = 0;
}
}
ListDemo.class:
import java.util.Scanner;
import static onewaynorepeatslist.OneWayNoRepeatsList.DEFAULT_SIZE;
import static onewaynorepeatslist.OneWayNoRepeatsList.START_POSITION;
public class ListDemo extends OneWayNoRepeatsList{
public static void main (String [] args){
OneWayNoRepeatsList set = new OneWayNoRepeatsList();
String [] nama = new String [5];
Scanner input = new Scanner(System.in);
nama [0] = input.nextLine();
nama [1] = input.nextLine();
nama [2] = input.nextLine();
nama [3] = input.nextLine();
nama [4] = input.nextLine();
System.out.println();
for (int i = 0; i<nama.length;i++){
System.out.println(nama[i]);
}
set.addItem("a");
set.atLastEntry(5);
set.eraseList();
}
}
Hasil Output: