Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Leer Challenge: Building a Server Retry Mechanism | Loops
Java Basics
course content

Cursusinhoud

Java Basics

Java Basics

1. Getting Started
2. Basic Types and Operations
3. Loops
4. Arrays
5. String

book
Challenge: Building a Server Retry Mechanism

Taak

Swipe to start coding

Implement a program that attempts to connect to a server up to 5 times. If the connection is successful, the program prints a success message. If it fails after 5 attempts, it prints an error message.

  1. Create a method called tryConnect(int successfulAttempt) that returns a boolean indicating whether the connection succeeded.
  2. Inside this method:
    • Start a do-while loop that runs up to 5 times.
    • On each iteration, increment the retry counter.
    • If the retry count equals the given successfulAttempt, print a success message and return true.
    • If not, print "Server unavailable, retrying...".
  3. If the loop finishes without success, return false.
  4. In the main method, call tryConnect(...) with a test value and print "Failed to connect to the server after 5 attempts." if the result is false.

Oplossing

java

solution

Switch to desktopSchakel over naar desktop voor praktijkervaringGa verder vanaf waar je bent met een van de onderstaande opties
Was alles duidelijk?

Hoe kunnen we het verbeteren?

Bedankt voor je feedback!

Sectie 3. Hoofdstuk 4
toggle bottom row

book
Challenge: Building a Server Retry Mechanism

Taak

Swipe to start coding

Implement a program that attempts to connect to a server up to 5 times. If the connection is successful, the program prints a success message. If it fails after 5 attempts, it prints an error message.

  1. Create a method called tryConnect(int successfulAttempt) that returns a boolean indicating whether the connection succeeded.
  2. Inside this method:
    • Start a do-while loop that runs up to 5 times.
    • On each iteration, increment the retry counter.
    • If the retry count equals the given successfulAttempt, print a success message and return true.
    • If not, print "Server unavailable, retrying...".
  3. If the loop finishes without success, return false.
  4. In the main method, call tryConnect(...) with a test value and print "Failed to connect to the server after 5 attempts." if the result is false.

Oplossing

java

solution

Switch to desktopSchakel over naar desktop voor praktijkervaringGa verder vanaf waar je bent met een van de onderstaande opties
Was alles duidelijk?

Hoe kunnen we het verbeteren?

Bedankt voor je feedback!

Sectie 3. Hoofdstuk 4
Switch to desktopSchakel over naar desktop voor praktijkervaringGa verder vanaf waar je bent met een van de onderstaande opties
Onze excuses dat er iets mis is gegaan. Wat is er gebeurd?
some-alt