Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
204 views

10 Contoh Coding Bahasa Pemrograman Java

The document provides 10 examples of Java coding constructs including: 1) Printing "Hello World" 2) Using variables 3) Different data types 4) Operators 5) String manipulation 6) If/else statements 7) Switch statements 8) While loops 9) Arrays 10) User input It also provides 6 examples of C++ coding and 8 examples of PHP and HTML coding constructs.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
204 views

10 Contoh Coding Bahasa Pemrograman Java

The document provides 10 examples of Java coding constructs including: 1) Printing "Hello World" 2) Using variables 3) Different data types 4) Operators 5) String manipulation 6) If/else statements 7) Switch statements 8) While loops 9) Arrays 10) User input It also provides 6 examples of C++ coding and 8 examples of PHP and HTML coding constructs.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 15

10 Contoh Coding Bahasa Pemrograman Java

Bahasa pemrograman java merupakan bahasa pemrograman yang bisa kita gunakan


untuk mendevelop aplikasi berbasis desktop, web, dan mobile.
 

1. Menampilkan Hello World
public class MyClass {
public static void main(String[] args) {
System.out.println("Hello World");
}
}

Hasilnya :

2. Variabel pada Java


public class MyClass {
public static void main(String[] args) {
int myNum;
myNum = 15;
System.out.println(myNum);
}
}

Hasilnya :

3. Penggunaan Tipe Data pada Java


public class MyClass {
public static void main(String[] args) {
int myNum = 5;               // integer (whole number)
float myFloatNum = 5.99f;    // floating point number
char myLetter = 'D';         // character
boolean myBool = true;       // boolean
String myText = "Hello";     // String
System.out.println(myNum);
System.out.println(myFloatNum);
System.out.println(myLetter);
System.out.println(myBool);
System.out.println(myText);
}
}

Hasilnya :

4. Operator pada Java


public class MyClass {
public static void main(String[] args) {
int sum1 = 100 + 50;
int sum2 = sum1 + 250;
int sum3 = sum2 + sum2;
System.out.println(sum1);
System.out.println(sum2);
System.out.println(sum3);
}
}

Hasilnya :

5. Penggunaan String pada Java
public class MyClass {
public static void main(String[] args) {
String txt = "Hello World";
System.out.println(txt.toUpperCase());
System.out.println(txt.toLowerCase());
}
}

Hasilnya :

6. IF dan Else
public class MyClass {
public static void main(String[] args) {
int time = 22;
if (time < 10) {
System.out.println("Good morning.");
} else if (time < 20) {
System.out.println("Good day.");

}  else {
System.out.println("Good evening.");
}
}
}

Hasilnya :

7. Switch
public class MyClass {
public static void main(String[] args) {
int day = 4;
switch (day) {
case 1:
System.out.println("Monday");
break;
case 2:
System.out.println("Tuesday");
break;
case 3:
System.out.println("Wednesday");
break;
case 4:
System.out.println("Thursday");
break;
case 5:
System.out.println("Friday");
break;
case 6:
System.out.println("Saturday");
break;
case 7:
System.out.println("Sunday");
break;
}
}
}

Hasilnya :

8. Penggunaan While
public class MyClass {
public static void main(String[] args) {
int i = 0;
while (i < 5) {
System.out.println(i);
i++;
}
}
}

Hasilnya :

