Menu
Question Index
...

In Car class, implement the setTyreCount method to restrict the value of number of tyres to either 4 or 6. Also implement setNumberOfSeats to restrict the number of seats from 3 to 10. If any other value is passed, the previous value should be retained.

Input Output
Previous Number Of Tyres = 4, Previous Number Of Seats = 5, New Number Of Tyres = 8, New Number Of Seats = 10 New Number Of Tyres = 4, New Number Of Seats = 10
Previous Number Of Tyres = 4, Previous Number Of Seats = 10, New Number Of Tyres = 10, New Number Of Seats = 14 New Number Of Tyres = 4, New Number Of Seats = 10
Previous Number Of Tyres = 4, Previous Number Of Seats = 10, New NumberOfTyres = 6, New Number Of Seats = 14 New Number Of Tyres = 6, New Number Of Seats = 10



class ClassCarValidation
{
    public static void main(String s[])
    {
        Car hyundai = new Car("Hyundai Eon");
        hyundai.setTyreCount(4);
        hyundai.setNumberOfSeats(5);
        hyundai.print();
        hyundai.setTyreCount(8);
        hyundai.print();
        hyundai.setNumberOfSeats(10);
        hyundai.print();    
        hyundai.setDetails(6, 14);
        hyundai.print();

    }



}
class Car
{
    String name;
    int numberOfTyres;
    int numberOfSeats;
    Car(String name)
    {
        this.name = name;
    }

    void setTyreCount(int numberOfTyres)
    {
    }

    void setNumberOfSeats(int numberOfSeats)
    {
    }

    void setDetails(int numberOfTyres, int numberOfSeats)
    {
        setTyreCount(numberOfTyres);
        setNumberOfSeats(numberOfSeats);  
    }
    
    void print()
    {
            System.out.println("The car " + name + " has " + numberOfTyres + " tyres and " + numberOfSeats + " seats.");
    }
}

Doubts

Problems

Topic: Creating A Class For Data Validation

Read this topic
Take test on this topic

0
Wrong
Score more than 2 points

© meritcampus 2019

All Rights Reserved.

Open In App