Q:

Write a java program which prompts the user to enter a vehicle plate number in the format s-LLL-DDDD, where L is an UPPERCASE

0

Write a java program which prompts the user to enter a vehicle plate number in the format  s-LLL-DDDD, where L is an UPPERCASE LETTER and D is a digit, and s equals: 

■ KSA: for Saudi vehicle plate number

■ USA: for American vehicle plate number 

■ UK: for British vehicle plate number

 

Your program should:

Verify if the vehicle plate number is valid.

Give the nationality of the vehicule.

 

Here are sample runs:

Enter a vehicle plate number: KSA-ABC-2357 is a valid Saudi vehicle plate number

Enter a vehicle plate number: USA-ABC-2357 is a valid American vehicle plate number

Enter a vehicle plate number: 562-dFe-7896 is an invalid vehicle plate number

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

import java.util.Scanner;
public class Main
{
    /** Return true if the vehicule plate number is valid */
public static boolean isValid(String plate)
{
    String parts[]=plate.split("-",3);
    if(!(parts[0].equals("KSA") || parts[0].equals("USA") || parts[0].equals("UK")))
    return false;
    if(!(parts[1].length()==3))
    return false;
    
    char charArray[]= parts[1].toCharArray();
    for(int i=0;i<charArray.length;i++)
    if(!(Character.isUpperCase(charArray[i])))
    return false;
    
    if(!(parts[2].length()==4))
    return false;
    
    charArray= parts[2].toCharArray();
    for(int i=0;i<charArray.length;i++)
    if(!(Character.isDigit(charArray[i])))
    return false;
    
    return true;
}
/** Return the nationality of the vehicule */
public static String getNationality(String nationality)
{
    if(nationality.equals("KSA"))
    return "Saudi";
    else if(nationality.equals("USA"))
    return "American";
    else if(nationality.equals("UK"))
    return "United Kingdom";
    else return "";
}
	public static void main(String[] args) {
		Scanner input=new Scanner(System.in);
		System.out.print("Enter a vehicle plate number:");
		String plate=input.next();
		boolean v=isValid(plate);
		if(v==true)
		System.out.print("is a valid ");
		else System.out.print("is an invalid ");
		
		String parts[]=plate.split("-",2);
    
		System.out.println(getNationality(parts[0])+" vehicle plate number");
		
	}
}

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now