forked from HarshwardhanPatil07/Contribution
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrectangle.java
More file actions
105 lines (86 loc) · 1.98 KB
/
Copy pathrectangle.java
File metadata and controls
105 lines (86 loc) · 1.98 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
package practice_java;
import java.util.*;
import java.lang.*;
class OutOfRange extends Exception//creating exception
{
Double a;
OutOfRange(Double b) {
a=b;
}
public String toString(){
return ("Out Of Range Value provided "+a) ;
}
}
public class Rectangle
{ Scanner sc=new Scanner(System.in);
Double length, width;
private String area;
private String perimeter;
public Double getLength()
{
return this.length;
}
public Double setLength(Double length)
{
try {
if(length < 0.0 || length > 20.0)//checking exception
{
throw new OutOfRange(length);
}
}
catch (OutOfRange e)
{
System.out.println(e);
}
return this.length = length;
}
public Double getWidth()
{
return this.width;
}
public Double setWidth(Double width)
{
try {
if(width < 0.0 || width > 20.0)
{
throw new OutOfRange(width);
}
}
catch (OutOfRange e)
{
System.out.println(e);
}
return this.width = width;
}
public double area()
{
return (this.length*this.width);
}
public double perimeter()
{
return (2*(this.length+this.width));
}
public Rectangle(double x1, double x2, double x3, double x4, double y1, double y2, double y3, double y4)//taking cordinates
{
if(Math.hypot(x1-x2, y1-y2)==Math.hypot(x3-x4, y3-y4))
{
System.out.println("Rectangle is valid ");
}
else if(x1==x3 && x3==x4 && y1==y2 && y3==y4)
{
System.out.println("It is square");
}
else
System.out.println("Rectangle is invalid ");
this.setLength(Math.sqrt((y4 - y1) * (y4 - y1) + (x4 - x1) * (x4 - x1)));
this.setWidth(Math.sqrt((y2 - y1) * (y2 - y1) + (x2 - x1) * (x2 - x1)));
System.out.println("Length is "+getLength());
System.out.println("Width is" +getWidth());
}
public static void main(String args[])
{
Rectangle obj=new Rectangle(3.0,5.0,4.0,5.0,3.0,7.0,4.0,7.0);
System.out.println("area is" +obj.area());
System.out.println("perimeter is" +obj.perimeter());
}
}