JetBrains Academy: Instance methods - Kamil-Jankowski/Learning-JAVA GitHub Wiki

JetBrains Academy: Instance methods

Application as a class:

You are given a class named Application with one string field name.

Create an instance method named run that takes an array of strings and returns nothing. The method should output the name of the application and then all arguments, each on a new line.

class Application {

    String name;

    public void run (String[] input){
        System.out.println(this.name);
        for (String str : input){
            System.out.println(str);
        }
    }
}

Car:

You want to create a program that models the behavior of cars. For this purpose, you've created a class named Car containing three fields: the int field yearModel, the string field make, and the int field speed.

You want to add functionality to your cars, so you need methods. Add the following instance methods to your class:

  • accelerate that adds 5 to the speed each time it's called;
  • brake that subtracts 5 from the speed field each time it's called, the speed cannot be less than zero.
class Car {

    int yearModel;
    String make;
    int speed;

    public void accelerate(){
        this.speed += 5;
    }

    public void brake(){
        this.speed -= 5;
        this.speed = this.speed < 0 ? 0 : this.speed;
    }
}

Circle:

Given is a class named Circle. This class has one double field radius.

Create two instance methods for this class:

  • getLength that returns the double length of the circumference of this circle;
  • getArea that returns the double area of this circle.
class Circle {

    double radius;

    public double getLength(){
        return 2 * Math.PI * this.radius;
    }

    public double getArea(){
        return Math.PI * Math.pow(this.radius, 2);
    }
}

Complex numbers:

Jake wants to write a program that works with complex numbers. He created a class Complex that represents a complex number with its real and imaginary parts. Now he needs instance methods so that he can calculate the sum and difference of two complex numbers.

Help Jake and create two instance methods for his class:

  • add that takes another complex number as an argument and adds its corresponding fields to the current instance;
  • subtract that takes another complex number as an argument and subtracts its corresponding fields from the current instance;

Both methods should return nothing.

class Complex {

    double real;
    double image;

    public void add (Complex number){
        this.real += number.real;
        this.image += number.image;
    }

    public void subtract (Complex number){
        this.real -= number.real;
        this.image -= number.image;
    }
}

Counter:

You are given a class named Counter. Write two instance methods:

  • inc that increases the value of the field current by one;
  • getCurrent that returns the current value;
class Counter {

    int current;

    public void inc(){
        this.current++;
    }

    public int getCurrent(){
        return current;
    }
}

In a box:

You are given a class named Box that has three double fields.

Create an instance method to calculate the volume of the box. The method must be named getVolume. It should take no arguments and return a double result.

class Box {

    double height;
    double width;
    double length;

    public double getVolume() {
        return this.height * this.width * this.length;
    }
}

Patient needs a doctor:

Here's a class named Patient. It has one string field name.

You want to create a method that would allow your patient to call for a doctor. Add the method say that prints the message containing the name of a patient (instead of "...") and his request for a doctor: "Hello, my name is ..., I need a doctor."

class Patient {

    String name;

    public void say(){
        System.out.printf("Hello, my name is %s, I need a doctor.", name);
    }
}

Simulating a clock:

You are given a class Clock. It has two int fields: hours and minutes. The clock has a minute scale from 0 to 59 and an hour scale from 1 to 12. The clock does not know whether it's day or night (no AM or PM).

Add the method next that increases the value of minutes by 1. The minutes must be reset to zero when the current hour ends. Do not forget to change hours as well.

class Clock {

    int hours = 12;
    int minutes = 0;

    public void next(){
        this.minutes += 1;
        if (this.minutes > 59){
            this.minutes = 0;
            this.hours += 1;
            this.hours = this.hours > 12 ? 1 : this.hours;
        }
    }
}