Laporan Praktikum Algoritma Dan Pemrograman: Pertemuan Ke - 4
Laporan Praktikum Algoritma Dan Pemrograman: Pertemuan Ke - 4
Laporan Praktikum Algoritma Dan Pemrograman: Pertemuan Ke - 4
LAPORAN PRAKTIKUM
ALGORITMA DAN PEMROGRAMAN
Pertemuan Ke - 4
Disusun Oleh :
NAMA : Novita
NIM : 183110020
JURUSAN : Manajemen Informatika
JENJANG : D3
STMIK AKAKOM
YOGYAKARTA
2018
LEARNING 4
JAVA WITH NETBEANS IDE 8.2
PRACTICAL 2 : STRING CLASS METHODS
a. Fungsi Length()
public static void main(String[] arg){ // This makes the main method public that
means that we can call the method from outside the class
public static void main(String[] arg){ // This makes the main method public
that means that we can call the method from outside the class
String str = "hello, world"; //create variable str and input str
public static void main(String[] arg){ // This makes the main method public
that means that we can call the method from outside the class
String phoneNum = "404-543-2345"; //create and input variable phoneNum
int idx1 = phoneNum.indexOf('_'); //create variable idx1
System.out.println("index of first dash : "+ idx1); //display var idx1
int idx2 = phoneNum.indexOf('_',4); //create variable idx2
System.out.println("second dash indx : "+ idx1); //display var idx2
}
}
OUTPUT :
public static void main(String[] arg){ // This makes the main method public
that means that we can call the method from outside the class
String greeting = "Hello, World";//make variable greeting and input
String sub = greeting.substring(0,5); // make variable sub and input
System.out.println(sub); //display sub
String w = greeting.substring(7,11); // make variable sub and input
System.out.println(w); //display sub
String tail = greeting.substring(7); // make variable sub and input
System.out.println(tail); //display tail
}
}
OUTPUT :
public static void main(String[] arg){ // This makes the main method public
that means that we can call the method from outside the class
String str = "Using String replace to replace character"; //create
and input variable str
String newStr = str.replace("r","R"); //change var newStr to str
System.out.println(newStr); //display var newStr
}
}
OUTPUT:
f. parameter
public static void main(String[] arg){ // This makes the main method public
that means that we can call the method from outside the class
String str = "Using String replace to replace character"; //create
and input variable str
String newStr = str.replace("r","R"); //change var newStr to str
System.out.println(newStr); //display var newStr
String newFirstStr = str.replace("re","RE"); //change var newFirtsStr to
newStr
System.out.println(newFirstStr); //display var newFirtsStr
}
}
OUTPUT :
public static void main(String[] arg){ // This makes the main method public
that means that we can call the method from outside the class
String s1 = "Novita"; //create and input variable s1
String s2 = "Ceniz"; //create and input variable s2
s1 = s1 + s2 ; //decribe s1
System.out.println(s1); //display s1
}
}
OUTPUT :
h. Consolidate string
public static void main(String[] arg){ // This makes the main method public
that means that we can call the method from outside the class
String newString = "belajar java" +8; //create
System.out.println(newString);
String newString1= "8" +8;
System.out.println(newString1);
String numString= "8+8";
}
}
OUTPUT :
i. Concat()
public static void main(String[] arg){ // This makes the main method public
that means that we can call the method from outside the class
String myString = "belajar java" +8;
myString= myString.concat("World");
System.out.println(myString);
}
OUTPUT :
j. CompareTo()
package latihanstring1;
public class LatihanString9 {//to create a new class
public static void main(String[] arg){ // This makes the main method public
that means that we can call the method from outside the class
String s1 = "Novita";
String s2 = "Novita";
String s3 = "yuhuuu";
System.out.println(s1.compareTo(s2));
System.out.println(s1.compareTo(s3));
System.out.println(s3.compareTo(s1));
}
}
OUTPUT :
PRACTICAL 3 CLASS RANDOM
public static void main(String[] arg){ // This makes the main method public
that means that we can call the method from outside the class
Random mdNum = new Random(); //create new random mdNum
int randomNum = mdNum.nextInt();//create variable randomNum
System.out.println("random number: "+randomNum);//display
randomNum
}
}
OUTPUT :
public static void main(String[] arg){ // This makes the main method public
that means that we can call the method from outside the class
Random num = new Random();//create new random num
System.out.println("random number1: "
+num.nextInt()); //display num
System.out.println("random number2:
"+num.nextInt());//display num
System.out.println("random number3:
"+num.nextInt());//display num
System.out.println("random number4:
"+num.nextInt());//display num
System.out.println("random number5:
"+num.nextInt());//display num
}
}
OUTPUT :
c. Use random number type Double
public static void main(String[] arg){ // This makes the main method public
that means that we can call the method from outside the class
Random num = new Random();//create new random num
double randomNum = num.nextInt(); //create new variable
System.out.println("random number: "+randomNum); //display
variable
}
}
OUTPUT :
public static void main(String[] arg){ // This makes the main method public
that means that we can call the method from outside the class
Scanner numberScanner = new Scanner(System.in); //create new
variable scanner
System.out.println("masukan nomer antara 1 sampai 10:
");//display text
int userNum = numberScanner.nextInt();//create new variable scanner
Random rnd = new Random();//create new variable scanner
int winningNum = rnd.nextInt(10)+1; //count Num
System.out.println("angka mu : "+userNum); //display variableNum
System.out.println("the winning number is: "+winningNum);
//display count winningNum
}
}
OUTPUT :
public static void main(String[] arg){ // This makes the main method public
that means that we can call the method from outside the class
System.out.println(Math.sqrt(121.0));//display count math √
}
}
OUTPUT :
public static void main(String[] arg){ // This makes the main method public
that means that we can call the method from outside the class
double result = Math.min(3,7) + Math.abs(-50);//count math
(-) and Absolut
System.out.println("Result is" +result); //display result
}
}
OUTPUT :
public static void main(String[] arg){ // This makes the main method public
that means that we can call the method from outside the class
Scanner sc = new Scanner(System.in);//create new scanner
System.out.println("Enter The Radius: ");//input radius
double radius = sc.nextDouble();//input into radius
double area = Math.PI*radius*radius; //count area
System.out.println("The Area of Circle is : " +area);//display
area
sc.close(); //close scanner
}
}
OUTPUT :
public static void main(String[] arg){ // This makes the main method public
that means that we can call the method from outside the class
try (Scanner sc = new Scanner(System.in)) { //create scanner
System.out.println("Enter The Radius: "); //display input radius double
radius = sc.nextDouble();// create variable scanner
area(radius);//read radius to method area
System.out.println("The Area of Circle is : "+area); //display area
}
}
}
OUTPUT :
Task1 : Count BMI (Body Mass Index)
package APLIKASIKONSOL; //adress class