google

Sabtu, 27 Desember 2008

Biggest Number

public class B{
public static void main(String[]args){

Integer a=6;
int b=14;
int c=18;

int x=(a>b)?a:b;
int y=(b>c)?b:c;

if (x>y){
System.out.println("Angka terbesar:"+x);
}
else {
System.out.println("Angka terbesar:"+y);
}




}
}


http://dayaprime.com

Get Date

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtility {

/* Add Day/Month/Year to a Date
add() is used to add values to a Calendar object.
You specify which Calendar field is to be affected by the operation
(Calendar.YEAR, Calendar.MONTH, Calendar.DATE).
*/

public static final String DATE_FORMAT = "dd-MM-yyyy";
//See Java DOCS for different date formats
// public static final String DATE_FORMAT = "yyyy-MM-dd";

public static void addToDate() {
System.out.println("1. Add to a Date Operation\n");
SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
//Gets a calendar using the default time zone and locale.
Calendar c1 = Calendar.getInstance();
Date d1 = new Date();
// System.out.println("Todays date in Calendar Format : "+c1);
System.out.println("c1.getTime() : " + c1.getTime());
System.out.println("c1.get(Calendar.YEAR): "+ c1.get(Calendar.YEAR));
System.out.println("Todays date in Date Format : " + d1);
c1.set(1999, 0, 20); //(year,month,date)
System.out.println("c1.set(1999,0 ,20) : " + c1.getTime());
c1.add(Calendar.DATE, 20);
System.out.println("Date + 20 days is : "+ sdf.format(c1.getTime()));
System.out.println();
System.out.println("-------------------------------------");

http://dayaprime.com
}

Colour TUner

/*
* FormColor.java
*
* Created on 22 November 2008, 21:15
*/

package com.nawolo.colortuner;

import java.awt.Color;
import javax.swing.JPanel;
import javax.swing.SpinnerNumberModel;


/**
*
* @author Gateway
*/
public class FormColor extends javax.swing.JFrame {
int red,green,blue;
SpinnerNumberModel modelRed, modelGreen,modelBlue;

/** Creates new form FormColor */
public FormColor() {
initComponents();

Integer value=new Integer(225);
Integer min=new Integer (0);
Integer max=new Integer (225);
Integer step=new Integer (1);

modelRed=new SpinnerNumberModel(value,min,max,step);
modelGreen=new SpinnerNumberModel(value,min,max,step);
modelBlue=new SpinnerNumberModel(value,min,max,step);

jSpinner1.setModel(modelRed);
jSpinner2.setModel(modelGreen);
jSpinner3.setModel(modelBlue);
red=green=blue=255;
changePanelColor();

}

private void changePanelColor(){
Color clr=new Color (red, green, blue);
jPanel1.setBackground(clr);
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
// //GEN-BEGIN:initComponents
private void initComponents() {
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
jSpinner1 = new javax.swing.JSpinner();
jSpinner2 = new javax.swing.JSpinner();
jSpinner3 = new javax.swing.JSpinner();
jPanel1 = new javax.swing.JPanel();
jButton1 = new javax.swing.JButton();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("ColorTuner");
jLabel1.setText("Red");

jLabel2.setText("Green");

jLabel3.setText("Blue");

jSpinner1.setFont(new java.awt.Font("Courier New", 0, 18));
jSpinner1.addChangeListener(new javax.swing.event.ChangeListener() {
public void stateChanged(javax.swing.event.ChangeEvent evt) {
jSpinner1StateChanged(evt);
}
});

jSpinner2.setFont(new java.awt.Font("Courier New", 0, 18));
jSpinner2.addChangeListener(new javax.swing.event.ChangeListener() {
public void stateChanged(javax.swing.event.ChangeEvent evt) {
jSpinner2StateChanged(evt);
}
});

jSpinner3.setFont(new java.awt.Font("Courier New", 0, 18));
jSpinner3.addChangeListener(new javax.swing.event.ChangeListener() {
public void stateChanged(javax.swing.event.ChangeEvent evt) {
jSpinner3StateChanged(evt);
}
});

jPanel1.setBackground(new java.awt.Color(204, 255, 51));
jPanel1.setBorder(javax.swing.BorderFactory.createMatteBorder(1, 1, 1, 1, new java.awt.Color(255, 255, 255)));
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 157, Short.MAX_VALUE)
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 90, Short.MAX_VALUE)
);

jButton1.setText("Clear");
jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
jButton1MouseClicked(evt);
}
});
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel2)
.addComponent(jLabel1)
.addComponent(jLabel3))
.addGap(28, 28, 28)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jSpinner1, javax.swing.GroupLayout.PREFERRED_SIZE, 56, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jSpinner2, javax.swing.GroupLayout.PREFERRED_SIZE, 56, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jSpinner3, javax.swing.GroupLayout.PREFERRED_SIZE, 56, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(37, 37, 37)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jButton1))
.addContainerGap())
);

layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jSpinner1, jSpinner2, jSpinner3});

layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(21, 21, 21)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton1)
.addGap(43, 43, 43))
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jLabel1)
.addComponent(jSpinner1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jSpinner2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel2))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 10, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jSpinner3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel3))
.addGap(64, 64, 64))))
);
java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
setBounds((screenSize.width-337)/2, (screenSize.height-219)/2, 337, 219);
}//
//GEN-END:initComponents

private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton1MouseClicked
// TODO add your handling code here:
red=green=blue=255;
changePanelColor();
}//GEN-LAST:event_jButton1MouseClicked

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
// TODO add your handling code here:

}//GEN-LAST:event_jButton1ActionPerformed

private void jSpinner3StateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_jSpinner3StateChanged
// TODO add your handling code here:
blue=modelBlue.getNumber().intValue();
changePanelColor();

}//GEN-LAST:event_jSpinner3StateChanged

private void jSpinner2StateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_jSpinner2StateChanged
// TODO add your handling code here:
green=modelGreen.getNumber().intValue();
changePanelColor();
}//GEN-LAST:event_jSpinner2StateChanged

private void jSpinner1StateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_jSpinner1StateChanged
// TODO add your handling code here:
red=modelRed.getNumber().intValue();
changePanelColor();
}//GEN-LAST:event_jSpinner1StateChanged

/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new FormColor().setVisible(true);
}
});
}

// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton jButton1;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JPanel jPanel1;
private javax.swing.JSpinner jSpinner1;
private javax.swing.JSpinner jSpinner2;
private javax.swing.JSpinner jSpinner3;
// End of variables declaration//GEN-END:variables

}

http://dayaprime.com

Parse from String to Integer

Count1.java

import javax.swing.JOptionPane;
public class Count1{


public static void main (String [] args){


String jml1=JOptionPane.showInputDialog("Enter first number:");
String jml2=JOptionPane.showInputDialog("Enter Second number:");
Integer number=Integer.parseInt(jml1);
Integer number1=Integer.parseInt(jml2);
JOptionPane.showMessageDialog(null,"Hasil:"+(number+number1));
}

}


CobaCount.java

import javax.swing.JOptionPane;
public class CobaCount{
public static void main (String[]args ){
Count1 Countdoank=new Count1();
JOptionPane.showMessageDialog(null,Countdoank.Hasil());
System.exit(0);
}
}

http://dayaprime.com

Date Format

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.text.*;

/**
* A simple AWT GUI to let you try out the
* SimpleDateFormat class.
*/
public class TryDateFormat extends Frame
{
private Label timeCaption;
private Label formatCaption;
private TextField timeValue;
private TextField formatValue;
private Button formatButton;
private Button helpButton;
private Button exitButton;
private TextArea resultArea;
private Dialog helpDialog;

public TryDateFormat() {
super("Try Java SimpleDateFormat!");
Date now = new Date();
Panel ucPanel = new Panel();
Panel uwPanel = new Panel();
Panel usPanel = new Panel();
Panel uPanel = new Panel();

ucPanel.setLayout(new GridLayout(2,1));
uwPanel.setLayout(new GridLayout(2,1));
usPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
uPanel.setLayout(new BorderLayout());
uPanel.setBackground(Color.green);

timeCaption = new Label("Time value:", Label.RIGHT);
timeValue = new TextField("" + now.getTime());
timeValue.setBackground(Color.white);
formatCaption = new Label("Format specifier:", Label.RIGHT);
formatValue = new TextField("yyyyMMddHHmmss");
formatValue.setBackground(Color.white);
formatButton = new Button("Do Format");
helpButton = new Button("Help");
exitButton = new Button("Exit");
resultArea = new TextArea(12,52);
resultArea.setFont(new Font("Monospaced", Font.PLAIN, 12));
resultArea.setBackground(Color.white);

uwPanel.add(timeCaption);
uwPanel.add(formatCaption);
uPanel.add("West", uwPanel);
ucPanel.add(timeValue);
ucPanel.add(formatValue);
uPanel.add("Center", ucPanel);
usPanel.add(formatButton);
usPanel.add(helpButton);
usPanel.add(exitButton);
uPanel.add("South", usPanel);
this.add("North", uPanel);
this.add("Center", resultArea);

helpDialog = new Dialog(this, "TryDateFormat Help", false);
StringBuffer buf = new StringBuffer(1500);
int maxlen = 0;
for(int i = 0; i < help.length; i++) {
buf.append(" "); buf.append(help[i]); buf.append("\n");
if (help[i].length() > maxlen) maxlen = help[i].length();
}
TextArea helptext = new TextArea(20, maxlen + 2);
helptext.setText(buf.toString());
helptext.setEditable(false);
helptext.setFont(new Font("Monospaced", Font.PLAIN, 11));
helpDialog.add("Center", helptext);
Button dismissButton = new Button("Dismiss");
helpDialog.add("South", dismissButton);
helpDialog.pack();


// event handlers
exitButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
} });
helpButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
helpDialog.show();
} });
dismissButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
helpDialog.hide();
} });
helpDialog.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
helpDialog.hide();
} });
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
} });
formatButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
doFormat();
} });

