Loading...
Loading...
A college gives each new student a roll number from their department: a prefix and a three-digit counter, such as CSE001, CSE002. Each department has its own counter.
Write a class IdGenerator with fields prefix and next, a constructor IdGenerator(String prefix, int start) and a method String nextId(). Write a class Student whose constructor Student(String name, IdGenerator gen) takes its id from the generator.
id name, the counter printed with at least three digits.Example
Input
CSE 1 ECE 41 4 CSE Asha ECE Ravi CSE Meena ECE Kiran
Output
CSE001 Asha ECE041 Ravi CSE002 Meena ECE042 Kiran
Each department counts on its own.
Try the problem first. Open one hint at a time.
A constructor can take another object as a parameter and call its methods.
String.format("%03d", next) gives 001, 041, 100.
Pick the generator by comparing the prefix with equals.
Write your Java code, run it on the examples and submit it to hidden tests.
Solve in the editor