Wednesday, June 26, 2013

Java 24: h1-h6 (Java)

1)Java Programs including: 

1.1) application: main()
1.2) applets: init() and paint()
import java.awt.*;

public class RootApplet extends javax.swing.JApplet{

int number;

public void init(){
number = 225;
}

public void paint(Graphics screen){
Graphics2D screen2D = (Graphics2D) screen;
screen2D.drawString("The square root of " + number +                " is " + Math.sqrt(number), 10, 50);
}

}

2) int

final int TOUCH = 6; //TOUCH is a constant

int x = 3;
int y = x++ *10; // y = 30, x =4

int x = 3;
int y = ++x *10;// y =40, x=4

int z = 0b0000_1101; // z =13 

3) String

You can insert several special characters into a string in this manner. The
following list shows these special characters; note that each is preceded by
a backslash (\).
\’ Single quotation mark
\” Double quotation mark
\\ Backslash
\t Tab
\b Backspace
\r Carriage return
\f Formfeed
\n Newline


public class Test{
public static void main(String[] args) {
String i = "'c'";
i += 1;
i = i.toUpperCase();
i = i.toLowerCase();
System.out.println(i);
System.out.println(i.length());
System.out.println(i.equals("'c'1"));
System.out.println(i.indexOf("c"));
}
}
'c'1
4
true
1

No comments:

Post a Comment