Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

Java 

Compilation of Java Exercises for Beginners

Lately people have been asking for Java exercises and assignments, basically something they can work on and test their skills. So, I thought it would be a good idea to make a compilation of Java exercises for beginners.

This compilation covers the following topics:
  • Data Types

  • Decision Statements

  • Loops

  • Arrays

  • Multi-dimensional Arrays

  • Methods

  • Recursion

  • Classes and Objects


The exercises are for absolute beginners. However, it gradually gets more advanced with each topic.

Sorry about the multiple posts, I couldn't keep everything in one post. It's weird how fast you can run out of characters. You'd think that 8,000 character are more than enough.

Note: All user's inputs are in green.

● Data Types

Exercise #1
(Click to View)

Exercise #2
(Click to View)

Exercise #3
(Click to View)

Exercise #4
(Click to View)

Exercise #5
(Click to View)

Exercise #6
(Click to View)

Exercise #7
(Click to View)

Exercise #8
(Click to View)

Exercise #9
(Click to View)

Exercise #10
(Click to View)


● Decision Statements

Exercise #1
(Click to View)

Exercise #2
(Click to View)

Exercise #3
(Click to View)

Exercise #4
(Click to View)

Exercise #5
(Click to View)

Exercise #6
(Click to View)

Exercise #7
(Click to View)

Exercise #8
(Click to View)

Exercise #9
(Click to View)

Exercise #10
(Click to View)


● Loops

Exercise #1
(Click to View)

Exercise #2
(Click to View)

Exercise #3
(Click to View)

Exercise #4
(Click to View)

Exercise #5
(Click to View)

Exercise #6
(Click to View)

Exercise #7
(Click to View)

Exercise #8
(Click to View)



*

Exercise #9
(Click to View)









*


Exercise #10
(Click to View)

Exercise #11
(Click to View)

Exercise #12
(Click to View)


● Arrays

Exercise #1
(Click to View)

Exercise #2
(Click to View)

Exercise #3
(Click to View)

Exercise #4
(Click to View)

Exercise #5
(Click to View)

Exercise #6
(Click to View)

Exercise #7
(Click to View)

Exercise #8
(Click to View)

Exercise #9
(Click to View)

Exercise #10
(Click to View)


● Multidimensional Arrays

Exercise #1
(Click to View)

Exercise #2
(Click to View)

Exercise #3
(Click to View)
30 15
20 10 9
8* 7* 1

Should be transposed to:
5* 20 8
30 10 7
15 9* 1

Hint: You need to use swapping to do the transpose in place.

Note: Asterisks are only added to format the matrix, it has nothing to do with the exercise.

Sample Run 1:
Enter Matrix Size: 3
Enter Row#0 Elements:
5
30
15

Enter Row#1 Elements:
20
10
9

Enter Row#2 Elements:
8
7
1


Result:
5* 20 8
30 10 7
15 9* 1

Exercise #4
(Click to View)


● Methods

Exercise #1
(Click to View)

Exercise #2
(Click to View)

Exercise #3
(Click to View)

Exercise #4
(Click to View)

Exercise #5
(Click to View)


Exercise #6
(Click to View)

Exercise #7
(Click to View)

Exercise #8
(Click to View)


Exercise #9
(Click to View)

Exercise #10
(Click to View)

● Recursion

Exercise #1
(Click to View)

Exercise #2
(Click to View)

Exercise #3
(Click to View)

Exercise #4
(Click to View)

Exercise #5
(Click to View)

Exercise #6
(Click to View)

Exercise #7
(Click to View)

Exercise #8
(Click to View)


● Classes and Objects

Exercise #1
(Click to View)
  • Constructor() A no argument constructor that sets the radius field to 0.0

  • Constructor() Accepts the radius of the circle as an argument.

  • setRadius() A setter method for the radius field.

  • getRadius() A getter method for the radius field.

  • getArea() Returns the area of the circle which is calculated as Area = PI radius^2

  • getDiameter(): Returns the diameter of the circle which is calculated as Diameter = 2 radius

  • getCircumference(): Returns the circumference of the circle, which is calculated as circumference = 2 PI * radius


