0% found this document useful (0 votes)
4 views11 pages

Java Program for Set Operations

The program allows the user to input elements into two sets (A and B) and then calculates the intersection of the two sets. It prompts the user to enter the number of elements for each set and then input each element. It then prints out the two sets and their intersection set. The user is prompted to run it again or terminate.

Uploaded by

Ova Amalia
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views11 pages

Java Program for Set Operations

The program allows the user to input elements into two sets (A and B) and then calculates the intersection of the two sets. It prompts the user to enter the number of elements for each set and then input each element. It then prints out the two sets and their intersection set. The user is prompted to run it again or terminate.

Uploaded by

Ova Amalia
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Adinda Fitra Rahmawanti Dewi

11150930000002
Array 1
package array1;

/**

* @author E11

*/

import [Link];

public class Array1 {

/**

* @param args the command line arguments

*/

public static void main(String[] args) {

Scanner masuk = new Scanner ([Link]);

int i, n;

String pil;

do{

[Link]("Masukkan Banyak Unsur Himpunan A = ");

n = [Link]();

int angka[] = new int [n];

for(i=0; i<n; i++){

[Link]("Masukkan Unsur Ke-" +(i+1)+ " = ");

angka[i] = [Link]();

[Link]("Himpunan A = {");
for(i=0; i<n; i++){

if(i==(n-1)){

[Link](angka[i]+ "}");

else{

[Link](angka[i]+ ", ");

[Link]("Lagi (y/t) : ");

pil = [Link]();

[Link]("===================================");

[Link]("");

}while([Link] ("y"));

}
run:

Masukkan Banyak Unsur Himpunan A = 2

Masukkan Unsur Ke-1 = 3

Masukkan Unsur Ke-2 = 4

Himpunan A = {3, 4}

Lagi (y/t) : y

===================================

Masukkan Banyak Unsur Himpunan A = 4

Masukkan Unsur Ke-1 = 5

Masukkan Unsur Ke-2 = 2

Masukkan Unsur Ke-3 = 3

Masukkan Unsur Ke-4 = 1

Himpunan A = {5, 2, 3, 1}

Lagi (y/t) : t

===================================

BUILD SUCCESSFUL (total time: 16 seconds)


Array 2
package array2;

/**

* @author E11

*/

import [Link];

public class array2a {

public static void main(String [] args){

Scanner masuk = new Scanner ([Link]);

int n, m, i, j, c, d;

int a = 0;

int z = 0;

String pil;

[Link](" Irisan Dalam Himpunan ");

do{

[Link]("======================================");

[Link]("");

[Link]("Jumlah Unsur Untuk Himpunan A = ");

n = [Link]();

int A[] = new int [n];

for(i=0; i<n; i++){

[Link]("Masukkan Unsur Ke-" +(i+1)+ " = ");

A[i] = [Link]();

}
[Link]("");

[Link]("Jumlah Unsur Untuk Himpunan B = ");

m = [Link]();

int B[] = new int [m];

for(j=0; j<m; j++){

[Link]("Masukkan Unsur Ke-" +(j+1)+ " = ");

B[j] = [Link]();

[Link]("");

[Link]("Himpunan A = {");

for(i=0; i<n; i++){

if(i==(n-1)){

[Link](A[i]+ "}");

else{

[Link](A[i]+ ", ");

[Link]("Himpunan B = {");

for(j=0; j<m; j++){

if(j==(m-1)){

[Link](B[j]+ "}");

else{

[Link](B[j]+ ", ");

}
[Link]("Irisan Himpunan A dan B = {");

for(i=0; i<n; i++){

for(j=0;j<m; j++){

if(A[i] == B[j]){

if(z==0){

[Link](A[i]);

z = z + 1;

else{

[Link](", " + A[i]);

[Link]("}");

[Link]("Lagi (y/t) : ");

pil = [Link]();

z = 0;

}while([Link] ("y"));

}
run:

Irisan Dalam Himpunan

======================================

Jumlah Unsur Untuk Himpunan A = 2

Masukkan Unsur Ke-1 = 3

Masukkan Unsur Ke-2 = 4

Jumlah Unsur Untuk Himpunan B = 4

Masukkan Unsur Ke-1 = 3

Masukkan Unsur Ke-2 = 2

Masukkan Unsur Ke-3 = 4

Masukkan Unsur Ke-4 = 5

Himpunan A = {3, 4}

Himpunan B = {3, 2, 4, 5}

Irisan Himpunan A dan B = {3, 4}

Lagi (y/t) : y

======================================

Jumlah Unsur Untuk Himpunan A = 4

Masukkan Unsur Ke-1 = 5

Masukkan Unsur Ke-2 = 8

Masukkan Unsur Ke-3 = 6

Masukkan Unsur Ke-4 = 9

Jumlah Unsur Untuk Himpunan B = 2

Masukkan Unsur Ke-1 = 3

Masukkan Unsur Ke-2 = 5

Himpunan A = {5, 8, 6, 9}
Himpunan B = {3, 5}

Irisan Himpunan A dan B = {5}

Lagi (y/t) : t

BUILD SUCCESSFUL (total time: 25 seconds)

You might also like