Java Class 3
Literals
/*boolean flag1 = false;
boolean flag2 = true;
int binaryNumber = 0b10010;
int octalNumber = 027;
int decNumber = 34;
char letter = 'a';
int hexNumber = 0x2F; // 0x represents hexadecimal
int binNumber = 0b10010; // 0b represents binary
String str1 = "Java Programming";*/
2
class Lit {
public static void main(String[] args) {
double myDouble = 3.4;
float myFloat = 3.4F;
double myDoubleScientific = 3.445e2;
[Link](myDouble); // prints 3.4
[Link](myFloat); // prints 3.4
[Link](myDoubleScientific); // prints 344.5} }}
3
3.4
3.4
344.5
4
Overloading
// Demonstrate method overloading.
class OverloadDemo {
void test() {
[Link]("No parameters");
}
// Overload test for one integer parameter.
void test(int a) {
[Link]("a: " + a);
}
// Overload test for two integer parameters.
void test(int a, int b) {
[Link]("a and b: " + a + " " + b);
}
// overload test for a double parameter
double test(double a) {
[Link]("double a: " + a);
return a*a;
}
}
5
class Overload {
public static void main(String args[]) {
OverloadDemo ob = new OverloadDemo();
double result;
// call all versions of test()
[Link]();
[Link](10);
[Link](10, 20);
result = [Link](123.25);
[Link]("Result of [Link](123.25): " + result);
}
}
6
No parameters
a: 10
a and b: 10 20
double a: 123.25
Result of [Link](123.25): 15190.5625
7
Overloading Constructors
class Box {
double width;
double height;
double depth;
Box(double w, double h, double d) {
width = w;
height = h;
depth = d;
}
Box() {
width = -1; // use -1 to indicate
height = -1; // an uninitialized
depth = -1; // box
}
Box(double len) {
width = height = depth = len;
}
Box (Box b)
{[Link]= [Link]; [Link]=[Link]; [Link]=[Link];}
8
…
double volume() {
return width * height * depth;}}
class OverloadCons {
public static void main(String args[]) {
Box mybox1 = new Box(10, 20, 15);
Box mybox2 = new Box();
Box mycube = new Box(7);
Box bx =new Box(mybox1);
double vol;
vol = [Link]();
[Link]("Volume of mybox1 is " + vol);
vol = [Link]();
[Link]("Volume of mybox2 is " + vol);
vol = [Link]();
[Link]("Volume of mycube is " + vol);
vol = [Link]();
[Link]("Volume of bx is " + vol);
}}
9
…
/*The output produced by this program is shown here:
Volume of mybox1 is 3000.0
Volume of mybox2 is -1.0
Volume of mycube is 343.0
Volume of bx is 3000.0*/
10
Pass an Object
class Coord {
int x, y;
Coord(int i, int j) {
x = i;
y = j;}
boolean equals(Coord o) {
if(o.x == x && o.y == y) return true;
else return false;}}
class PassOb {
public static void main(String args[]) {
Coord ob1 = new Coord(100, 22);
Coord ob2 = new Coord(100, 22);
Coord ob3 = new Coord(-1, -1);
[Link]("ob1 == ob2: " + [Link](ob2));
[Link]("ob1 == ob3: " + [Link](ob3));
}} 11
ob1 == ob2: true
ob1 == ob3: false
12
CallByValue
class Test {
void meth(int i, int j) {
i *= 2;
j /= 2;
}
}
class CallByValue {
public static void main(String args[]) {
Test ob = new Test();
int a = 15, b = 20;
[Link]("a and b before call: " +
a + " " + b);
13
[Link](a, b);
[Link]("a and b after call: " +
a + " " + b);
}
}
a and b before call: 15 20
a and b after call: 15 20
14
CallByRef
class Test {
int a, b;
Test(int i, int j) {
a = i;
b = j;
}
void meth(Test o) {
o.a *= 2;
o.b /= 2;
}
}
class CallByRef {
public static void main(String args[]) {
Test ob = new Test(15, 20);
15
[Link]("ob.a and ob.b before call: " +
ob.a + " " + ob.b);
[Link](ob);
[Link]("ob.a and ob.b after call: " +
ob.a + " " + ob.b);
}
}
ob.a and ob.b before call: 15 20
ob.a and ob.b after call: 30 10
16
Scanner
import [Link];
class Inp{
public static void main(String args[])
{
Scanner sc = new Scanner([Link]);
[Link]("Enter Name:");
String name=[Link]();
[Link]("Enter Class Roll:");
int classRoll=[Link]();
[Link]("Enter Expected Salary:");
double expSal= [Link]();
17
[Link](name+" "+ classRoll+" "+expSal);
}
}
Enter Name:asd
Enter Class Roll:12
Enter Expected Salary:123.4
asd 12 123.4
18