Multi File Programming Question
Marks : 20 Negative Marks : 0
Define an abstract class Themepark and inherit 2 classes Queensland' and 'Wonderla'
from the abstract class. In both the theme parks, the entrance fee for adults is Rs. 500 and
for children it is Rs. 300. If a family buys 'n' adult tickets and 'm' children tickets, define a
method in the abstract class to calculate the total cost.
Also, declare an abstract method playGame() which must be redefined in the subclasses.
In Queensland, there are a total of 30 games. Hence create a Boolean array named
"Games' of size 30 which initially stores false values for all the elements. If the player
enters any game code that has already been played, a warning message should be
displayed and the user should be asked for another choice. In Wonderla, there are a total
of 40 different games. Thus create an integer array with 40 elements. Here, the games ca
e replayed, until the user wants to quit. Finally display the games played as shown in
ample output.
Answers
Program in Java:
import java.util.*;
abstract class Themepark
{
public int TotalCost(int m , int n)
{
return (300 * m) + (500 * n);
}
abstract void playGame();
}
class Queensland extends Themepark
{
public void playGame()
{
Scanner Sc = new Scanner(System.in);
boolean Games[] = new boolean[30];
Arrays.fill(Games, false);
int c;
do
{
System.out.print("\nEnter the number of the game you want to play (1 - 30). \nPress 0 to exit. \nEnter you choice : ");
c = Sc.nextInt();
if(c >= 1 && c <= 30)
{
if(Games[c-1] == true)
{
System.out.println("Warning! You cannot play this game again. Please try another game.");
}
else
{
Games[c-1] = true;
System.out.println("Thankyou for playing this game.");
}
}
else
{
if(c == 0)
{
System.out.println("Thankyou for playing. Bye!");
}
else
{
System.out.println("Invalid input! Please enter valid input");
}
}
}
while(c != 0);
}
}
class Wonderla extends Themepark
{
public void playGame()
{
Scanner Sc = new Scanner(System.in);
int Games[] = new int[40];
Arrays.fill(Games, 0);
int c;
do
{
System.out.print("\nEnter the number of the game you want to play (1 - 40). \nPress 0 to exit. \nEnter you choice : ");
c = Sc.nextInt();
if(c >= 1 && c <= 40)
{
Games[c-1]++;
System.out.println("Thankyou for playing this game.");
}
else
{
if(c == 0)
{
System.out.println("Thankyou for playing. Bye!");
}
else
{
System.out.println("Invalid Input! Please enter a valid input.");
}
}
}
while(c != 0);
int rep = 0;
for(int i = 0 ; i < 40 ; i++)
{
if(Games[i] > 1)
{
rep++;
}
}
System.out.println("\nTotal numbers of games repeated : " + rep);
int not_played = 0;
for(int i=0 ; i < 40 ; i++)
{
if(Games[i] == 0)
{
not_played++;
}
}
System.out.println("\nTotal number of games not played : " + not_played);
int one = 0;
for(int i=0 ; i < 40 ; i++)
{
if(Games[i] == 1)
{
one++;
}
}
System.out.println("\nTotal number of games that are played only once : " + one);
}
}
public class MyClass
{
public static void main(String args[])
{
Scanner Sc = new Scanner(System.in);
int m , n;
System.out.println("Welcome to Queensland");
System.out.print("Enter total number of adults : ");
n = Sc.nextInt();
System.out.print("Enter total number of children : ");
m = Sc.nextInt();
Queensland Q = new Queensland();
System.out.println("Total cost of tickets : " + Q.TotalCost(m , n));
Q.playGame();
System.out.println("\nWelcome to Wonderla");
System.out.print("Enter total number of adults : ");
n = Sc.nextInt();
System.out.print("Enter total number of children : ");
m = Sc.nextInt();
Wonderla W = new Wonderla();
System.out.println("Total cost of tickets : " + W.TotalCost(m , n));
W.playGame();
}
}
Output:
Welcome to Queensland
Enter total number of adults : 2
Enter total number of children : 2
Total cost of tickets : 1600
Enter the number of the game you want to play (1 - 30).
Press 0 to exit.
Enter you choice : 1
Thankyou for playing this game.
Enter the number of the game you want to play (1 - 30).
Press 0 to exit.
Enter you choice : 2
Thankyou for playing this game.
Enter the number of the game you want to play (1 - 30).
Press 0 to exit.
Enter you choice : 1
Warning! You cannot play this game again. Please try another game.
Enter the number of the game you want to play (1 - 30).
Press 0 to exit.
Enter you choice : 0
Thankyou for playing. Bye!
Welcome to Wonderla
Enter total number of adults : 2
Enter total number of children : 1
Total cost of tickets : 1300
Enter the number of the game you want to play (1 - 40).
Press 0 to exit.
Enter you choice : 1
Thankyou for playing this game.
Enter the number of the game you want to play (1 - 40).
Press 0 to exit.
Enter you choice : 2
Thankyou for playing this game.
Enter the number of the game you want to play (1 - 40).
Press 0 to exit.
Enter you choice : 3
Thankyou for playing this game.
Enter the number of the game you want to play (1 - 40).
Press 0 to exit.
Enter you choice : 2
Thankyou for playing this game.
Enter the number of the game you want to play (1 - 40).
Press 0 to exit.
Enter you choice : 1
Thankyou for playing this game.
Enter the number of the game you want to play (1 - 40).
Press 0 to exit.
Enter you choice : 0
Thankyou for playing. Bye!
Total numbers of games repeated : 2
Total number of games not played : 37
Total number of games that are played only once : 1