You are on page 1of 12

20

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()

package latihanstring1; //paket from java class

import java.util.Scanner; //create java scanner

public class LatihanString1 { //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

Scanner input = new Scanner(System.in); //create new scanner input

String nama; //create variable nama

System.out.println("masukkan nama anda : "); //display

nama= input.nextLine(); //read var nama

System.out.println(nama.length()); //count many character


}
}
OUTPUT:

b. Display every character with string

package latihanstring1; //paket from java class


public class LatihanString2 {//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 str = "hello, world"; //create variable str and input str

System.out.println(str.charAt(0)); //display str character zero


System.out.println(str.charAt(1)); //display str character one
System.out.println(str.charAt(2)); //display str character two
System.out.println(str.charAt(3)); //display str character three
System.out.println(str.charAt(4)); //display str character four
System.out.println(str.charAt(5)); //display str character fif
System.out.println(str.charAt(6)); //display str character six
System.out.println(str.charAt(7)); //display str character seven
System.out.println(str.charAt(8)); //display str character eight
System.out.println(str.charAt(9)); //display str character nine
System.out.println(str.charAt(10)); //display str character ten
System.out.println(str.charAt(11)); //display str character eleven
}
}
OUTPUT :

c. Get value index from character on String

package latihanstring1; // paket java class name

public class LatihanString3 { //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 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 :

d. Get substring from a string

package latihanstring1; // paket java class name

public class LatihanString4 { //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 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 :

e. Change character string

package latihanstring1; // paket java class name


public class LatihanString5 { //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 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

package latihanstring1; // paket java class name


public class LatihanString5b { //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 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 :

g. Declaration and create a string

package latihanstring1; // paket java class name


public class LatihanString6 { //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"; //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

package latihanstring1; // paket java class name


public class LatihanString7 { //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 newString = "belajar java" +8; //create
System.out.println(newString);
String newString1= "8" +8;
System.out.println(newString1);
String numString= "8+8";
}
}

OUTPUT :
i. Concat()

package latihanstring1; // paket java class name

public class LatihanString8 {//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 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

a. Use random number type integer

package latihanrandom1; //paket java class name


import java.util.Random;//make java random
public class LatihanRandom1 { //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
Random mdNum = new Random(); //create new random mdNum
int randomNum = mdNum.nextInt();//create variable randomNum
System.out.println("random number: "+randomNum);//display
randomNum
}
}

OUTPUT :

b. Create randoms number

package latihanrandom1; //paket java class name


import java.util.Random;//make java random
public class LatihanRandom2 { //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
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

package latihanrandom1;//paket java class name


import java.util.Random;// make java random
public class LatihanRandom3 {//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
Random num = new Random();//create new random num
double randomNum = num.nextInt(); //create new variable
System.out.println("random number: "+randomNum); //display
variable
}
}

OUTPUT :

d. Create random number with range

package latihanrandom1; //paket java class name


import java.util.Random; // make java random
import java.util.Scanner; // make java Scanner
public class Lottery {//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
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 :

PRACTICAL 4 : CLASS MATH

a. Use function sqrt

package latihanmath; //paket java class name


public class LatihanMath {//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
System.out.println(Math.sqrt(121.0));//display count math √
}
}

OUTPUT :

b. Use function min and abs

package latihanmath; //paket java class name


public class LatihanMath2 {//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
double result = Math.min(3,7) + Math.abs(-50);//count math
(-) and Absolut
System.out.println("Result is" +result); //display result
}
}
OUTPUT :

c. Use function field PI

package latihanmath; //paket java class name


import java.util.Scanner; //make java scanner
public class AreaOfCircle { //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
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 :

EXERCISE 1: MAKE METHODS


Change program AreaOfCircle with method and parameter :

package latihanmath; //adress class

import java.util.Scanner; //new java scanner

public class latihanmethod { //class name


private static double area; //make method
private static void area(double radius) { //variable method
area = Math.PI*radius*radius; //count area with math.PI syntax
}
private double radius; //create variable radius
void hitung() {//count hitung
}

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

import java.util.Scanner; //new java scanner


public class latihanlatihan { //class name
public static void main(String[] args) { // 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 scanner
System.out.println("Enter the weight in pounds = "); //display
input weight
double weight = sc.nextDouble();//create variable scanner
System.out.println("Enter the height inches = ");//display input
height
double height = sc.nextDouble(); // create variable scanner
double bmi = (weight/Math.pow(height,2))*70; //count variable bmi
with math.pow syntax
System.out.println("Your Body Mass Index = " +bmi); //display
bmi
}
}

You might also like