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 {...
4) Write a Java program using Conditions: Write a program where it will ask user to...
4) Write a Java program using Conditions: Write a program where it will ask user to enter a number and after that it will give you answer how many digits that number has. Steps: 1) Create Scanner object and prompt user to enter the number and declare variable integer for input 2) Use if else condition with && condition where you will specify the digits of numbers by writing yourself the digit number that should display: Example(num<100 && num>=1), and...
Create a program that generates a file of random numbers, and then prints them in neat...
Create a program that generates a file of random numbers, and then prints them in neat fashion to another file, and also saves to that file the average and standard deviation of those numbers. I) First, you would need to generate a file of random numbers that consists of N random numbers (100 < N < 1000). Each random digit should be a real number (type double) between 0 and 50. This file and its digits would now serve as...
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...
/* 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...
Program Behavior Each time your program is run, it will prompt the user to enter the...
Program Behavior Each time your program is run, it will prompt the user to enter the name of an input file to analyze. It will then read and analyze the contents of the input file, then print the results. Here is a sample run of the program. User input is shown in red. Let's analyze some text! Enter file name: sample.txt Number of lines: 21 Number of words: 184 Number of long words: 49 Number of sentences: 14 Number of...
Write a Java program named BinaryConversion that will convert base 2 numbers to base 10 numbers....
Write a Java program named BinaryConversion that will convert base 2 numbers to base 10 numbers. The data for this program will be entered from the keyboard using JOptionPane one 16-bit binary number at a time. Note that each base 2 number is actually read in as a String. The program should continue until a 16-bit base 2 number consisting of all 0’s is entered. Once the 16-bit number has been entered your program should make sure that the input...
Write a program that asks the user to type in ages. They will type a negative...
Write a program that asks the user to type in ages. They will type a negative age when they finish entering the ages. The program will print out the average of all of the ages and the oldest age entered. It should end with a newline. Sample output #1 Type a negative for age to exit Enter your age: 21 Enter your age: 22 Enter your age: 21 Enter your age: -8 The average age is: 21.33 The oldest person...
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....
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...