Monday, April 16, 2007

Sep 2006 - Part A - Q4

Student aStudent[] = new Student[20];

Thursday, April 12, 2007

Jan 2005 - Part B - Q3

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Divide extends JApplet implements ActionListener{

private JButton aJButton;
private JTextField aText,bText,cText;
private JLabel aLabel,bLabel,cLabel;
private GridLayout aGridLayout;
private JPanel aJPanel;

private double number1,number2;

public void init(){

Container c = getContentPane();

aLabel = new JLabel("Masukkan No:");
bLabel = new JLabel("Masukkan Pembahagi:");
cLabel = new JLabel("Jawapan:");

aText = new JTextField();
bText = new JTextField();
cText = new JTextField();cText.setEditable(false);

aJPanel = new JPanel();
aGridLayout = new GridLayout(3,2);
aJPanel.setLayout(aGridLayout);

aJPanel.add(aLabel);
aJPanel.add(aText);
aJPanel.add(bLabel);
aJPanel.add(bText);
aJPanel.add(cLabel);
aJPanel.add(cText);
c.add(aJPanel,BorderLayout.NORTH);

aJButton = new JButton("HASIL BAHAGI");
aJButton.addActionListener(this);
c.add(aJButton,BorderLayout.CENTER);
}

public void actionPerformed(ActionEvent e){

number1=0;
number2=0;

try{

number1=Double.parseDouble(aText.getText());
number2=Double.parseDouble(bText.getText());

if(number2!=0){
cText.setText(""+number1/number2);
}else if(number2==0)
{
cText.setText("Can't divide by zero!");
}

}catch(NumberFormatException nfe){

cText.setFont(new Font("Arial",Font.PLAIN,10));
cText.setText("Please input numeric value at the above textfields.");

}
}

}

Wednesday, March 28, 2007

Jan 2004 - Part B - Q1a

Sebelum panggilan objek, nilai times ialah 3
n=3
%%%%%%
n=2
%%%%%%
n=1
%%%%%%
Selepas panggilan object, nilai times ialah 3

Jan 2004 - Part B - Q5

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;

public class ChangeText extends JApplet implements ItemListener{
private JCheckBox aBold,aItalic;
private JTextField aTextField;
private FlowLayout aFlowLayout=new FlowLayout();
private int aBol=Font.PLAIN;
private int aIta=Font.PLAIN;
public void init()
{
Container c = getContentPane();
getContentPane().setLayout(aFlowLayout);
aBold = new JCheckBox("Bold");
aItalic = new JCheckBox("Italic");
aTextField = new JTextField("Lihat perubahan teks");

aBold.addItemListener(this);
aItalic.addItemListener(this);
c.add(aTextField);
c.add(aBold);
c.add(aItalic);
}

public void itemStateChanged(ItemEvent e)
{

if(e.getSource()==aBold)
{
if(e.getStateChange()==ItemEvent.SELECTED)
{aBol=Font.BOLD;}else
{aBol=Font.PLAIN;}
}

if(e.getSource()==aItalic)
{
if(e.getStateChange()==ItemEvent.SELECTED)
{aIta=Font.ITALIC;}else
{aIta=Font.PLAIN;}
}

aTextField.setFont(new Font("TimesRoman",aBol+aIta,14));
repaint();

}

}

Tuesday, March 27, 2007

Jan 2004 - Part B - Q2a

class Circle{
private double xCenter;
private double yCenter;
private double radius;

Circle(){
xCenter=2;
yCenter=2;
radius = 1;
}

Circle(double x, double y, double r)
{
xCenter = x;
yCenter = y;
radius = r;
}

double getX(){return xCenter;}
double getY(){return yCenter;}
double getR(){return radius;}

}

public class Circle_2 extends Circle{

public Circle_2(double x,double y,double r){super(x,y,r);}

public void papar(Circle_2 a)
{
System.out.println("xCenter:" + a.getX());
System.out.println("yCenter:" + a.getY());
System.out.println("Radius:" + a.getR());
}

public static void main(String args[])
{
Circle_2 aCircle = new Circle_2(10,10,2);
aCircle.papar(aCircle);
}

}

Jan 2004 - Part B - Q1b

public class Prime{

public boolean isPrime(int n){

boolean prime = true;

for(long i=3;i<=Math.sqrt(n);i=i+2)
{
if(n%i==0){prime=false;}
break;
}

if((n%2!=0 && prime && n>3)|| n==2)
{return true;}
else
{return false;}
}

public void cariPerdana (int dari, int hingga){

for(int i=dari; i<=hingga;i++)
{
if(isPrime(i)==true){System.out.println(i);}
}

}

public static void main(String args[])
{
Prime aPrime = new Prime();

System.out.println("Nombor perdana yang terdapat dari 1 hingga 100 ialah:-");
aPrime.cariPerdana(1,100);

}

}

Monday, March 26, 2007

Sep 2006 - Part B - Q5c

public class NumberHolder{

public int anInt;
public float aFloat;

public static void main(String args[])
{
NumberHolder aNumberHolder = new NumberHolder();
aNumberHolder.anInt = 10;
aNumberHolder.aFloat = 10.5f;

System.out.println("anInt:" + aNumberHolder.anInt);
System.out.println("aFloat:" + aNumberHolder.aFloat);
}

}

Blog Archive