0% found this document useful (0 votes)
11 views12 pages

Program Java

The document contains code for several Java classes that demonstrate different Java concepts like constructors, methods, exceptions handling, and more. The classes include code to convert infix to postfix notation, demonstrate constructors and parameter passing, and more.

Uploaded by

yundaseptiana868
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views12 pages

Program Java

The document contains code for several Java classes that demonstrate different Java concepts like constructors, methods, exceptions handling, and more. The classes include code to convert infix to postfix notation, demonstrate constructors and parameter passing, and more.

Uploaded by

yundaseptiana868
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Sequential.

java
/*

* To change this license header, choose License Headers in Project Properties.

* To change this template file, choose Tools | Templates

* and open the template in the editor.

*/

package sequintial;

/**

* @author asus

*/

import java.util.Scanner;

public class Sequintial {

/**

* @param args the command line arguments

*/

public static void main(String[] args) {

// TODO code application logic here

Scanner scanner = new Scanner (System.in);

System.out.print ("masukkan jumlah data: ");

int jumlahData = scanner.nextInt();

int[] npmMahasiswa = new int[jumlahData];


String[] namaMahasiswa = new String[jumlahData];

for (int i = 0; i < jumlahData; i++) {

System.out.print("Masukkan NPM mahasiswa ke-" + (i + i) + ": ");

npmMahasiswa[i] = scanner.nextInt();

scanner.nextLine();

System.out.print("Masukkan nama mahasiswa ke-" + (i + i) + ": ");

namaMahasiswa[i] = scanner.nextLine();

Methode.java

/*

* To change this license header, choose License Headers in Project Properties.

* To change this template file, choose Tools | Templates

* and open the template in the editor.

*/

package methode;

/**

* @author asus
*/

public class Methode {

/**

* @param args the command line arguments

*/

private int panjang;

public Methode (int panjang){

this.panjang=panjang;

public static void showInfo(){

System.out.println("Dipanggil Melalui matrik Methode showInfo");

public void setpanjang (int panjang){

this. panjang=panjang;

public int getPanjang(){

return this.panjang;

public int getLuas(){

int luas =this.panjang + this.panjang;

return luas;

}
public int getVolumekubus(){

int volume=this.panjang*this.panjang*this.panjang;

return volume;

public int getKeliling(){

int keliling=4*this.panjang;

return keliling;

public int getLuaspermukaan(){

int luas_permukaan=6*(this.panjang*this.panjang);

return luas_permukaan;

public int showException() throws Exception{

return 1/0;

public static void main(String[] args) {

Methode.showInfo();

Methode persegi= new Methode(10);

System.out.println("panjang="+persegi.getPanjang());

System.out.println("(Luas) panjang * lebar = "+persegi.getLuas());

System.out.println("(Keliling) 4 * panjang = "+persegi.getKeliling());

System.out.println("(Luaspermukaan) 6 * (panjang * panjang)= " +persegi.getLuaspermukaan());

System.out.println("(Volumekubus) panjang * panjang * panjang = " +persegi.getVolumekubus());


persegi.setpanjang(20);

System.out.println("panjang= "+persegi.getPanjang());

System.out.println("(Luas) panjang * lebar = "+persegi.getLuas());

System.out.println("(Keliling) 4 * panjang = "+persegi.getKeliling());

System.out.println("(Luaspermukaan) 6 * (panjang * panjang)= "+persegi.getLuaspermukaan());

System.out.println("(Volumekubus) panjang * panjang * panjang = "+ persegi.getVolumekubus());

try{

System.out.println(persegi.showException());

} catch (Exception e){

e.printStackTrace();

InfixToPostfix

import java.util.Stack;

import java.util.Scanner;

public class InfixToPostfix {

/**

* @param args the command line arguments

*/

String infixExp = "";


String postfixExp = "";

Stack<Character> s = new

Stack<Character>();

public void setInfixExp(String infixExp){

this.infixExp = infixExp;

public boolean isOperator(char ch){

if(ch =='+'||ch =='-'||ch =='*'||ch =='/'||ch =='^'){

return true;

return false;

public int degreeOp(char op){

if(op =='+'||op =='-'){

return 1;

}else if(op =='*'||op =='/'){

return 2;

}else if(op =='^'){

return 3;

}else{

return 0;

public String toPostfix(){

char ch;

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

ch = infixExp.charAt(i);
if(isOperator(ch)){

if(s.isEmpty()|| degreeOp(ch)>

degreeOp(s.peek())) { //pertandingan derajat relasi

s.push(ch);

}else{

postfixExp +=s.pop();

do{

if(s.isEmpty()|| degreeOp(ch)>

degreeOp(s.peek())){

break;

}else{

//System.out.println(ch);

postfixExp += s.pop();

} while (degreeOp(ch) <= degreeOp(s.peek()));

//perbandingan derajat relasi

s.push(ch);

}else if (ch ==')'){

do{

postfixExp +=s.pop();

}while (s.peek()!='(');

s.pop();

}else if (ch =='('){

s.push(ch);

}else{

postfixExp += ch;

}
if(!s.isEmpty()){

do{

postfixExp +=s.pop();

}while(!s.isEmpty());

return postfixExp;

public static void main(String[] args) {

// TODO code application logic here

InfixToPostfix itp =new InfixToPostfix();

String infix;

Scanner keyIn =new Scanner(System.in);

//(a+b)/((c-d)*e^f)

//(A+B)/((C-D)*E^F)

System.out.print("Input Data Infix:");

infix = keyIn.nextLine();

itp.setInfixExp(infix);

System.out.println("Hasil Konversi Data Infix ke Postfix :"+itp.toPostfix());

Constructor
/*

* To change this license header, choose License Headers in Project Properties.


* To change this template file, choose Tools | Templates

* and open the template in the editor.

*/

package constructor;

/**

* @author asus

*/

public class Constructor {

/**

* @param args the command line arguments

*/

String warna, merek;

public Constructor (String paramWarna, String paramMerek){

warna =paramWarna;

merek =paramMerek;

void maju(){

System.out.println("Mobil"+merek+"warna"+warna+"bergerak maju");

void mundur(){

System.out.println("Mobil"+merek+"warna"+warna+"bergera mundur");

public static void main(String[] args) {

// TODO code application logic here


Constructor mobilsaya=new Constructor("biru","daihatsu");

mobilsaya.maju();

mobilsaya.mundur();

Marsya
/*

* To change this license header, choose License Headers in Project Properties.

* To change this template file, choose Tools | Templates

* and open the template in the editor.

*/

package jhonri;

/**

* @author asus

*/

public class Jhonri {

/**

* @param args the command line arguments

*/
String nama;

int usia;

String citacita;

int tinggibadan;

public Jhonri(String nama, int usia, String citacita, int tinggibadan){

this.nama = nama;

this.usia = usia;

this.citacita = citacita;

this.tinggibadan = tinggibadan;

void perkenalan (){

System.out.println(nama + ": marsya silalahi");

void usia (){

System.out.println("berusia"+ usia +"tahun");

void citacita (){

System.out.println(citacita+ ":pengusaha");

void tinggibadan (){

System.out.println(tinggibadan+ ":tinggibadan");

public static void main(String[] args) {

// TODO code application logic here

Jhonri marsya = new Jhonri ("nama", 19, "citacita", 155);

marsya.perkenalan();

marsya.usia();
marsya.citacita();

marsya.tinggibadan();

You might also like