In: Computer Science
Write multiple if statements:
If carYear is before 1967, print "Probably has few safety
features." (without quotes).
If after 1971, print "Probably has head rests.".
If after 1992, print "Probably has anti-lock brakes.".
If after 2000, print "Probably has tire-pressure monitor.".
End each phrase with period and newline. Ex: carYear = 1995
prints:
Probably has head rests. Probably has anti-lock brakes.
public class SafetyFeatures {
public static void main (String [] args) {
int carYear;
carYear = 1968;
Screenshot of the code:
Sample Output:
//Here the sample output will be nothing as the carYear=1968 does not satisfy any of the 'if' condition
Code to copy:
public class SafetyFeatures
{
public static void main (String [] args)
{
//Declare the variable carYear.
int carYear;
//Assign a value to the variable carYear.
//Here, assign the value 1968 (according to the question).
carYear = 1968;
//If carYear is before 1967,
//print "Probably has few safety features."
if(carYear<1967)
System.out.println("Probably has few safety features. \n");
//If carYear is after 1971,
//print "Probably has head rests.".
if(carYear>1971)
System.out.println("Probably has head rests. \n");
//If carYear is after 1992,
//print "Probably has anti-lock brakes.".
if(carYear>1992)
System.out.println("Probably has anti-lock brakes.\n");
//If carYear is after 2000,
//print "Probably has tire-pressure monitor."
if(carYear>2000)
System.out.println("Probably has tire-pressure monitor. \n");
}
}
Scenario 2:
If the value of carYear is 1995, then the screenshot of the code and sample output is as follows:
Sample Output:
//Here the sample output will be according to the 'if' condition in which carYear>1992, so "Probably has anti-lock brakes" will be printed.
Code to copy:
public class SafetyFeatures
{
public static void main (String [] args)
{
//Declare the variable carYear.
int carYear;
//Assign a value to the variable carYear.
//Here, assign the value 1995.
carYear = 1995;
//If carYear is before 1967,
//print "Probably has few safety features."
if(carYear<1967)
System.out.println("Probably has few safety features. \n");
//If carYear is after 1971,
//print "Probably has head rests.".
if(carYear>1971)
System.out.println("Probably has head rests. \n");
//If carYear is after 1992,
//print "Probably has anti-lock brakes.".
if(carYear>1992)
System.out.println("Probably has anti-lock brakes.\n");
//If carYear is after 2000,
//print "Probably has tire-pressure monitor."
if(carYear>2000)
System.out.println("Probably has tire-pressure monitor. \n");
}
}