JAWABAN SOAL UJIAN TENGAH SEMESTER
MATA KULIAH DASAR-DASAR PEMROGRAMAN
Nama : Syafrudin M Top
Kelas : A
Jawaban soal Ujian Tengah Semester dasar dasar pemrograman java tahun 2013 Teknik informatika Universitas Islam Negeri Malang
Dosen Pengajar : A'la Syauqi
- program yang menghasilkan deret bilangan genap, ganjil, dan prima dengan batas atas atas dan batas bawah yang diperoleh dari input user. Dengan menggunakan perulangan kita bias mencari deret bilangan ganjil, genap, dan bilangan prima.
Source codenya:
package UTS_Daspro |
/* |
* To change this template, choose Tools | Templates |
* and open the template in the editor. |
*/ |
import java.io.*; |
/** |
* |
* @author syafrudin |
*/ |
public class nomor1 { |
public static void main(String[] args) throws IOException{ |
BufferedReader datain=new BufferedReader (new InputStreamReader(System.in)); |
|
System.out.print("masukkan batas bawah: "); |
String a=datain.readLine(); |
int aa=Integer.parseInt(a); |
System.out.print("masukkan batas atas: "); |
String b=datain.readLine(); |
int bb=Integer.parseInt(b); |
|
for (int i=aa+1;i<bb;i++){ |
if (i%2==0){ |
System.out.print(i+" ");} |
} |
System.out.println(); |
for (int i=aa+1;i<bb;i++){ |
if (i%2==1){ |
System.out.print(i+" ");} |
} |
System.out.println(); |
for (int i=aa+1;i<bb;i++){ |
if (i==2||i==3||i==5||i==7){ |
System.out.print(i+" ");} |
else if(i==1){ |
} |
else if(i!=2||i!=3){ |
if (i%2!=1 || i%3==0 || i%5==0 || i%7==0){ |
}else{ |
System.out.print(i+" "); |
} |
} |
} |
System.out.println(); |
} |
} |
- program untuk mensimulasikan penggunaan operator bitwise dengan operand yang diinputkan oleh user. Operator bitwise yang digunakan dipilih oleh user berdasarkan menu yang disediakan oleh program
source code:
import java.util.Scanner; |
/* |
* To change this template, choose Tools | Templates |
* and open the template in the editor. |
*/ |
/** |
* |
* @author syafrudin |
*/ |
public class nomor_2 { |
public static void main(String[] args) { |
int bil1 = 0; |
int bil2 = 0; |
int[] bitBilangan1 = new int[8]; |
int[] bitBilangan2 = new int[8]; |
int variabelMod; |
int indexBitBilangan1 = 0; |
int indexBitBilangan2 = 0; |
int pilihan = 0; |
Scanner inputDariUser = new Scanner(System.in); |
|
System.out.print("masukkan bilangan pertama [1 - 10]: "); |
bil1 = Integer.parseInt(inputDariUser.next()); |
System.out.print("masukkan bilangan kedua [1 - 10]: "); |
bil2 = Integer.parseInt(inputDariUser.next()); |
|
System.out.println("pilih salah satu operator berikut: "); |
System.out.println("1. &" + "\n2. |" + "\n3. ^" + "\n4. >>" + "\n5. <<" ); |
System.out.print("pilihan anda [1 - 5]: "); |
pilihan = Integer.parseInt(inputDariUser.next()); |
|
|
switch(pilihan) { |
case 1 : |
for(variabelMod = bil1; variabelMod>=1; ) { |
|
int angka1 = variabelMod % 2; |
bitBilangan1[indexBitBilangan1] = angka1; |
indexBitBilangan1++; |
variabelMod = variabelMod/2; |
} |
for(variabelMod = bil2; variabelMod>=1; ) { |
|
int angka2 = variabelMod % 2; |
bitBilangan2[indexBitBilangan2] = angka2; |
indexBitBilangan2++; |
variabelMod = variabelMod/2; |
} |
|
for(int index1=bitBilangan1.length-1; index1>-1; index1--) { |
System.out.print(bitBilangan1[index1] + "\t"); |
} |
System.out.println(bil1); |
System.out.println("-------------------------------------------------------------------"); |
|
for(int index2=bitBilangan2.length-1; index2>-1; index2--) { |
System.out.print(bitBilangan2[index2] + "\t"); } |
System.out.println(bil2); |
System.out.println("-------------------------------------------------------------------"); |
|
for(int indexHasil=bitBilangan1.length-1; indexHasil>-1; indexHasil--) { |
System.out.print((bitBilangan1[indexHasil] & bitBilangan2[indexHasil]) + "\t"); |
} |
System.out.print((bil1 & bil2) + "\t\n"); |
break; |
|
case 2: |
for(variabelMod = bil1; variabelMod>=1; ) { |
|
int angka1 = variabelMod % 2; |
bitBilangan1[indexBitBilangan1] = angka1; |
indexBitBilangan1++; |
variabelMod = variabelMod/2; |
} |
for(variabelMod = bil2; variabelMod>=1; ) { |
int angka2 = variabelMod % 2; |
bitBilangan2[indexBitBilangan2] = angka2; |
indexBitBilangan2++; |
variabelMod = variabelMod/2; |
} |
for(int index1=bitBilangan1.length-1; index1>-1; index1--) { |
System.out.print(bitBilangan1[index1] + "\t"); |
} |
System.out.println(bil1); |
System.out.println("-------------------------------------------------------------------"); |
for(int index2=bitBilangan2.length-1; index2>-1; index2--) { |
System.out.print(bitBilangan2[index2] + "\t"); |
} |
System.out.println(bil2); |
System.out.println("-------------------------------------------------------------------"); |
|
for(int indexHasil=bitBilangan1.length-1; indexHasil>-1; indexHasil--) { |
System.out.print((bitBilangan1[indexHasil] | bitBilangan2[indexHasil]) + "\t"); |
} |
System.out.print((bil1 | bil2) + "\t\n"); |
break; |
|
case 3: |
for(variabelMod = bil1; variabelMod>=1; ) { |
|
int angka1 = variabelMod % 2; |
bitBilangan1[indexBitBilangan1] = angka1; |
indexBitBilangan1++; |
variabelMod = variabelMod/2; |
} |
for(variabelMod = bil2; variabelMod>=1; ) { |
int angka2 = variabelMod % 2; |
bitBilangan2[indexBitBilangan2] = angka2; |
indexBitBilangan2++; |
variabelMod = variabelMod/2; |
} |
for(int index1=bitBilangan1.length-1; index1>-1; index1--) { |
System.out.print(bitBilangan1[index1] + "\t"); |
} |
System.out.println(bil1); |
System.out.println("-------------------------------------------------------------------"); |
|
for(int index2=bitBilangan2.length-1; index2>-1; index2--) { |
System.out.print(bitBilangan2[index2] + "\t"); |
} |
System.out.println(bil2); |
System.out.println("-------------------------------------------------------------------"); |
for(int indexHasil=bitBilangan1.length-1; indexHasil>-1; indexHasil--) { |
System.out.print((bitBilangan1[indexHasil] ^ bitBilangan2[indexHasil]) + "\t"); |
} |
System.out.print((bil1 ^ bil2) + "\t\n"); |
break; |
case 4: |
for(variabelMod = bil1; variabelMod>=1; ) { |
|
int angka1 = variabelMod % 2; |
bitBilangan1[indexBitBilangan1] = angka1; |
indexBitBilangan1++; |
variabelMod = variabelMod/2; |
} |
for(variabelMod = bil2; variabelMod>=1; ) { |
|
int angka2 = variabelMod % 2; |
bitBilangan2[indexBitBilangan2] = angka2; |
indexBitBilangan2++; |
variabelMod = variabelMod/2; |
} |
for(int index1=bitBilangan1.length-1; index1>-1; index1--) { |
System.out.print(bitBilangan1[index1] + "\t"); } |
System.out.println(bil1); |
System.out.println("-------------------------------------------------------------------"); |
for(int index2=bitBilangan2.length-1; index2>-1; index2--) { |
System.out.print(bitBilangan2[index2] + "\t"); |
} |
System.out.println(bil2); |
System.out.println("-------------------------------------------------------------------"); |
|
for(int indexHasil=bitBilangan1.length-1; indexHasil>-1; indexHasil--) { |
System.out.print((bitBilangan1[indexHasil] >> bitBilangan2[indexHasil]) + "\t"); |
} |
System.out.print((bil1 >> bil2) + "\t\n"); |
break; |
|
case 5: |
for(variabelMod = bil1; variabelMod>=1; ) { |
|
int angka1 = variabelMod % 2; |
bitBilangan1[indexBitBilangan1] = angka1; |
indexBitBilangan1++; |
variabelMod = variabelMod/2; |
} |
for(variabelMod = bil2; variabelMod>=1; ) { |
|
int angka2 = variabelMod % 2; |
bitBilangan2[indexBitBilangan2] = angka2; |
indexBitBilangan2++; |
variabelMod = variabelMod/2; |
} |
for(int index1=bitBilangan1.length-1; index1>-1; index1--) { |
System.out.print(bitBilangan1[index1] + "\t"); |
} |
System.out.println(bil1); |
System.out.println("-------------------------------------------------------------------"); |
|
for(int index2=bitBilangan2.length-1; index2>-1; index2--) { |
System.out.print(bitBilangan2[index2] + "\t"); |
} |
System.out.println(bil2); |
System.out.println("-------------------------------------------------------------------"); |
|
for(int indexHasil=bitBilangan1.length-1; indexHasil>-1; indexHasil--) { |
System.out.print((bitBilangan1[indexHasil] << bitBilangan2[indexHasil]) + "\t"); |
} |
System.out.print((bil1 << bil2) + "\t\n"); |
break; |
} |
} |
} |
- program untuk mensimulasikan struktur data sederhana stack. Dimana dalam program tersebut terdapat empat menu utama ialah: menambah data, mengeluarkan data, menampilkan data, dan keluar program. Pada menu menambah data dan mengeluarkan data buatlah pengecekan data (penuh/kosong) sebelum menjalankan prosesnya
source codenya:
import java.util.Scanner; |
/* |
* To change this template, choose Tools | Templates |
* and open the template in the editor. |
*/ |
/** |
* |
* @author syafrudin |
*/ |
public class nomor_3 { |
static String[] data = new String[5]; |
static int indexMAX = data.length-1; |
static int indexMIN = 0; |
static int top = -1; |
public static void main(String[] args) { |
Pilihan(); |
} |
|
public static void Pilihan() { |
int pilih = 0; |
System.out.println("pilihlah menu berikut: "); |
System.out.println("1. menambahkan data" + |
"\n2. mengeluarkan data" + |
"\n3. menampilkan data" + |
"\n4. exit program"); |
|
|
Scanner inputDariUser = new Scanner(System.in); |
System.out.print("pilihan anda no: "); |
try{ |
pilih = Integer.parseInt(inputDariUser.next()); |
} |
catch(Exception e) { |
System.out.println(e.getMessage()); |
Pilihan(); |
} |
switch(pilih) { |
|
case 1: |
TambahData(); |
break; |
case 2: |
MengeluarkanData(); |
break; |
case 3: |
TampilData(); |
break; |
case 4: |
Keluar(); |
break; |
default: |
System.out.println("perhatikan menunya..!"); |
Pilihan(); |
break; |
} |
} |
public static void TambahData() { |
top++; |
String nama = ""; |
if (top>=data.length){ |
System.out.println("\nMaaf! Data sudah penuh."); |
top--;} |
else{ |
Scanner inputDariUser = new Scanner(System.in); |
System.out.print("masukkan nama: "); |
nama = inputDariUser.next(); |
data[top] = nama;} |
System.out.println(""); |
Pilihan(); |
} |
public static void TampilData() { |
for(int index = top; index>=indexMIN; index--) { |
System.out.println(data[index]); |
} |
System.out.println(""); |
Pilihan(); |
} |
public static void MengeluarkanData() { |
top--; |
System.out.println(""); |
if (top==indexMIN-2){ |
System.out.println("Maaf! Data telah kosong.\n"); |
top++; |
}else{ |
} |
Pilihan(); |
} |
public static void Keluar(){ |
System.exit(0); |
} |
} |
- program untuk menampilkan faktorial dari suatu bilangan yang diinputkan oleh user.
Source codenya seperti di bawah ini:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
import java.io.*;
/**
*
* @author syafrudin
*/
public class nomor4 {
public static void main(String[] args)throws IOException {
BufferedReader datain=new BufferedReader(new
InputStreamReader(System.in));
System.out.print("masukkan sebuah bilangan : ");
String a=datain.readLine();
int b=Integer.parseInt(a);
int nil=1;
System.out.print(b+"! = ");
for (int i=1;i<=b;i++){
System.out.print(i+" x ");
nil*=i;
}
System.out.print(" = "+nil);
System.out.println();
}
}
0 Komentar untuk "Soal Jawaban Ujian Tengah Semester UTS Dasar Pemrograman Java 2013"