Question

A state's Department of Motor Vehicles needs a program to generate license plate numbers. A license...

A state's Department of Motor Vehicles needs a program to generate license plate numbers. A license plate number consists of three letters followed by three digits, as in CBJ523. (So "number" is a bit inaccurate, but that's the standard word used for license plates). The plate numbers are given out in sequence, so given the current number, the program should output the next number. If the input is CBJ523, the output should be CBJ524. If the input is CBJ999, the output should be CBK000. For the last number ZZZ999, the next is AAA000.

Hints:

  • Treat each character individually.

  • Initially, don't try to create an "elegant" solution. Just consider each of the six places one at a time, starting from the right.

  • For each place, if less than the max for that place ('9' for digits, 'Z' for letters), just increment that place. (Note that you can just type c = c + 1 to get the next higher character for a digit like '5' or for a letter like 'K').

  • If a place is at the max, set it with the '0' or 'A', and then set a boolean variable to true to indicate a "carry" is needed. (If a carry isn't needed, set to false).

  • With the above process, you'll have 6 separate if-else statements.

import java.util.Scanner;

public class main {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);

/* Provide your code here. */

Homework Answers

Answer #1

CODE: (compiled in bluej)

import java.util.Scanner;

public class main {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
System.out.println("Enter the license plate number: ");
String l=scnr.nextLine();
boolean car=false; //To keep a track of if carry is required.
if(l.charAt(5)<'9') //If the rightmost digit is less than 9.
{
char c=l.charAt(5); //extract the digit.
c=(char)(c+1); //increment it by one. char is used for type casting.
l=l.substring(0,5)+c; //add the new character to the remaining string.
}
else
{
car=true; //setting carry to true, so that the next character is checked and incremented.
l=l.substring(0,5)+'0'; //setting the value to 0.
}
if(car==true && l.charAt(4)<'9') //we check this only if car==true,
{
char c=l.charAt(4);
c=(char)(c+1);
l=l.substring(0,4)+c+l.substring(5);
car=false; //if we have incremented the value,we set it to false, so that the others are not checked.
}
else if(car==true) //if the value is equal to 9, we need to set this value to 0 as well and move to the next.
{
l=l.substring(0,4)+'0'+l.substring(5); //take the first 4 characters, add a 0, then take the rest of the characters.
}
//The same process is repeated for all. if the character is less than 9 or Z, we increment it and set car to false, else we set it to 0 or A
//and check for the next character or number.
if(car==true && l.charAt(3)<'9')
{
char c=l.charAt(3);
c=(char)(c+1);
l=l.substring(0,3)+c+l.substring(4);
car=false;
}
else if(car==true)
{
l=l.substring(0,3)+'0'+l.substring(4);
}
if(car==true && l.charAt(2)<'Z')
{
char c=l.charAt(2);
c=(char)(c+1);
l=l.substring(0,2)+c+l.substring(3);
car=false;
}
else if(car==true)
{
l=l.substring(0,2)+'A'+l.substring(3);
}
if(car==true && l.charAt(1)<'Z')
{
char c=l.charAt(1);
c=(char)(c+1);
l=l.substring(0,1)+c+l.substring(2);
car=false;
}
else if(car==true)
{
l=l.substring(0,1)+'A'+l.substring(2);
}
if(car==true && l.charAt(0)<'Z')
{
char c=l.charAt(0);
c=(char)(c+1);
l=l.substring(0,0)+c+l.substring(1);
car=false;
}
else if(car==true)
{
l=l.substring(0,0)+'A'+l.substring(1);
}
System.out.println("The new license number will be: "+l);
}
}

import java.util.Scanner;

public class main {
    public static void main(String[] args) {
        Scanner scnr = new Scanner(System.in);
        System.out.println("Enter the license plate number: ");
        String l=scnr.nextLine();
        boolean car=false; //To keep a track of if carry is required. 
        if(l.charAt(5)<'9') //If the rightmost digit is less than 9.
        {
            char c=l.charAt(5); //extract the digit.
            c=(char)(c+1); //increment it by one. char is used for type casting.
            l=l.substring(0,5)+c; //add the new character to the remaining string.
        }
        else
        {
            car=true; //setting carry to true, so that the next character is checked and incremented.
            l=l.substring(0,5)+'0'; //setting the value to 0.
        }
        if(car==true && l.charAt(4)<'9') //we check this only if car==true, 
        {
            char c=l.charAt(4);
            c=(char)(c+1);
            l=l.substring(0,4)+c+l.substring(5);
            car=false; //if we have incremented the value,we set it to false, so that the others are not checked.
        }
        else if(car==true) //if the value is equal to 9, we need to set this value to 0 as well and move to the next.
        {
            l=l.substring(0,4)+'0'+l.substring(5); //take the first 4 characters, add a 0, then take the rest of the characters.
        }
        //The same process is repeated for all. if the character is less than 9 or Z, we increment it and set car to false, else we set it to 0 or A
        //and check for the next character or number.
        if(car==true && l.charAt(3)<'9')
        {
            char c=l.charAt(3);
            c=(char)(c+1);
            l=l.substring(0,3)+c+l.substring(4);
            car=false;
        }
        else if(car==true)
        {
            l=l.substring(0,3)+'0'+l.substring(4);
        }
        if(car==true && l.charAt(2)<'Z')
        {
            char c=l.charAt(2);
            c=(char)(c+1);
            l=l.substring(0,2)+c+l.substring(3);
            car=false;
        }
        else if(car==true)
        {
            l=l.substring(0,2)+'A'+l.substring(3);
        }
        if(car==true && l.charAt(1)<'Z')
        {
            char c=l.charAt(1);
            c=(char)(c+1);
            l=l.substring(0,1)+c+l.substring(2);
            car=false;
        }
        else if(car==true)
        {
            l=l.substring(0,1)+'A'+l.substring(2);
        }
        if(car==true && l.charAt(0)<'Z')
        {
            char c=l.charAt(0);
            c=(char)(c+1);
            l=l.substring(0,0)+c+l.substring(1);
            car=false;
        }
        else if(car==true)
        {
            l=l.substring(0,0)+'A'+l.substring(1);
        }
        System.out.println("The new license number will be: "+l);
    }
}

