0
0
Javaprogramming~10 mins

Class definition in Java - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to define a class named "Car".

Java
public [1] Car {
    // class body
}
Drag options to blanks, or click blank then click option'
Apackage
Bclass
Cmethod
Dinterface
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using 'interface' instead of 'class'.
Using 'method' which is not a keyword for class definition.
2fill in blank
medium

Complete the code to declare a private integer field named "speed" inside the class.

Java
public class Car {
    [1] int speed;
}
Drag options to blanks, or click blank then click option'
Aprivate
Bpublic
Cprotected
Dstatic
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using 'public' which allows access from anywhere.
Using 'static' which is not an access modifier.
3fill in blank
hard

Fix the error in the constructor definition to properly initialize the "speed" field.

Java
public class Car {
    private int speed;

    public Car(int [1]) {
        speed = [1];
    }
}
Drag options to blanks, or click blank then click option'
AspeedValue
Bspeed
Cvalue
DspeedInput
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using the same name for parameter and field without 'this' keyword.
Not assigning the parameter to the field.
4fill in blank
hard

Fill both blanks to create a method named "getSpeed" that returns the speed.

Java
public class Car {
    private int speed;

    public [1] [2]() {
        return speed;
    }
}
Drag options to blanks, or click blank then click option'
Aint
Bvoid
CgetSpeed
DsetSpeed
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using 'void' as return type which means no value is returned.
Naming the method 'setSpeed' which is used to set a value.
5fill in blank
hard

Fill all three blanks to create a setter method named "setSpeed" that sets the speed field.

Java
public class Car {
    private int speed;

    public [1] [2]([3] newSpeed) {
        speed = newSpeed;
    }
}
Drag options to blanks, or click blank then click option'
Avoid
Bint
CsetSpeed
Dspeed
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using 'int' as return type for setter methods.
Using wrong method name like 'speed' instead of 'setSpeed'.