Write a program that demonstrates the Circle class by asking the user for the circle's radius, creating a Circle object, and then reporting the circle's area, diameter, and circumference.

Sample Run 1:
Enter Circle Radius: 10.5

Area is 346.3602975
Diameter is 21.0
Circumference is 65.97339

Exercise #2
(Click to View)

Exercise #3
(Click to View)

Exercise #4
(Click to View)

There is more, but I can't make any more posts for now. Anyway, I really hope this helps.
If you're facing any problems post here or PM Me PM. Good luck.
Hey thanks man, I believe this is one of the most needed post in the section.
We recommend you take the time to read the HF Help Documents section. The answer to your questions should be contained in there.
This is great for anyone looking for some exercises to boost their java knowledge, great contribution to the community :) thankyou!
my god if this only exsisted when i started. just code and see for ourselves.
This is a great thread man, keep up the quality!

I am sure this is useful for beginners and might even be useful for the other members here to refresh their knowledge.
Thanks everyone. Yea true, doesn't have to be just for beginner. Some of thus exercises are actually challenging..well, at least for me.
Thanks for this M0dy! I will definitely try some of these myself with my new IDE. ;)
Very HQ post man, props and Great job!!
I think this is a great compilation, suited for all types of programmers. Keep up the HQ posts.
For the first section: "Data Types", the volume answer is wrong just a heads up

It should be - Volume: 4849.048260815845 or 4849.05 (rounded)


Aside from this, amazing stuff! Great revision for me 🤝
[Image: DPVMM5G.png]
+1 (or rather +3) - nice thread!

I might be tempted to use some of the recursion tasks for my work's tech-screenings during the recruitment process. Some of them are not trivial (at least not for me at 00:30 in the night after a long day), but could easily be done within reasonable time (10-20 minutes). If that's OK with you :P

For your next handful, you could specify tasks that are more technical, but of same size, such as implementing a searching algorithm (binary is easy), or implementing BFS and DFS.
[Image: TEGE4Zx.png]

Puzzle level: 2/5
I wonder if I may borrow some of these problems when I will be 'teaching' Java in september/october :)
For the decision statements exercise 3:

Quote:Sample Run 1:
Annual Salary: 113850
Rating: 3
Raise: 6831
New Salary: 120681


The rating should be 1, not 3 :)
[Image: DPVMM5G.png]
(Apr 23, 2015 05:09 AM)madcrazydrumma Wrote:  For the decision statements exercise 3:

The rating should be 1, not 3 :)

Yea true, my bad. I checked multiple times but apparently I missed few things, and I can't edit this anymore.

(Apr 22, 2015 03:37 PM)nOrr Wrote:  +1 (or rather +3) - nice thread!

I might be tempted to use some of the recursion tasks for my work's tech-screenings during the recruitment process. Some of them are not trivial (at least not for me at 00:30 in the night after a long day), but could easily be done within reasonable time (10-20 minutes). If that's OK with you :P

For your next handful, you could specify tasks that are more technical, but of same size, such as implementing a searching algorithm (binary is easy), or implementing BFS and DFS.

(Apr 23, 2015 12:40 AM)Maxiim Wrote:  I wonder if I may borrow some of these problems when I will be 'teaching' Java in september/october :)

You guys can use whatever you want. I simply collected those in one topic for easier access. :) and will sure try nOrr.


Possibly Related Threads...
Thread / Author Replies Views Last Post
[C#] by PitbullT0rApr 21, 2021 04:35 AM 3 213 Apr 21, 2021 04:35 AM
Last Post: Exalted
[Java] by KatasFeb 14, 2021 06:13 AM 2 305 Feb 14, 2021 06:13 AM
Last Post: Juoku
[Python] by LatelyNov 27, 2020 07:42 AM 3 125 Nov 27, 2020 07:42 AM
Last Post: G.P.D.