public class LocalClassExample {
static String regularExpression = "[^0-9]";
public static void validatePhoneNumber(
String phoneNumber1, String phoneNumber2) {
final int numberLength = 10;
// Valid in Java SE 8 and later:
// int numberLength = 10;
class PhoneNumber {
String formattedPhoneNumber = null;
PhoneNumber(String phoneNumber) {
// numberLength = 7;
String currentNumber = phoneNumber.replaceAll(
regularExpression, "");
if (currentNumber.length() == numberLength)
formattedPhoneNumber = currentNumber;
else
formattedPhoneNumber = null;
}
public String getNumber() {
return formattedPhoneNumber;
}
// Valid in Java SE 8 and later:
// public void printOriginalNumbers() {
// System.out.println("Original numbers are " + phoneNumber1 +
// " and " + phoneNumber2);
// }
}
PhoneNumber myNumber1 = new PhoneNumber(phoneNumber1);
PhoneNumber myNumber2 = new PhoneNumber(phoneNumber2);
// Valid in Java SE 8 and later:
// myNumber1.printOriginalNumbers();
if (myNumber1.getNumber() == null)
System.out.println("First number is invalid");
else
System.out.println("First number is " + myNumber1.getNumber());
if (myNumber2.getNumber() == null)
System.out.println("Second number is invalid");
else
System.out.println("Second number is " + myNumber2.getNumber());
}
public static void main(String... args) {
validatePhoneNumber("123-456-7890", "456-7890");
}
}
Tuesday, July 2, 2013
Local Class Example;
More on Classes
More on Classes
This section covers more aspects of classes that depend on using object references and the
Increased encapsulation—Consider two top-level classes, A and B, where B needs access to members of A that would otherwise be declared
More readable, maintainable code—Nesting small classes within top-level classes places the code closer to where it is used.
Note: A static nested class interacts with the instance members of its outer class (and other classes) just like any other top-level class. In effect, a static nested class is behaviorally a top-level class that has been nested in another top-level class for packaging convenience.
Static nested classes are accessed using the enclosing class name:
For example, to create an object for the static nested class, use this syntax:
Objects that are instances of an inner class exist within an instance of the outer class. Consider the following classes:
An instance of

