0% found this document useful (0 votes)
25 views

Java Class 3

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views

Java Class 3

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 18

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;

System.out.println(myDouble); // prints 3.4


System.out.println(myFloat); // prints 3.4
System.out.println(myDoubleScientific); // prints 344.5} }}

3
3.4
3.4
344.5

4
Overloading
// Demonstrate method overloading.
class OverloadDemo {
void test() {
System.out.println("No parameters");
}
// Overload test for one integer parameter.
void test(int a) {
System.out.println("a: " + a);
}
// Overload test for two integer parameters.
void test(int a, int b) {
System.out.println("a and b: " + a + " " + b);
}
// overload test for a double parameter
double test(double a) {
System.out.println("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()
ob.test();
ob.test(10);
ob.test(10, 20);
result = ob.test(123.25);
System.out.println("Result of ob.test(123.25): " + result);
}
}

6
No parameters
a: 10
a and b: 10 20
double a: 123.25
Result of ob.test(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)
{this.width= b.width; this.height=b.height; this.depth=b.depth;}

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 = mybox1.volume();
System.out.println("Volume of mybox1 is " + vol);
vol = mybox2.volume();
System.out.println("Volume of mybox2 is " + vol);
vol = mycube.volume();
System.out.println("Volume of mycube is " + vol);
vol = bx.volume();
System.out.println("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);
System.out.println("ob1 == ob2: " + ob1.equals(ob2));
System.out.println("ob1 == ob3: " + ob1.equals(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;
System.out.println("a and b before call: " +
a + " " + b);
13
ob.meth(a, b);
System.out.println("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
System.out.println("ob.a and ob.b before call: " +
ob.a + " " + ob.b);
ob.meth(ob);
System.out.println("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 java.util.Scanner;
class Inp{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter Name:");
String name=sc.next();
System.out.print("Enter Class Roll:");
int classRoll=sc.nextInt();
System.out.print("Enter Expected Salary:");
double expSal= sc.nextDouble();

17
System.out.println(name+" "+ classRoll+" "+expSal);
}
}
Enter Name:asd
Enter Class Roll:12
Enter Expected Salary:123.4
asd 12 123.4

18

You might also like