SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Stuart Reges, Marty Stepp
Chapter:
Inheritance And Interfaces
Exercise:
Exercises
Question:5 | ISBN:9780136091813 | Edition: 2

Question

Implement a class called WalkupTicket to represent a walk-up event ticket. Walk-up tickets are also constructed by number, and they have a price of $50.

TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

// package Inheritance;

public class Ticket {

	int ticketNumber;
	int ticketPrice;

	public Ticket(int ticketNumber) {

		this.ticketNumber = ticketNumber;

	}

	public Ticket() {

	}

	public int getTicketNumber() {
		return ticketNumber;
	}

	public void setTicketNumber(int ticketNumber) {
		this.ticketNumber = ticketNumber;
	}

	public int getTicketPrice() {
		return ticketPrice;
	}

	public void setTicketPrice(int ticketPrice) {
		this.ticketPrice = ticketPrice;
	}

}

// newly added wakeUpTicket class 
public class WakeUpTicket extends Ticket {

	static int price = 50;

	public WakeUpTicket(int ticketNumber) {
		super(ticketNumber);

	}

	public int getPrice() {
		return price;
	}

	public void setPrice(int price) {
		WakeUpTicket.price = price;
	}

	@Override
	public String toString() {
		return "Ticket number: " + this.getTicketNumber() + ", price: " + this.getPrice() + ".";
	}

	public static void main(String args[]) {

		// constructed a wakeup ticket with ticket number 34
		WakeUpTicket wakeup = new WakeUpTicket(34);
		WakeUpTicket wakeup1 = new WakeUpTicket(78);
		WakeUpTicket wakeup2 = new WakeUpTicket(54);

		// print ticket number and price overriding the toString() method
		System.out.println(wakeup);
		System.out.println(wakeup1);
		System.out.println(wakeup2);

	}

}
Ticket number: 34, price: 50.
Ticket number: 78, price: 50.
Ticket number: 54, price: 50.

 

0 0

Discussions

Post the discussion to improve the above solution.