Pages

Wednesday, 27 July 2016

Chapter 4 Exercise 25, Introduction to Java Programming, Tenth Edition Y. Daniel LiangY.

*4.25 (Generate vehicle plate numbers) Assume a vehicle plate number consists of three uppercase letters followed by four digits. Write a program to generate a plate number.

public class ProgrammingEx4_25 {
 
 public static void main(String[] args) {
  //Randomly picking the letters 
  //ASCII code for A-Z is 65-90
  char letter1 = (char) ((int)(Math.random()*26+65));
  char letter2 = (char) ((int)(Math.random()*26+65));
  char letter3 = (char) ((int)(Math.random()*26+65));
   
  int numbers =  (int)(Math.random()*10000);
  //Zeros padding using format method
  String sNumbers = String.format("%04d" ,numbers );
   
  System.out.println("The plate number is " + letter1 + letter2 +letter3 + sNumbers);
   
 
 }
 
}

No comments :

Post a Comment