9. Penggunaan Array
public class MyClass {
public static void main(String[] args) {
String[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
for (int i = 0; i < cars.length; i++) {
System.out.println(cars[i]);
}
}
}

Hasilnya :

10. User Input
import java.util.Scanner; // import the Scanner class
class MyClass {
public static void main(String[] args) {

Scanner myObj = new Scanner(System.in);


String userName;

// Enter username and press Enter

System.out.println("Enter username");
userName = myObj.nextLine();
System.out.println("Username is: " + userName);
}
}

Hasilnya :

Pelajari contoh coding java lainnya pada artikel : 16 Contoh Pemrograman Java 

6 Contoh Coding Bahasa Pemrograman C++


1. Hello world
#include <iostream>
using namespace std;

int main() {
cout << "Hello World!";
return 0;
}

Hasilnya :

2. Mendeklarasikan variabel
#include <iostream>
using namespace std;

int main() {
int myNum = 15;   // Now myNum is 15
myNum = 10;       // Now myNum is 10
cout << myNum;
return 0;
}

Hasilnya :

3. Operator
#include <iostream>
using namespace std;

int main() {
int sum1 = 100 + 50;        // 150 (100 + 50)
int sum2 = sum1 + 250;      // 400 (150 + 250)
int sum3 = sum2 + sum2;     // 800 (400 + 400)
cout << sum1 << "\n";
cout << sum2 << "\n";
cout << sum3;
return 0;
}

Hasilnya :

4. User Input
#include <iostream>
using namespace std;

int main() {
int x, y;
int sum;
cout << "Type a number: ";
cin >> x;
cout << "Type another number: ";
cin >> y;
sum = x + y;
cout << "Sum is: " << sum;
return 0;
}

Hasilnya :

5. Penggunaan String
#include <iostream>
#include <string>
using namespace std;

int main () {
string firstName = "John ";
string lastName = "Doe";
string fullName = firstName + lastName;
cout << fullName;
return 0;
}

Hasilnya :

6. Array
#include <iostream>
#include <string>
using namespace std;

int main() {
string cars[4] = {"Volvo", "BMW", "Ford", "Mazda"};
cars[0] = "Opel";
cout << cars[0];
return 0;
}

Hasilnya : 

8 Contoh Coding Bahasa Pemrograman PHP dan HTML


Bahasa pemrograman ini cukup populer di Indonesia. Bahasa pemrograman ini
digunakan untuk pengaturan server pada sebuah aplikasi. Untuk mempelajari bahasa
pemrograman PHP kita membutuhkan localhost contohnya XAMPP, Text Editor, dan
Browser.

1. Echo/ Print
<!DOCTYPE html>
<html>
<body>

<?php
echo "<h2>PHP is Fun!</h2>";
echo "Hello world!<br>";
echo "I'm about to learn PHP!<br>";
echo "This ", "string ", "was ", "made ", "with multiple parameters.";
?>

</body>
</html>

Hasilnya :

2. Tipe data
<!DOCTYPE html>
<html>
<body>

<?php
$x = "Hello world!";
$y = 'Hello world!';

echo $x;
echo "<br>";
echo $y;
?>

</body>
</html>

Hasilnya :

3. Variabel
<!DOCTYPE html>
<html>
<body>
<?php
$txt = "W3Schools.com";
echo "I love " . $txt . "!";
?>

</body>
</html>

Hasilnya :

4. Konstanta
<!DOCTYPE html>
<html>
<body>

<?php
// case-sensitive constant name
define("GREETING", "Welcome to W3Schools.com!");
echo GREETING;
?>

</body>
</html>

Hasilnya :

5. If dan Else
<!DOCTYPE html>
<html>
<body>

<?php
$t = date("H");
if ($t < "20") {
echo "Have a good day!";
} else {
echo "Have a good night!";
}
?>

</body>
</html>

Hasilnya :

6. Array
<!DOCTYPE html>
<html>
<body>

<?php
$cars = array("Volvo", "BMW", "Toyota");
echo "I like " . $cars[0] . ", " . $cars[1] . " and " . $cars[2] . ".";
?>

</body>
</html>

Hasilnya :

7. Form input
<!DOCTYPE HTML>
<html>
<body>

<form action="welcome.php" method="post">


Name: <input type="text" name="name"><br>
E-mail: <input type="text" name="email"><br>
<input type="submit">
</form>

</body>
</html>

Hasilnya :

8. Form Validasi
<!DOCTYPE HTML>
<html>
<head>
</head>
<body>

<?php
// define variables and set to empty values
$name = $email = $gender = $comment = $website = "";

if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = test_input($_POST["name"]);
$email = test_input($_POST["email"]);
$website = test_input($_POST["website"]);
$comment = test_input($_POST["comment"]);
$gender = test_input($_POST["gender"]);
}

function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>

Hasilnya : 

5 Contoh Coding Bahasa Pemrograman Javascript


JavaScript adalah bahasa pemrograman yang paling populer di dunia. Mengapa
begitu ? karena JavaScript bisa digunakan di sisi front end ataupun back end. Untuk
mempelajarinya kita harus menginstall Text Editor dan juga Browser.

1. Output dengan innerHTML
<!DOCTYPE html>
<html>
<body>
<h2>My First Web Page</h2>

<p>My First Paragraph.</p>


<p id="demo"></p>

<script>
document.getElementById("demo").innerHTML = 5 + 6;
</script>

</body>
</html>

Hasilnya :

2. Variabel
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Variables</h2>
<p>In this example, x, y, and z are variables.</p>
<p id="demo"></p>

<script>
var x = 5;
var y = 6;
var z = x + y;
document.getElementById("demo").innerHTML =
"The value of z is: " + z;
</script>

</body>
</html>

Hasilnya :

3. Operator
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Operators</h2>
<p>x = 5, y = 2, calculate z = x + y, and display z:</p>
<p id="demo"></p>

<script>
var x = 5;
var y = 2;
var z = x + y;
document.getElementById("demo").innerHTML = z;
</script>

</body>
</html>
Hasilnya :

4. Aritmatika
<!DOCTYPE html>
<html>
<body>
<p>A typical arithmetic operation takes two numbers and produces a new number.</p>
<p id="demo"></p>
<script>
var x = 100 + 50;
document.getElementById("demo").innerHTML = x;
</script>
</body>
</html>

Hasilnya :

5. Event
<!DOCTYPE html>
<html>
<body>
<button onclick="document.getElementById('demo').innerHTML=Date()">The time
is?</button>
<p id="demo"></p>
</body>
</html>

Hasilnya :

You might also like