Tugas Personal Ke-2 (Minggu 7 / Sesi 11) Nama: Mochamad Lutfy Firmansyah (2201918614)

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 18

Tugas Personal ke-2

(Minggu 7 / Sesi 11)


Nama : Mochamad Lutfy Firmansyah (2201918614)

1. Tulislah program java dengan deskripsi sebagai berikut:


Anda diminta untuk membuat program yang menerima input berupa nilai panjang, lebar dan
tinggi berupa angka positif. Jika nilai input berupa angka negative maka akan dilakukan
exception.
Jawab :
Coding :
public static void nomor1(){
int volume;
System.out.println("Masukkan Panjang : ");
int panjang = input.nextInt();
System.out.println("Masukkan Lebar : ");
int lebar = input.nextInt();
System.out.println("Masukkan Tinggi : ");
int tinggi = input.nextInt();

if(panjang < 0 || lebar < 0 || tinggi < 0){


System.out.println("Data yang dimasukkan tidak boleh
negatif!");
} else {
try {
volume = panjang * lebar * tinggi;
System.out.println("Volume = " + volume);
} catch (ArithmeticException ae){
System.out.println("Arithmetic exception occoured : " +
ae.toString());
}
}
}

ISYS6514 – Business Application Development


Output :

ISYS6514 – Business Application Development


2. Buatlah Program Java Stack Output yang di harapkan adalah seperti gambar di bawah ini

 
Jawab :
Class Stack.java :
public class Stack {
private int maxSize;
private int[] data;
private int top;

public Stack() {
maxSize = 3;
data = new int[3];
data[0] = 5;
data[1] = 3;
data[2] = 2;
}

public void push(int p) {


data[++top] = p;
System.out.println(p);
}

public void pop() {


if(data[2] != 0){
data[2] = 0;
} else if(data[1] != 0){
data[1] = 0;
} else if(data[0] != 0){
data[0] = 0;

ISYS6514 – Business Application Development


}
}

public boolean isEmpty() {


return (top == -1);
}

public void isFull(){


System.out.println("STACK IS FULL");
}

public void clear(){


top = -1;
data[0] = 0;
data[1] = 0;
data[2] = 0;
}

public int getSize(){


return data.length;
}

public void printStack(){


for(int i = 0; i < data.length; i++){
if(data[i] != 0){
System.out.println(data[i]);
}
}
}
}

ISYS6514 – Business Application Development


ISYS6514 – Business Application Development
Akses class Stack.java
public static void nomor2(){
Stack stack = new Stack();
if(stack.getSize() == 3){
stack.isFull();
stack.clear();
}
System.out.println("--------PUSH--------");
stack.push(5);
stack.push(3);
stack.push(2);
System.out.println("--------POP--------");
stack.pop();
stack.printStack();
}

Output :

ISYS6514 – Business Application Development


3. Buatlah program mencari nilai maksimum dan minimum dari 3 input berupa nilai bilangan.
Jawab :
Coding :
public static void nomor3(){
int[] bil = new int[3];
System.out.println("Masukkan Bilangan - 1 : ");
bil[0] = input.nextInt();
System.out.println("Masukkan Bilangan - 2 : ");
bil[1] = input.nextInt();
System.out.println("Masukkan Bilangan - 3 : ");
bil[2] = input.nextInt();

Arrays.sort(bil);
System.out.println("Nilai Minimum = " + bil[0]);
System.out.println("Nilai Maximum = " + bil[bil.length - 1]);
}

Output :

ISYS6514 – Business Application Development


4. Buatlah program java yang dapat menampilkan:

Jawab :
Coding :
import javax.swing.JButton;
import javax.swing.JFrame;

public class gui extends JFrame{


public gui(){
this.setTitle("My First GUI");
this.setSize(700, 400);
}
public static void main(String[] args) {
gui myGui = new gui();
myGui.setVisible(true);

JButton btn = new JButton("Press");

myGui.add(btn);

}
}

ISYS6514 – Business Application Development


Output :

5. Terdapat database sebagai berikut:

Buatlah JDBC programming berdasarkan database diatas!


Jawab :
Database dibuat menggunakan mySql
Coding :

ISYS6514 – Business Application Development


import java.sql.Statement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;

public class jdbc {


public static void main(String[] args){
try{
Class.forName("com.mysql.jdbc.Driver");
} catch(ClassNotFoundException e){
System.out.println("Class not found "+ e);
}

System.out.println("JDBC Class found");

try {
Connection con =
DriverManager.getConnection("jdbc:mysql://localhost:3306/ebookshop",
"root", "");
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM books");
while (rs.next()) {
int id = rs.getInt("id");
String title = rs.getString("title");
String author = rs.getString("author");
Float price = rs.getFloat("price");
int qty = rs.getInt("qty");
System.out.println("ID = " + id + " || Title = " + title +
" || Author = " + author + " || Price = " + price + " || Qty = " + qty);
}
} catch (SQLException e) {
System.out.println("SQL exception occured" + e);
}
}

ISYS6514 – Business Application Development


}

Output :

ISYS6514 – Business Application Development


Coding Seluruhnya

Class Tugas2.java
import java.text.SimpleDateFormat;

import java.util.Arrays;

import java.util.Date;

import java.util.Scanner;

public class Tugas2 {

public static Scanner input = new Scanner(System.in);

public static void main(String[] args) {

// TODO code application logic here

System.out.println("Tugas Personal 2");

System.out.println("Mochamad Lutfy Firmansyah - 2201918614");

boolean cek = true;

do{

System.out.println("==================================");

System.out.println("1. Volume");

System.out.println("2. Stack Output");

System.out.println("3. Nilai maksimum dan minimum");

System.out.println("4. Exit");

System.out.println("==================================");

System.out.print("Choice : ");

int choice = input.nextInt();

switch(choice){

case 1 :

nomor1();

break;

ISYS6514 – Business Application Development


case 2 :

nomor2();

break;

case 3 :

nomor3();

break;

default :

cek = false;

break;

}while(cek == true);

public static void nomor1(){

int volume;

System.out.println("Masukkan Panjang : ");

int panjang = input.nextInt();

System.out.println("Masukkan Lebar : ");

int lebar = input.nextInt();

System.out.println("Masukkan Tinggi : ");

int tinggi = input.nextInt();

if(panjang < 0 || lebar < 0 || tinggi < 0){

System.out.println("Data yang dimasukkan tidak boleh negatif!");

} else {

try {

volume = panjang * lebar * tinggi;

System.out.println("Volume = " + volume);

} catch (ArithmeticException ae){

System.out.println("Arithmetic exception occoured : " +


ae.toString());

ISYS6514 – Business Application Development


}

public static void nomor2(){

Stack stack = new Stack();

if(stack.getSize() == 3){

stack.isFull();

stack.clear();

System.out.println("--------PUSH--------");

stack.push(5);

stack.push(3);

stack.push(2);

System.out.println("--------POP--------");

stack.pop();

stack.printStack();

public static void nomor3(){

int[] bil = new int[3];

System.out.println("Masukkan Bilangan - 1 : ");

bil[0] = input.nextInt();

System.out.println("Masukkan Bilangan - 2 : ");

bil[1] = input.nextInt();

System.out.println("Masukkan Bilangan - 3 : ");

bil[2] = input.nextInt();

Arrays.sort(bil);

System.out.println("Nilai Minimum = " + bil[0]);

System.out.println("Nilai Maximum = " + bil[bil.length - 1]);

ISYS6514 – Business Application Development


Class Stack.java
public class Stack {

private int maxSize;

private int[] data;

private int top;

public Stack() {

maxSize = 3;

data = new int[3];

data[0] = 5;

data[1] = 3;

data[2] = 2;

public void push(int p) {

data[++top] = p;

System.out.println(p);

public void pop() {

if(data[2] != 0){

data[2] = 0;

} else if(data[1] != 0){

data[1] = 0;

} else if(data[0] != 0){

data[0] = 0;

ISYS6514 – Business Application Development


public boolean isEmpty() {

return (top == -1);

public void isFull(){

System.out.println("STACK IS FULL");

public void clear(){

top = -1;

data[0] = 0;

data[1] = 0;

data[2] = 0;

public int getSize(){

return data.length;

public void printStack(){

for(int i = 0; i < data.length; i++){

if(data[i] != 0){

System.out.println(data[i]);

ISYS6514 – Business Application Development


Class gui.java
import javax.swing.JButton;

import javax.swing.JFrame;

public class gui extends JFrame{

public gui(){

this.setTitle("My First GUI");

this.setSize(700, 400);

public static void main(String[] args) {

gui myGui = new gui();

myGui.setVisible(true);

JButton btn = new JButton("Press");

myGui.add(btn);

Class jdbc.java
import java.sql.Statement;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.ResultSet;

import java.sql.SQLException;

public class jdbc {

public static void main(String[] args){

try{

ISYS6514 – Business Application Development


Class.forName("com.mysql.jdbc.Driver");

} catch(ClassNotFoundException e){

System.out.println("Class not found "+ e);

System.out.println("JDBC Class found");

try {

Connection con =
DriverManager.getConnection("jdbc:mysql://localhost:3306/ebookshop", "root",
"");

Statement stmt = con.createStatement();

ResultSet rs = stmt.executeQuery("SELECT * FROM books");

while (rs.next()) {

int id = rs.getInt("id");

String title = rs.getString("title");

String author = rs.getString("author");

Float price = rs.getFloat("price");

int qty = rs.getInt("qty");

System.out.println("ID = " + id + " || Title = " + title + "


|| Author = " + author + " || Price = " + price + " || Qty = " + qty);

} catch (SQLException e) {

System.out.println("SQL exception occured" + e);

ISYS6514 – Business Application Development

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy