0% found this document useful (0 votes)
1 views10 pages

Java 7

The document contains two Java programming practicals focusing on object-oriented concepts. The first practical involves creating an abstract class for musical instruments and allows users to select and play different instruments. The second practical involves generating a train system with random compartments, each providing specific notices based on their type.

Uploaded by

deephirpara4
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views10 pages

Java 7

The document contains two Java programming practicals focusing on object-oriented concepts. The first practical involves creating an abstract class for musical instruments and allows users to select and play different instruments. The second practical involves generating a train system with random compartments, each providing specific notices based on their type.

Uploaded by

deephirpara4
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

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]();
}
}

You might also like