this.pack();
this.show();
}

String help[] = {
"To use this program, enter a time value",
"in the top text field, and a SimpleDateFormat",
"format specifier in the second text field,",
"then click on the Format button. Results",
"appear in the text area at the bottom of",
"the window.",
"",
"The time value must be in seconds since the",
"epoch, midnight 1/1/1970.",
"",
"The format specifier is simply a pattern",
"string; certain letters in the pattern will",
"be replaced by values of time or date, ",
"giving complete control over the formatting.",
"The most useful format codes are:",
"",
"Symbol Meaning Presentation Ex.",
"------ ------- ------------ ----",
"G era designator (Text) AD",
"y year (Number) 1996",
"M month in year (Text) July",
"M month in year (Number) 07",
"d day in month (Number) 10",
"h hour in am/pm (Number) 12",
"H hour in day (Number) 0",
"m minute in hour (Number) 30",
"s second in minute (Number) 55",
"S millisecond (Number) 978",
"E day in week (Text) Tue",
"D day in year (Number) 189",
"a am/pm marker (Text) PM",
"z time zone (Text) PST",
"' quote text (Text) 'at'",
"",
"So, for example this format: h:mm a",
"gives this result: 12:08 PM",
};

public void doFormat() {
long tv = 1;
String tvs = timeValue.getText();
try { tv = Long.parseLong(tvs); }
catch (NumberFormatException nfe) { timeValue.setText("1"); }

String fmt = formatValue.getText();

Date d = new Date(tv);
/* this uses the current Locale, supposedly */
String res = "";
try {
SimpleDateFormat sdf = new SimpleDateFormat(fmt);
res = sdf.format(d);
}
catch (Exception e) {
res = "<<" + e.getMessage() + ">>";
}

resultArea.append("At time: " + tv);
resultArea.append("\n");
resultArea.append("Using format: ");
resultArea.append(fmt);
resultArea.append("\n");
resultArea.append("gives result: ");
resultArea.append(res);
resultArea.append("\n\n");
}

public static void main(String [] args) {
TryDateFormat tdf = new TryDateFormat();
}
}
http://dayaprime.com

Swich function

import javax.swing.JOptionPane;

public class Latihan{

public static void main (String [] args) {

String angka = JOptionPane.showInputDialog("Masukkan Angka : ");
int nilai = Integer.parseInt(angka);
char nilHuruf;

switch(nilai){
case 4 : nilHuruf = 'A';break;
case 3 : nilHuruf = 'B';break;
case 2 : nilHuruf = 'C';break;
case 1 : nilHuruf = 'D';break;
case 0 : nilHuruf = 'E';break;
default : nilHuruf = 'E';break;
}

JOptionPane.showMessageDialog(null, "Nilai Angka = " + nilai +"\n"+ "Nilai Huruf = " + nilHuruf );

}
}
http://dayaprime.com

Prime number

import javax.swing.JOptionPane;

public class Prima {
public static void main (String [] args) {
String numString;
int numInt;
int i;
int jum=0;

numString = JOptionPane.showInputDialog("Masukkan Angka Bilangan Prima : ");
numInt = Integer.parseInt(numString);

for (i=1 ; i<=numInt ; i++){
if (numInt % i == 0) {
jum = jum + 1;
}
}

if (jum==2 && numInt !=1){
JOptionPane.showMessageDialog(null, "Bilangan Prima","Pesan",1);
}

else{JOptionPane.showMessageDialog(null, "Bukan Bilangan Prima","Pesan",1);
}
}
}