Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit837f635

Browse files
authored
Merge pull requestTheAlgorithms#769 from abhijay94/Development
Added adapter pattern and its test case
2 parents13a0883 +c31c39a commit837f635

File tree

8 files changed

+69
-3
lines changed

8 files changed

+69
-3
lines changed

‎src/main/java/com/conversions/BinaryToHexadecimal.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public String binToHex(String binStr) {
3636
Stringhex ="";
3737

3838
intcurrentBit;
39-
BigIntegertenValue =newBigInteger("10");
39+
BigIntegertenValue =BigInteger.valueOf(10);
4040
while (binary.compareTo(BigInteger.ZERO) !=0) {
4141
// to store decimal equivalent of number formed by 4 decimal digits
4242
intcode4 =0;

‎src/main/java/com/conversions/DecimalToHexadecimal.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
publicclassDecimalToHexadecimal {
66
privatestaticfinalchar[]hexChars = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
7-
privatestaticfinalBigIntegervalueHex =newBigInteger("16");
7+
privatestaticfinalBigIntegervalueHex =BigInteger.valueOf(16);
88

99
/**
1010
* This method converts and decimal number to a Hexadecimal number

‎src/main/java/com/conversions/DecimalToOctal.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
publicclassDecimalToOctal {
66
privatestaticfinalchar[]octalChars = {'0','1','2','3','4','5','6','7'};
7-
privatestaticfinalBigIntegervalueOctal =newBigInteger("8");
7+
privatestaticfinalBigIntegervalueOctal =BigInteger.valueOf(8);
88

99
/**
1010
* This method converts and decimal number to a octal number
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
packagesrc.main.java.com.designpatterns.structural.adapter;
2+
3+
publicclassBugattiVeyronimplementsMovable {
4+
@Override
5+
publicdoublegetSpeed() {
6+
return268;
7+
}
8+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
packagesrc.main.java.com.designpatterns.structural.adapter;
2+
3+
publicinterfaceMovable {
4+
// Returns the speed of the movable in MPH
5+
doublegetSpeed();
6+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
packagesrc.main.java.com.designpatterns.structural.adapter;
2+
3+
/**
4+
* An Adapter pattern acts as a connector between two incompatible interfaces that otherwise cannot be connected
5+
* directly. An Adapter wraps an existing class with a new interface so that it becomes compatible with the client’s
6+
* interface.
7+
* <br>
8+
* The main motive behind using this pattern is to convert an existing interface into another interface that the client
9+
* expects. It’s usually implemented once the application is designed.
10+
*
11+
* @see <a href="https://en.wikipedia.org/wiki/Adapter_pattern">Adapter Pattern</a>
12+
*/
13+
publicinterfaceMovableAdapter {
14+
// Returns the speed of the movable in KPH
15+
doublegetSpeed();
16+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
packagesrc.main.java.com.designpatterns.structural.adapter;
2+
3+
publicclassMovableAdapterImplimplementsMovableAdapter {
4+
privateMovableluxuryCars;
5+
6+
publicMovableAdapterImpl(MovableluxuryCars) {
7+
this.luxuryCars =luxuryCars;
8+
}
9+
10+
@Override
11+
publicdoublegetSpeed() {
12+
returnconvertMPHtoKMPH(luxuryCars.getSpeed());
13+
}
14+
15+
privatedoubleconvertMPHtoKMPH(doublemph) {
16+
returnmph *1.60934;
17+
}
18+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
packagesrc.test.java.com.designpatterns.structural.adapter;
2+
3+
importorg.junit.Test;
4+
importsrc.main.java.com.designpatterns.structural.adapter.BugattiVeyron;
5+
importsrc.main.java.com.designpatterns.structural.adapter.Movable;
6+
importsrc.main.java.com.designpatterns.structural.adapter.MovableAdapter;
7+
importsrc.main.java.com.designpatterns.structural.adapter.MovableAdapterImpl;
8+
9+
importstaticorg.junit.Assert.assertEquals;
10+
11+
publicclassMovableAdapterTest {
12+
@Test
13+
publicvoidtestMovableAdapter() {
14+
MovablebugattiVeyron =newBugattiVeyron();
15+
MovableAdapterbugattiVeyronAdapter =newMovableAdapterImpl(bugattiVeyron);
16+
assertEquals(bugattiVeyronAdapter.getSpeed(),431.30312,0.00001);
17+
}
18+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp