Question

Add Encapsulation , Set and get to this program: import java.util.*; abstract class BookTheTicket {//abstract class...

Add Encapsulation , Set and get to this program:

import java.util.*;
abstract class BookTheTicket {//abstract class
private String movieName;
private String theatreName;
private int ticketCost;
abstract void viewAvailablerefreshments();//abstract method
void myAllmovies() {
System.out.println("-------Listing the movies:------");
System.out.println(" 1.Men in Black ------------ $40 \n 2.Toy story---------$.50 \n 3.Harry Potter --------$60"+
"\n 4.lord of the rings ----- $.70 \n 5.the hobbit ----- $.80 ");
}
}

import java.util.Scanner;
class theater extends BookTheTicket{
private int numOfTickets;
void theater() {
System.out.println("*******Listing the theatre:******* \n 1.VOX Cinema \n 2.AMC cinema");
int k;
int an;
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
if (n == 1) {
System.out.println("*******VOX Cinema theater*******");
System.out.println("*******choose a movie name*******");
System.out.println("------Listing the movie:------\n 1.Men in Black \n 2.Toy story ");
int monum = sc.nextInt();
if (monum == 1) {
System.out.println("Men in Black new movie @ VOX Cinema theater");
System.out.println("Enter how many ticket to be booked:---");
int r = sc.nextInt();
int left= numOfTickets-r;
int amount=40*r;
System.out.println("press 1 to continue booking ticket----");
int h = sc.nextInt();
System.out.println("\n\n\n");
if (h == 1) {
System.out.println("******************************************************");
System.out.println("theater ------------------- VOX Cinema theater");
System.out.println("movie --------------------- Men in Black");
System.out.println("cost ---------------------- $"+amount);
System.out.println("******************************************************");
}
}
if (monum == 2) {
System.out.println("Toy story @ VOX Cinema theater");
System.out.println("Enter how many ticket to be booked:---");
int r = sc.nextInt();
int left= numOfTickets-r;
int amount=50*r;
System.out.println("press 1 to continue booking ticket----");
int h = sc.nextInt();
System.out.println("\n\n\n");
if (h == 1) {
System.out.println("******************************************************");
System.out.println("theater ------------------- VOX Cinema");
System.out.println("movie --------------------- Toy story");
System.out.println("cost ---------------------- Rs"+amount);
System.out.println("******************************************************");
}
}
}
if (n == 2) {
System.out.println("*******AMC cinema theater*******");
System.out.println("*******choose a movie name*******");
System.out.println("------Listing the movie:------\n 1.Harry Potter \n 2.lord of the rings ");
int monum = sc.nextInt();
if (monum == 1) {
System.out.println("Harry Potter @ AMC cinema theater");
System.out.println("Enter how many ticket to be booked:---");
int r = sc.nextInt();
int left= numOfTickets-r;
int amount=60*r;
System.out.println("press 1 to continue booking ticket----");
int h = sc.nextInt();
System.out.println("\n\n\n");
if (h == 1) {
System.out.println("******************************************************");
System.out.println("theater ------------------- AMC cinema");
System.out.println("movie --------------------- Harry Potter");
System.out.println("cost ---------------------- Rs."+amount);
System.out.println("******************************************************");
}
}
if (monum== 2) {
System.out.println("lord of the rings @ AMC cinema theater");
System.out.println("Enter how many ticket to be booked:---");
int r = sc.nextInt();
int left= numOfTickets-r;
int amount=70*r;
System.out.println("press 1 to continue booking ticket----");
int h = sc.nextInt();
System.out.println("\n\n\n");
if (h == 1) {
System.out.println("******************************************************");
System.out.println("theater ------------------- AMC cinema ");
System.out.println("movie --------------------- lord of the rings");
System.out.println("cost ---------------------- Rs."+amount);
System.out.println("******************************************************");
}
}
}
}
void viewAvailablerefreshments(){
System.out.println("Coca Cola");
System.out.println("Spicy Popcorn");
System.out.println("Butter Popcorn");
System.out.println("Caramel Popcorn");
System.out.println("water");
System.out.println("Sandwich");
}
}

class calculaterefreshmentbill {//polymorphism
double out (int a){
return a*50; }
double out (int a, int b){
return a*50+b*80; }
double out(int a,int b,int c) {   
return a*50+b*90+c*10; }
}

import java.util.Scanner;
public class Main {
public static void main(String[] args) {
BookTheTicket ob1 = new BookTheTicket(){ void viewAvailablerefreshments(){}};
BookTheTicket [] moviename = new BookTheTicket[2];
moviename[0] = ob1;
theater ob2 = new theater();
moviename[1] = ob2;
System.out.println("~~~~~~~~~~ hello   customer~~~~~~~~~~~\n");
System.out.println("---------1.list of movies and their prices--------- \n-------2.acces and book tickets through theaters---");
System.out.println("\n ~~~~~~~~~~~~thanks ~~~~~~~~~~~");
Scanner d = new Scanner(System.in);
int h = d.nextInt();
switch (h) {
case 1: {
ob1.myAllmovies();
break;
}
case 2: {
ob2.theater();
}
}
System.out.println("Press 1 for adding refreshments");
h=d.nextInt();
if(h==1)
{
calculaterefreshmentbill ob=new calculaterefreshmentbill();
System.out.println("Enter the quantity of cocacola");
int c=d.nextInt();
System.out.println("Enter the quantity of sandwich");
int sd=d.nextInt();
System.out.println("Enter the quantity of water");
int sa=d.nextInt();
if(c==1 && sd==0 && sa==0)
{
double cost1=ob.out(c);
System.out.println("Cost="+cost1);//calculating cost for one coca cola
}
else if(c==1 && sd==1 && sa==0)//calculating cost for one coca cola and one sandwich
{
double cost2=ob.out(c,sd);
System.out.println("Cost="+cost2);
}
else if(c==1 && sd==1 && sa==1)//calculating cost for one coca cola ,one sandwich and one water
{
double cost3=ob.out(c,sd,sa);
System.out.println("Cost="+cost3);
}
}
else{
System.out.println("Thankyou for visiting");
}
}
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Theory:

A class is said to be encapsulated if it has private instance variables and public getter and setter methods.

So we have to change the code of BookTheTicket class and theatre class code to add appropriate private fields and setter,getter methods.

Source Code:

BookTheTicket.java:

import java.util.*;
abstract class BookTheTicket {//abstract class
   private String movieName;
   private String theatreName;             //encapsulation
   private int ticketCost;
  
  
   //getter and setter methods for above private fields
   public String getMovieName() {
       return movieName;
   }
   public void setMovieName(String movieName) {
       this.movieName = movieName;
   }
   public String getTheatreName() {
       return theatreName;
   }
   public void setTheatreName(String theatreName) {
       this.theatreName = theatreName;
   }
   public int getTicketCost() {
       return ticketCost;
   }
   public void setTicketCost(int ticketCost) {
       this.ticketCost = ticketCost;
   }
  

  
  
   abstract void viewAvailablerefreshments();//abstract method
   void myAllmovies() {
       System.out.println("-------Listing the movies:------");
       System.out.println(" 1.Men in Black ------------ $40 \n 2.Toy story---------$.50 \n 3.Harry Potter --------$60"+
       "\n 4.lord of the rings ----- $.70 \n 5.the hobbit ----- $.80 ");
   }
}

theater.java:

import java.util.Scanner;
class theater extends BookTheTicket{
   private int numOfTickets;    //encapsulation
  

//getter and setter methods
   public int getNumOfTickets() {
       return numOfTickets;
   }
   public void setNumOfTickets(int numOfTickets) {
       this.numOfTickets = numOfTickets;
   }


   void theater() {
       System.out.println("*******Listing the theatre:******* \n 1.VOX Cinema \n 2.AMC cinema");
       int k;
       int an;
       Scanner sc = new Scanner(System.in);
       int n = sc.nextInt();
       if (n == 1) {
       System.out.println("*******VOX Cinema theater*******");
       System.out.println("*******choose a movie name*******");
       System.out.println("------Listing the movie:------\n 1.Men in Black \n 2.Toy story ");
       int monum = sc.nextInt();
       if (monum == 1) {
       System.out.println("Men in Black new movie @ VOX Cinema theater");
       System.out.println("Enter how many ticket to be booked:---");
       int r = sc.nextInt();
       int left= numOfTickets-r;
       int amount=40*r;
       System.out.println("press 1 to continue booking ticket----");
       int h = sc.nextInt();
       System.out.println("\n\n\n");
       if (h == 1) {
       System.out.println("******************************************************");
       System.out.println("theater ------------------- VOX Cinema theater");
       System.out.println("movie --------------------- Men in Black");
       System.out.println("cost ---------------------- $"+amount);
       System.out.println("******************************************************");
       }
       }
       if (monum == 2) {
       System.out.println("Toy story @ VOX Cinema theater");
       System.out.println("Enter how many ticket to be booked:---");
       int r = sc.nextInt();
       int left= numOfTickets-r;
       int amount=50*r;
       System.out.println("press 1 to continue booking ticket----");
       int h = sc.nextInt();
       System.out.println("\n\n\n");
       if (h == 1) {
       System.out.println("******************************************************");
       System.out.println("theater ------------------- VOX Cinema");
       System.out.println("movie --------------------- Toy story");
       System.out.println("cost ---------------------- Rs"+amount);
       System.out.println("******************************************************");
       }
       }
       }
       if (n == 2) {
       System.out.println("*******AMC cinema theater*******");
       System.out.println("*******choose a movie name*******");
       System.out.println("------Listing the movie:------\n 1.Harry Potter \n 2.lord of the rings ");
       int monum = sc.nextInt();
       if (monum == 1) {
       System.out.println("Harry Potter @ AMC cinema theater");
       System.out.println("Enter how many ticket to be booked:---");
       int r = sc.nextInt();
       int left= numOfTickets-r;
       int amount=60*r;
       System.out.println("press 1 to continue booking ticket----");
       int h = sc.nextInt();
       System.out.println("\n\n\n");
       if (h == 1) {
       System.out.println("******************************************************");
       System.out.println("theater ------------------- AMC cinema");
       System.out.println("movie --------------------- Harry Potter");
       System.out.println("cost ---------------------- Rs."+amount);
       System.out.println("******************************************************");
       }
       }
       if (monum== 2) {
       System.out.println("lord of the rings @ AMC cinema theater");
       System.out.println("Enter how many ticket to be booked:---");
       int r = sc.nextInt();
       int left= numOfTickets-r;
       int amount=70*r;
       System.out.println("press 1 to continue booking ticket----");
       int h = sc.nextInt();
       System.out.println("\n\n\n");
       if (h == 1) {
       System.out.println("******************************************************");
       System.out.println("theater ------------------- AMC cinema ");
       System.out.println("movie --------------------- lord of the rings");
       System.out.println("cost ---------------------- Rs."+amount);
       System.out.println("******************************************************");
       }
       }
       }
   }
   void viewAvailablerefreshments(){
       System.out.println("Coca Cola");
       System.out.println("Spicy Popcorn");
       System.out.println("Butter Popcorn");
       System.out.println("Caramel Popcorn");
       System.out.println("water");
       System.out.println("Sandwich");
   }
}

calculaterefreshmentbill.java

class calculaterefreshmentbill {//polymorphism
double out (int a){
return a*50; }

double out (int a, int b){
return a*50+b*80; }

double out(int a,int b,int c) {
return a*50+b*90+c*10; }
}

Main.java:

import java.util.Scanner;
public class Main {
public static void main(String[] args) {
BookTheTicket ob1 = new BookTheTicket(){ void viewAvailablerefreshments(){}};
BookTheTicket [] moviename = new BookTheTicket[2];
moviename[0] = ob1;
theater ob2 = new theater();
moviename[1] = ob2;
System.out.println("~~~~~~~~~~ hello   customer~~~~~~~~~~~\n");
System.out.println("---------1.list of movies and their prices--------- \n-------2.acces and book tickets through theaters---");
System.out.println("\n ~~~~~~~~~~~~thanks ~~~~~~~~~~~");
Scanner d = new Scanner(System.in);
int h = d.nextInt();
switch (h) {
case 1: {
ob1.myAllmovies();
break;
}
case 2: {
ob2.theater();
}
}
System.out.println("Press 1 for adding refreshments");
h=d.nextInt();
if(h==1)
{
calculaterefreshmentbill ob=new calculaterefreshmentbill();
System.out.println("Enter the quantity of cocacola");
int c=d.nextInt();
System.out.println("Enter the quantity of sandwich");
int sd=d.nextInt();
System.out.println("Enter the quantity of water");
int sa=d.nextInt();
if(c==1 && sd==0 && sa==0)
{
double cost1=ob.out(c);
System.out.println("Cost="+cost1);//calculating cost for one coca cola
}
else if(c==1 && sd==1 && sa==0)//calculating cost for one coca cola and one sandwich
{
double cost2=ob.out(c,sd);
System.out.println("Cost="+cost2);
}
else if(c==1 && sd==1 && sa==1)//calculating cost for one coca cola ,one sandwich and one water
{
double cost3=ob.out(c,sd,sa);
System.out.println("Cost="+cost3);
}
}
else{
System.out.println("Thankyou for visiting");
}
}
}

CODE SCREENSHOT:

BookTheTicket.java

theater.java:

Add a comment
Know the answer?
Add Answer to:
Add Encapsulation , Set and get to this program: import java.util.*; abstract class BookTheTicket {//abstract class...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I have this java program that I need to add an abstract method and polymorphism to...

    I have this java program that I need to add an abstract method and polymorphism to it : import java.util.Scanner; //class and encapsulation class BookTheTicket { private String movieName; private String theatreName; private int ticketCost; void myAllmovies() { System.out.println("-------Listing the movies:------"); System.out.println(" 1.DDLJ ------------ $40 \n 2.kkr---------$.50 \n 3.game-movie --------$60 \n 4.fun movie ----- $.70 "); } } // inheritence class theater extends BookTheTicket{ private int numOfTickets; void theater() { System.out.println("*******Listing the theatre:******* \n 1.coco cola tld \n 2.koi gandhi...

  • convert this program from java to c++ import java.util.Scanner; class Main { public static void main(String[]...

    convert this program from java to c++ import java.util.Scanner; class Main { public static void main(String[] args) { Scanner sc=new Scanner(System.in); String name,seat,fcls; int st,i; String price; int ch1; char ch; seat=""; String time[]={"7.00","9.00","11.00","13.00","15.00"}; String arrive[]={"9.30","11.30","13.30","15.30","17.30"}; int cp[]=new int[5]; do{ System.out.println("Welcome to CSO1511 Flight Booking System"); System.out.println("Enter full name?"); name=sc.nextLine(); System.out.println("The available travel times for flights are:"); System.out.println(" Depart \t Arrive"); System.out.println("1. 7.00 \t\t 9.30"); System.out.println("2. 9.00 \t\t 11.30"); System.out.println("3. 11.00 \t\t 13.30"); System.out.println("4. 13.00 \t\t 15.30"); System.out.println("5. 15.00 \t\t...

  • need help to write the main method as the template import java.util.*; public class oBST {...

    need help to write the main method as the template import java.util.*; public class oBST { static float optimalBST(String words[], float freq[], int n) { //2D dp matrix float dp[][] = new float[n + 1][n + 1]; int root[][] = new int[n+1][n+1]; // For a single word, cost is equal to frequency of the word for (int i = 0; i < n; i++) dp[i][i] = freq[i]; // Now consider for size 2, 3, ... . for (int L =...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT