Created
October 23, 2017 23:40
-
-
Save dyazincahya/a6bc337d5a66c5a30f5b2972af2894a2 to your computer and use it in GitHub Desktop.
Kumpulan tugas JAVA bid, robi, iqbal, cahya (Semester 3)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* program array 2 dimensi | |
* | |
* @author (Cahya Dyazin) | |
* @version (0.1 11/10/2017) | |
*/ | |
import java.util.Scanner; | |
public class array2dim2 | |
{ | |
public static void main(String[] args) | |
{ | |
System.out.print('\u000C'); | |
int input_b, input_k, index; | |
Scanner s = new Scanner(System.in); | |
System.out.print("Masukan banyak baris: "); | |
input_b = s.nextInt(); | |
System.out.print("Masukan banyak kolom: "); | |
input_k = s.nextInt(); | |
String[][] z = new String[input_b][input_k]; | |
for(int k=0; k<input_b*input_k; k++) | |
{ | |
System.out.print("Masukan nilai ke: ["+k/input_k+"]["+k%input_k+"]: "); | |
z[k/input_k][k%input_k] = s.next(); | |
} | |
for(int k=0; k<input_b*input_k; k++) | |
{ | |
System.out.print(z[k/input_k][k%input_k]+"\t\t"); | |
if(k%input_k==input_k - 1) | |
{ | |
System.out.println(); | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* program array 2 dimensi | |
* | |
* @author (Cahya, Robi, Iqbal, Budi) | |
* @version (0.1 8/10/2017) | |
*/ | |
public class no1 | |
{ | |
public static void main(String [] args) | |
{ | |
// membuat isi elemen array | |
int [][] angka = { | |
{0, 1, 2, 3, 4}, | |
{5, 6, 7, 8, 9}, | |
{10, 11, 12, 13, 14}, | |
{15, 16, 17, 18, 19} | |
}; | |
int i, j; // i = baris v, j = kolom > | |
for (i=0;i<4;i++) | |
{ | |
for (j=0;j<5;j++) | |
{ | |
// menampilkan isi elemen baris dan kolom | |
System.out.print(angka[i][j]+" "); | |
} | |
System.out.println(""); | |
}//pindah baris | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* program substring | |
* | |
* @author (Cahya, Robi, Iqbal, Budi) | |
* @version (0.1 8/10/2017) | |
*/ | |
public class no2 | |
{ | |
public static void main(String [] args) | |
{ | |
String a = "Saya_adalah_String_a"; | |
/** | |
* Dua method substring tersebut adalah sebagai berikut : | |
* 1. substring(int beginIndex) | |
* 2. substring(int beginIndex, int endIndex) | |
* Sebagai catatan "int beginIndex" dimulai dari 0 | |
**/ | |
String b = a.substring(2); | |
String c = a.substring(0, 2); | |
System.out.println(b); | |
System.out.println(c); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* program Hitung Nilai | |
* | |
* @author (Cahya, Robi, Iqbal, Budi) | |
* @version (0.1 8/10/2017) | |
*/ | |
import java.io.*; | |
public class no3 | |
{ | |
public static void main(String [] args) | |
{ | |
String a = "iniibubudi"; | |
String b = "123456789"; | |
// variabel C akan menghasilkan nilai 2345 | |
int c= Integer.parseInt(a.substring(1,5)); | |
// variabel D akan menghasilkan nilai 67 | |
int d= Integer.parseInt(a.substring(5,7)); | |
System.out.println(a.substring(1,5)); | |
// variabel C + D sama dengan 2345+67=2412 | |
System.out.println(c+d); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.*; | |
import java.util.*; | |
public class no3new | |
{ | |
public static void main(String[] args) throws IOException | |
{ | |
String filename; | |
Scanner input = new Scanner(System.in); | |
System.out.print("Masukkan nama file atau direktori"); | |
System.out.print(" atau tekan <Enter> untuk keluar:"); | |
filename = input.nextLine(); | |
while (!filename.equals("")) //menguji bukan <ENTER> | |
{ | |
File fileDir = new File(filename); | |
if (!fileDir.exists()) { | |
System.out.println("Nama file tidak ada"); | |
break; | |
} | |
System.out.print(filename + " adalah suatu "); | |
if (fileDir.isFile()) | |
{ | |
System.out.println("file."); | |
} else { | |
System.out.println("direktori."); | |
} | |
System.out.print("File/direktori ini "); | |
if (!fileDir.canRead()) | |
{ | |
System.out.print("tidak "); | |
} | |
System.out.println("dapat dibaca."); | |
System.out.print("File/direktori ini "); | |
if (!fileDir.canWrite()) | |
{ | |
System.out.print("tidak "); | |
} | |
System.out.println("dapat ditulis."); | |
if (fileDir.isDirectory()) | |
{ | |
System.out.println("Isi:"); | |
String[] fileList = fileDir.list(); | |
//tampilkan isi direktori | |
for (int i = 0; i < fileList.length; i++) | |
System.out.println(" " + fileList[i]); | |
} else { | |
System.out.print("Ukuran file: "); | |
System.out.println(fileDir.length() + " bytes."); | |
} | |
canWrite | |
System.out.print("Masukkan nama file atau direktori"); | |
System.out.print(" atau tekan <Enter> untuk keluar:"); | |
filename = input.nextLine(); | |
} | |
input.close(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* program Volume Balok | |
* | |
* @author (Cahya, Robi, Iqbal, Budi) | |
* @version (0.1 8/10/2017) | |
*/ | |
import java.io.*; | |
public class no4 | |
{ | |
public static void main(String[] args) | |
{ | |
String s; | |
int p,l,t,volume; | |
try | |
{ | |
FileReader fr = new FileReader("dataku.txt"); | |
BufferedReader br = new BufferedReader(fr); | |
while((s=br.readLine()) != null) | |
{ | |
p = Integer.parseInt(s.substring(2,4)); | |
l = Integer.parseInt(s.substring(3,5)); | |
t = Integer.parseInt(s.substring(8,10)); | |
volume=p*l*t; | |
System.out.printf("Panjang %i Lebar %i "+" Tinggi %i maka volume = %i\n", p,l,t,volume); | |
} | |
br.close(); | |
} | |
catch(IOException e) { | |
System.err.println("File read error"); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Aplikasi untuk mengcopy file | |
import java.io.*; | |
import java.util.*; | |
public class no4new | |
{ | |
public static void main(String[] args) throws IOException | |
{ | |
//cek kedua nama file sudah diinput | |
if (args.length < 2) | |
{ | |
System.out.println("Anda harus memasukkan dua nama file."); | |
System.out.println("Syntax:"); | |
System.out.println(" java Copy <sumber> <tujuan>"); | |
return; | |
} | |
Scanner sumber = new Scanner(new File(args[0])); | |
PrintWriter tujuan = new PrintWriter(new File(args[1])); | |
String input; | |
while (sumber.hasNext()) | |
{ | |
input = sumber.nextLine(); | |
tujuan.println(input); | |
} | |
sumber.close(); | |
tujuan.close(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment