JetBrains Academy: toString() - Kamil-Jankowski/Learning-JAVA GitHub Wiki

JetBrains Academy: toString()

Account:

There is a class named Account. It includes three fields: id, code and balance.

Override the method toString() in this class. The method should return a string representation of an instance of the Account.

Do not make the class public.

class Account {

    private long id;
    private String code;
    private Long balance;

    public Account(long id, String code, Long balance) {
        this.id = id;
        this.code = code;
        this.balance = balance;
    }

    @Override
    public String toString(){
        return String.format("Account{id=%d, code='%s', balance=%d}", id, code, balance);
    }
}

Users as strings:

You are given a class named User. It has three string fields: login, firstName, lastName. Override the method toString() in the class to return string representations of users.

The overridden method must return a string including all field-value pairs separated by commas.

Here is an example: "login=javagod,firstName=James,lastName=Gosling".

class User {

    private String login;
    private String firstName;
    private String lastName;

    public User(String login, String firstName, String lastName) {
        this.login = login;
        this.firstName = firstName;
        this.lastName = lastName;
    }

    @Override
    public String toString(){
        return String.format("login=%s,firstName=%s,lastName=%s", login, firstName, lastName);
    }
}

Books as strings:

Here's a class named Book. It has three fields: string field title, int field yearOfPublishing and an array of strings authors. Override the method toString() in the class to return string representations of books.

The overridden method must return a string including all field-value pairs separated by commas. An array of authors always includes at least one author. Do not add a comma after the last author.

Here is an example: "title=Java 8 & 9 in Action,yearOfPublishing=2017,authors=[Mario Fusco,Alan Mycroft]".

class Book {

    private String title;
    private int yearOfPublishing;
    private String[] authors;

    public Book(String title, int yearOfPublishing, String[] authors) {
        this.title = title;
        this.yearOfPublishing = yearOfPublishing;
        this.authors = authors;
    }

   @Override
   public String toString(){
       String fromArray = java.util.Arrays.toString(authors).replace(", ",  ",");
       return "title="+title+",yearOfPublishing="+yearOfPublishing+",authors="+ fromArray;
   }
}

Time as a string:

You are given a class named Time. It has three int fields: hours, minutes and seconds. Override the method toString() in the class to return a string representation of an object.

The overridden method must return a string with hours, minutes and seconds separated by colons. If a number contains only a single digit, add a zero first.

Here are some examples: "23:59:59", "11:08:05", "01:01:01".

class Time {

    private int hours;
    private int minutes;
    private int seconds;

    public Time(int hours, int minutes, int seconds) {
        this.hours = hours;
        this.minutes = minutes;
        this.seconds = seconds;
    }

    @Override
    public String toString(){
        return String.format("%02d:%02d:%02d", hours, minutes, seconds);
    }
}

Convert persons and patients to string:

Here are two classes Person and Patient. The second class extends the first one.

Override toString() in both classes to return a string representation of persons and patients.

If an object is Person, the overridden method toString() must return something like:

Person{name=Case Maxwell,age=30}

If an object is Patient, the overridden method toString() must return something like:

Patient{name=John Purcell,age=30,height=182}

class Person {

    protected String name;
    protected int age;

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString(){
        return String.format("Person{name=%s,age=%d}", name, age);
    }
}

class Patient extends Person {

    protected int height;

    public Patient(String name, int age, int height) {
        super(name, age);
        this.height = height;
    }

    @Override
    public String toString(){
        return String.format("Patient{name=%s,age=%d,height=%d}", name, age, height);
    }
}

Convert vehicles and cars to string:

There are two classes Vehicle and Car. The second class extends the first one.

Override toString() in both classes to return a string representation of vehicles and cars.

If an object is Vehicle, the overridden method toString() must return something like:

Vehicle{licensePlate=ABC123}

If an object is Car, the overridden method toString() must return something like:

Car{licensePlate=ABC123,numberOfSeats=4}

class Vehicle {

    protected String licensePlate;

    public Vehicle(String licensePlate) {
        this.licensePlate = licensePlate;
    }

    @Override
    public String toString(){
        return String.format("Vehicle{licensePlate=%s}", licensePlate);
    }
}

class Car extends Vehicle {

    protected int numberOfSeats;

    public Car(String licensePlate, int numberOfSeats) {
        super(licensePlate);
        this.numberOfSeats = numberOfSeats;
    }

    @Override
    public String toString(){
        return String.format("Car{licensePlate=%s,numberOfSeats=%d}", licensePlate, numberOfSeats);
    }
}