Test

Java Practices - Javañol Guide

Arteco - Information Technologies
  • :)
  • :0
  • :D
  • ;)
  • :]
foto Ramón Arnau

Ramón Arnau

Gerente de Arteco Consulting SL

Programming Practices to Learn Java as Described in the Javañol Guide Book of Java

Start your journey into the programming world with our innovative book, "Javañol Guide" to learn Java programming! This is not just another programming book; it's your gateway to a career full of opportunities and creativity. Javañol is specifically designed for Spanish speakers, breaking language and technical barriers, allowing you to master Java from beginner to advanced. Don't miss the opportunity to become an expert in one of the most demanded and versatile programming languages in the world. Buy your copy today and start turning your ideas into reality!

Learn Java Programming - PRACTICE 1

The objective of this practice is to create a command interpreter that allows the user to input statements interactively from the keyboard. Through these commands, the user will be able to execute instructions to manipulate the file system in a basic way.

Practice 1 Requirements

The operations that the user must be able to execute through the terminal are:

  • ls > list files in the current directory.
  • cd arg → change directory.
  • cat arg → print the content of a file.
  • rm arg → delete the specified file or directory.
  • mkdir arg → create the specified directory at the current level.
  • cp arg1 arg2 → copy the file from arg1 to the location arg2.
  • append arg1 arg2 → append the content of arg2 to the end of the file specified in arg1.
  • replace arg1 arg2 arg3 → replace the content of the file specified in arg1 by searching for references to arg2 and replacing them with arg3.

To carry out this task, the programmer must first create a Maven mono-module project, using the instructions provided in the Building with Maven chapter. The assistant will provide a class containing the public static void main method for this purpose.

The application should read user input via System.in until the user types exit. Until then, each of the commands written by the user will be executed. The main loop will delegate the execution of the requested action to a specific class. As many specific classes as enumerated commands must be created.

To validate the development, there must be as many unit tests as necessary to achieve a code coverage above 90% of the source code.

Learn Java Programming - PRACTICE 2

The goal is to create a system that allows playing Battleship against the CPU in console mode through commands entered by the user in the command line terminal.

Practice 2 Requirements

The game should consist of choosing cells on the opponent's board using a turn-based system and organized into two phases. The first phase is the placement of ships on a matrix and the second phase starts the turn system to launch missiles against the opponent.

The project should also be generated with Maven and with all the unit test cases that validate 90% of the written source code.

The system requirements are:

  • User interaction with the game system will be through console commands. So, a command interpreter must be created to read user input. Possibility to use libraries like commons-cli for this purpose.
  • The operations that the interpreter must offer as a minimum are: start game, launch missile, print game (board and inventory), and exit.
  • The placement of ships on the board, both for the player and for the CPU, will be random using three random variables through Math.random(). The variables will indicate X and Y position and whether it's horizontal or vertical. It's allowed for ships to touch, with the inclusion of a mechanism to prevent it being optional.
  • A log should be created in a text file containing the actions performed in all games, both player and CPU actions, clearly marking the start and end of games and their result.
  • Both the matrix size and the inventory of ships must be configurable. A square matrix of size N x N is assumed. The global variable N determines the size. The inventory will be given by an instance of List<Fleet>. Where Fleet class = {name, number, size}. Both will determine how many ships should be placed in each game. It's optional for the user to be able to change the configuration through commands.
  • A possible configuration is:
    • Default matrix size: N = 10
    • 1 aircraft carrier occupying 5 cells
    • 2 cruisers, occupying 4 cells each
    • 3 destroyers, occupying 3 cells each
    • 2 frigates, of 2 cells each
    • 4 submarines, of 1 cell each
  • If the user does not indicate their launch within a game in progress after some time, the turn is automatically returned to the CPU to execute its turn.

Learn Java Programming - PRACTICE 3

The objective of this practice is to create a network-accessible service that allows booking hotel rooms for an imaginary hotel for a month. This service will be consumed by a second Java application acting as a client, allowing the user to interactively perform operations on the reservation system.

Practice 3 Functional Requirements

Reservations will always be for full days from 12 am to 12 am the next day. Even if a reservation spans two days, only one reservation per day will be considered, which is the one that has the room reserved, for example, at 7 pm. It will be understood that all rooms are double, with a single base price per room.

The hotel will have two types of rooms, standard or suite, with different prices for each type. Also, with two types of board for each of them: half board or full board.

The board will represent a % increase over the room price. A reservation will be for only one room. Currently, the hotel only has 5 standard rooms and 2 suites.

Practice 3 Technical Requirements

The client and the service will exchange information through some object serialization system, for example, JSON. Both the server and the client will use TCP/IP Sockets to establish the connection and exchange documents. The data of the available rooms, reservations, and prices will come from an SQL database using JPA and QueryDSL.

For availability management, a calendar must be implemented using any data structure that allows knowing which rooms are available each day of August. Some synchronization mechanism must be incorporated to ensure that NO two reservations are made for the same room on the same days.

The server must expose the following operations:

  • availability: Returns the list of room TYPES available between two dates. Each record returned must contain the TYPE of room it refers to and a list of possible board plans for the room indicating the standard price per day for that plan (and that type of room). If there are no available rooms of a type, obviously that type should be omitted in the response. Inputs:

    • from: LocalDate

    • to: LocalDate

    • roomType: Room type (optional)Outputs:

    • Object of type HotelAvailResponse with the availability contained within.

  • valuate: Evaluate availability. That is, given two dates, the room type, and the requested board plan, obtain the total price of the possible reservation. Returns an error if it's not available. Inputs:

    • Validated object of type PreBookingRequest with all mandatory fields.

      • from: LocalDate
      • to: LocalDate
      • roomType: Room type
      • mealPlan: Board planOutputs:
    • Object of type PreBookingResponse containing the input parameters, plus a timestamp of when the operation was performed and the price of the possible reservation.

  • confirmation: Make a reservation. Given two dates, the room type, the chosen board plan, and the customer code (any String). Re-evaluates and blocks the room for the indicated days if successful. Returns information about the reservation: the final price, the room identifier, and the reservation identifier. This operation must modify the calendar and the reservations table. Inputs:

    • Validated object of type BookingRequest with all mandatory fields.

      • from: LocalDate
      • to: LocalDate
      • roomType: Room type
      • mealPlan: Board plan
      • customer: String identifying the customerOutputs:
    • Object of type BookingResponse containing all the reservation information.

  • cancellation: Given a reservation identifier, releases the room for the reservation dates leaving the calendar and the database updated. Inputs:

    • bookingId: Reservation identifierOutputs:
    • Confirmation of the operation performed correctly or an error if the reservation is not found in the system.
  • bookings: List of reservations, with the possibility of indicating an optional date range as a filter. Returns the list of confirmed non-cancelled reservations. Produces the same information as the confirmation, but with those reservations that are in the interval. Inputs:

    • from: LocalDate for the start of the range

    • to: LocalDate for the end of the rangeOutputs:

    • List of reservations in the form of Collection<BookingResponse>.

Implementation Notes for Practice 3

To facilitate the start of the project, the programmer must define how the documents exchanged between the server and the client will be. As a guide, a possible definition of the JSON response document indicating which Java classes produce such document is provided.

The expected availability response by the server must contain the following format:

    {  "availability" : [
       {
         "roomType" : "regular",
         "prices" : [
            {
              "mealPlan" : "MP",
              "dayPrice": 100
            },
            {
              "mealPlan" : "PC",
              "dayPrice": 150
            }
          ]
        }, {
          "roomType" : "suite",
          "prices" : [
            {
               "mealPlan" : "MP",
               "dayPrice": 170
            },
            {
               "mealPlan" : "PC",
               "dayPrice": 200
            }
           ]
        }
      ]
    }

The JSON message above will be automatically generated from the serialization of the following Java model:

    public class HotelAvailResponse{
        private List<RoomAvail> availability;
        /* getters and setters */
    }

    public class RoomAvail{
        private String roomType;
        private List<RoomPrice> prices;
        /* getters and setters */
    }

    public class RoomPrice{
        private String mealPlan;
        private Float dayPrice;
        /* getters and setters */
    }

Stay Connected

Newsletter

Stay up to date with the latest in technology and business! Subscribe to our newsletter and receive exclusive updates directly to your inbox.

Online Meeting

Don't miss the opportunity to explore new possibilities. Schedule an online meeting with us today and let's start building the future of your business together!

  • :)
  • :0
  • :D
  • ;)
  • :]