Lap Activity jeni 6

import java.awt.Graphics;
import java.ArrayList;
import javax.swing.JFrame;
public class ArrayListCoba extends JFrame{
public static void main(String args[]){
new ArrayListCoba();
}
//membuat object arraylist
ArrayList al=new ArrayList();
ArrayListCoba(){
al.add(“test array list”);
al.add(1);
this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);
this.setSize(500, 500);
this.setVisible(true);
setTitle(“Test Array List”);
}
public void paint (Graphics g){
g.drawString(“banyaknya element di ArrayList adalah : “+String.valueOf(al.size()), 10, 50);
g.drawString(“Isinya adalah sebagai berikut : “, 10, 100);
g.drawString(“element ke-0 : “+(String) al.get(0), 10, 150);
g.drawString(“Element ke-1 : “+String.valueOf( al.get(1)), 10, 200);
al.remove(0);
g.drawString(“banyaknya element di ArrayList setelah dihapus adalah: “+String.valueOf(al.size()), 10, 250);
g.drawString(“Element ke1 adalah : “+String.valueOf( al.get(1)), 10, 300);
g.drawString(“Method isEmpty() : “+String.valueOf(al.isEmpty()), 10, 350);
}
}

#———————————————————————————————

import java.awt.Graphics;
import java.util.LinkedList;
import javax.swing.JFrame;
public class PercobaanLinkedList extends JFrame {
public static void main(String args[]){
new PercobaanLinkedList();
}
LinkedList ll=new LinkedList();
PercobaanLinkedList(){
ll.add(“test linkedList”);
ll.add(“coba lagi”);
ll.add(10243);
this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);
this.setSize(500, 500);
this.setVisible(true);
setTitle(:Test LinkedList”);
}
public void paint (Graphics g){
g.drawString(“Jumlah Element : “+String.valueOf(ll.size()), 10, 50);
g.drawString(“Element ke-1 : “+ll.get(0), 10, 100);
g.drawString(“Element ke-2 : “+ll.get(1), 10, 150);
g.drawString(“Element ke-3 : “+ll.get(2), 10, 200);
g.drawString(“Method getFirst : “+ll.getFirst(), 10, 250);
g.drawString(“Method getLast : “+ll.getLast(), 10, 300);
ll.addLast(“Terakhir”);
g.drawString(“Method getLast : “+ll.getLast(), 10, 350);
ll.remove();
g.drawString(“Jumlah Element Sekarang : “+String.valueOf(ll.size()), 10, 400);
}
}

#———————————————————————————————–

import java.awt.Graphics;
import java.util.Vector;
import javax.swing.JFrame;

public class PercobaanVektor extends Jframe{
public static void main(String args[]){
new PercobaanVektor();
}
Vector vk;
PercobaanVektor(){
vk = new Vector();
vk.add(“test vektor”);
vk.addElement(“Coba Add lagi”);
this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);
this.setSize(500, 500);
this.setVisible(true);
setTitle(“test vektor”);
}
public void paint(Graphics g){
g.drawString(“jumalah Element Vektor : “+String.valueOf(vk.size()), 10, 50);
g.drawString(“element vektor ke-0 : “+vk.elementAt(0), 10, 100);
g.drawString(“element vektor ke-1 : “+vk.elementAt(1), 10, 150);
g.drawString(“Element vektor pertama : “+vk.fistElement(), 10, 200);
g.drawString(“element vektor terakhir : “+vk.lastElement(), 10, 250);
g.drawString(“Method isEmpty : “+vk.isEmpty(), 10, 300);
vk.clear();
g.drawString(“method isEmpty : “+vk.isEmpty(), 10, 350);
}

#——————————————————————————————-

import java.util.*;
public class TestEaitNotifyThread {
public ThreadA ta;
public ThreadB tb;
private String msg;
public TestWaitNotifyThread(){
ta = new ThreadA();
tb = new ThreadB();
}
class ThreadA implements Runnable{
Thread th= new Thread(this);
public Thread(){
th.start();
}
public void run() {
int i=0;
while(i<100) {
try{
Thread.sleep(1000);
}catch (Exception e) {}
setMessage(“Hi”+i);
i++;
}
}
}
class ThreadB implements Runnable{
Thread th= new Thread(this);
public ThreadB(){
th.start();
}
public void run() {
int i=0;
while(true) {
waitForMessage();
System.out.println(msg);
i++
}
}
}
public synchronized void set Message(String message) {
msg=message;
System.out.println(“set message : “+msg);
notify();
}
public synchronized void waitForMessage() {
try {
wait();
}
catch (InterruptedException ex) {
}
}
public static void main (String arg[]) {
new TestWaitNotifyThread();
}
}

#———————————————————————————————–

import java.util.*;
public class TestSynchronizedThread {
private ArrayList<String> messageList;
private ThreadA ta;
private ThreadB tb;
public TestSynchronizedThread(){
messegeList = new ArrayList<String>();
ta = new ThreadA();
tb = new ThreadB();
}

class ThreadA implements Runnable{
Thread th= new Thread(this);
public ThreadA(){
th.start();
}
public void run() {
int i=0;
while(i<100){
try{
Thread.sleep(20);
} catch (Exception e) {}
/*
syschronized(messegeList){
messegeList.add(“Hi “+i);
}
/*
messegeList.add(“Hi “+i);
System.out.println(“adding messege : Hi”+i);
i++
}
}
}

class ThreadB implement Runnable{
Thread th= new Thread(this);
public ThreadB(){
th.start();
}
public void run() {
int i=0;
while(i<50){
try{
Thread.sleep(40);
}catch(Exception e){}
/*
synchronized(messegeList){
for(int j=0;j<MessegeList.get(j));
System.out.println(messegeList.get(j));
}
*/
for(int j=0;j<messegeList.size();j++)
System.out.println(messegeList.get(j));
i++;
}
}
}

public static void main(String arg[]){
new TestWaitNotifyThread();
}
}

#———————————————————————————————

public class TestThread implements Runnable{
private Thread th;
private boolean running;
private long endTime;
public TestThread(){
running=true;
th = new Thread (this);
endTime= System.currentTimeMillis()+10000;
}
public void run() {
int i=0;
while(running){
try {
Thread.sleep(1000);
} catch (Exception e) {}
System.out.println(“Run “+i)
if (System.currenttimeMillis()>endTime)
running=false;
i++;
}
}
public void start(){
th.start();
}
public static void main (String [] agr){
new TestThread().start();
}
}

Tinggalkan Balasan

Isikan data di bawah atau klik salah satu ikon untuk log in:

Logo WordPress.com

You are commenting using your WordPress.com account. Logout / Ubah )

Gambar Twitter

You are commenting using your Twitter account. Logout / Ubah )

Foto Facebook

You are commenting using your Facebook account. Logout / Ubah )

Foto Google+

You are commenting using your Google+ account. Logout / Ubah )

Connecting to %s