Friday, 4 October 2013

Sample Color MENU solution

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

public class MenuColor extends JFrame {

JPanel panel1 ;
JMenuBar menubar;
JMenu   colorM ;
JMenuItem blueMI , redMI , greenMI , yellowMI ;

public MenuColor() {
super("Menu Color");


panel1 = new JPanel();

blueMI = new JMenuItem("BLUE");
redMI = new JMenuItem("RED");
greenMI = new JMenuItem("GREEN");
yellowMI = new JMenuItem("YELLOW");
colorM = new JMenu("COLOR");
menubar = new JMenuBar() ;

colorListener newCL = new colorListener();

blueMI.addActionListener(newCL);
yellowMI.addActionListener(newCL);
greenMI.addActionListener(newCL);
redMI.addActionListener(newCL);

/*
blueMI.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
panel1.setBackground(Color.BLUE );
   
}
});

greenMI.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
panel1.setBackground(Color.GREEN );
   
}
});
*/





Container c = getContentPane();
c.add(panel1);


colorM.add(blueMI);
colorM.add(greenMI);
colorM.add(redMI);
colorM.add(yellowMI);

   menubar.add(colorM);
 
setJMenuBar(menubar);
setVisible(true);
}


class colorListener implements ActionListener{
public void actionPerformed(ActionEvent e){
  if ( e.getSource().equals(greenMI) )
  panel1.setBackground(Color.GREEN);
  else if ( e.getSource().equals(blueMI) )
  panel1.setBackground(Color.BLUE);
  else if ( e.getSource().equals(redMI) )
  panel1.setBackground(Color.RED);
  else if ( e.getSource().equals(yellowMI) )
      panel1.setBackground(Color.YELLOW);
  else
panel1.setBackground(Color.YELLOW);
 }

}






public static void main (String args []){


  new MenuColor() ;

}

}

Monday, 23 September 2013

Lab 5 - Exercise 2 - Hockey & Cricket

Performing set operations on hash sets
a) Allow user to enter two set of staff names, one for those who play cricket and one for those
who play tennis.
b) Read and store the names in two HashSet objects.
c) Using these two sets perform the necessary operations to print the following:
i. The names of staff playing both cricket and tennis
ii. The names of staff playing either one or both
iii. The names of staff playing cricket but not tennis
iv. The names of staff playing tennis but not cricket

Thursday, 28 March 2013

LAB 5 -exercise 1 - ArrayList -July 2013


Array List Exercise ( LAB 4 )
--------------------------------------------------------------------------------------------------------
1) Create 1 class and 2 subclasses of that class . Implement appropriate polymorphic method. the method should print out something.
2) Creating an ArrayList of the specific superclass of above
3) Add 5 Item/Element/object of superclass/subclasses into the above ArrayList
4) Check and print the size of ArrayList
5) Check and print all the item inside the array list.
6) Check Index of an Item in Java Arraylist
7) Retriev Item from arrayList in a loop ( without using iterator interface )
8) Check ArrayList for an Item
9) Check if ArrayList is Empty
.
10) Remove an Item from ArrayList
11) Copy data from one ArrayList to another ArrayList in Java
12) Replace an element at a particular index
13) Clear all data from ArrayList
14) Convert from ArrayList to Array in Java
15) Traverse ArrayList in Java using iterator

Sunday, 17 March 2013

Lab 3 July 2013 - Additional Exercise 3



  1. Firstly, create and compile a simple class called Parent. Give it the following behaviour:
    1. A default constructor that does nothing other than print out “Parent default constructor” using System.out
    2. A single method called getMessage which returns a String, e.g. “Parent message”
  2. Next, create and compile a class called Child. Give it the following behaviour
    1. Do not give it a constructor
    2. Override the parent’s getMessage method to return an alternative String. E.g. “Child message”
    3. A main method which creates an instance of the Child object, and then writes the value returned by its getMessage method to the command line.
    4. What happens when the class is run?
  3. Alter the Child class to give it a default constructor which prints out “Child default constructor”. Compile and run the application again and identify what happens.
  4. Alter the implementation of the getMessage method in the Child class so that it first calls the parent class method, then concatenates the result with its own value to build a combined message. E.g. returning “Parent message and Child message”.
  5. Alter the Parent class by
    1. Adding a new constructor that accepts a String argument. This should be used to initialise a private member variable, myMessage. Again write a message to the console that indicates that the constructor has been called.
    2. Alter the getMessage method so that it returns the value of the myMessage rather than a fixed message.
    3. Now alter the Child class so that it from its default constructor it calls the new constructor on the parent class.
  6. Alter the myMessage member variable in the Parent class so that it is declared to be protected. Confirm that the Child class can now refer to the variable directly, rather than having to call its parent’s version of getMessage to build the combined message. Can you think of the pros and cons of the two different mechanisms?

Sunday, 21 October 2012

Lab 14- recursive -2013

Using recursive style , create a class that receive from a user a string and a number   then  have recursive  methods that :

1.  reverse the string recursively.  eg prog  become gorp
2.  count  the number of character in the screen recursively  eg.  prog = 4
3.  using a number entered , create a fibonacci  recursive  that return the result of that particular position of the number you entered.  eg 0 = 0 , 2 = 1 , 3 =3

Monday, 19 March 2012

LAB 03 July 2013 - Interface


Consider the following Printface interface, which is designed to standard printing status method for the class that implements it :

public interface Printface {
              public double goprint() ;
}

a) Write an abstract class CollegeMembers that implements Printface interface. This class also have a instance variable Name and ID. Goprint() is set as abstract method here and is not being implemented here.

b) Write a concrete subclass Lecturer that provide a concrete representation of the Collegemembers but does not provide any additional functionality . Implement Goprint() by printing the name and ID of the lecturer.

c) Write a concrete subclass Student that extends CollegeMembers and have additional instance variables passportno (string) , mykadno(string) and nationality (string) . Create necessary constructor for the class. This class should implement Goprint() based the rules below :
If nationality is not malaysian , print name, id and passport number
Else print name , id and mykadno

d) Create a driver class and write declarations for a reference lecturerA of type Lecturer and StudentB of type Student. Create new object for both class and refer the new objects by reference lecturerA and StudentB. Call both Goprint for each object.

Wednesday, 14 March 2012

P2 lab 1 2013

1. Create a class with at least 2 attribute  and 2 methods.
2. Inside the constructor , as for user input for your attributes.
3. Handle the exception correctly , throw it to the caller.
4. Create a driver class that create two object from the above class  ( 1 ) .