OUTPUT:

Enter the license plate number:
ZZZ999
The new license number will be: AAA000
Enter the license plate number:
CBJ999
The new license number will be: CBK000

You can check for your own outputs as well. If you have any doubts or require any clarifications just leave a comment and i will answer it as soon as possible. Thank you :)

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Complete the program below. The program takes in two positive integers, n and m, and should...
Complete the program below. The program takes in two positive integers, n and m, and should print out all the powers of n that are less than or equal to m, on seperate lines. Assume both n and m are positive with n less than or equal to m. For example, if the input values are n = 2 and m = 40 the output should be: 2 4 8 16 32 Starter code: import java.util.Scanner; public class Powers {...
Covert the following Java program to a Python program: import java.util.Scanner; /** * Displays the average...
Covert the following Java program to a Python program: import java.util.Scanner; /** * Displays the average of a set of numbers */ public class AverageValue { public static void main(String[] args) { final int SENTINEL = 0; int newValue; int numValues = 0;                         int sumOfValues = 0; double avg; Scanner input = new Scanner(System.in); /* Get a set of numbers from user */ System.out.println("Calculate Average Program"); System.out.print("Enter a value (" + SENTINEL + " to quit): "); newValue =...
Question: Squares. Write a program class named SquareDisplay that asks the user for a positive integer...
Question: Squares. Write a program class named SquareDisplay that asks the user for a positive integer no greater than 15. The program should then display a square on the screen using the character ‘X’. The number entered by the user will be the length of each side of the square. For example, if the user enters 5, the program should display the following:       XXXXX       XXXXX       XXXXX       XXXXX       XXXXX INPUT and PROMPTS. The program prompts for an integer as follows: "Enter...
7.6 LAB: Exception handling to detect input String vs. Integer The given program reads a list...
7.6 LAB: Exception handling to detect input String vs. Integer The given program reads a list of single-word first names and ages (ending with -1), and outputs that list with the age incremented. The program fails and throws an exception if the second input on a line is a String rather than an Integer. At FIXME in the code, add a try/catch statement to catch java.util.InputMismatchException, and output 0 for the age. Ex: If the input is: Lee 18 Lua...
/* This program should check if the given integer number is prime. Reminder, an integer number...
/* This program should check if the given integer number is prime. Reminder, an integer number greater than 1 is prime if it divisible only by itself and by 1. In other words a prime number divided by any other natural number (besides 1 and itself) will have a non-zero remainder. Your task: Write a method called checkPrime(n) that will take an integer greater than 1 as an input, and return true if that integer is prime; otherwise, it should...
TO BE DONE IN JAVA Your task is to complete the AddTime program so that it...
TO BE DONE IN JAVA Your task is to complete the AddTime program so that it takes multiple sets of hours, minutes and seconds from the user in hh:mm:ss format and then computes the total time elapsed in hours, minutes and seconds. This can be done using the Time object already given to you. Tasks: Complete addTimeUnit() in AddTime so that it processes a string input in hh:mm:ss format and adds a new Time unit to the ArrayList member variable....
Write a program that takes two numbers from the Java console representing, respectively, an investment and...
Write a program that takes two numbers from the Java console representing, respectively, an investment and an interest rate (you will expect the user to enter a number such as .065 for the interest rate, representing a 6.5% interest rate). Your program should calculate and output (in $ notation) the future value of the investment in 5, 10, and 20 years using the following formula: future value = investment * (1 + interest rate)year We will assume that the interest...
Take the Java program Pretty.java and convert it to the equivalent C program. You can use...
Take the Java program Pretty.java and convert it to the equivalent C program. You can use the file in.txt as sample input for your program. import java.io.*; import java.util.*; public class Pretty { public static final int LINE_SIZE = 50; public static void main(String[] parms) { String inputLine; int position = 1; Scanner fileIn = new Scanner(System.in); while (fileIn.hasNextLine()) { inputLine = fileIn.nextLine(); if (inputLine.equals("")) { if (position > 1) { System.out.println(); } System.out.println(); position = 1; } else {...
The language is Java. Using a singly-linked list, implement the four queue methods enqueue(), dequeue(), peek(),...
The language is Java. Using a singly-linked list, implement the four queue methods enqueue(), dequeue(), peek(), and isEmpty(). For this assignment, enqueue() will be implemented in an unusual manner. That is, in the version of enqueue() we will use, if the element being processed is already in the queue then the element will not be enqueued and the equivalent element already in the queue will be placed at the end of the queue. Additionally, you must implement a circular queue....
TrackMinMax For this lab, you will create a generic version of the IntTrackMinMax class you wrote...
TrackMinMax For this lab, you will create a generic version of the IntTrackMinMax class you wrote in a previous lab, called TrackMinMax. The API is: Function Signature Description constructor TrackMinMax() constructor check void check(T i) compares i to the current minimum and maximum values and updates them accordingly getMin T getMin() returns the minimum value provided to check() so far getMax T getMax() returns the maximum value provided to check() so far toString String toString() returns the string "[min,max]" As...