Dibawah Ini merupakan source code dari soal buku Panduan Buku Praktikum Java Bab 4

NO 1 :

import java.util.List;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.Collections;

public class bab41 {
public static void main(String[] args) {
ArrayList nama=new ArrayList();
Scanner input=new Scanner(System.in);
String namaa;
int n;
System.out.print("Masukkan Banyak Data = ");
n=input.nextInt();
input=new Scanner(System.in);
for(int i=0;i<n;i++)
{
System.out.print("Nama ");
namaa=input.nextLine();
nama.add(namaa);
}
//mengurutkan data array list
Collections.sort(nama);
System.out.println("Hasil Pengurutan = ");
for (int i=0;i<nama.size();i++)
{
System.out.println(""+ nama.get(i) + " = "+ nama.get(i).toString().length() );
}
}
}

no 2 :

import java.util.Scanner;
import java.util.Arrays;
public class bab42 {

public static void main(String[] args) {
Scanner input=new Scanner(System.in);
int bil[],temp[][],n,jumlah=0;
double rata2,median;
temp=new int[100][100];
System.out.print("Banyak Data = ");
n=input.nextInt();
bil=new int[n];
for (int i=0;i<n;i++)
{
System.out.printf("Data ke %d = ",i);
bil[i]=input.nextInt();
jumlah=jumlah+ bil[i];
}
Arrays.sort(bil);
if (n%2==0)
median=(double)(bil[2
4
1n/2]+bil[n/2+1])/2;
else
median=bil[(int)n/2];
System.out.println("Daftar Bilangan = ");
for (int i=0;i<n;i++)
{
System.out.printf("Bil ke %d = %d \n",i,bil[i]);
}
rata2=jumlah/n;
System.out.println("Rata - Rata = "+ rata2);
System.out.println("Median = "+ median);
System.out.print("Bilangan Prima = ");
boolean cek=false;
for (int i=0;i<n;i++)
{
if (bil[i]==2)
System.out.print(""+bil[i]);
else
for (int j=2;j<bil[i];j++)
{
if (bil[i] % j == 0)
{
cek=false;
break;
}
else
cek=true;
}
if (cek)
System.out.print(""+bil[i]);
}
System.out.println("");
}
}

Nb :
– Sesuaikan Penamaan Nama file denga nama class,
– Tanyakan Jika Kurang Mengerti.

About these ads