-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathassiment 2.java
More file actions
245 lines (216 loc) · 4.73 KB
/
assiment 2.java
File metadata and controls
245 lines (216 loc) · 4.73 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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
Aditya Agarwal
ASSIGNMENT 2
Question 6-Write a Java program to create a class named Shape and create three sub classes Circle, Triangle and Square,
each class has two-member function named draw () and erase (). Implement this concepts using polymorphism
solution--
class Shape
{
void draw()
{
System.out.println("Shape draw()");
}
void erase()
{
System.out.println (" Shape erase()");
}
}
class Circle extends Shape
{
void draw()
{
System.out.println ("Circle draw()");
}
void erase()
{
System.out.println ("Circle erase()");
}
}
class Triangle extends Shape
{
void draw()
{
System.out.println("Triangle erase()");
}
}
class Square extends Shape
{
void draw()
{
System.out.println ("Square draw()");
}
void erase()
{
System.out.println ("Square erase()");
}
}
public class Shapes
{
public static Shape randshape()
{
switch((int)(Math.random()*3))
{
case 0: return new Circle();
case 1: return new Square();
case 2: return new Triangle();
default : System.out.println("default");
return new Shape();
}
}
public static void main (String arg[])
{
Shape s[] = new Shape[9];
for(int i = 0;i< s.length; i++)
s[i] = randshape();
for(int i= 0;i < s.length; i++)
s[i].draw();
}
}
Question7-- Write a Java program to give a simple example for abstract class
solution--
abstract class Shape{
abstract void draw();
}
class Rectangle extends Shape{
void draw(){System.out.println("drawing rectangle");}
}
class Circle1 extends Shape{
void draw(){System.out.println("drawing circle");}
}
class TestAbstraction1{
public static void main(String args[]){
Shape s=new Circle1();
s.draw();
}
}
Question 8--- Write a Java program to give example for multiple inheritance in Java
Solution___
interface Car
{
int speed=60;
public void distanceTravelled();
}
interface Bus
{
int distance=100;
public void speed();
}
public class Vehicle implements Car,Bus
{
int distanceTravelled;
int averageSpeed;
public void distanceTravelled()
{
distanceTravelled=speed*distance;
System.out.println("Total Distance Travelled is : "+distanceTravelled);
}
public void speed()
{
int averageSpeed=distanceTravelled/speed;
System.out.println("Average Speed maintained is : "+averageSpeed);
}
public static void main(String args[])
{
Vehicle v1=new Vehicle();
v1.distanceTravelled();
v1.speed();
}
}
Question 9--Write a Java program to create class Number with only one private instance variable as a double primitive type,
include the following methods isZero(), isPositive(), isNegative( ), isOdd( ), isEven( ), isPrime(), isAmstrong() in
this class and all above methods should return boolean primitive type like for isPositive() should return
“Positive = True”.
solution---
class Number
{
private double db1;
private long lg;
public Number ( )
{
db1 = 108.0d;
lg = 249;
}
public Number(double d, long l)
{
db1 = d;
lg = 1;
}
public boolean isZero ( )
{
if (db1 == 0.0)
return true;
else
return false;
}
public boolean isPositive ( )
{
if(db1 > 0.0)
return true;
else
return false;
}
public boolean isNegative ( )
{
if (db1 < 0.0)
return true ;
else
return false;
}
public boolean isodd( )
{
if (db1 % 2 != 0.0)
return true;
else return false;
}
public boolean isEven ( )
{
if (db1 % 2 == 0.0)
return true ;
else return false;
}
public boolean isPrime ( )
{
int i, lastn;
double a;
boolean flag;
a = Math.sqrt(lg);
lastn = (int)a;
flag = true;
for (i=2; i<lastn; i++)
{
if (lg != i)
{
if( lg % i ==0)
{
flag = false;
break;
}
}
}
if (flag)
return true;
else return false;
}
public boolean isAmstrong ( )
{
if (db1 == 0.0)
return true;
else return false;
}
public void dispBinary ( )
{
System.out.println("ByteValue of lg :" + Long.toBinaryString(lg));
}
public static void main (String args [ ])
{
Number mynum = new Number( );
double d = 199;
System.out.println(" The given numbers are 108.0d and 249");
System.out.println ("isZero " + mynum.isZero() );
System.out.println ("isPositive " + mynum. isPositive());
System.out.println ("isNegative " + mynum.isNegative() );
System.out.println ("isOdd " + mynum.isodd());
System.out.println ("isEven " + mynum.isEven());
System.out.println (" isPrime " +mynum.isPrime());
System.out.println ("isAmstrong " + mynum.isAmstrong());
}
}