JAVA Classes

JAVA Classes

By ben06x | Electronics | 25 Apr 2021


In this article I will deal with some tricky things about classes in JAVA.

 

1.Rectangle class

 

We create a new Java Project named Rectangle, to which we add the following Rectangle class:

7152ec6aa81ee3dd99c7ded1ae88fb6f5090c382d933b96b8c01b9359070c4ac.png

 

Our Rectangle class contains:

  • a public attribute, representing the length of the rectangle
  • a public attribute, representing the width of the rectangle
  • a constructor to instantiate our rectangle
  • a public method returning the area of the rectangle 
  • a public method returning the perimeter of the rectangle

We generate the documentation note:

40b9f6f4087acd3de0ccb8eab305a534397531800b614eff8acaca396a91540d.png

 

We test our Rectangle class with the creation of the TestRectangle class, by creating an instance Rectangle and displaying its length, width, area, and perimeter on standard output.

96e19b44cb9c693f9d2c6229f2249b3aba0a7b4a37597dabcf575e07aed01f75.png

 

We have the following display:

066911c43b9bbf2ecbfd42fb801a9050407f17e2e51c20c3f773052488ce3280.png

We now modify our TestRectangle class to be able to change the length and width of our Rectangle:

f5fba88eea51aca3e6e243cfce25ee9a3a72b2603a549200a14cb022c9420f1c.png

 

We have the following display:

6360b3e15c981c6ca41dafa31058acb0d64a32732d05f4f9b4b9dbbd82bb6b76.png

 

We now create the new RectangleWithFinalKW class, by modifying our Rectangle class, adding the final keyword to the height_ and width_ attributes.

2e4e0e6f2ad23e2a0cb4e73f35eb48ea487e72cc181d17824252969d9c543163.png

 

After testing we have the following error:

47b506da4dac6c1277d2dd53143908849008e28c26d9366cabf1f4dd9111ae6e.png

We can only modify our attributes once, here at creation on line 17, inducing a error on lines 24 and 25.

 

We now create the new RectangleWithPrivateKW class, by modifying our class Rectangle, making the height_ and width_ attributes private. By doing just that, we have the following errors:

3f2cb92f0ff7668aa9800c1359bf71806eac0e83c604bf2a07d19a9b99b4768b.png

Indeed, the attributes being private, we cannot access them here.

To overcome this problem, we complete our RectangleWithPrivateKW class as next :

5bfb69a6f8eb714c007a670ca2cbc83f11ef580cff79d99ece800571fce61b13.png

 

We modify our TestRetcangle class to adapt it to our private attributes with the get functions:

e9a0618d46b7dcc635d504d1361de68277c5d8640b3f4cbe34a8f738c8efbe89.png

 

We have the following display:

e9eaeae50cbde9de1d70d5796b25a5d240a0a76f3329bdd80cda3ba3bb96558a.png

 

We now create the new RectangleWithNoKW class, by modifying our Rectangle class, to make our height_ and width_ attributes keywordless:

ac25b39ce603ff1d0d823e41785dd6af8a81a79db1017346545cb90afe477188.png

 

We modify our TestRectangle class to adapt it to our previous class:

f28b27e9298f73404cb67ed281fc2efe0ff549680f2841724db368cc0ced7a6e.png

 

We have the following display:

e9eaeae50cbde9de1d70d5796b25a5d240a0a76f3329bdd80cda3ba3bb96558a.png

 

Here without a keyword, it works well because the private package is in the same package, so we can access it here. If we were outside we couldn't have.

 

2. Rectangle with UID

 

We want to assign a unique ID for each Rectangle.

To do this, we define:

  • the cnt static storyteller, making it variable in our class
  • the uid_ attribute

 

Thus, the following RectangleFull class is created:

e181003a6d565384a931fd2752e915831690c966cd8fb64e45503c2eb276bf9c.png

 

In our TestRectangle class, we access the cnt variable by the name of the RectangleFull class directly. Our TestRectangle class looks like this:

2b09d1b21e71e1aed7eec63375c083ed03f4b028d2390ad3599875d2dafa4d7a.png

 

We have the following display:

9c715416056feb37cd6f24f20d520edf4d5029755f1e682b505ca02f21048dff.png

 

Note that each Rectangle has a different ID.

 

3. Circle class

 

We define the Circle class in the same type as our previous Rectangle class.
This Circle class has:

  • the private attribute radius_
  • its constructor, initializing the radius of our circle
  • the getRadius() function, returning the radius of the circle
  • the getArea() function, returning the area of the circle
  • the getPerimeter () function, returning the perimeter of the circle
  • the constant PI

 

f0793852d5ad409ed0858e0d143d81fc46d338fb63742e87a36c05c92714c4e3.png

 

We create a circle of radius 3.0 and display its characteristics:

c4433bcfd181a68f859249cb933f9c873d5c12b6d2ecceee3d12f4ed0a089c7d.png

 

We have the following display:

377c0637e65e3195f5adfe9abb6939a5c1c81fef64f14c889f2f2a426711b2fe.png

 

We now define the CircleWithMath class, using the Math methods / constants.

2791d6b0997342897accdb55f62c5f1fd1e840f828fa16ad1d2e5ff7f7406888.png

 

We also create a circle of radius 3.0 and display its characteristics:

4e60d2f3550d30a53bd3d0736891fadd4a1ce4eeaa85da2f0fb1ecd2de68ec84.png

 

24bebf0f7fafc76f2a8967920ed351383520da87c5f15a7e461913073670962d.png

 

In this article, I dealt with some tricky things about classes in JAVA, and showed some classes example.

 

 

This article is part of my Electronics posts blog, in which you can find few articles about things i learnt in my post-graduated school :)

 

 

How do you rate this article?

3



Electronics
Electronics

Some articles about things i learnt in my post-graduated school :)

Publish0x

Send a $0.01 microtip in crypto to the author, and earn yourself as you read!

20% to author / 80% to me.
We pay the tips from our rewards pool.