Date: 12-03-2026
Roll No. and Name: 24BCE179 DEEP HIRPARA
Course Code and Name: JAVA PROGRAMMING
Practical No: 7
Aim:
A)
import [Link].*;
abstract class Instrument{
abstract void play();
}
class Piano extends Instrument{
void play() {
[Link]("Piano is playing tan tan tan tan");
}
}
class Flute extends Instrument{
void play() {
[Link]("Flute is playing toot toot toot toot");
}
}
class Guitar extends Instrument{
void play() {
[Link]("Guitar is playing tin tin tin");
}
}
public class Java7a {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
int mainChoice = 1;
[Link]("Welcome to the Music Room!");
while(mainChoice!=0){
[Link](":::Select Option:::");
[Link]("1. Fill the 10 Instruments array and play Concert");
[Link]("0. Exit");
[Link]("Enter your choice: ");
mainChoice=[Link]();
switch(mainChoice){
case 1:
Instrument[] myInstruments=new Instrument[10];
[Link]("\nYou need to pick 10 instruments.");
for(int i=0;i<[Link];i++){
[Link]("\nSelect Instrument for slot " + (i + 1) + ":");
[Link]("1. Piano");
[Link]("2. Flute");
[Link]("3. Guitar");
[Link]("Your choice: ");
int instChoice=[Link]();
switch(instChoice){
case 1:
myInstruments[i]=new Piano();
[Link]("-> Piano added to slot "+(i+1));
break;
case 2:
myInstruments[i]=new Flute();
[Link]("-> Flute added to slot " +(i+1));
break;
case 3:
myInstruments[i]=new Guitar();
[Link]("-> Guitar added to slot " +(i+1));
break;
default:
[Link]("-> Invalid [Link] again.");
i--;
}
}
[Link]("Start Playing....\n");
for (int i=0;i<[Link];i++) {
[Link]("Playing slot " +(i+1)+ ": ");
myInstruments[i].play();
}
break;
case 0:
[Link]("Shut down the Music...");
break;
default:
[Link]("Invalid choice!");
}
}
[Link]();
}
}
B)
import [Link];
import [Link];
abstract class Compartment{
String category;
Compartment(String category){
[Link]=category;
}
abstract void notice();
}
class FirstClass extends Compartment{
FirstClass(){
super("First Class");
}
void notice(){
[Link]("Notice: " + category + " :- Premium AC tickets required.
Enjoy your ride.");
}
}
class Ladies extends Compartment{
Ladies(){
super("Ladies");
}
void notice(){
[Link]("Notice: " + category + " :- Reserved exclusively for
women and children.");
}
}
class General extends Compartment{
General(){
super("General");
}
void notice(){
[Link]("Notice: " + category + " :- Beware of pickpockets. Take
care of belongings.");
}
}
class Luggage extends Compartment{
Luggage(){
super("Luggage");
}
void notice(){
[Link]("Notice: " + category + " :- Goods and heavy cargo only.
No passengers allowed.");
}
}
public class Java7b{
public static void main(String[] args){
Scanner input=new Scanner([Link]);
Random rng=new Random();
int menuChoice=1;
[Link]("Welcome to the Railway System!");
while(menuChoice!=0){
[Link](":::Select Option:::");
[Link]("1. Generate Train (10 Random Coaches)");
[Link]("0. Exit");
[Link]("Enter your choice: ");
menuChoice = [Link]();
if(menuChoice == 1){
Compartment[] train = new Compartment[10];
for(int i = 0; i < [Link]; i++) {
int randomNum = [Link](4) + 1;
switch(randomNum) {
case 1: train[i] = new FirstClass(); break;
case 2: train[i] = new Ladies(); break;
case 3: train[i] = new General(); break;
case 4: train[i] = new Luggage(); break;
}
}
[Link]("The Train Coaches in the Train with related
Notices...");
for(int i=0;i<[Link];i++){
[Link]("Coach "+(i+1)+" -> ");
train[i].notice();
}
}
else if(menuChoice==0){
[Link]("Exiting the railway system. See You again!");
}
else{
[Link]("Invalid input!");
}
}
[Link]();
}
}