Graderpractice for programmers

Back to 254275 OOP - Labs 1-5 Review

K. Fast cars ✰

Problem

A car consists of a model (String) and a speed (int). If the speed is greater than 200 km/h then it is a "fast" car. Complete the Car class by writing 3 methods:

  • getModel() returns the model (String)
  • getSpeed() returns the speed (int)
  • isFast() returns true if the speed is greater than 200 (boolean)

Submit the Car.java file ONLY!

Template:
class Car {
    // Variables
    private String model;
    private int speed;

    // Constructor
    public Car(String model, int speed) {
        this.model = model;
        this.speed = speed;
    }

    // Methods

}
Test code
import java.util.Scanner;

class Test {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        Car c = new Car(sc.nextLine(), sc.nextInt());
        System.out.println(c.getModel() + " has a speed of " + c.getSpeed() + "km/h");
        if (c.isFast()) {
            System.out.println("It's a supercar!");
        }
    }
}

Examples

Input

Ferrari 488
330

Output

Ferrari 488 has a speed of 330km/h
It's a supercar!

Input

Suzuki Swift
155

Output

Suzuki Swift has a speed of 155km/h

Submit your solution

java