To
instantiate an inner class, you must first instantiate the outer class.
Then, create the inner object within the outer object with this syntax:
Additionally, there are two special kinds of inner classes:
local classes and
anonymous classes.
Note: If you want more information on the taxonomy of the different kinds of classes in the Java programming language (which can be tricky to describe concisely, clearly, and correctly), you might want to read Joseph Darcy's blog: Nested, Inner, Member and Top-Level Classes.
dot operator that you learned about in the preceding sections on objects:- Returning values from methods.
- The
thiskeyword. - Class vs. instance members.
- Access control.
- The Java programming language allows you to define a class within another class. Such a class is called a nested class and is illustrated here:
Nested Classes
class OuterClass { ... class NestedClass { ... } }
Terminology: Nested classes are divided into two categories: static and non-static. Nested classes that are declaredstaticare simply called static nested classes. Non-static nested classes are called inner classes.
A nested class is a member of its enclosing class. Non-static nested classes (inner classes) have access to other members of the enclosing class, even if they are declared private. Static nested classes do not have access to other members of the enclosing class. As a member of theclass OuterClass { ... static class StaticNestedClass { ... } class InnerClass { ... } }OuterClass, a nested class can be declaredprivate,public,protected, or package private. (Recall that outer classes can only be declaredpublicor package private.)
Why Use Nested Classes?
There are several compelling reasons for using nested classes, among them:
- It is a way of logically grouping classes that are only used in one place.
- It increases encapsulation.
- Nested classes can lead to more readable and maintainable code.
Increased encapsulation—Consider two top-level classes, A and B, where B needs access to members of A that would otherwise be declared
private.
By hiding class B within class A, A's members can be declared private
and B can access them. In addition, B itself can be hidden from the
outside world.More readable, maintainable code—Nesting small classes within top-level classes places the code closer to where it is used.
Static Nested Classes
As with class methods and variables, a static nested class is associated with its outer class. And like static class methods, a static nested class cannot refer directly to instance variables or methods defined in its enclosing class — it can use them only through an object reference.Note: A static nested class interacts with the instance members of its outer class (and other classes) just like any other top-level class. In effect, a static nested class is behaviorally a top-level class that has been nested in another top-level class for packaging convenience.
OuterClass.StaticNestedClass
OuterClass.StaticNestedClass nestedObject =
new OuterClass.StaticNestedClass();
Inner Classes
As with instance methods and variables, an inner class is associated with an instance of its enclosing class and has direct access to that object's methods and fields. Also, because an inner class is associated with an instance, it cannot define any static members itself.Objects that are instances of an inner class exist within an instance of the outer class. Consider the following classes:
class OuterClass {
...
class InnerClass {
...
}
}
InnerClass can exist only within an instance of OuterClass and has direct access to the methods and fields of its enclosing instance.
The next figure illustrates this idea.
An Instance of InnerClass Exists Within an Instance of OuterClass
OuterClass.InnerClass innerObject = outerObject.new InnerClass();
Note: If you want more information on the taxonomy of the different kinds of classes in the Java programming language (which can be tricky to describe concisely, clearly, and correctly), you might want to read Joseph Darcy's blog: Nested, Inner, Member and Top-Level Classes.
Shadowing
If a declaration of a type (such as a member variable or a parameter name) in a particular scope (such as an inner class or a method definition) has the same name as another declaration in the enclosing scope, then the declaration shadows the declaration of the enclosing scope. You cannot refer to a shadowed declaration by its name alone. The following example,ShadowTest, demonstrates this:
public class ShadowTest {
public int x = 0;
class FirstLevel {
public int x = 1;
void methodInFirstLevel(int x) {
System.out.println("x = " + x);
System.out.println("this.x = " + this.x);
System.out.println("ShadowTest.this.x = " + ShadowTest.this.x);
}
}
public static void main(String... args) {
ShadowTest st = new ShadowTest();
ShadowTest.FirstLevel fl = st.new FirstLevel();
fl.methodInFirstLevel(23);
}
}
The following is the output of this example:x = 23 this.x = 1 ShadowTest.this.x = 0This example defines three variables named
x: The member variable of the class ShadowTest, the member variable of the inner class FirstLevel, and the parameter in the method methodInFirstLevel. The variable x defined as a parameter of the method methodInFirstLevel shadows the variable of the inner class FirstLevel. Consequently, when you use the variable x in the method methodInFirstLevel, it refers to the method parameter. To refer to the member variable of the inner class FirstLevel, use the keyword this to represent the enclosing scope:System.out.println("this.x = " + this.x);
Refer to member variables that enclose larger scopes by the class
name to which they belong. For example, the following statement accesses
the member variable of the class ShadowTest from the method methodInFirstLevel:System.out.println("ShadowTest.this.x = " + ShadowTest.this.x);
Local Classes
Local classes are classes that are defined in a block, which is a group of zero or more statements between balanced braces. You typically find local classes defined in the body of a method.
This section covers the following topics:
- Declaring Local Classes
- Accessing Members of Enclosing Class
- Shadowing
- Local Classes Are Non-Static
Declaring Local Classes
You can define a local class inside any block (which is a group of zero or more statements between balanced braces; see Expressions, Statements, and Blocks for more information). For example, you can define a local class in a method body, aforloop, or anifclause.
The following example,LocalClassExample, validates two phone numbers. It defines the local classPhoneNumberin the methodvalidatePhoneNumber:
public class LocalClassExample { static String regularExpression = "[^0-9]"; public static void validatePhoneNumber( String phoneNumber1, String phoneNumber2) { final int numberLength = 10; // Valid in Java SE 8 and later: // int numberLength = 10; class PhoneNumber { String formattedPhoneNumber = null; PhoneNumber(String phoneNumber) { // numberLength = 7; String currentNumber = phoneNumber.replaceAll( regularExpression, ""); if (currentNumber.length() == numberLength) formattedPhoneNumber = currentNumber; else formattedPhoneNumber = null; } public String getNumber() { return formattedPhoneNumber; } // Valid in Java SE 8 and later: // public void printOriginalNumbers() { // System.out.println("Original numbers are " + phoneNumber1 + // " and " + phoneNumber2); // } } PhoneNumber myNumber1 = new PhoneNumber(phoneNumber1); PhoneNumber myNumber2 = new PhoneNumber(phoneNumber2); // Valid in Java SE 8 and later: // myNumber1.printOriginalNumbers(); if (myNumber1.getNumber() == null) System.out.println("First number is invalid"); else System.out.println("First number is " + myNumber1.getNumber()); if (myNumber2.getNumber() == null) System.out.println("Second number is invalid"); else System.out.println("Second number is " + myNumber2.getNumber()); } public static void main(String... args) { validatePhoneNumber("123-456-7890", "456-7890"); } }The example validates a phone number by first removing all characters from the phone number except the digits 0 through 9. After, it checks whether the phone number contains exactly ten digits (the length of a phone number in North America). This example prints the following:
First number is 1234567890 Second number is invalid
Accessing Members of Enclosing Class
A local class has access to the members of its enclosing class. In the previous example, thePhoneNumberconstructor accesses the memberLocalClassExample.regularExpression.
In addition, a local class has access to local variables. However, a local class can only access local variables that are declared final. For example, thePhoneNumberconstructor can access the local variablenumberLengthbecause it is declared final.
Starting in Java SE 8, if you declare the local class in a method, it can access the method's parameters. For example, you can define the following method in thePhoneNumberlocal class:
public void printOriginalNumbers() { System.out.println("Original numbers are " + phoneNumber1 + " and " + phoneNumber2); }The methodprintOriginalNumbersaccesses the method parametersphoneNumber1andphoneNumber2.
Starting in Java SE 8, a local class can access local variables and parameters of the enclosing block that are final or effectively final. A variable or parameter whose value is never changed after it is initialized is effectively final. For example, suppose the variablenumberLengthis not declared final, and you add the highlighted assignment statement in thePhoneNumberconstructor:
PhoneNumber(String phoneNumber) { numberLength = 7; String currentNumber = phoneNumber.replaceAll( regularExpression, ""); if (currentNumber.length() == numberLength) formattedPhoneNumber = currentNumber; else formattedPhoneNumber = null; }Because of this assignment statement, the variablenumberLengthis not effectively final anymore. As a result, the Java compiler generates an error message similar to "local variables referenced from an inner class must be final or effectively final" where the inner classPhoneNumbertries to access thenumberLengthvariable:
if (currentNumber.length() == numberLength)
Shadowing
Declarations of a type (such as a variable) in a local class shadow declarations in the enclosing scope that have the same name. See Shadowing for more information.
Local Classes Are Inner Classes
Local classes are like inner classes because they cannot define or declare any static members. Local classes in static methods, like classPhoneNumber, which is defined in the static methodvaidatePhoneNumber, can only refer to static members of the enclosing class. For example, if you do not define the member variableregularExpressionas static, the Java compiler generates an error similar to "non-static variableregularExpressioncannot be referenced from a static context."
Local classes are non-static because they have access to instance members of the enclosing block. Consequently, they cannot contain most kinds of static declarations.
You cannot declare an interface inside a block; interfaces are inherently static. For example, the following code excerpt does not compile because the interfaceHelloThereis defined inside the body of the methodgreetInEnglish:
public void greetInEnglish() { interface HelloThere { public void greet(); } class EnglishHelloThere implements HelloThere { public void greet() { System.out.println("Hello " + name); } } HelloThere myGreeting = new EnglishHelloThere(); myGreeting.greet(); }You cannot declare static initializers or member interfaces in a local class. The following code excerpt does not compile because the methodEnglishGoodbye.sayGoodbyeis declaredstatic. The compiler generates an error similar to "modifier 'static' is only allowed in constant variable declaration" when it encounters this method definition:
public void sayGoodbyeInEnglish() { class EnglishGoodbye { public static void sayGoodbye() { System.out.println("Bye bye"); } } EnglishGoodbye.sayGoodbye(); }A local class can have static members provided that they are constant variables. (A constant variable A variable is a variable of primitive type or typeStringthat is declared final and initialized with a compile-time constant expression. A compile-time constant expression is typically a string or an arithmetical expression that can be evaluated at compile time. See Understanding Instance and Class Members for more information.) The following code excerpt compiles because the static memberEnglishGoodbye.farewellis a constant variable:
public void sayGoodbyeInEnglish() { class EnglishGoodbye { public static final String farewell = "Bye bye"; public void sayGoodbye() { System.out.println(farewell); } } EnglishGoodbye myEnglishGoodbye = new EnglishGoodbye(); myEnglishGoodbye.sayGoodbye(); }
Monday, July 1, 2013
Objects
Objects
A typical Java program creates many objects, which as you know,
interact by invoking methods. Through these object interactions, a
program can carry out various tasks, such as implementing a GUI, running
an animation, or sending and receiving information over a network. Once
an object has completed the work for which it was created, its
resources are recycled for use by other objects.
Here's a small program, called
This program creates, manipulates, and displays information about various objects. Here's the output:
The following three sections use the above example to describe the
life cycle of an object within a program. From them, you will learn how
to write code that creates and uses objects in your own programs. You
will also learn how the system cleans up after an object when its life
has ended.
Here's a small program, called
CreateObjectDemo, that creates three objects: one
Point object and two
Rectangle objects. You will need all three source files to compile this program.public class CreateObjectDemo {
public static void main(String[] args) {
// Declare and create a point object and two rectangle objects.
Point originOne = new Point(23, 94);
Rectangle rectOne = new Rectangle(originOne, 100, 200);
Rectangle rectTwo = new Rectangle(50, 100);
// display rectOne's width, height, and area
System.out.println("Width of rectOne: " + rectOne.width);
System.out.println("Height of rectOne: " + rectOne.height);
System.out.println("Area of rectOne: " + rectOne.getArea());
// set rectTwo's position
rectTwo.origin = originOne;
// display rectTwo's position
System.out.println("X Position of rectTwo: " + rectTwo.origin.x);
System.out.println("Y Position of rectTwo: " + rectTwo.origin.y);
// move rectTwo and display its new position
rectTwo.move(40, 72);
System.out.println("X Position of rectTwo: " + rectTwo.origin.x);
System.out.println("Y Position of rectTwo: " + rectTwo.origin.y);
}
}
Width of rectOne: 100 Height of rectOne: 200 Area of rectOne: 20000 X Position of rectTwo: 23 Y Position of rectTwo: 94 X Position of rectTwo: 40 Y Position of rectTwo: 72
Classes and Objects
Lesson: Classes and Objects
You will learn to use your classes to create objects, and how to use the objects you create.
This lesson also covers nesting classes within other classes, and enumerations
The introduction to object-oriented concepts in the lesson titled Object-oriented Programming Concepts used a bicycle class as an example, with racing bikes, mountain bikes, and tandem bikes as subclasses. Here is sample code for a possible implementation of a
Bicycle class, to give you an overview of a class declaration. Subsequent sections of this
lesson
will back up and explain class declarations step by step. For the moment, don't concern yourself with the details.This section shows you the anatomy of a class, and how to declare fields, methods, and constructors.
public class Bicycle {
// the Bicycle class has
// three fields
public int cadence;
public int gear;
public int speed;
// the Bicycle class has
// one constructor
public Bicycle(int startCadence, int startSpeed, int startGear) {
gear = startGear;
cadence = startCadence;
speed = startSpeed;
}
// the Bicycle class has
// four methods
public void setCadence(int newValue) {
cadence = newValue;
}
public void setGear(int newValue) {
gear = newValue;
}
public void applyBrake(int decrement) {
speed -= decrement;
}
public void speedUp(int increment) {
speed += increment;
}
}
MountainBike class that is a subclass of Bicycle might look like this:public class MountainBike extends Bicycle {
// the MountainBike subclass has
// one field
public int seatHeight;
// the MountainBike subclass has
// one constructor
public MountainBike(int startHeight, int startCadence,
int startSpeed, int startGear) {
super(startCadence, startSpeed, startGear);
seatHeight = startHeight;
}
// the MountainBike subclass has
// one method
public void setHeight(int newValue) {
seatHeight = newValue;
}
}
MountainBike inherits all the fields and methods of Bicycle and adds the field seatHeight and a method to set it (mountain bikes have seats that can be moved up and down as the terrain demands).Monday, June 24, 2013
Constructors...
Difference between default constructor & empty constructor
in java...
|
When we don't create a
constructor Java creates a default constructor. But when we create one or
more constructors Java doesn't create any default constructors... If we
create one or more constructors and we want to create a object without any
constructor We have to declare a empty constructor...
an example is shown below....
class Constructor{ String name; //declaring attributes int age; String address; String school; /////////////////////////////////// /*default constructor is something like this public Constructor() { } */ ////////////////////////////////// public Constructor(String name1,int age1){ name = name1; age = age1; System.out.println("My name is: "+name); System.out.println("I am "+age+" years old."); } public Constructor(String address1){ address = address1; System.out.println("My address is: "+address); } //////////////////////////////////// //When we creates constructors & if we want to make a object without constructor //we have to declare a empty constructor... public Constructor() { } /////////////////////////////////// public void school(String school1){ school = school1; System.out.println("I studied at "+school+" ."); } public static void main(String [] args){ Constructor student = new Constructor("Manathunga",19); Constructor place = new Constructor("Colombo,Srilanka"); Constructor empty = new Constructor(); //from empty constructor... empty.school("Sivali central college"); } } Hint : We put ( ) brackets after a object is created
ex : Constructor
empty = new Constructor();
We can put these brackets without any hesitation because
java makes a default constructor.But we can't see it...
ex: public
Constructor()
{ }
|
Advance Calculator (Java Platform);
import java.awt.*;
import java.awt.event.*;
class Cal extends Frame implements ActionListener{
Button bMc = new Button("MC");
Button bMPlus = new Button("M+");
Button bMMin = new Button("M-");
Button bBkcs = new Button("<--");
Button bCe = new Button("CE");
Button bPlusOrEqual = new Button("+/-");
Button bSquareRoot = new Button("v");
Button bPresentage = new Button("%");
Button b11 = new Button("7");
Button b12 = new Button("8");
Button b13 = new Button("9");
Button bDivide = new Button("/");
Button b16 = new Button("4");
Button b17 = new Button("5");
Button b18 = new Button("6");
Button bMultiply = new Button("*");
Button b21 = new Button("1");
Button b22 = new Button("2");
Button b23 = new Button("3");
Button bMinus = new Button("-");
Button bPlus = new Button("+");
Button b26 = new Button("0");
Button bPoint = new Button(".");
Button bEquals = new Button("=");
TextField t = new TextField(25);
String fun1,fun2,fun3,fun4,fun5;
Cal(){
Panel p1 = new Panel();
Panel p2 = new Panel();
setSize(25,100);
add(p1,BorderLayout.NORTH);
add(p2,BorderLayout.CENTER);
GridLayout g = new GridLayout(6,4);
bMc.addActionListener(this);
bMPlus.addActionListener(this);
bMMin.addActionListener(this);
bBkcs.addActionListener(this);
bCe.addActionListener(this);
bPlusOrEqual.addActionListener(this);
bSquareRoot.addActionListener(this);
b11.addActionListener(this);
b12.addActionListener(this);
b13.addActionListener(this);
bDivide.addActionListener(this);
bPresentage.addActionListener(this);
b16.addActionListener(this);
b17.addActionListener(this);
b18.addActionListener(this);
bMultiply.addActionListener(this);
b21.addActionListener(this);
b22.addActionListener(this);
b23.addActionListener(this);
bMinus.addActionListener(this);
bPlus.addActionListener(this);
b26.addActionListener(this);
bPoint.addActionListener(this);
bEquals.addActionListener(this);
p2.setLayout(g);
p1.add(t);
p2.add(bMc);
p2.add(bMPlus);
p2.add(bMMin);
p2.add(bBkcs);
p2.add(bCe);
p2.add(bPlusOrEqual);
p2.add(bSquareRoot);
p2.add(bPresentage);
p2.add(b11);
p2.add(b12);
p2.add(b13);
p2.add(bDivide);
p2.add(b16);
p2.add(b17);
p2.add(b18);
p2.add(bMultiply);
p2.add(b21);
p2.add(b22);
p2.add(b23);
p2.add(bMinus);
p2.add(bPlus);
p2.add(b26);
p2.add(bPoint);
p2.add(bEquals);
setSize(300,400);
setBackground(Color.green);
}
public void actionPerformed(ActionEvent e){
if(e.getSource().equals(b11)){
t.setText(t.getText() + e.getActionCommand());
}else if (e.getSource().equals(b12)){
t.setText(t.getText() + e.getActionCommand());
}else if (e.getSource().equals(b13)){
t.setText(t.getText() + e.getActionCommand());
}else if (e.getSource().equals(b16)){
t.setText(t.getText() + e.getActionCommand());
}else if (e.getSource().equals(b17)){ t.setText(t.getText() + e.getActionCommand());
}else if (e.getSource().equals(b18)){
t.setText(t.getText() + e.getActionCommand());
}else if (e.getSource().equals(b21)){
t.setText(t.getText() + e.getActionCommand());
}else if (e.getSource().equals(b22)){
t.setText(t.getText() + e.getActionCommand());
}else if (e.getSource().equals(b23)){
t.setText(t.getText() + e.getActionCommand());
}else if (e.getSource().equals(bPoint)){
t.setText(t.getText() + e.getActionCommand());
}else if (e.getSource().equals(b26)){
t.setText(t.getText() + e.getActionCommand());
}else if (bPlus.equals(e.getSource())){
fun1 = t.getText();
t.setText(null);
}else if(bEquals.equals(e.getSource())){
String Val = String.valueOf(Integer.parseInt(fun1) + Integer.parseInt(t.getText()));
t.setText(Val);
}else if(bEquals.equals(e.getSource())){
String Val = String.valueOf(Integer.parseInt(fun1) + Integer.parseInt(t.getText()));
t.setText(Val);
}else if (bCe.equals(e.getSource())){
fun2 = t.getText();
t.setText(null);
}else if(bCe.equals(e.getSource())){
String Val = String.valueOf(Integer.parseInt(fun2) + Integer.parseInt(t.getText()));
t.setText(Val);
}else if(bEquals.equals(e.getSource())){
String Val = String.valueOf(Integer.parseInt(fun2) + Integer.parseInt(t.getText()));
t.setText(Val);
}else if (bMinus.equals(e.getSource())){
fun3 = t.getText();
t.setText(null);
}else if(bCe.equals(e.getSource())){
String Val = String.valueOf(Integer.parseInt(fun3) - Integer.parseInt(t.getText()));
t.setText(Val);
}else if(bEquals.equals(e.getSource())){
String Val = String.valueOf(Integer.parseInt(fun3) - Integer.parseInt(t.getText()));
t.setText(Val);
}else if (bMultiply.equals(e.getSource())){
fun4 = t.getText();
t.setText(null);
}else if(bCe.equals(e.getSource())){
String Val = String.valueOf(Integer.parseInt(fun4) * Integer.parseInt(t.getText()));
t.setText(Val);
}else if(bEquals.equals(e.getSource())){
String Val = String.valueOf(Integer.parseInt(fun4) * Integer.parseInt(t.getText()));
t.setText(Val);
}else if (bDivide.equals(e.getSource())){
fun5 = t.getText();
t.setText(null);
}else if(bCe.equals(e.getSource())){
String Val = String.valueOf(Integer.parseInt(fun5) / Integer.parseInt(t.getText()));
t.setText(Val);
}else if(bEquals.equals(e.getSource())){
String Val = String.valueOf(Integer.parseInt(fun5) / Integer.parseInt(t.getText()));
t.setText(Val);
}
}
public static void main(String args[]){
Cal c = new Cal();
c.setVisible(true);
}
}
import java.awt.event.*;
class Cal extends Frame implements ActionListener{
Button bMc = new Button("MC");
Button bMPlus = new Button("M+");
Button bMMin = new Button("M-");
Button bBkcs = new Button("<--");
Button bCe = new Button("CE");
Button bPlusOrEqual = new Button("+/-");
Button bSquareRoot = new Button("v");
Button bPresentage = new Button("%");
Button b11 = new Button("7");
Button b12 = new Button("8");
Button b13 = new Button("9");
Button bDivide = new Button("/");
Button b16 = new Button("4");
Button b17 = new Button("5");
Button b18 = new Button("6");
Button bMultiply = new Button("*");
Button b21 = new Button("1");
Button b22 = new Button("2");
Button b23 = new Button("3");
Button bMinus = new Button("-");
Button bPlus = new Button("+");
Button b26 = new Button("0");
Button bPoint = new Button(".");
Button bEquals = new Button("=");
TextField t = new TextField(25);
String fun1,fun2,fun3,fun4,fun5;
Cal(){
Panel p1 = new Panel();
Panel p2 = new Panel();
setSize(25,100);
add(p1,BorderLayout.NORTH);
add(p2,BorderLayout.CENTER);
GridLayout g = new GridLayout(6,4);
bMc.addActionListener(this);
bMPlus.addActionListener(this);
bMMin.addActionListener(this);
bBkcs.addActionListener(this);
bCe.addActionListener(this);
bPlusOrEqual.addActionListener(this);
bSquareRoot.addActionListener(this);
b11.addActionListener(this);
b12.addActionListener(this);
b13.addActionListener(this);
bDivide.addActionListener(this);
bPresentage.addActionListener(this);
b16.addActionListener(this);
b17.addActionListener(this);
b18.addActionListener(this);
bMultiply.addActionListener(this);
b21.addActionListener(this);
b22.addActionListener(this);
b23.addActionListener(this);
bMinus.addActionListener(this);
bPlus.addActionListener(this);
b26.addActionListener(this);
bPoint.addActionListener(this);
bEquals.addActionListener(this);
p2.setLayout(g);
p1.add(t);
p2.add(bMc);
p2.add(bMPlus);
p2.add(bMMin);
p2.add(bBkcs);
p2.add(bCe);
p2.add(bPlusOrEqual);
p2.add(bSquareRoot);
p2.add(bPresentage);
p2.add(b11);
p2.add(b12);
p2.add(b13);
p2.add(bDivide);
p2.add(b16);
p2.add(b17);
p2.add(b18);
p2.add(bMultiply);
p2.add(b21);
p2.add(b22);
p2.add(b23);
p2.add(bMinus);
p2.add(bPlus);
p2.add(b26);
p2.add(bPoint);
p2.add(bEquals);
setSize(300,400);
setBackground(Color.green);
}
public void actionPerformed(ActionEvent e){
if(e.getSource().equals(b11)){
t.setText(t.getText() + e.getActionCommand());
}else if (e.getSource().equals(b12)){
t.setText(t.getText() + e.getActionCommand());
}else if (e.getSource().equals(b13)){
t.setText(t.getText() + e.getActionCommand());
}else if (e.getSource().equals(b16)){
t.setText(t.getText() + e.getActionCommand());
}else if (e.getSource().equals(b17)){ t.setText(t.getText() + e.getActionCommand());
}else if (e.getSource().equals(b18)){
t.setText(t.getText() + e.getActionCommand());
}else if (e.getSource().equals(b21)){
t.setText(t.getText() + e.getActionCommand());
}else if (e.getSource().equals(b22)){
t.setText(t.getText() + e.getActionCommand());
}else if (e.getSource().equals(b23)){
t.setText(t.getText() + e.getActionCommand());
}else if (e.getSource().equals(bPoint)){
t.setText(t.getText() + e.getActionCommand());
}else if (e.getSource().equals(b26)){
t.setText(t.getText() + e.getActionCommand());
}else if (bPlus.equals(e.getSource())){
fun1 = t.getText();
t.setText(null);
}else if(bEquals.equals(e.getSource())){
String Val = String.valueOf(Integer.parseInt(fun1) + Integer.parseInt(t.getText()));
t.setText(Val);
}else if(bEquals.equals(e.getSource())){
String Val = String.valueOf(Integer.parseInt(fun1) + Integer.parseInt(t.getText()));
t.setText(Val);
}else if (bCe.equals(e.getSource())){
fun2 = t.getText();
t.setText(null);
}else if(bCe.equals(e.getSource())){
String Val = String.valueOf(Integer.parseInt(fun2) + Integer.parseInt(t.getText()));
t.setText(Val);
}else if(bEquals.equals(e.getSource())){
String Val = String.valueOf(Integer.parseInt(fun2) + Integer.parseInt(t.getText()));
t.setText(Val);
}else if (bMinus.equals(e.getSource())){
fun3 = t.getText();
t.setText(null);
}else if(bCe.equals(e.getSource())){
String Val = String.valueOf(Integer.parseInt(fun3) - Integer.parseInt(t.getText()));
t.setText(Val);
}else if(bEquals.equals(e.getSource())){
String Val = String.valueOf(Integer.parseInt(fun3) - Integer.parseInt(t.getText()));
t.setText(Val);
}else if (bMultiply.equals(e.getSource())){
fun4 = t.getText();
t.setText(null);
}else if(bCe.equals(e.getSource())){
String Val = String.valueOf(Integer.parseInt(fun4) * Integer.parseInt(t.getText()));
t.setText(Val);
}else if(bEquals.equals(e.getSource())){
String Val = String.valueOf(Integer.parseInt(fun4) * Integer.parseInt(t.getText()));
t.setText(Val);
}else if (bDivide.equals(e.getSource())){
fun5 = t.getText();
t.setText(null);
}else if(bCe.equals(e.getSource())){
String Val = String.valueOf(Integer.parseInt(fun5) / Integer.parseInt(t.getText()));
t.setText(Val);
}else if(bEquals.equals(e.getSource())){
String Val = String.valueOf(Integer.parseInt(fun5) / Integer.parseInt(t.getText()));
t.setText(Val);
}
}
public static void main(String args[]){
Cal c = new Cal();
c.setVisible(true);
}
}
Calculator Hard coding....(Java platform);
import java.util.Scanner;
class A{
public static void main(String ar[]){
String m = null;
String Gender=null;
int day=0;
boolean abc = true;
while (abc==true) {
System.out.println("enter your NIC without V or X(1950-2050)");
Scanner s=new Scanner(System.in);
String id=s.next();
String y=id.substring(0,2);
int y1 = Integer.parseInt(y);
if(y1>50){
y1=(y1+1900);
}else{
y1=(y1+2000);
}
String d=id.substring(2,5);
int d1=Integer.parseInt(d);
if(d1>366){
Gender="female";
d1=(d1-500);
}else{
Gender="male";
}
if(d1<=31){
m="jan";
day=d1;
}else if(d1<=60){
m="feb";
day=(d1-31);
}else if(d1<=91){
m="mar";
day=(d1-60);
}else if(d1<=121){
m="apr";
day=(d1-91);
}else if(d1<=152){
m="may";
day=(d1-121);
}else if(d1<=182){
m="jun";
day=(d1-152);
}else if(d1<=213){
m="jul";
day=(d1-182);
}else if(d1<=244){
m="aug";
day=(d1-213);
}else if(d1<=274){
m="sep";
day=(d1-244);
}else if(d1<=305){
m="oct";
day=(d1-274);
}else if(d1<=335){
m="nov";
day=(d1-305);
}else if(d1<=366){
m="des";
day=(d1-335);
}
System.out.println("year-"+y1+" "+"month-"+m+" "+"day-"+day);
System.out.println("Gender-"+Gender);
System.out.println();
// System.out.println("do you want to continue?press C.press any key to exit");
Scanner w=new Scanner(System.in);
String w1=w.next();
if(w1.equals("C")){
abc=true;
}else{
abc=false;
}
}
}
}
class A{
public static void main(String ar[]){
String m = null;
String Gender=null;
int day=0;
boolean abc = true;
while (abc==true) {
System.out.println("enter your NIC without V or X(1950-2050)");
Scanner s=new Scanner(System.in);
String id=s.next();
String y=id.substring(0,2);
int y1 = Integer.parseInt(y);
if(y1>50){
y1=(y1+1900);
}else{
y1=(y1+2000);
}
String d=id.substring(2,5);
int d1=Integer.parseInt(d);
if(d1>366){
Gender="female";
d1=(d1-500);
}else{
Gender="male";
}
if(d1<=31){
m="jan";
day=d1;
}else if(d1<=60){
m="feb";
day=(d1-31);
}else if(d1<=91){
m="mar";
day=(d1-60);
}else if(d1<=121){
m="apr";
day=(d1-91);
}else if(d1<=152){
m="may";
day=(d1-121);
}else if(d1<=182){
m="jun";
day=(d1-152);
}else if(d1<=213){
m="jul";
day=(d1-182);
}else if(d1<=244){
m="aug";
day=(d1-213);
}else if(d1<=274){
m="sep";
day=(d1-244);
}else if(d1<=305){
m="oct";
day=(d1-274);
}else if(d1<=335){
m="nov";
day=(d1-305);
}else if(d1<=366){
m="des";
day=(d1-335);
}
System.out.println("year-"+y1+" "+"month-"+m+" "+"day-"+day);
System.out.println("Gender-"+Gender);
System.out.println();
// System.out.println("do you want to continue?press C.press any key to exit");
Scanner w=new Scanner(System.in);
String w1=w.next();
if(w1.equals("C")){
abc=true;
}else{
abc=false;
}
}
}
}
Wednesday, April 3, 2013
How can I get started
How can I get started developing Java programs.
Writing Java applets and applications needs development tools like JDK. The JDK includes the Java Runtime Environment, the Java compiler and the Java APIs. It's easy for both new and experienced programmers to get started.
Where can I get JDK download?
To download the latest version of the Java Development Kit (JDK), go to JDK downloads.
Developers can also refer to the Oracle Technology Network for Java Developers for everything you need to know about Java technology, including documentation and training.
Developers can also refer to the Oracle Technology Network for Java Developers for everything you need to know about Java technology, including documentation and training.
What if I am new to Java?
If you are new and interested to get started developing Java programs, please refer to new to Javato find useful information for beginners.
How do I get Java certification?
Earning an Oracle Java technology certification provides a clear demonstration of the technical skills, professional dedication and motivation for which employers are willing to pay a premium. Recognized industry-wide, Oracle's Java technology training and certification options help ensure that you have the necessary skills to efficiently meet the challenges of your IT organization.
» Learn more about Java Certification
» Learn more about Java Certification
Java Developer Conferences
- JavaOne is the premier Java developer conference where you can learn about the latest Java technologies, deepen your technical understanding, and ask questions directly to your fellow strategists and developers. Oracle runs annual JavaOne conferences, including the flagship JavaOne in San Francisco and regional conferences. Visit www.oracle.com/javaonefor more information on upcoming events and locations.
- Oracle Technology Network Developer Days are free, hands-on Java developer workshops conducted globally on a regular basis.
- Oracle also sponsors a variety of third party Java technology conferences and events. Search the Oracle Events catalog for an upcoming event near you.
Java Magazine
Java Magazine, a bimonthly, digital-only publication, is an essential source of knowledge about Java technology, the Java programming language, and Java-based applications for people who rely on them in their professional careers, or who aspire to. It includes profiles of innovative Java applications, Java technical how-to's, Java community news, and Information about new Java books, conferences and events.
Oracle Academy
The Oracle Academy provides a complete portfolio of software, curriculum, hosted technology, faculty training, support, and certification resources to K-12, vocational, and higher education institutions for teaching use. Faculty can flexibly insert these resources into computer science and business programs, ensuring that students gain industry-relevant skills prior to entering the workforce. The Oracle Academy supports over 1.5 million students in 95 countries. Oracle Academy recently expanded its curriculum to include Java. To learn more, visit Oracle Academy Java Programming.
Subscribe to:
Posts (Atom)


