Skip to content

Commit 576fc0c

Browse files
Merge pull request #4 from jayanpraveen/master
added oop programs
2 parents d3d799f + 6b8e21f commit 576fc0c

File tree

3 files changed

+106
-0
lines changed

3 files changed

+106
-0
lines changed

OOP/classDemo.java

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
class Pet
2+
{
3+
String name; // Instance variables
4+
int count;
5+
6+
public void setName(String name)
7+
{
8+
this.name = name;
9+
}
10+
11+
public void setCount(int count)
12+
{
13+
this.count = count;
14+
}
15+
16+
void getName()
17+
{
18+
System.out.println("Pet: "+ name);
19+
}
20+
21+
void getCount()
22+
{
23+
System.out.println("Count: " + count);
24+
}
25+
}
26+
27+
public class classDemo
28+
{
29+
public static void main(String[] args)
30+
{
31+
Pet obj = new Pet();
32+
33+
obj.setName("Dog");
34+
obj.setCount(10);
35+
36+
obj.getName();
37+
obj.getCount();
38+
39+
obj.setName("Cat");
40+
obj.setCount(7);
41+
42+
obj.getName();
43+
obj.getCount();
44+
}
45+
}

OOP/constructorDemo.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
class Shape
2+
{
3+
float length, breadth, height;
4+
float result;
5+
6+
Shape()
7+
{
8+
System.out.println("Shapes:");
9+
}
10+
Shape (float length, float breadth)
11+
{
12+
result = length * breadth;
13+
System.out.println("Area of rectangle: " + result);
14+
}
15+
16+
Shape (float length, float breadth, float height)
17+
{
18+
result = length * breadth * height;
19+
System.out.println("Area of triangle: " + result);
20+
}
21+
}
22+
23+
public class constructorDemo
24+
{
25+
public static void main(String[] args)
26+
{
27+
new Shape();
28+
new Shape(54f, 12f);
29+
new Shape(48f, 24f, 45f);
30+
}
31+
}

OOP/thisDemo.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
class Sample
2+
{
3+
int value;
4+
5+
Sample()
6+
{
7+
this("Passed_value");
8+
}
9+
10+
public Sample(String str)
11+
{
12+
System.out.println(str + " by this()");
13+
}
14+
15+
public void setValue (int value)
16+
{
17+
this.value = value;
18+
}
19+
}
20+
public class thisDemo
21+
{
22+
public static void main(String[] args)
23+
{
24+
Sample obj = new Sample();
25+
26+
obj.setValue(123);
27+
System.out.println(obj.value);
28+
}
29+
}
30+

0 commit comments

Comments
 (0)