Beruflich Dokumente
Kultur Dokumente
Minggu 8
Mochamad Rezza Gumilang
Review Questions:
1. What is the difference between ArrayList and Vector?
2.
3. A call to a method with a void return type is always a statement itself, but a call to a
value-returning method is always a component of an expression. Is this statement true or
false?
4. What does JVM do when an exception occurs? How do you catch an exception?
5. What is a checked exception and what is unchecked exception?
Programming Exercise:
A program is required to receive a set of data that inserted by a user. The data
contains with item_id, item_name, and the price. After the user inserted all of the data, the
program shows some menus:
1. Show the data
2. Add data
3. Remove the data
4. Update the data
5. Exit
If a user chooses number 1, the program will show the data as the example below:
Item_id
item_name
price
pencil
10 $
book
15 $
eraser
5 $
If a user chooses number 2, the program will ask a user to insert a new data (item_id,
item_name, and price). The same item_id should not be inserted twice (no redundant
item_id). The new data will be inserted after a user finished inserting the data.
If a user chooses number 3, the program will ask the user to insert the item_id wanted to
be deleted.
If a user chooses number 4, the program will ask the user to insert the item_id wanted to
be updated
program that you created should apply ArrayList or Vector, selection, repetition, exception
handling for the inserted data type and method.
JAWABAN
1. ArrayList merupakan bagian dari Collection API dimana ArrayList implements List dan List
sendiri extends Collection sedangkan Vector Turunan dari interface collection.
2. Menurut saya return main method itu tidak memberikan nilai return atau nilai balikan
Programming Exercise:
Code
package test;
import java.util.ArrayList;
import java.util.Scanner;
/**
*
* @author Mochamad Rezza Gumilang
*/
public class TP4 {
private ArrayList<Data> List = new ArrayList<Data>();
private Scanner sc;
public void create(){
showMenu();
}
private int showData(int pWhare, boolean isShowMenu){
System.out.format("%1s%20s%16s", "ID ", "ITEM NAME", "PRICE"+"\n");
System.out.println("========================================");
if (pWhare != -1){
System.out.format("%1s%20s%16s",List.get(pWhare).id,List.get(pWhare).item_name,
List.get(pWhare).price+" $"+"\n");
}
else {
for (int i=0; i<List.size(); i++){
System.out.format("%1s%20s%16s", List.get(i).id, List.get(i).item_name, List.get(i).price +" $"+"\n");
}
}
if (isShowMenu){
showMenu();
}
return 0;
}
private int addData(){
sc = new Scanner(System.in);
int status = 0;
try {
System.out.print("Id : ");
int id = sc.nextInt();
System.out.print("Item Name : ");
String name = sc.next();
System.out.print("Price : ");
long price = sc.nextLong();
if (isValue(id) == -1){
List.add(new Data(id, name, price));
status = 1;
System.out.println("Input Success");
}
else{
System.out.println("Duplicate Id");
}
} catch (Exception e) {
System.out.println("Error Input data");
}
showMenu();
return status;
}
private int deleteData(){
sc = new Scanner(System.in);
System.out.print("Please Insert item_id wanted to be deleted : ");
int pId = sc.nextInt();
System.out.println("item id "+pId+" wanted to be deleted (true/false)");
boolean isDelete = sc.nextBoolean();
int status = 0;
if (isDelete){
if (isValue(pId) != -1) {
List.remove(isValue(pId));
status = 1;
System.out.println("Delete Success");
}
else {
System.out.println("Not Found Data ");
}
}
showMenu();
return status;
}
private int updateData(){
sc = new Scanner(System.in);
System.out.print("Place insert the item_id wanted to be updated : ");
int id = sc.nextInt();
boolean isFound = false;
int status = 0;
if (isValue(id) != -1) {
id = isValue(id);
showData(id, false);
System.out.println("===============================================");
System.out.print("Item Name : ");
String name = sc.next();
System.out.print("Price : ");
long price = sc.nextLong();
List.get(id).item_name = name;
List.get(id).price
= price;
status = 1;
System.out.println("Update Success");
}
else {
System.out.println("Not Found Data ");
status = 0;
}
showMenu();
return status;
}
private void showMenu(){
sc = new Scanner(System.in);
System.out.println("");
System.out.println("
Menu");
System.out.println("================");
System.out.println("1. Show the data");
System.out.println("2. Add data");
System.out.println("3. Remove the data");
System.out.println("4. Update the data");
System.out.println("5. Exit");
System.out.println("");
System.out.print("Choose Menu : ");
int menu = sc.nextInt();
switch (menu){
case 1:
showData(-1, true);
break;
case 2:
addData();
break;
case 3:
deleteData();
break;
case 4:
updateData();
break;
case 5:
System.exit(0);
break;
default:
System.exit(0);
break;
}
}
private int isValue(int pId){
int status = -1;
OUTPUT