-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathA 6 java.txt
More file actions
197 lines (157 loc) · 4.43 KB
/
A 6 java.txt
File metadata and controls
197 lines (157 loc) · 4.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
1. WAP for abstract class and methods taking example of college as super class and
CSE, Extc, Mech departments as subclasses.
abstract class Clg{
String clgName;
public Clg(String name){
this.clgName=name;
}
abstract void department();
}
class CSE extends Clg{
CSE(String clgName){
super(clgName);
}
void department(){
System.out.println("College Name : "+this.clgName);
System.out.println("Computer Science and Engnieering");
}
}
class ENTC extends Clg{
ENTC(String clgName){
super(clgName);
}
void department(){
System.out.println("College Name : "+this.clgName);
System.out.println("Computer Science and Engnieering");
}
}
class MECH extends Clg{
MECH(String clgName){
super(clgName);
}
void department(){
System.out.println("College Name : "+this.clgName);
System.out.println("Computer Science and Engnieering");
}
}
public class Sample {
public static void main(String[] args) {
CSE cse=new CSE(" WIT ");
cse.department();
ENTC entc=new ENTC(" IIT ");
entc.department();
MECH mech=new MECH(" VIT ");
mech.department();
}
}
2 Write a program implementing simple interface calculate having two methods addition and subtraction
implementing class has to defends on method multiplication and division also your program should access interface variables
interface Calculator{
int sum(int a,int b);
int sub(int a,int b);
}
class Calculate implements Calculator{
public int sum(int a,int b){
return a+b;
}
public int sub(int a,int b){
return a-b;
}
public int mul(int a,int b){
return a*b;
}
public double div(double a,double b){
return a/b;
}
}
public class Sample {
public static void main(String[] args) {
Calculate cal=new Calculate();
System.out.println(cal.sum(2,3));
System.out.println(cal.sub(9,3));
System.out.println(cal.mul(2,4));
System.out.println(cal.div(7,3));
}
}
3 Write a program demonstrating shape example for area in the perimeter method using interface shape
import java.util.*;
interface Shape{
void area();
void perimeter();
}
//Square
class Square implements Shape{
private int side;
public Square(int side){
this.side=side;
}
public void area(){
System.out.println("area : "+side*side);
}
public void perimeter(){
System.out.println("Perimeter : "+4*side);
}
}
//Rectangle
class Rectangle implements Shape{
private int length,breadth;
public Rectangle(int length,int breadth){
this.length=length;
this.breadth=breadth;
}
public void area(){
System.out.println("area : "+length*breadth);
}
public void perimeter(){
System.out.println("Perimeter : "+2*(length+breadth));
}
}
//TRIANGLE
class Triangle implements Shape{
private double s1,s2,s3;
public Triangle(double s1,double s2,double s3){
this.s1=s1;
this.s2=s2;
this.s3=s3;
}
public void area(){
System.out.println("area : "+0.5*s1*s2);
}
public void perimeter(){
double per=s1+s2+s3;
System.out.println("Perimeter : "+per);
}
}
//CIRCLE
class SemiCircle implements Shape{
private float radius;
public SemiCircle(float radius){
this.radius=radius;
}
public void area(){
System.out.println("area : "+(3.14*radius*radius)/2);
}
public void perimeter(){
System.out.println("Perimeter : "+3.14*radius);
}
}
public class Sample2 {
public static void main(String[] args) {
Square s=new Square(5);
System.out.println("FOR SQUARE");
s.area();
s.perimeter();
Rectangle r=new Rectangle(5,10);
System.out.println("FOR RECTANGLE");
r.area();
r.perimeter();
Triangle t=new Triangle(5,10,4);
System.out.println("FOR TRIANGLE");
t.area();
t.perimeter();
SemiCircle sc=new SemiCircle(5);
System.out.println("FOR SEMICIRCLE");
sc.area();
sc.perimeter();
}
}