Tugas2-Struktur Data
Apa itu Array?
Array adalah sebuah variabel yang bisa menyimpan banyak data dalam satu variabel.
Array menggunakan indeks untuk memudahkan akses terhadap data yang disimpannya.Indeks array selalu dimulai dari 0
…
…dan perlu diketahui juga, indeks tidak selalu dalam bentuk angka. Bisa juga karakter atau teks.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Array App Program | |
* Program to implement an array type conventionally | |
* | |
* @author Afira Rolobessy | |
* @version 1.0 | |
* @since March 27th 2021 | |
* */ | |
public class ArrayApp { | |
/** | |
* Main Method | |
* Description : | |
* This method is used as main method. | |
* | |
* @param args arguments to the console app while compiled and launched. | |
* */ | |
public static void main(String[] args){ | |
long[] arr; | |
arr = new long[100]; | |
arr[0] = 77; | |
arr[1] = 99; | |
arr[2] = 44; | |
arr[3] = 55; | |
arr[4] = 22; | |
arr[5] = 88; | |
arr[6] = 11; | |
arr[7] = 0; | |
arr[8] = 66; | |
arr[9] = 33; | |
int nElems = 10; | |
//another variable declaration | |
int j; | |
long searchKey; | |
// display all arrays | |
for (j = 0; j < nElems; j++){ | |
System.out.print(arr[j] + " "); | |
} | |
System.out.println(); | |
// search element in array | |
searchKey = 66; | |
for (j = 0; j < nElems; j++){ | |
if(arr[j] == searchKey){ | |
break; | |
} | |
} | |
// delete element | |
for (int k = j; k < nElems; k++){ | |
arr[k] = arr[k+1]; | |
} | |
nElems--; | |
// display current array element | |
for (j = 0; j < nElems; j++){ | |
System.out.print(arr[j] + " "); | |
} | |
System.out.println(); | |
} //end main() | |
} //end class arrayapp | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Class Person | |
* Description : | |
* This class represents person object. | |
* | |
* Methods : | |
* public void displayPerson() | |
* public String getLastName() | |
* */ | |
class Person | |
{ | |
private String lastName; | |
private String firstName; | |
private int age; | |
public Person(String last, String first, int B){ | |
lastName = last; | |
firstName = first; | |
age = B; | |
} | |
public void displayPerson(){ | |
System.out.println("Last name : "+lastName); | |
System.out.println("First name : "+firstName); | |
System.out.println("Age : "+age); | |
} | |
public String getLast(){ | |
return lastName; | |
} | |
} | |
class ClassDataArray{ | |
private Person[] B; | |
private int nElems; | |
public ClassDataArray(int max){ | |
B = new Person[max]; | |
nElems = 0; | |
} | |
public Person find(String searchName){ | |
int j; | |
for(j = 0; j < nElems; j++) | |
if(B[j].getLast().equals(searchName)) | |
break; | |
if(j == nElems) | |
return null; | |
else | |
return B[j]; | |
} | |
public void insert(String last,String first,int age){ | |
B[nElems] = new Person(last,first,age); | |
nElems++; | |
} | |
public boolean delete(String searchName){ | |
int j; | |
for(j = 0; j < nElems; j++) | |
if(B[j].getLast().equals(searchName)) | |
break; | |
if(j == nElems) | |
return false; | |
else{ | |
for(int k=j; k < nElems; k++) | |
B[k] = B[k+1]; | |
nElems--; | |
return true; | |
} | |
} | |
public void display(){ | |
for(int j = 0; j < nElems; j++) | |
B[j].displayPerson(); | |
} | |
} | |
public class ClassDataApp{ | |
public static void main(String[] args){ | |
int maxSize = 100; | |
ClassDataArray arr; | |
arr = new ClassDataArray(maxSize); | |
//Insert Elements | |
arr.insert("Evans", "Patty", 24); | |
arr.insert("Smith", "Loraine", 37); | |
arr.insert("Yee", "Tom", 43); | |
arr.insert("Adams", "Henry", 63); | |
arr.insert("Hashimoto", "Sato", 21); | |
arr.insert("Stimson", "Henry", 29); | |
arr.insert("Velasquez", "Jose", 72); | |
arr.insert("Lamarque", "Henry", 54); | |
arr.insert("Vang", "Minh", 22); | |
arr.insert("Creswell", "Lucinda", 18); | |
//Display Elements | |
arr.display(); | |
//Search Element | |
String searchKey = "Stimson"; | |
Person found; | |
found = arr.find(searchKey); | |
if(found !=null){ | |
System.out.println("Found "); | |
found.displayPerson(); | |
} | |
else | |
System.out.println("Can't find "+searchKey); | |
//Delete Elements | |
System.out.println("Deleting Smith, Yee, and Creswell"); | |
arr.delete("Smith"); | |
arr.delete("Yee"); | |
arr.delete("Creswell"); | |
//Display Elements | |
arr.display(); | |
} | |
} // class ClassDataApp |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* High Array | |
* | |
* @author Afira Rolobessy | |
* | |
*/ | |
public class HighArray | |
{ | |
/** | |
* Constructor | |
* Description : | |
* Constructor will create new array with a specific size. | |
* | |
* @param size size of the array. | |
* */ | |
public HighArray(int size){ | |
array = new long[size]; | |
nElems = 0; | |
} | |
/** | |
* Method find | |
* Description : | |
* This method is used to search element from the array. | |
* | |
* @param searchKey key to be looked for from the array. | |
* @return true if key found, else false. | |
* */ | |
public boolean find(long searchKey){ | |
int j; | |
for(j = 0; j <nElems; j++) | |
if(a[j] == searchKey) | |
break; | |
if(j == nElems) | |
return false; | |
else | |
return true; | |
} | |
public void insert(long value){ | |
a[nElems] = value; | |
nElems++; | |
} | |
public boolean delete(long value){ | |
int j; | |
for(j = 0; j < nElems; j++) | |
if(value == a[j]) | |
break; | |
if(j == nElems) | |
return false; | |
else{ | |
for(int k=j; k < nElems; k++) | |
a[k] = a[k+1]; | |
nElems--; | |
return true; | |
} | |
} | |
/** | |
* Method display | |
* Description : | |
* This method is used to display all elements of the array. | |
* */ | |
public void display(){ | |
for(int j = 0; j < nElems; j++) | |
System.out.print(a[j]+" "); | |
System.out.println(" "); | |
} | |
} | |
public class HighArrayApp{ | |
public static void main(String[] args){ | |
int maxSize = 100; | |
HighArray arr; | |
arr = new HighArray(maxSize); | |
arr.insert(77); | |
arr.insert(99); | |
arr.insert(44); | |
arr.insert(55); | |
arr.insert(22); | |
arr.insert(88); | |
arr.insert(11); | |
arr.insert(0); | |
arr.insert(66); | |
arr.insert(33); | |
arr.display(); | |
//Search Element | |
int searchKey = 35; | |
if(arr.find(searchKey)) | |
System.out.println("Found "+searchKey); | |
else | |
System.out.println("Can't find "+searchKey); | |
//Delete Elements | |
arr.delete(0); | |
arr.delete(55); | |
arr.delete(99); | |
//Display Elements | |
arr.display(); | |
} //end main() | |
} //end class arrayapp |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* High Array | |
* | |
* @author Afira Rolobessy | |
* | |
*/ | |
public class OrderedArray | |
{ | |
private long[] a; | |
private int nElems; | |
public OrderedArray(int max){ | |
a = new long[max]; | |
nElems = 0; | |
} | |
public int size(){ | |
return nElems; | |
} | |
public int find(long searchKey){ | |
int lowerBound = 0; | |
int upperBound = nElems - 1; | |
int curln; | |
while(true){ | |
curln = (lowerBound + upperBound) / 2; | |
if(a[curln] == searchKey) | |
return curln; | |
else if(lowerBound > upperBound) | |
return nElems; | |
else{ | |
if(a[curln] < searchKey) | |
lowerBound = curln + 1; | |
else | |
upperBound = curln - 1; | |
} | |
} | |
} | |
public void insert (long value){ | |
int j; | |
for(j = 0; j < nElems; j++) | |
if(a[j] > value) | |
break; | |
for(int k = nElems; k > j; k--) | |
a[k] = a[k-1]; | |
a[j] = value; | |
nElems++; | |
} | |
public boolean delete(long value){ | |
int j = find(value); | |
if(j == nElems) | |
return false; | |
else{ | |
for(int k=j; k < nElems; k++) | |
a[k] = a[k+1]; | |
nElems--; | |
return true; | |
} | |
} | |
public void display(){ | |
for(int j=0; j < nElems; j++) | |
System.out.print(a[j]+" "); | |
System.out.println(" "); | |
} | |
class OrderedArrayApp { | |
/** | |
* Main Method | |
* Description : | |
* This method is used as main method. | |
* | |
* @param args arguments to the console app while compiled and launched. | |
* */ | |
} | |
public class OrderedArrayApp{ | |
//Initialize the array | |
public static void main(String[] args){ | |
int maxSize = 100; | |
OrderedArray arr; | |
arr = new OrderedArray(maxSize); | |
arr.insert(77); | |
arr.insert(99); | |
arr.insert(44); | |
arr.insert(55); | |
arr.insert(22); | |
arr.insert(88); | |
arr.insert(11); | |
arr.insert(0); | |
arr.insert(66); | |
arr.insert(33); | |
//Search Element | |
int searchKey = 55; | |
if(arr.find(searchKey) != arr.size()) | |
System.out.println("Found "+searchKey); | |
else | |
System.out.println("Can't find "+searchKey); | |
//Display Elements | |
arr.display(); | |
//Delete Elements | |
arr.delete(0); | |
arr.delete(55); | |
arr.delete(99); | |
//Display Elements After Removing Some Elements | |
arr.display(); | |
} | |
} // end class OrderedApp |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Class Data Array | |
* | |
* @author Afira Rolobessy | |
* | |
*/ | |
public class Person | |
{ | |
private String lastName; | |
private String firstName; | |
private int age; | |
/** | |
* Constructor | |
* Description : | |
* Constructor will create new Person with the given lastname, firstname, and age. | |
* | |
* @param last lastname of the person. | |
* @param first firstname of the person. | |
* @param age age of the person. | |
* */ | |
public Person(String last, String first, int a){ | |
lastName = last; | |
firstName = first; | |
age = a; | |
} | |
public void displayPerson(){ | |
System.out.println("Last name : "+lastName); | |
System.out.println("First name : "+firstName); | |
System.out.println("Age : "+age); | |
} | |
public String getLast(){ | |
return lastName; | |
} | |
} | |
class ClassDataArray{ | |
private Person[] a; | |
private int nElems; | |
public ClassDataArray(int max){ | |
a = new Person[max]; | |
nElems = 0; | |
} | |
public Person find(String searchName){ | |
int j; | |
for(j = 0; j < nElems; j++) | |
if(a[j].getLast().equals(searchName)) | |
break; | |
if(j == nElems) | |
return null; | |
else | |
return a[j]; | |
} | |
/** | |
* Method displayPerson | |
* Description : | |
* This method will display all information of the person (firstname, lastname, and age). | |
* */ | |
public void insert(String last,String first,int age){ | |
a[nElems] = new Person(last,first,age); | |
nElems++; | |
} | |
public boolean delete(String searchName){ | |
int j; | |
for(j = 0; j < nElems; j++) | |
if(a[j].getLast().equals(searchName)) | |
break; | |
if(j == nElems) | |
return false; | |
else{ | |
for(int k=j; k < nElems; k++) | |
a[k] = a[k+1]; | |
nElems--; | |
return true; | |
} | |
} | |
public void displayA(){ | |
for(int j = 0; j < nElems; j++) | |
a[j].displayPerson(); | |
} | |
} | |
public class ClassDataApp{ | |
public static void main(String[] args){ | |
int maxSize = 100; | |
ClassDataArray arr; | |
arr = new ClassDataArray(maxSize); | |
//Insert Elements | |
arr.insert("Evans", "Patty", 24); | |
arr.insert("Smith", "Loraine", 37); | |
arr.insert("Yee", "Tom", 43); | |
arr.insert("Adams", "Henry", 63); | |
arr.insert("Hashimoto", "Sato", 21); | |
arr.insert("Stimson", "Henry", 29); | |
arr.insert("Velasquez", "Jose", 72); | |
arr.insert("Lamarque", "Henry", 54); | |
arr.insert("Vang", "Minh", 22); | |
arr.insert("Creswell", "Lucinda", 18); | |
//Display Elements | |
arr.displayA(); | |
//Search Element | |
String searchKey = "Stimson"; | |
Person found; | |
found = arr.find(searchKey); | |
if(found !=null){ | |
System.out.println("Found "); | |
found.displayPerson(); | |
} | |
else | |
System.out.println("Can't find "+searchKey); | |
//Delete Elements | |
System.out.println("Deleting Smith, Yee, and Creswell"); | |
arr.delete("Smith"); | |
arr.delete("Yee"); | |
arr.delete("Creswell"); | |
//Display Elements | |
arr.displayA(); | |
} | |
} // and class Class |
Komentar
Posting Komentar