Array
*** ARRAY
/*
* To change this
template, choose Tools | Templates
* and open the
template in the editor.
*/
package modul5;
/**
*
* @author anik nurul
*/
public class tugas1A {
public static void
main (String []args){
int data
[]={4,6,4,2,8,4,2,10};
for (int a=0;
a<data.length; a++)
{
System.out.print(data[a]+" ");
}
}
}
*** ARRAY INPUT
/*
* To change this
template, choose Tools | Templates
* and open the
template in the editor.
*/
package modul5;
/**
*
* @author anik nurul
*/
import javax.swing.*;
public class tugas1B {
public static void
main(String[] args) {
int data []=new int
[5];
for (int a=0;
a<=data.length;a++)
{
data[a]=Integer.parseInt(JOptionPane.showInputDialog(null,"Masukkan
Index ke "+a));
System.out.println("Index ke "+a+" adalah
"+data[a]);
}
}
}
*** MENCARI MAX MIN DAN POSISI INDEXNYA
/*
* To change this
template, choose Tools | Templates
* and open the
template in the editor.
*/
package modul5;
/**
*
* @author anik nurul
*/
import javax.swing.*;
public class tugas1B {
public static void
main(String[] args) {
int data []=new int
[5];
for (int a=0; a<=data.length;a++)
{
data[a]=Integer.parseInt(JOptionPane.showInputDialog(null,"Masukkan
Index ke "+a));
System.out.println("Index ke "+a+" adalah
"+data[a]);
}
}
}
*** MENCARI TOTAL NILAI DAN RATA RATA
/*
* To change this template,
choose Tools | Templates
* and open the
template in the editor.
*/
package modul5;
/**
*
* @author anik nurul
*/
public class tugas2B {
public static
void main(String[] args) {
int data[]={4, 6, 4, 2, 8, 4, 2, 10};
int i= data[0];
//System.out.println(i);
int jumlah = 0;
int rata=0;
for (int a=0;
a<data.length;a++){
if
(i<data[a]);
i=data[a];
if
(i>data[a]);
i=data[a];
jumlah =
jumlah+data[a];
}
rata=jumlah/data.length;
System.out.println("Total
= "+jumlah+"\nRata-rata = "+rata);
}
}
*** MENCARI MAX DAN MIN DENGAN ARAY YANG DI INPUTKAN
/*
* To change this
template, choose Tools | Templates
* and open the
template in the editor.
*/
package modul5;
/**
*
* @author anik nurul
*/
import javax.swing.*;
public class tugas2C {
public static void main(String[]args){
int data[]=new
int[5];
int a=0;
int max=0;
int min=100;
int total=0;
int rata2=0;
for(a=0;
a<data.length; a++)
{
data[a]=Integer.parseInt(JOptionPane.showInputDialog
(null,"Masukkan
nilai elemen indeks ke"+a));
System.out.println("Index ke "+a+" adalah
"+data[a]);
}
for(a=0;
a<data.length; a++)
{
if(data[a]>max)
{ max= data[a]; }
if(data[a]<min)
{ min= data[a]; }
total=total+data[a];
}
rata2=total/data.length;
System.out.print("Nilai maximum adalah "+max);
System.out.print("\nNilai minimal adalah "+min);
System.out.print("\nRata-rata nilai adalah "+rata2);
}
}
*** MEMBUAT MATRIK
/*
* To change this
template, choose Tools | Templates
* and open the
template in the editor.
*/
package modul5;
/**
*
* @author anik nurul
*/
import javax.swing.*;
public class tugas3A {
public static void
main(String[] args) {
int data[] =
new int[3];
for (int a
= 0; a < data.length; a++) {
data[a] = Integer.parseInt(JOptionPane.showInputDialog(null,
"Masukkan Index ke " + a));
System.out.print("["+data[a]+"]"+"
");
}
}
}
*** MENCARI MAX DAN MIN DENGAN ARAY YANG DI INPUTKAN
/*
* To change this
template, choose Tools | Templates
* and open the
template in the editor.
*/
package modul5;
/**
*
* @author anik nurul
*/
import javax.swing.*;
public class tugas3C {
public static void
main(String[] args) {
int data[] =
new int[3];
for (int a
= 0; a < data.length; a++) {
data[a]
= Integer.parseInt(JOptionPane.showInputDialog(null, "Masukkan Index ke
" + a));
System.out.println("Index ke " + a + " adalah " +
data[a]);
}
int min =
data[0];int max=0;int indeks1=0,indeks2=0;
for (int i =
0; i < data.length; i++) {
if (max
< data[i]) {
max =
data[i];
indeks1 = i;
}
if (min
> data[i]) {
min =
data[i];
indeks2
=i;
}
}
System.out.println("Nilai minimal = "+min+"
indek ke- "+indeks1);
System.out.println("Nilai maximal = "+max+" indek ke-
"+indeks2);
}}
*** MENCARI RATA-RATA DENGAN ARAY YANG DI INPUTKAN
/*
* To change this
template, choose Tools | Templates
* and open the
template in the editor.
*/
package modul5;
/**
*
* @author anik nurul
*/
import javax.swing.*;
public class tugas3D {
public static void
main(String[] args) {
int data[] = new
int[5];
for (int a
= 0; a < data.length; a++) {
data[a] = Integer.parseInt(JOptionPane.showInputDialog(null,
"Masukkan Index ke " + a));
System.out.println("Index ke " + a + " adalah " +
data[a]);
}
int min =
data[0];int max=0;int indeks1=0,indeks2=0; int jumlah=0; int rata=0;
for (int i =
0; i < data.length; i++) {
if (max
< data[i]) {
max =
data[i];
indeks1 = i;
}
if (min
> data[i]) {
min =
data[i];
indeks2
=i;
}
//System.out.println(min+" "+indeks1);
// System.out.println(max+"
"+indeks2);
jumlah
= jumlah+data[i];
}rata=jumlah/data.length;
System.out.println("rata-rata = "+rata);
}
}
*** MENGURUTKAN LARIK
/*
* To change this
template, choose Tools | Templates
* and open the
template in the editor.
*/
package modul5;
/**
*
* @author anik nurul
*/
import
javax.swing.JOptionPane;
public class tugas3E {
public static void
main(String[] args) {
int[] larikInt =
new int [4];
for (int b =
0; b < larikInt.length; b++) {
larikInt[b] = Integer.parseInt(JOptionPane.showInputDialog(null,
"Masukkan Element Index Ke " + b));
System.out.println("Index Ke " + b + " Adalah " +
larikInt[b]);
}
//
Mengurutkan larik
Elemen(larikInt);
// Menampilkan
elemen larik setelah diurutkan
System.out.println("\nElemen larik setelah diurutkan :");
for (int x =
0; x < larikInt.length; x++) {
System.out.print(larikInt[x] + "
");
}
// Menambah
spasi baris kosong
//
System.out.println();
} // Akhir blok
metoda main
// Metoda
mengurutkanElemen
public static void
Elemen(int[] larikA) {
for (int x =
larikA.length - 1; x >= 1; x--) {
for (int j
= 0; j <= x; j++) {
if
(larikA[j] > larikA[x]) {
menukar(larikA, j, x);
}
}
}
}
// metoda menukar
public static void
menukar(int[] larikB, int indekj, int indekx) {
int sementara;
sementara = larikB[indekx];
larikB[indekx]
= larikB[indekj];
larikB[indekj]
= sementara;
}
} // Akhir blok kelas
Comments
Post a Comment