Code 34: Composition
Code a:
public class Test{
public static void main(String args[]){
Class3 Class3Object = new Class3(4,5,6);
}
}
Code b:
public class Class3 {
private int month;
private int day;
private int year;
public Class3(int m, int d, int y){
month = m;
day = d;
year = y;
System.out.printf("This Constructor for this is %s\n",this);
}
public String toString(){
return String.format("%d/%d/%d", month, day, year);
}
}
--------------------------------------------------
Code 35: Enum
Enum - Declare Constants
Code a:
public class Test{
public static void main(String args[]){
// Note use of enhanced for loop
for (Class2 people : Class2.values()){
System.out.printf("%s\t%s\t%s\n", people, people.getName(),people.getDesc());
}
}
}
Code b:
public enum Class2{
Name1("This is Name 1", "This is String 1 a"),
Name2("This is Name 2", "This is String 2 a"),
Name3("This is Name 3", "This is String 3 a");
private final String name;
private final String description;
Class2(String n, String d){
name = n;
description = d;
}
public String getName(){
return name;
}
public String getDesc(){
return description;
}
}
Code 36: Enum - Range Sets
import java.util.EnumSet;
public class Test{
public static void main(String args[]){
// Note use of enhanced for loop
for (Class2 people : Class2.values())
System.out.printf("%s\t%s\t%s\n", people, people.getName(),people.getDesc());
System.out.println("\nAnd now demonstrating the Enum for Range \n");
for (Class2 people : EnumSet.range(Class2.Name2, Class2.Name5))
System.out.printf("%s\t%s\t%s\n", people, people.getName(),people.getDesc());
}
}
Code b:
public enum Class2{
Name1("This is Name 1", "This is String 1 a"),
Name2("This is Name 2", "This is String 2 a"),
Name3("This is Name 3", "This is String 3 a"),
Name4("This is Name 4", "This is String 4 a"),
Name5("This is Name 5", "This is String 5 a"),
Name6("This is Name 6", "This is String 6 a"),
Name7("This is Name 7", "This is String 7 a");
private final String name;
private final String description;
Class2(String n, String d){
name = n;
description = d;
}
public String getName(){
return name;
}
public String getDesc(){
return description;
}
}
Code 37: Static
Code a:
public class Test{
public static void main(String args[]){
Class2 member1 = new Class2("Rohit", "Sharma");
Class2 member2 = new Class2("Shikhar", "Dhawan");
Class2 member3 = new Class2("Virat", "Kohli");
Class2 member4 = new Class2("MS", "Dhoni");
}
}
Code b:
public class Class2{
private String first;
private String last;
private static int members= 0;
public Class2(String fn, String ln){
first = fn;
last = ln;
members++;
System.out.printf("New members in Team INDIA %s %s , Count of Members as of now %d \n", first,last, members);
}
}
Code 38: Final
public class Test{
public static void main(String args[]){
Class2 Class2Object = new Class2(10);
for(int i=0; i<5;i++){
Class2Object.add();
System.out.printf("Sum is %s" , Class2Object);
}
}
}
Code b:
public class Class2{
private int total;
private final int NUMBER;
public Class2(int x){
NUMBER=x;
}
public void add(){
total+=NUMBER;
}
public String toString(){
return String.format("Sum is %d\n", total);
}
}
Code 39: Inheritance
Code a:
public class Test{
public static void main(String args[]){
Class2 Class2Object = new Class2();
Class2Object.inherit();
}
}
Code b:
public class Class2 extends Class3{
}
Code c:
public class Class3 {
public void inherit(){
System.out.println("This is Class 3 Ans i am the Super Class");
}
}
Code 40: GUI - Graphical User Interface
import javax.swing.JOptionPane;
public class Test{
public static void main(String args[]){
String fn = JOptionPane.showInputDialog("Enter First Number");
String sn = JOptionPane.showInputDialog("Enter Second Number");
int num1 = Integer.parseInt(fn);
int num2 = Integer.parseInt(sn);
int sum = num1+num2;
JOptionPane.showMessageDialog(null, "The Sum of Two numbers is " +sum, "Sum", JOptionPane.PLAIN_MESSAGE);
}
}
Code 41: GUI with JFrame
Code a:
import javax.swing.JFrame;
public class Test{
public static void main(String args[]){
Class2 Class2Object = new Class2();
Class2Object.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Class2Object.setSize(275,275);
Class2Object.setVisible(true);
}
}
Code b:
import java.awt.FlowLayout;
// JFrame Gives you basic window features
import javax.swing.JFrame;
import javax.swing.JLabel;
public class Class2 extends JFrame{
private JLabel item1;
public Class2(){
super("This is my Title");
//Gives us default Layout
setLayout(new FlowLayout());
item1 = new JLabel("This is a sample Label / text");
item1.setToolTipText("This is a Tooltip");
//Add item to window
add(item1);
}
}
Code 42: Event handling , Action Listener & Event Handling program
Code a:
import javax.swing.JFrame;
public class Test{
public static void main(String args[]){
Class2 Class2Object = new Class2();
Class2Object.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Class2Object.setSize(275,275);
Class2Object.setVisible(true);
}
}
Code b:
import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
// JFrame Gives you basic window features
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JOptionPane;
public class Class2 extends JFrame{
private JTextField item1;
private JTextField item2;
private JTextField item3;
private JPasswordField passwordField;
public Class2(){
super("This is my Title");
//Gives us default Layout
setLayout(new FlowLayout());
item1 = new JTextField(10);
add(item1);
item2 = new JTextField("Eneter Text here");
add(item2);
item3 = new JTextField("Uneditable",20);
item3.setEditable(false);
add(item3);
passwordField = new JPasswordField("my password",20);
add(passwordField);
thehandler handler = new thehandler();
item1.addActionListener(handler);
item2.addActionListener(handler);
item3.addActionListener(handler);
passwordField.addActionListener(handler);
}
private class thehandler implements ActionListener{
public void actionPerformed(ActionEvent event){
String string = "";
if (event.getSource()==item1)
string=String.format("Field 1 : %s", event.getActionCommand());
else if (event.getSource()==item2)
string=String.format("Field 2 : %s", event.getActionCommand());
else if (event.getSource()==item3)
string=String.format("Field 3 : %s", event.getActionCommand());
else if (event.getSource()==passwordField)
string=String.format("Password : %s", event.getActionCommand());
JOptionPane.showMessageDialog(null, string);
}
}
}
No comments:
Post a Comment